UNPKG

2.47 kBJavaScriptView Raw
1'use strict';
2
3import { isWorkletFunction } from "./commonTypes.js";
4import { ReanimatedError, registerReanimatedError } from "./errors.js";
5import { setupCallGuard, setupConsole } from "./initializers.js";
6import { registerLoggerConfig } from "./logger/index.js";
7import NativeReanimatedModule from './NativeReanimated';
8import { shouldBeUseWeb } from "./PlatformChecker.js";
9import { makeShareableCloneOnUIRecursive, makeShareableCloneRecursive } from "./shareables.js";
10const SHOULD_BE_USE_WEB = shouldBeUseWeb();
11
12/**
13 * Lets you create a new JS runtime which can be used to run worklets possibly
14 * on different threads than JS or UI thread.
15 *
16 * @param name - A name used to identify the runtime which will appear in
17 * devices list in Chrome DevTools.
18 * @param initializer - An optional worklet that will be run synchronously on
19 * the same thread immediately after the runtime is created.
20 * @returns WorkletRuntime which is a
21 * `jsi::HostObject<reanimated::WorkletRuntime>` - {@link WorkletRuntime}
22 * @see https://docs.swmansion.com/react-native-reanimated/docs/threading/createWorkletRuntime
23 */
24// @ts-expect-error Check `runOnUI` overload.
25
26export function createWorkletRuntime(name, initializer) {
27 // Assign to a different variable as __reanimatedLoggerConfig is not a captured
28 // identifier in the Worklet runtime.
29 const config = __reanimatedLoggerConfig;
30 return NativeReanimatedModule.createWorkletRuntime(name, makeShareableCloneRecursive(() => {
31 'worklet';
32
33 registerReanimatedError();
34 registerLoggerConfig(config);
35 setupCallGuard();
36 setupConsole();
37 initializer?.();
38 }));
39}
40
41// @ts-expect-error Check `runOnUI` overload.
42
43/** Schedule a worklet to execute on the background queue. */
44export function runOnRuntime(workletRuntime, worklet) {
45 'worklet';
46
47 if (__DEV__ && !SHOULD_BE_USE_WEB && !isWorkletFunction(worklet)) {
48 throw new ReanimatedError('The function passed to `runOnRuntime` is not a worklet.' + (_WORKLET ? ' Please make sure that `processNestedWorklets` option in Reanimated Babel plugin is enabled.' : ''));
49 }
50 if (_WORKLET) {
51 return (...args) => global._scheduleOnRuntime(workletRuntime, makeShareableCloneOnUIRecursive(() => {
52 'worklet';
53
54 worklet(...args);
55 }));
56 }
57 return (...args) => NativeReanimatedModule.scheduleOnRuntime(workletRuntime, makeShareableCloneRecursive(() => {
58 'worklet';
59
60 worklet(...args);
61 }));
62}
63//# sourceMappingURL=runtimes.js.map
\No newline at end of file