'use strict'; import { mockedRequestAnimationFrame } from './runLoop/uiRuntime/mockedRequestAnimationFrame'; import { RuntimeKind } from './runtimeKind'; import { isWorkletFunction } from './workletFunction'; const NOOP = () => {}; const NOOP_FACTORY = () => NOOP; const UI_RUNTIME_HOLDER = {}; const UI_SCHEDULER_HOLDER = {}; const ID = (value: TValue) => value; const IMMEDIATE_CALLBACK_INVOCATION = (callback: () => TCallback) => callback(); globalThis._WORKLET = false; globalThis.__RUNTIME_KIND = RuntimeKind.ReactNative; globalThis._log = console.log; globalThis._getAnimationTimestamp = () => performance.now(); // requestAnimationFrame react-native jest's setup is incorrect as it polyfills // the method directly using setTimeout, therefore the callback doesn't get the // expected timestamp as the only argument. // We override this setup here to make sure that callbacks get the proper timestamps // when executed. For non-jest environments we define requestAnimationFrame in setupRequestAnimationFrame // @ts-ignore TypeScript uses Node definition for rAF, setTimeout, etc which returns a Timeout object rather than a number globalThis.requestAnimationFrame = mockedRequestAnimationFrame; const WorkletAPI = { abortWorkletRuntime: NOOP, callMicrotasks: NOOP, createSerializable: ID, createSerializableArray: ID, createSerializableArrayBuffer: ID, createSerializableArrayBufferView: ID, createSerializableBigInt: ID, createSerializableBoolean: ID, createSerializableError: ID, createSerializableHostObject: ID, createSerializableMap: ID, createSerializableNull: ID, createSerializableNumber: ID, createSerializableObject: ID, createSerializableRegExp: ID, createSerializableRemoteFunction: ID, createSerializableSet: ID, createSerializableString: ID, createSerializableUndefined: ID, createSerializableWorklet: ID, createShareable(_hostRuntimeId: number, initial: TValue) { let value = initial; const set = (next: TValue | ((prev: TValue) => TValue)) => { value = typeof next === 'function' ? (next as (prev: TValue) => TValue)(value) : next; }; return { isHost: false, __shareableRef: true, getAsync: () => Promise.resolve(value), getSync: () => value, setAsync: set, setSync: set, }; }, createSynchronizable: ( initialValue: TValue, config?: { fixedType?: boolean } ) => { let value = initialValue; const synchronizable = { __synchronizableRef: true, getDirty: () => value, getBlocking: () => value, setBlocking: (newValue: TValue | ((prev: TValue) => TValue)) => { value = typeof newValue === 'function' ? (newValue as (prev: TValue) => TValue)(value) : newValue; }, lock: NOOP, unlock: NOOP, }; if (config?.fixedType) { Object.assign(synchronizable, { setDirty: (newValue: TValue) => { value = newValue; }, }); } return synchronizable; }, createWorkletRuntime: NOOP_FACTORY, executeOnUIRuntimeSync: ID, getCurrentThreadId: () => '0', getDynamicFeatureFlag: () => false, getRuntimeKind: () => RuntimeKind.ReactNative, getSerializableMapping: NOOP, getStaticFeatureFlag: () => false, getUIRuntimeHolder: () => UI_RUNTIME_HOLDER, getUISchedulerHolder: () => UI_SCHEDULER_HOLDER, hasSerializableMapping: () => false, isBundleModeEnabled: () => false, isRNRuntime: () => true, isSerializableRef: ID, isShareable: (value: unknown) => typeof value === 'object' && value !== null && !!(value as Record).__shareableRef, isShareableRef: () => true, isSynchronizable: () => false, isUIRuntime: () => false, // maybe it should be true? isWorkerRuntime: () => false, isWorkletFunction: isWorkletFunction, isWorkletRuntime: () => false, makeShareable: ID, makeShareableCloneOnUIRecursive: ID, makeShareableCloneRecursive: ID, registerCustomSerializable: NOOP, removeSerializableMapping: NOOP, runOnJS( fun: (...args: Args) => ReturnValue ): (...args: Args) => void { return (...args) => queueMicrotask( args.length ? () => (fun as (...args: Args) => ReturnValue)(...args) : (fun as () => ReturnValue) ); }, runOnRNSync: IMMEDIATE_CALLBACK_INVOCATION, runOnRuntime: ID, runOnRuntimeAsync( _workletRuntime: unknown, worklet: (...args: Args) => ReturnValue, ...args: Args ): Promise { return WorkletAPI.runOnUIAsync(worklet, ...args); }, runOnRuntimeAsyncWithId( _runtimeId: number, worklet: (...args: Args) => ReturnValue, ...args: Args ): Promise { return WorkletAPI.runOnUIAsync(worklet, ...args); }, runOnRuntimeSync( _workletRuntime: unknown, worklet: (...args: Args) => ReturnValue, ...args: Args ): ReturnValue { return worklet(...args); }, runOnRuntimeSyncWithId( _runtimeId: number, worklet: (...args: Args) => ReturnValue, ...args: Args ): ReturnValue { return worklet(...args); }, runOnUI( worklet: (...args: Args) => ReturnValue ): (...args: Args) => void { return (...args) => { // Mocking time in Jest is tricky as both requestAnimationFrame and queueMicrotask // callbacks run on the same queue and can be interleaved. There is no way // to flush particular queue in Jest and the only control over mocked timers // is by using jest.advanceTimersByTime() method which advances all types // of timers including immediate and animation callbacks. Ideally we'd like // to have some way here to schedule work along with React updates, but // that's not possible, and hence in Jest environment instead of using scheduling // mechanism we just schedule the work ommiting the queue. This is ok for the // uses that we currently have but may not be ok for future tests that we write. mockedRequestAnimationFrame(() => { worklet(...args); }); }; }, runOnUIAsync( worklet: (...args: Args) => ReturnValue, ...args: Args ): Promise { return new Promise((resolve) => { mockedRequestAnimationFrame(() => { const result = worklet(...args); resolve(result); }); }); }, runOnUISync: IMMEDIATE_CALLBACK_INVOCATION, RuntimeKind: RuntimeKind, scheduleOnRN( fun: (...args: Args) => ReturnValue, ...args: Args ): void { WorkletAPI.runOnJS(fun)(...args); }, scheduleOnRuntime: IMMEDIATE_CALLBACK_INVOCATION, scheduleOnRuntimeWithId( _runtimeId: number, worklet: (...args: Args) => ReturnValue, ...args: Args ): void { WorkletAPI.scheduleOnUI(worklet, ...args); }, scheduleOnUI( worklet: (...args: Args) => ReturnValue, ...args: Args ): void { WorkletAPI.runOnUI(worklet)(...args); }, serializableMappingCache: new Map(), setDynamicFeatureFlag: NOOP, setSerializableMapping: NOOP, shareableMappingCache: new Map(), toggleSlowAnimationsOnUIRuntime: () => false, UIRuntimeId: RuntimeKind.UI, WorkletsModule: {}, }; module.exports = { __esModule: true, ...WorkletAPI, };