{"version":3,"names":["NativeReanimatedModule","isJest","shouldBeUseWeb","makeShareableCloneOnUIRecursive","makeShareableCloneRecursive","IS_JEST","SHOULD_BE_USE_WEB","_runOnUIQueue","setupMicrotasks","microtasksQueue","isExecutingMicrotasksQueue","global","queueMicrotask","callback","push","__callMicrotasks","index","length","_maybeFlushUIUpdatesQueue","callMicrotasksOnUIThread","callMicrotasks","runOnUI","worklet","__DEV__","_WORKLET","Error","__workletHash","undefined","_len","arguments","args","Array","_key","scheduleOnUI","queue","forEach","_ref","executeOnUIRuntimeSync","_len2","_key2","result","runOnUIImmediately","_len3","_key3","runWorkletOnJS","_len4","_key4","runOnJS","fun","_len5","_key5","_len6","_key6","__remoteFunction","_len7","_key7","_scheduleOnJS"],"sources":["threads.ts"],"sourcesContent":["'use strict';\nimport NativeReanimatedModule from './NativeReanimated';\nimport { isJest, shouldBeUseWeb } from './PlatformChecker';\nimport type { WorkletFunction } from './commonTypes';\nimport {\n makeShareableCloneOnUIRecursive,\n makeShareableCloneRecursive,\n} from './shareables';\n\nconst IS_JEST = isJest();\nconst SHOULD_BE_USE_WEB = shouldBeUseWeb();\n\n/**\n * An array of [worklet, args] pairs.\n * */\nlet _runOnUIQueue: Array<[WorkletFunction, unknown[]]> = [];\n\nexport function setupMicrotasks() {\n 'worklet';\n\n let microtasksQueue: Array<() => void> = [];\n let isExecutingMicrotasksQueue = false;\n global.queueMicrotask = (callback: () => void) => {\n microtasksQueue.push(callback);\n };\n\n global.__callMicrotasks = () => {\n if (isExecutingMicrotasksQueue) {\n return;\n }\n try {\n isExecutingMicrotasksQueue = true;\n for (let index = 0; index < microtasksQueue.length; index += 1) {\n // we use classic 'for' loop because the size of the currentTasks array may change while executing some of the callbacks due to queueMicrotask calls\n microtasksQueue[index]();\n }\n microtasksQueue = [];\n global._maybeFlushUIUpdatesQueue();\n } finally {\n isExecutingMicrotasksQueue = false;\n }\n };\n}\n\nfunction callMicrotasksOnUIThread() {\n 'worklet';\n global.__callMicrotasks();\n}\n\nexport const callMicrotasks = SHOULD_BE_USE_WEB\n ? () => {\n // on web flushing is a noop as immediates are handled by the browser\n }\n : callMicrotasksOnUIThread;\n\n/**\n * Lets you asynchronously run [workletized](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#to-workletize) functions on the [UI thread](https://docs.swmansion.com/react-native-reanimated/docs/threading/runOnUI).\n *\n * This method does not schedule the work immediately but instead waits for other worklets\n * to be scheduled within the same JS loop. It uses queueMicrotask to schedule all the worklets\n * at once making sure they will run within the same frame boundaries on the UI thread.\n *\n * @param fun - A reference to a function you want to execute on the [UI thread](https://docs.swmansion.com/react-native-reanimated/docs/threading/runOnUI) from the [JavaScript thread](https://docs.swmansion.com/react-native-reanimated/docs/threading/runOnUI).\n * @returns A function that accepts arguments for the function passed as the first argument.\n * @see https://docs.swmansion.com/react-native-reanimated/docs/threading/runOnUI\n */\n// @ts-expect-error This overload is correct since it's what user sees in his code\n// before it's transformed by Reanimated Babel plugin.\nexport function runOnUI(\n worklet: (...args: Args) => ReturnValue\n): (...args: Args) => void;\n\nexport function runOnUI(\n worklet: WorkletFunction\n): (...args: Args) => void {\n 'worklet';\n if (__DEV__ && !SHOULD_BE_USE_WEB && _WORKLET) {\n throw new Error(\n '[Reanimated] `runOnUI` cannot be called on the UI runtime. Please call the function synchronously or use `queueMicrotask` or `requestAnimationFrame` instead.'\n );\n }\n if (__DEV__ && !SHOULD_BE_USE_WEB && worklet.__workletHash === undefined) {\n throw new Error('[Reanimated] `runOnUI` can only be used on worklets.');\n }\n return (...args) => {\n if (IS_JEST) {\n // Mocking time in Jest is tricky as both requestAnimationFrame and queueMicrotask\n // callbacks run on the same queue and can be interleaved. There is no way\n // to flush particular queue in Jest and the only control over mocked timers\n // is by using jest.advanceTimersByTime() method which advances all types\n // of timers including immediate and animation callbacks. Ideally we'd like\n // to have some way here to schedule work along with React updates, but\n // that's not possible, and hence in Jest environment instead of using scheduling\n // mechanism we just schedule the work ommiting the queue. This is ok for the\n // uses that we currently have but may not be ok for future tests that we write.\n NativeReanimatedModule.scheduleOnUI(\n makeShareableCloneRecursive(() => {\n 'worklet';\n worklet(...args);\n })\n );\n return;\n }\n if (__DEV__) {\n // in DEV mode we call shareable conversion here because in case the object\n // can't be converted, we will get a meaningful stack-trace as opposed to the\n // situation when conversion is only done via microtask queue. This does not\n // make the app particularily less efficient as converted objects are cached\n // and for a given worklet the conversion only happens once.\n makeShareableCloneRecursive(worklet);\n makeShareableCloneRecursive(args);\n }\n //\n _runOnUIQueue.push([worklet as WorkletFunction, args]);\n if (_runOnUIQueue.length === 1) {\n queueMicrotask(() => {\n const queue = _runOnUIQueue;\n _runOnUIQueue = [];\n NativeReanimatedModule.scheduleOnUI(\n makeShareableCloneRecursive(() => {\n 'worklet';\n // eslint-disable-next-line @typescript-eslint/no-shadow\n queue.forEach(([worklet, args]) => {\n worklet(...args);\n });\n callMicrotasks();\n })\n );\n });\n }\n };\n}\n\n// @ts-expect-error Check `executeOnUIRuntimeSync` overload above.\nexport function executeOnUIRuntimeSync(\n worklet: (...args: Args) => ReturnValue\n): (...args: Args) => ReturnValue;\n\nexport function executeOnUIRuntimeSync(\n worklet: WorkletFunction\n): (...args: Args) => ReturnValue {\n return (...args) => {\n return NativeReanimatedModule.executeOnUIRuntimeSync(\n makeShareableCloneRecursive(() => {\n 'worklet';\n const result = worklet(...args);\n return makeShareableCloneOnUIRecursive(result);\n })\n );\n };\n}\n\n// @ts-expect-error Check `runOnUI` overload above.\nexport function runOnUIImmediately(\n worklet: (...args: Args) => ReturnValue\n): WorkletFunction;\n/**\n * Schedule a worklet to execute on the UI runtime skipping batching mechanism.\n */\nexport function runOnUIImmediately(\n worklet: WorkletFunction\n): (...args: Args) => void {\n 'worklet';\n if (__DEV__ && !SHOULD_BE_USE_WEB && _WORKLET) {\n throw new Error(\n '[Reanimated] `runOnUIImmediately` cannot be called on the UI runtime. Please call the function synchronously or use `queueMicrotask` or `requestAnimationFrame` instead.'\n );\n }\n if (__DEV__ && !SHOULD_BE_USE_WEB && worklet.__workletHash === undefined) {\n throw new Error(\n '[Reanimated] `runOnUIImmediately` can only be used on worklets.'\n );\n }\n return (...args) => {\n NativeReanimatedModule.scheduleOnUI(\n makeShareableCloneRecursive(() => {\n 'worklet';\n worklet(...args);\n })\n );\n };\n}\n\ntype ReleaseRemoteFunction = {\n (...args: Args): ReturnValue;\n};\n\ntype DevRemoteFunction = {\n __remoteFunction: (...args: Args) => ReturnValue;\n};\n\ntype RemoteFunction =\n | ReleaseRemoteFunction\n | DevRemoteFunction;\n\nfunction runWorkletOnJS(\n worklet: WorkletFunction,\n ...args: Args\n): void {\n // remote function that calls a worklet synchronously on the JS runtime\n worklet(...args);\n}\n\n/**\n * Lets you asynchronously run non-[workletized](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#to-workletize) functions that couldn't otherwise run on the [UI thread](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#ui-thread).\n * This applies to most external libraries as they don't have their functions marked with \"worklet\"; directive.\n *\n * @param fun - A reference to a function you want to execute on the JavaScript thread from the UI thread.\n * @returns A function that accepts arguments for the function passed as the first argument.\n * @see https://docs.swmansion.com/react-native-reanimated/docs/threading/runOnJS\n */\nexport function runOnJS(\n fun:\n | ((...args: Args) => ReturnValue)\n | RemoteFunction\n | WorkletFunction\n): (...args: Args) => void {\n 'worklet';\n type FunWorklet = Extract>;\n type FunDevRemote = Extract>;\n if (SHOULD_BE_USE_WEB || !_WORKLET) {\n // if we are already on the JS thread, we just schedule the worklet on the JS queue\n return (...args) =>\n queueMicrotask(\n args.length\n ? () => (fun as (...args: Args) => ReturnValue)(...args)\n : (fun as () => ReturnValue)\n );\n }\n if ((fun as FunWorklet).__workletHash) {\n // If `fun` is a worklet, we schedule a call of a remote function `runWorkletOnJS`\n // and pass the worklet as a first argument followed by original arguments.\n\n return (...args) =>\n runOnJS(runWorkletOnJS)(\n fun as WorkletFunction,\n ...args\n );\n }\n if ((fun as FunDevRemote).__remoteFunction) {\n // In development mode the function provided as `fun` throws an error message\n // such that when someone accidentally calls it directly on the UI runtime, they\n // see that they should use `runOnJS` instead. To facilitate that we put the\n // reference to the original remote function in the `__functionInDEV` property.\n fun = (fun as FunDevRemote).__remoteFunction;\n }\n return (...args) => {\n _scheduleOnJS(\n fun as\n | ((...args: Args) => ReturnValue)\n | WorkletFunction,\n args.length > 0\n ? // TODO TYPESCRIPT this cast is terrible but will be fixed\n (makeShareableCloneOnUIRecursive(args) as unknown as unknown[])\n : undefined\n );\n };\n}\n"],"mappings":"AAAA,YAAY;;AACZ,OAAOA,sBAAsB,MAAM,oBAAoB;AACvD,SAASC,MAAM,EAAEC,cAAc,QAAQ,mBAAmB;AAE1D,SACEC,+BAA+B,EAC/BC,2BAA2B,QACtB,cAAc;AAErB,MAAMC,OAAO,GAAGJ,MAAM,EAAE;AACxB,MAAMK,iBAAiB,GAAGJ,cAAc,EAAE;;AAE1C;AACA;AACA;AACA,IAAIK,aAAsE,GAAG,EAAE;AAE/E,OAAO,SAASC,eAAeA,CAAA,EAAG;EAChC,SAAS;;EAET,IAAIC,eAAkC,GAAG,EAAE;EAC3C,IAAIC,0BAA0B,GAAG,KAAK;EACtCC,MAAM,CAACC,cAAc,GAAIC,QAAoB,IAAK;IAChDJ,eAAe,CAACK,IAAI,CAACD,QAAQ,CAAC;EAChC,CAAC;EAEDF,MAAM,CAACI,gBAAgB,GAAG,MAAM;IAC9B,IAAIL,0BAA0B,EAAE;MAC9B;IACF;IACA,IAAI;MACFA,0BAA0B,GAAG,IAAI;MACjC,KAAK,IAAIM,KAAK,GAAG,CAAC,EAAEA,KAAK,GAAGP,eAAe,CAACQ,MAAM,EAAED,KAAK,IAAI,CAAC,EAAE;QAC9D;QACAP,eAAe,CAACO,KAAK,CAAC,EAAE;MAC1B;MACAP,eAAe,GAAG,EAAE;MACpBE,MAAM,CAACO,yBAAyB,EAAE;IACpC,CAAC,SAAS;MACRR,0BAA0B,GAAG,KAAK;IACpC;EACF,CAAC;AACH;AAEA,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