1 | 'use strict';
|
2 | import { isWorkletFunction } from './commonTypes';
|
3 | import type { WorkletFunction } from './commonTypes';
|
4 | import { setupCallGuard, setupConsole } from './initializers';
|
5 | import NativeReanimatedModule from './NativeReanimated';
|
6 | import { shouldBeUseWeb } from './PlatformChecker';
|
7 | import {
|
8 | makeShareableCloneOnUIRecursive,
|
9 | makeShareableCloneRecursive,
|
10 | } from './shareables';
|
11 |
|
12 | const SHOULD_BE_USE_WEB = shouldBeUseWeb();
|
13 |
|
14 | export type WorkletRuntime = {
|
15 | __hostObjectWorkletRuntime: never;
|
16 | readonly name: string;
|
17 | };
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | export function createWorkletRuntime(
|
29 | name: string,
|
30 | initializer?: () => void
|
31 | ): WorkletRuntime;
|
32 |
|
33 | export function createWorkletRuntime(
|
34 | name: string,
|
35 | initializer?: WorkletFunction<[], void>
|
36 | ): WorkletRuntime {
|
37 | return NativeReanimatedModule.createWorkletRuntime(
|
38 | name,
|
39 | makeShareableCloneRecursive(() => {
|
40 | 'worklet';
|
41 | setupCallGuard();
|
42 | setupConsole();
|
43 | initializer?.();
|
44 | })
|
45 | );
|
46 | }
|
47 |
|
48 |
|
49 | export function runOnRuntime<Args extends unknown[], ReturnValue>(
|
50 | workletRuntime: WorkletRuntime,
|
51 | worklet: (...args: Args) => ReturnValue
|
52 | ): WorkletFunction<Args, ReturnValue>;
|
53 |
|
54 |
|
55 |
|
56 | export function runOnRuntime<Args extends unknown[], ReturnValue>(
|
57 | workletRuntime: WorkletRuntime,
|
58 | worklet: WorkletFunction<Args, ReturnValue>
|
59 | ): (...args: Args) => void {
|
60 | 'worklet';
|
61 | if (__DEV__ && !SHOULD_BE_USE_WEB && !isWorkletFunction(worklet)) {
|
62 | throw new Error(
|
63 | '[Reanimated] The function passed to `runOnRuntime` is not a worklet.' +
|
64 | (_WORKLET
|
65 | ? ' Please make sure that `processNestedWorklets` option in Reanimated Babel plugin is enabled.'
|
66 | : '')
|
67 | );
|
68 | }
|
69 | if (_WORKLET) {
|
70 | return (...args) =>
|
71 | global._scheduleOnRuntime(
|
72 | workletRuntime,
|
73 | makeShareableCloneOnUIRecursive(() => {
|
74 | 'worklet';
|
75 | worklet(...args);
|
76 | })
|
77 | );
|
78 | }
|
79 | return (...args) =>
|
80 | NativeReanimatedModule.scheduleOnRuntime(
|
81 | workletRuntime,
|
82 | makeShareableCloneRecursive(() => {
|
83 | 'worklet';
|
84 | worklet(...args);
|
85 | })
|
86 | );
|
87 | }
|