Skip to content

Commit

Permalink
chore: Add noop typescript worklets module (#6650)
Browse files Browse the repository at this point in the history
## Summary

Preparing for #6539 

## Test plan

This PR has no real runtime impact
  • Loading branch information
tjzel authored Oct 31, 2024
1 parent 1bb8ae0 commit f820cba
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';
import type {
ShadowNodeWrapper,
Value3D,
ValueRotation,
ShareableRef,
LayoutAnimationBatchItem,
IReanimatedModule,
IWorkletsModule,
} from '../commonTypes';
import { checkCppVersion } from '../platform-specific/checkCppVersion';
import { jsVersion } from '../platform-specific/jsVersion';
Expand All @@ -16,60 +16,13 @@ import type React from 'react';
import { getShadowNodeWrapperFromRef } from '../fabricUtils';
import { ReanimatedTurboModule } from '../specs';
import { ReanimatedError } from '../errors';
import { WorkletsModule } from '../worklets';
import type { ReanimatedModuleProxy } from './reanimatedModuleProxy';

export function createNativeReanimatedModule() {
export function createNativeReanimatedModule(): IReanimatedModule {
return new NativeReanimatedModule();
}

// this is the type of `__reanimatedModuleProxy` which is injected using JSI
export interface ReanimatedModuleProxy {
makeShareableClone<T>(
value: T,
shouldPersistRemote: boolean,
nativeStateSource?: object
): ShareableRef<T>;
scheduleOnUI<T>(shareable: ShareableRef<T>): void;
executeOnUIRuntimeSync<T, R>(shareable: ShareableRef<T>): R;
createWorkletRuntime(
name: string,
initializer: ShareableRef<() => void>
): WorkletRuntime;
scheduleOnRuntime<T>(
workletRuntime: WorkletRuntime,
worklet: ShareableRef<T>
): void;
registerEventHandler<T>(
eventHandler: ShareableRef<T>,
eventName: string,
emitterReactTag: number
): number;
unregisterEventHandler(id: number): void;
getViewProp<T>(
viewTagOrShadowNodeWrapper: number | ShadowNodeWrapper,
propName: string,
callback?: (result: T) => void
): Promise<T>;
enableLayoutAnimations(flag: boolean): void;
registerSensor(
sensorType: number,
interval: number,
iosReferenceFrame: number,
handler: ShareableRef<(data: Value3D | ValueRotation) => void>
): number;
unregisterSensor(sensorId: number): void;
configureProps(uiProps: string[], nativeProps: string[]): void;
subscribeForKeyboardEvents(
handler: ShareableRef<number>,
isStatusBarTranslucent: boolean,
isNavigationBarTranslucent: boolean
): number;
unsubscribeFromKeyboardEvents(listenerId: number): void;
configureLayoutAnimationBatch(
layoutAnimationsBatch: LayoutAnimationBatchItem[]
): void;
setShouldAnimateExitingForTag(viewTag: number, shouldAnimate: boolean): void;
}

function assertSingleReanimatedInstance() {
if (
global._REANIMATED_VERSION_JS !== undefined &&
Expand All @@ -83,9 +36,15 @@ See \`https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshoo
}

class NativeReanimatedModule implements IReanimatedModule {
/**
* We keep the instance of `WorkletsModule` here to keep correct coupling of
* the modules and initialization order.
*/
#workletsModule: IWorkletsModule;
#reanimatedModuleProxy: ReanimatedModuleProxy;

constructor() {
this.#workletsModule = WorkletsModule;
// These checks have to split since version checking depend on the execution order
if (__DEV__) {
assertSingleReanimatedInstance();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
'use strict';
import { createJSReanimatedModule } from './js-reanimated';
import { shouldBeUseWeb } from '../PlatformChecker';
import { createNativeReanimatedModule } from './NativeReanimated';

export const ReanimatedModule = shouldBeUseWeb()
? createJSReanimatedModule()
: createNativeReanimatedModule();
export { ReanimatedModule } from './reanimatedModuleInstance';
export type { ReanimatedModuleProxy } from './reanimatedModuleProxy';
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { SensorType } from '../../commonTypes';
import type {
IReanimatedModule,
IWorkletsModule,
ShareableRef,
Value3D,
ValueRotation,
Expand All @@ -17,8 +18,9 @@ import { mockedRequestAnimationFrame } from '../../mockedRequestAnimationFrame';
import type { WorkletRuntime } from '../../runtimes';
import { logger } from '../../logger';
import { ReanimatedError } from '../../errors';
import { WorkletsModule } from '../../worklets';

export function createJSReanimatedModule() {
export function createJSReanimatedModule(): IReanimatedModule {
return new JSReanimated();
}

Expand All @@ -31,6 +33,11 @@ const requestAnimationFrameImpl =
: globalThis.requestAnimationFrame;

class JSReanimated implements IReanimatedModule {
/**
* We keep the instance of `WorkletsModule` here to keep correct coupling of
* the modules and initialization order.
*/
#workletsModule: IWorkletsModule = WorkletsModule;
nextSensorId = 0;
sensors = new Map<number, WebSensor>();
platform?: Platform = undefined;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

import { createJSReanimatedModule } from './js-reanimated';
import { shouldBeUseWeb } from '../PlatformChecker';
import { createNativeReanimatedModule } from './NativeReanimated';

export const ReanimatedModule = shouldBeUseWeb()
? createJSReanimatedModule()
: createNativeReanimatedModule();
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

import { createJSReanimatedModule } from './js-reanimated';

export const ReanimatedModule = createJSReanimatedModule();
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict';

import type {
ShareableRef,
ShadowNodeWrapper,
Value3D,
ValueRotation,
LayoutAnimationBatchItem,
} from '../commonTypes';
import type { WorkletRuntime } from '../runtimes';

/** Type of `__reanimatedModuleProxy` injected with JSI. */
export interface ReanimatedModuleProxy {
makeShareableClone<T>(
value: T,
shouldPersistRemote: boolean,
nativeStateSource?: object
): ShareableRef<T>;

scheduleOnUI<T>(shareable: ShareableRef<T>): void;

executeOnUIRuntimeSync<T, R>(shareable: ShareableRef<T>): R;

createWorkletRuntime(
name: string,
initializer: ShareableRef<() => void>
): WorkletRuntime;

scheduleOnRuntime<T>(
workletRuntime: WorkletRuntime,
worklet: ShareableRef<T>
): void;

registerEventHandler<T>(
eventHandler: ShareableRef<T>,
eventName: string,
emitterReactTag: number
): number;

unregisterEventHandler(id: number): void;

getViewProp<T>(
viewTagOrShadowNodeWrapper: number | ShadowNodeWrapper,
propName: string,
callback?: (result: T) => void
): Promise<T>;

enableLayoutAnimations(flag: boolean): void;

registerSensor(
sensorType: number,
interval: number,
iosReferenceFrame: number,
handler: ShareableRef<(data: Value3D | ValueRotation) => void>
): number;

unregisterSensor(sensorId: number): void;

configureProps(uiProps: string[], nativeProps: string[]): void;

subscribeForKeyboardEvents(
handler: ShareableRef<number>,
isStatusBarTranslucent: boolean,
isNavigationBarTranslucent: boolean
): number;

unsubscribeFromKeyboardEvents(listenerId: number): void;

configureLayoutAnimationBatch(
layoutAnimationsBatch: LayoutAnimationBatchItem[]
): void;

setShouldAnimateExitingForTag(viewTag: number, shouldAnimate: boolean): void;
}
2 changes: 2 additions & 0 deletions packages/react-native-reanimated/src/commonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type {
} from 'react-native';
import type { WorkletRuntime } from './runtimes';

export interface IWorkletsModule {}

export interface IReanimatedModule {
registerSensor(
sensorType: number,
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-reanimated/src/privateGlobals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
} from './commonTypes';
import type { AnimatedStyle } from './helperTypes';
import type { FrameCallbackRegistryUI } from './frameCallback/FrameCallbackRegistryUI';
import type { ReanimatedModuleProxy } from './ReanimatedModule/NativeReanimated';
import type { ReanimatedModuleProxy } from './ReanimatedModule';
import type { SensorContainer } from './SensorContainer';
import type { LayoutAnimationsManager } from './layoutReanimation/animationsManager';
import type { ProgressTransitionRegister } from './layoutReanimation/sharedTransitions';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

import type { IWorkletsModule } from '../../commonTypes';

export function createJSWorkletsModule(): IWorkletsModule {
return new JSWorklets();
}

class JSWorklets implements IWorkletsModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

import type { IWorkletsModule } from '../../commonTypes';

export function createNativeWorkletsModule(): IWorkletsModule {
return new NativeWorklets();
}

class NativeWorklets {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

export { WorkletsModule } from './workletsModuleInstance';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

import { createNativeWorkletsModule } from './NativeWorklets';
import { shouldBeUseWeb } from '../../PlatformChecker';
import { createJSWorkletsModule } from './JSWorklets';

export const WorkletsModule = shouldBeUseWeb()
? createJSWorkletsModule()
: createNativeWorkletsModule();
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

import { createJSWorkletsModule } from './JSWorklets';

export const WorkletsModule = createJSWorkletsModule();
3 changes: 3 additions & 0 deletions packages/react-native-reanimated/src/worklets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

export { WorkletsModule } from './WorkletsModule';

0 comments on commit f820cba

Please # to comment.