'use strict'; import { IS_JEST } from './platformChecker'; import { mockedRequestAnimationFrame } from './runLoop/uiRuntime/mockedRequestAnimationFrame'; export function scheduleOnUI( worklet: (...args: Args) => ReturnValue, ...args: Args ): void { enqueueUI(worklet, args); } export function runOnUI( worklet: (...args: Args) => ReturnValue ): (...args: Args) => void { return (...args) => { scheduleOnUI(worklet, ...args); }; } export function runOnUISync( worklet: (...args: Args) => ReturnValue, ...args: Args ): ReturnValue; export function runOnUISync(): never { throw new Error('[Worklets] `runOnUISync` is not supported on web.'); } export function executeOnUIRuntimeSync( worklet: (...args: Args) => ReturnValue ): (...args: Args) => ReturnValue; export function executeOnUIRuntimeSync(): never { throw new Error( '[Worklets] `executeOnUIRuntimeSync` is not supported on web.' ); } export function runOnJS( fun: (...args: Args) => ReturnValue ): (...args: Args) => void { return (...args) => scheduleOnRN(fun, ...args); } export function scheduleOnRN( fun: (...args: Args) => ReturnValue, ...args: Args ): void { queueMicrotask( args.length ? () => (fun as (...args: Args) => ReturnValue)(...args) : (fun as () => ReturnValue) ); } export function runOnRNSync( fun: (...args: Args) => ReturnValue, ...args: Args ): ReturnValue { return fun(...args); } export function runOnUIAsync( worklet: (...args: Args) => ReturnValue, ...args: Args ): Promise { return new Promise((resolve, reject) => { enqueueUI(worklet, args, resolve, reject); }); } type UIJob = [ worklet: (...args: Args) => ReturnValue, args: Args, resolve?: (value: ReturnValue) => void, reject?: (reason?: unknown) => void, ]; let runOnUIQueue: UIJob[] = []; function enqueueUI( worklet: (...args: Args) => ReturnValue, args: Args, resolve?: (value: ReturnValue) => void, reject?: (reason?: unknown) => void ): void { const job = [worklet, args, resolve, reject]; runOnUIQueue.push(job as UIJob); if (runOnUIQueue.length === 1) { if (IS_JEST) { flushUIQueue(); } else { queueMicrotask(flushUIQueue); } } } let offset = 0; function flushUIQueue(): void { const queue = runOnUIQueue; runOnUIQueue = []; requestAnimationFrame(() => { offset = 0; while (queue.length > offset) { try { drainUIQueue(queue); } catch (e) { const [, , , jobReject] = queue[offset - 1]; if (jobReject) { jobReject(e); } else { console.error(e); } } } }); } // is-tree-shakable-suppress if (!globalThis.requestAnimationFrame) { /** * Server-side rendering evaluates web code in environments without * `requestAnimationFrame`, such as Node.js. Work scheduled there, for example * at module scope, falls back to a timer instead of throwing from a * microtask. */ globalThis.requestAnimationFrame = mockedRequestAnimationFrame as unknown as typeof globalThis.requestAnimationFrame; } function drainUIQueue(queue: UIJob[]): void { while (queue.length > offset) { const [workletFunction, workletArgs, jobResolve] = queue[offset]; offset++; const result = workletFunction(...workletArgs); if (jobResolve) { jobResolve(result); } } } /** * Returns the id of the thread which currently executes JavaScript on the * calling Runtime. * * @returns The id of the current thread. * @see https://docs.swmansion.com/react-native-worklets/docs/utility/getCurrentThreadId */ export function getCurrentThreadId(): string { throw new Error('[Worklets] `getCurrentThreadId` is not supported on web.'); }