Skip to content

Commit

Permalink
Unify platform checks (software-mansion#4594)
Browse files Browse the repository at this point in the history
<!-- Thanks for submitting a pull request! We appreciate you spending
the time to work on these changes. Please follow the template so that
the reviewers can easily understand what the code changes affect. -->

## Summary

This PR replaces direct platform checks such as `Platform.OS ===
'android'` or `process.env.JEST_WORKER_ID` with functions from
`PlatformChecker.ts` (or captured constants when used inside worklets).

## Test plan

<!-- Provide a minimal but complete code snippet that can be used to
test out this change along with instructions how to run it and a
description of the expected behavior. -->
  • Loading branch information
tomekzaw authored Jun 21, 2023
1 parent 3051b1f commit 74e5f1a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/createAnimatedComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
isJest,
isChromeDebugger,
shouldBeUseWeb,
isWeb,
} from './reanimated2/PlatformChecker';
import { initialUpdaterRun } from './reanimated2/animation';
import {
Expand Down Expand Up @@ -323,7 +324,7 @@ export default function createAnimatedComponent(
}

_detachStyles() {
if (Platform.OS === 'web' && this._styles !== null) {
if (isWeb() && this._styles !== null) {
for (const style of this._styles) {
if (style?.viewsRef) {
style.viewsRef.remove(this);
Expand Down Expand Up @@ -398,7 +399,7 @@ export default function createAnimatedComponent(
const component = this._component?.getAnimatableRef
? this._component.getAnimatableRef()
: this;
if (Platform.OS === 'web') {
if (isWeb()) {
viewTag = findNodeHandle(component);
viewName = null;
shadowNodeWrapper = null;
Expand Down
11 changes: 7 additions & 4 deletions src/reanimated2/Colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
*/

/* eslint no-bitwise: 0 */
import { Platform } from 'react-native';
import { makeRemote, makeShareable, isConfigured } from './core';
import { isAndroid, isWeb } from './PlatformChecker';

interface RGB {
r: number;
Expand Down Expand Up @@ -443,14 +443,17 @@ export const blue = (c: number): number => {
return c & 255;
};

const IS_WEB = isWeb();
const IS_ANDROID = isAndroid();

export const rgbaColor = (
r: number,
g: number,
b: number,
alpha = 1
): number | string => {
'worklet';
if (Platform.OS === 'web' || !_WORKLET) {
if (IS_WEB || !_WORKLET) {
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
}

Expand All @@ -459,7 +462,7 @@ export const rgbaColor = (
Math.round(r) * (1 << 16) +
Math.round(g) * (1 << 8) +
Math.round(b);
if (Platform.OS === 'android') {
if (IS_ANDROID) {
// on Android color is represented as signed 32 bit int
return c < (1 << 31) >>> 0 ? c : c - 4294967296; // 4294967296 == Math.pow(2, 32);
}
Expand Down Expand Up @@ -622,7 +625,7 @@ export function processColor(color: unknown): number | null | undefined {
return null;
}

if (Platform.OS === 'android') {
if (IS_ANDROID) {
// Android use 32 bit *signed* integer to represent the color
// We utilize the fact that bitwise operations in JS also operates on
// signed 32 bit integers, so that we can use those to convert from
Expand Down
4 changes: 4 additions & 0 deletions src/reanimated2/PlatformChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export function isWeb(): boolean {
return Platform.OS === 'web';
}

export function isAndroid(): boolean {
return Platform.OS === 'android';
}

export function shouldBeUseWeb() {
return isJest() || isChromeDebugger() || isWeb();
}
Expand Down
6 changes: 4 additions & 2 deletions src/reanimated2/animation/decay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
AnimatableValue,
Timestamp,
} from '../commonTypes';
import { Platform } from 'react-native';
import { isWeb } from '../PlatformChecker';

interface DecayConfig {
deceleration?: number;
Expand Down Expand Up @@ -38,6 +38,8 @@ export interface InnerDecayAnimation
current: number;
}

const IS_WEB = isWeb();

export function withDecay(
userConfig: DecayConfig,
callback?: AnimationCallback
Expand All @@ -59,7 +61,7 @@ export function withDecay(
);
}

const VELOCITY_EPS = Platform.OS !== 'web' ? 1 : 1 / 20;
const VELOCITY_EPS = IS_WEB ? 1 / 20 : 1;
const SLOPE_FACTOR = 0.1;

let decay: (animation: InnerDecayAnimation, now: number) => boolean;
Expand Down
6 changes: 3 additions & 3 deletions src/reanimated2/hook/useAnimatedStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,9 @@ For more, see the docs: https://docs.swmansion.com/react-native-reanimated/docs/

checkSharedValueUsage(initial.value);

if (process.env.JEST_WORKER_ID) {
return { viewDescriptors, initial: initial, viewsRef, animatedStyle };
if (isJest()) {
return { viewDescriptors, initial, viewsRef, animatedStyle };
} else {
return { viewDescriptors, initial: initial, viewsRef };
return { viewDescriptors, initial, viewsRef };
}
}

0 comments on commit 74e5f1a

Please sign in to comment.