@amazon-devices/react-native-worklets 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Common/cpp/worklets/Registries/EventHandlerRegistry.h +5 -3
- package/Common/cpp/worklets/RunLoop/AsyncQueueImpl.cpp +5 -6
- package/Common/cpp/worklets/RunLoop/EventLoop.cpp +6 -6
- package/Common/cpp/worklets/SharedItems/Serializable.h +9 -8
- package/Common/cpp/worklets/Tools/JSISerializer.h +4 -4
- package/Common/cpp/worklets/Tools/JSScheduler.h +5 -3
- package/Common/cpp/worklets/Tools/SingleInstanceChecker.h +3 -2
- package/Common/cpp/worklets/Tools/UIScheduler.h +4 -4
- package/Common/cpp/worklets/Tools/VersionUtils.h +1 -4
- package/Common/cpp/worklets/Tools/WorkletsSystraceSection.h +0 -25
- package/SystemModules/amzn__react-native-worklets-1.private.modules.txt +57 -33
- package/kepler-compatibility.json +3 -1
- package/lib/module/index.js +0 -25
- package/oss-attribution/attribution.txt +2262 -0
- package/oss-attribution/licenseInfos.json +585 -0
- package/package.json +1 -153
- package/src/index.ts +0 -52
- package/SystemBundles/cc12deac6e91c435b76698c8445aed85612f2f88e733218d14f88a35d06fb0f4.bundle +0 -10512
- package/SystemBundles/cc12deac6e91c435b76698c8445aed85612f2f88e733218d14f88a35d06fb0f4.map +0 -3771
- package/bundleMode/index.js +0 -59
- package/lib/module/PlatformChecker/PlatformChecker.js +0 -8
- package/lib/module/PlatformChecker/index.js +0 -18
- package/lib/module/WorkletsError.js +0 -29
- package/lib/module/WorkletsModule/JSWorklets.js +0 -111
- package/lib/module/WorkletsModule/NativeWorklets.js +0 -126
- package/lib/module/WorkletsModule/index.js +0 -4
- package/lib/module/WorkletsModule/workletsModuleInstance.js +0 -7
- package/lib/module/WorkletsModule/workletsModuleInstance.web.js +0 -5
- package/lib/module/WorkletsModule/workletsModuleProxy.js +0 -4
- package/lib/module/bundleUnpacker.js +0 -47
- package/lib/module/callGuard.js +0 -30
- package/lib/module/deprecated.js +0 -20
- package/lib/module/errors.js +0 -69
- package/lib/module/featureFlags/index.js +0 -48
- package/lib/module/initializers.js +0 -221
- package/lib/module/isSynchronizable.js +0 -6
- package/lib/module/logger.js +0 -15
- package/lib/module/privateGlobals.d.js +0 -7
- package/lib/module/publicGlobals.js +0 -6
- package/lib/module/runLoop/common/setImmediatePolyfill.js +0 -15
- package/lib/module/runLoop/common/setIntervalPolyfill.js +0 -26
- package/lib/module/runLoop/uiRuntime/mockedRequestAnimationFrame.js +0 -10
- package/lib/module/runLoop/uiRuntime/requestAnimationFrame.js +0 -52
- package/lib/module/runLoop/uiRuntime/setTimeoutPolyfill.js +0 -32
- package/lib/module/runLoop/workletRuntime/index.js +0 -19
- package/lib/module/runLoop/workletRuntime/queueMicrotask.js +0 -11
- package/lib/module/runLoop/workletRuntime/requestAnimationFramePolyfill.js +0 -48
- package/lib/module/runLoop/workletRuntime/setTimeout.js +0 -28
- package/lib/module/runLoop/workletRuntime/taskQueue.js +0 -37
- package/lib/module/runtimeKind.js +0 -39
- package/lib/module/runtimes.js +0 -100
- package/lib/module/serializable.js +0 -568
- package/lib/module/serializableMappingCache.js +0 -37
- package/lib/module/specs/NativeWorkletsModule.js +0 -5
- package/lib/module/specs/NativeWorkletsModule.kepler.js +0 -7
- package/lib/module/specs/index.js +0 -10
- package/lib/module/synchronizable.js +0 -9
- package/lib/module/synchronizableUnpacker.js +0 -42
- package/lib/module/threads.js +0 -347
- package/lib/module/utils/checkCppVersion.js +0 -30
- package/lib/module/utils/jsVersion.js +0 -9
- package/lib/module/valueUnpacker.js +0 -60
- package/lib/module/workletFunction.js +0 -37
- package/lib/module/workletRuntimeEntry.js +0 -32
- package/lib/module/workletTypes.js +0 -12
- package/scripts/validate-react-native-version.js +0 -43
- package/src/PlatformChecker/PlatformChecker.ts +0 -7
- package/src/PlatformChecker/index.ts +0 -30
- package/src/WorkletsError.ts +0 -36
- package/src/WorkletsModule/JSWorklets.ts +0 -200
- package/src/WorkletsModule/NativeWorklets.ts +0 -256
- package/src/WorkletsModule/index.ts +0 -7
- package/src/WorkletsModule/workletsModuleInstance.ts +0 -9
- package/src/WorkletsModule/workletsModuleInstance.web.ts +0 -5
- package/src/WorkletsModule/workletsModuleProxy.ts +0 -126
- package/src/bundleUnpacker.ts +0 -75
- package/src/callGuard.ts +0 -33
- package/src/deprecated.ts +0 -32
- package/src/errors.ts +0 -80
- package/src/featureFlags/index.ts +0 -72
- package/src/initializers.ts +0 -262
- package/src/isSynchronizable.ts +0 -13
- package/src/logger.ts +0 -16
- package/src/publicGlobals.ts +0 -39
- package/src/runLoop/common/setImmediatePolyfill.ts +0 -20
- package/src/runLoop/common/setIntervalPolyfill.ts +0 -38
- package/src/runLoop/uiRuntime/mockedRequestAnimationFrame.ts +0 -11
- package/src/runLoop/uiRuntime/requestAnimationFrame.ts +0 -66
- package/src/runLoop/uiRuntime/setTimeoutPolyfill.ts +0 -40
- package/src/runLoop/workletRuntime/index.ts +0 -18
- package/src/runLoop/workletRuntime/queueMicrotask.ts +0 -13
- package/src/runLoop/workletRuntime/requestAnimationFramePolyfill.ts +0 -61
- package/src/runLoop/workletRuntime/setTimeout.ts +0 -37
- package/src/runLoop/workletRuntime/taskQueue.ts +0 -44
- package/src/runtimeKind.ts +0 -37
- package/src/runtimes.ts +0 -193
- package/src/serializable.ts +0 -801
- package/src/serializableMappingCache.ts +0 -42
- package/src/specs/NativeWorkletsModule.kepler.ts +0 -11
- package/src/specs/NativeWorkletsModule.ts +0 -9
- package/src/specs/index.ts +0 -13
- package/src/synchronizable.ts +0 -32
- package/src/synchronizableUnpacker.ts +0 -67
- package/src/threads.ts +0 -452
- package/src/utils/checkCppVersion.ts +0 -34
- package/src/utils/jsVersion.ts +0 -7
- package/src/valueUnpacker.ts +0 -90
- package/src/workletFunction.ts +0 -42
- package/src/workletRuntimeEntry.ts +0 -35
- package/src/workletTypes.ts +0 -81
package/lib/module/runtimes.js
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
import { setupCallGuard } from './callGuard';
|
|
4
|
-
import { getMemorySafeCapturableConsole, setupConsole } from './initializers';
|
|
5
|
-
import { SHOULD_BE_USE_WEB } from './PlatformChecker';
|
|
6
|
-
import { setupRunLoop } from './runLoop/workletRuntime';
|
|
7
|
-
import { RuntimeKind } from './runtimeKind';
|
|
8
|
-
import { createSerializable, makeShareableCloneOnUIRecursive } from './serializable';
|
|
9
|
-
import { isWorkletFunction } from './workletFunction';
|
|
10
|
-
import { registerWorkletsError, WorkletsError } from './WorkletsError';
|
|
11
|
-
import { WorkletsModule } from './WorkletsModule';
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Lets you create a new JS runtime which can be used to run worklets possibly
|
|
15
|
-
* on different threads than JS or UI thread.
|
|
16
|
-
*
|
|
17
|
-
* @param config - Runtime configuration object - {@link WorkletRuntimeConfig}.
|
|
18
|
-
* @returns WorkletRuntime which is a
|
|
19
|
-
* `jsi::HostObject<worklets::WorkletRuntime>` - {@link WorkletRuntime}
|
|
20
|
-
* @see https://docs.swmansion.com/react-native-worklets/docs/threading/createWorkletRuntime/
|
|
21
|
-
*/
|
|
22
|
-
// @ts-expect-error Public API overload.
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* @deprecated Please use the new config object signature instead:
|
|
26
|
-
* `createWorkletRuntime({ name, initializer })`
|
|
27
|
-
*
|
|
28
|
-
* Lets you create a new JS runtime which can be used to run worklets possibly
|
|
29
|
-
* on different threads than JS or UI thread.
|
|
30
|
-
* @param name - A name used to identify the runtime which will appear in
|
|
31
|
-
* devices list in Chrome DevTools.
|
|
32
|
-
* @param initializer - An optional worklet that will be run synchronously on
|
|
33
|
-
* the same thread immediately after the runtime is created.
|
|
34
|
-
* @returns WorkletRuntime which is a
|
|
35
|
-
* `jsi::HostObject<worklets::WorkletRuntime>` - {@link WorkletRuntime}
|
|
36
|
-
* @see https://docs.swmansion.com/react-native-worklets/docs/threading/createWorkletRuntime/
|
|
37
|
-
*/
|
|
38
|
-
|
|
39
|
-
export function createWorkletRuntime(nameOrConfig, initializer) {
|
|
40
|
-
const runtimeBoundCapturableConsole = getMemorySafeCapturableConsole();
|
|
41
|
-
let name;
|
|
42
|
-
let initializerFn;
|
|
43
|
-
let useDefaultQueue = true;
|
|
44
|
-
let customQueue;
|
|
45
|
-
let animationQueuePollingRate;
|
|
46
|
-
let enableEventLoop = true;
|
|
47
|
-
if (typeof nameOrConfig === 'string') {
|
|
48
|
-
name = nameOrConfig;
|
|
49
|
-
initializerFn = initializer;
|
|
50
|
-
} else {
|
|
51
|
-
// TODO: Make anonymous name globally unique.
|
|
52
|
-
name = nameOrConfig?.name ?? 'anonymous';
|
|
53
|
-
initializerFn = nameOrConfig?.initializer;
|
|
54
|
-
useDefaultQueue = nameOrConfig?.useDefaultQueue ?? true;
|
|
55
|
-
customQueue = nameOrConfig?.customQueue;
|
|
56
|
-
animationQueuePollingRate = Math.round(nameOrConfig?.animationQueuePollingRate ?? 16);
|
|
57
|
-
enableEventLoop = nameOrConfig?.enableEventLoop ?? true;
|
|
58
|
-
}
|
|
59
|
-
if (initializerFn && !isWorkletFunction(initializerFn)) {
|
|
60
|
-
throw new WorkletsError('The initializer passed to `createWorkletRuntime` is not a worklet.');
|
|
61
|
-
}
|
|
62
|
-
return WorkletsModule.createWorkletRuntime(name, createSerializable(() => {
|
|
63
|
-
'worklet';
|
|
64
|
-
|
|
65
|
-
setupCallGuard();
|
|
66
|
-
registerWorkletsError();
|
|
67
|
-
setupConsole(runtimeBoundCapturableConsole);
|
|
68
|
-
if (enableEventLoop) {
|
|
69
|
-
setupRunLoop(animationQueuePollingRate);
|
|
70
|
-
}
|
|
71
|
-
initializerFn?.();
|
|
72
|
-
}), useDefaultQueue, customQueue, enableEventLoop);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// @ts-expect-error Check `runOnUI` overload.
|
|
76
|
-
|
|
77
|
-
/** Schedule a worklet to execute on the background queue. */
|
|
78
|
-
export function runOnRuntime(workletRuntime, worklet) {
|
|
79
|
-
'worklet';
|
|
80
|
-
|
|
81
|
-
if (__DEV__ && !SHOULD_BE_USE_WEB && !isWorkletFunction(worklet)) {
|
|
82
|
-
throw new WorkletsError('The function passed to `runOnRuntime` is not a worklet.');
|
|
83
|
-
}
|
|
84
|
-
if (globalThis.__RUNTIME_KIND !== RuntimeKind.ReactNative) {
|
|
85
|
-
return (...args) => globalThis._scheduleOnRuntime(workletRuntime, makeShareableCloneOnUIRecursive(() => {
|
|
86
|
-
'worklet';
|
|
87
|
-
|
|
88
|
-
worklet(...args);
|
|
89
|
-
}));
|
|
90
|
-
}
|
|
91
|
-
return (...args) => WorkletsModule.scheduleOnRuntime(workletRuntime, createSerializable(() => {
|
|
92
|
-
'worklet';
|
|
93
|
-
|
|
94
|
-
worklet(...args);
|
|
95
|
-
globalThis.__flushMicrotasks();
|
|
96
|
-
}));
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/** Configuration object for creating a worklet runtime. */
|
|
100
|
-
//# sourceMappingURL=runtimes.js.map
|
|
@@ -1,568 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
import { registerWorkletStackDetails } from './errors';
|
|
4
|
-
import { isSynchronizable } from './isSynchronizable';
|
|
5
|
-
import { logger } from './logger';
|
|
6
|
-
import { SHOULD_BE_USE_WEB } from './PlatformChecker';
|
|
7
|
-
import { serializableMappingCache, serializableMappingFlag } from './serializableMappingCache';
|
|
8
|
-
import { jsVersion } from './utils/jsVersion';
|
|
9
|
-
import { isWorkletFunction } from './workletFunction';
|
|
10
|
-
import { WorkletsError } from './WorkletsError';
|
|
11
|
-
import { WorkletsModule } from './WorkletsModule';
|
|
12
|
-
// for web and jest environments this file provides a stub implementation
|
|
13
|
-
// where no serializable references are used. Instead, the objects themselves are used
|
|
14
|
-
// instead of serializable references, because of the fact that we don't have to deal with
|
|
15
|
-
// running the code on separate VMs.
|
|
16
|
-
|
|
17
|
-
const MAGIC_KEY = 'REANIMATED_MAGIC_KEY';
|
|
18
|
-
function isHostObject(value) {
|
|
19
|
-
'worklet';
|
|
20
|
-
|
|
21
|
-
// We could use JSI to determine whether an object is a host object, however
|
|
22
|
-
// the below workaround works well and is way faster than an additional JSI call.
|
|
23
|
-
// We use the fact that host objects have broken implementation of `hasOwnProperty`
|
|
24
|
-
// and hence return true for all `in` checks regardless of the key we ask for.
|
|
25
|
-
return MAGIC_KEY in value;
|
|
26
|
-
}
|
|
27
|
-
export function isSerializableRef(value) {
|
|
28
|
-
return typeof value === 'object' && value !== null && '__serializableRef' in value;
|
|
29
|
-
}
|
|
30
|
-
function isPlainJSObject(object) {
|
|
31
|
-
'worklet';
|
|
32
|
-
|
|
33
|
-
return Object.getPrototypeOf(object) === Object.prototype;
|
|
34
|
-
}
|
|
35
|
-
function isTurboModuleLike(object) {
|
|
36
|
-
return isHostObject(Object.getPrototypeOf(object));
|
|
37
|
-
}
|
|
38
|
-
function getFromCache(value) {
|
|
39
|
-
const cached = serializableMappingCache.get(value);
|
|
40
|
-
if (cached === serializableMappingFlag) {
|
|
41
|
-
// This means that `value` was already a clone and we should return it as is.
|
|
42
|
-
return value;
|
|
43
|
-
}
|
|
44
|
-
return cached;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// The below object is used as a replacement for objects that cannot be transferred
|
|
48
|
-
// as serializable values. In createSerializable we detect if an object is of
|
|
49
|
-
// a plain Object.prototype and only allow such objects to be transferred. This lets
|
|
50
|
-
// us avoid all sorts of react internals from leaking into the UI runtime. To make it
|
|
51
|
-
// possible to catch errors when someone actually tries to access such object on the UI
|
|
52
|
-
// runtime, we use the below Proxy object which is instantiated on the UI runtime and
|
|
53
|
-
// throws whenever someone tries to access its fields.
|
|
54
|
-
const INACCESSIBLE_OBJECT = {
|
|
55
|
-
__init: () => {
|
|
56
|
-
'worklet';
|
|
57
|
-
|
|
58
|
-
return new Proxy({}, {
|
|
59
|
-
get: (_, prop) => {
|
|
60
|
-
if (prop === '_isReanimatedSharedValue' || prop === '__remoteFunction') {
|
|
61
|
-
// not very happy about this check here, but we need to allow for
|
|
62
|
-
// "inaccessible" objects to be tested with isSerializableRef check
|
|
63
|
-
// as it is being used in the mappers when extracting inputs recursively
|
|
64
|
-
// as well as with isRemoteFunction when cloning objects recursively.
|
|
65
|
-
// Apparently we can't check if a key exists there as HostObjects always
|
|
66
|
-
// return true for such tests, so the only possibility for us is to
|
|
67
|
-
// actually access that key and see if it is set to true. We therefore
|
|
68
|
-
// need to allow for this key to be accessed here.
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
throw new WorkletsError(`Trying to access property \`${String(prop)}\` of an object which cannot be sent to the UI runtime.`);
|
|
72
|
-
},
|
|
73
|
-
set: () => {
|
|
74
|
-
throw new WorkletsError('Trying to write to an object which cannot be sent to the UI runtime.');
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
const VALID_ARRAY_VIEWS_NAMES = ['Int8Array', 'Uint8Array', 'Uint8ClampedArray', 'Int16Array', 'Uint16Array', 'Int32Array', 'Uint32Array', 'Float32Array', 'Float64Array', 'BigInt64Array', 'BigUint64Array', 'DataView'];
|
|
80
|
-
const DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD = 30;
|
|
81
|
-
// Below variable stores object that we process in createSerializable at the specified depth.
|
|
82
|
-
// We use it to check if later on the function reenters with the same object
|
|
83
|
-
let processedObjectAtThresholdDepth;
|
|
84
|
-
function createSerializableWeb(value) {
|
|
85
|
-
return value;
|
|
86
|
-
}
|
|
87
|
-
function createSerializableNative(value, shouldPersistRemote = false, depth = 0) {
|
|
88
|
-
detectCyclicObject(value, depth);
|
|
89
|
-
const isObject = typeof value === 'object';
|
|
90
|
-
const isFunction = typeof value === 'function';
|
|
91
|
-
if (typeof value === 'string') {
|
|
92
|
-
return cloneString(value);
|
|
93
|
-
}
|
|
94
|
-
if (typeof value === 'number') {
|
|
95
|
-
return cloneNumber(value);
|
|
96
|
-
}
|
|
97
|
-
if (typeof value === 'boolean') {
|
|
98
|
-
return cloneBoolean(value);
|
|
99
|
-
}
|
|
100
|
-
if (typeof value === 'bigint') {
|
|
101
|
-
return cloneBigInt(value);
|
|
102
|
-
}
|
|
103
|
-
if (value === undefined) {
|
|
104
|
-
return cloneUndefined();
|
|
105
|
-
}
|
|
106
|
-
if (value === null) {
|
|
107
|
-
return cloneNull();
|
|
108
|
-
}
|
|
109
|
-
if (!isObject && !isFunction || value === null) {
|
|
110
|
-
return clonePrimitive(value, shouldPersistRemote);
|
|
111
|
-
}
|
|
112
|
-
const cached = getFromCache(value);
|
|
113
|
-
if (cached !== undefined) {
|
|
114
|
-
return cached;
|
|
115
|
-
}
|
|
116
|
-
if (Array.isArray(value)) {
|
|
117
|
-
return cloneArray(value, shouldPersistRemote, depth);
|
|
118
|
-
}
|
|
119
|
-
if (globalThis._WORKLETS_BUNDLE_MODE && isFunction && value.__bundleData) {
|
|
120
|
-
return cloneImport(value);
|
|
121
|
-
}
|
|
122
|
-
if (isFunction && !isWorkletFunction(value)) {
|
|
123
|
-
return cloneRemoteFunction(value);
|
|
124
|
-
}
|
|
125
|
-
// RN has introduced a new representation of TurboModules as a JS object whose prototype is the host object
|
|
126
|
-
// More details: https://github.com/facebook/react-native/blob/main/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.cpp#L182
|
|
127
|
-
if (isTurboModuleLike(value)) {
|
|
128
|
-
return cloneTurboModuleLike(value, shouldPersistRemote, depth);
|
|
129
|
-
}
|
|
130
|
-
if (isHostObject(value)) {
|
|
131
|
-
return cloneHostObject(value);
|
|
132
|
-
}
|
|
133
|
-
if (isPlainJSObject(value) && value.__init) {
|
|
134
|
-
return cloneInitializer(value, shouldPersistRemote, depth);
|
|
135
|
-
}
|
|
136
|
-
if (isPlainJSObject(value) && value.__workletContextObjectFactory) {
|
|
137
|
-
return cloneContextObject(value);
|
|
138
|
-
}
|
|
139
|
-
if ((isPlainJSObject(value) || isFunction) && isWorkletFunction(value)) {
|
|
140
|
-
return cloneWorklet(value, shouldPersistRemote, depth);
|
|
141
|
-
}
|
|
142
|
-
if (isSynchronizable(value)) {
|
|
143
|
-
return cloneSynchronizable(value);
|
|
144
|
-
}
|
|
145
|
-
if (isPlainJSObject(value) || isFunction) {
|
|
146
|
-
return clonePlainJSObject(value, shouldPersistRemote, depth);
|
|
147
|
-
}
|
|
148
|
-
if (value instanceof Set) {
|
|
149
|
-
return cloneSet(value);
|
|
150
|
-
}
|
|
151
|
-
if (value instanceof Map) {
|
|
152
|
-
return cloneMap(value);
|
|
153
|
-
}
|
|
154
|
-
if (value instanceof RegExp) {
|
|
155
|
-
return cloneRegExp(value);
|
|
156
|
-
}
|
|
157
|
-
if (value instanceof Error) {
|
|
158
|
-
return cloneError(value);
|
|
159
|
-
}
|
|
160
|
-
if (value instanceof ArrayBuffer) {
|
|
161
|
-
return cloneArrayBuffer(value, shouldPersistRemote);
|
|
162
|
-
}
|
|
163
|
-
if (ArrayBuffer.isView(value)) {
|
|
164
|
-
// typed array (e.g. Int32Array, Uint8ClampedArray) or DataView
|
|
165
|
-
return cloneArrayBufferView(value);
|
|
166
|
-
}
|
|
167
|
-
return inaccessibleObject(value);
|
|
168
|
-
}
|
|
169
|
-
if (globalThis._WORKLETS_BUNDLE_MODE) {
|
|
170
|
-
// TODO: Do it programatically.
|
|
171
|
-
createSerializableNative.__bundleData = {
|
|
172
|
-
imported: 'createSerializable',
|
|
173
|
-
// @ts-expect-error resolveWeak is defined by Metro
|
|
174
|
-
source: require.resolveWeak('./index')
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
export const createSerializable = SHOULD_BE_USE_WEB ? createSerializableWeb : createSerializableNative;
|
|
178
|
-
function detectCyclicObject(value, depth) {
|
|
179
|
-
if (depth >= DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD) {
|
|
180
|
-
// if we reach certain recursion depth we suspect that we are dealing with a cyclic object.
|
|
181
|
-
// this type of objects are not supported and cannot be transferred as serializable, so we
|
|
182
|
-
// implement a simple detection mechanism that remembers the value at a given depth and
|
|
183
|
-
// tests whether we try reenter this method later on with the same value. If that happens
|
|
184
|
-
// we throw an appropriate error.
|
|
185
|
-
if (depth === DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD) {
|
|
186
|
-
processedObjectAtThresholdDepth = value;
|
|
187
|
-
} else if (value === processedObjectAtThresholdDepth) {
|
|
188
|
-
throw new WorkletsError('Trying to convert a cyclic object to a serializable. This is not supported.');
|
|
189
|
-
}
|
|
190
|
-
} else {
|
|
191
|
-
processedObjectAtThresholdDepth = undefined;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
function clonePrimitive(value, shouldPersistRemote) {
|
|
195
|
-
return WorkletsModule.createSerializable(value, shouldPersistRemote);
|
|
196
|
-
}
|
|
197
|
-
function cloneString(value) {
|
|
198
|
-
return WorkletsModule.createSerializableString(value);
|
|
199
|
-
}
|
|
200
|
-
function cloneNumber(value) {
|
|
201
|
-
return WorkletsModule.createSerializableNumber(value);
|
|
202
|
-
}
|
|
203
|
-
function cloneBoolean(value) {
|
|
204
|
-
return WorkletsModule.createSerializableBoolean(value);
|
|
205
|
-
}
|
|
206
|
-
function cloneBigInt(value) {
|
|
207
|
-
return WorkletsModule.createSerializableBigInt(value);
|
|
208
|
-
}
|
|
209
|
-
function cloneUndefined() {
|
|
210
|
-
return WorkletsModule.createSerializableUndefined();
|
|
211
|
-
}
|
|
212
|
-
function cloneNull() {
|
|
213
|
-
return WorkletsModule.createSerializableNull();
|
|
214
|
-
}
|
|
215
|
-
function cloneObjectProperties(value, shouldPersistRemote, depth) {
|
|
216
|
-
const clonedProps = {};
|
|
217
|
-
for (const [key, element] of Object.entries(value)) {
|
|
218
|
-
// We don't need to clone __initData field as it contains long strings
|
|
219
|
-
// representing the worklet code, source map, and location, and we will
|
|
220
|
-
// serialize/deserialize it once.
|
|
221
|
-
if (key === '__initData' && clonedProps.__initData !== undefined) {
|
|
222
|
-
continue;
|
|
223
|
-
}
|
|
224
|
-
clonedProps[key] = createSerializable(element, shouldPersistRemote, depth + 1);
|
|
225
|
-
}
|
|
226
|
-
return clonedProps;
|
|
227
|
-
}
|
|
228
|
-
function cloneInitializer(value, shouldPersistRemote = false, depth = 0) {
|
|
229
|
-
const clonedProps = cloneObjectProperties(value, shouldPersistRemote, depth);
|
|
230
|
-
return WorkletsModule.createSerializableInitializer(clonedProps);
|
|
231
|
-
}
|
|
232
|
-
function cloneArray(value, shouldPersistRemote, depth) {
|
|
233
|
-
const clonedElements = value.map(element => createSerializable(element, shouldPersistRemote, depth + 1));
|
|
234
|
-
const clone = WorkletsModule.createSerializableArray(clonedElements, shouldPersistRemote);
|
|
235
|
-
serializableMappingCache.set(value, clone);
|
|
236
|
-
serializableMappingCache.set(clone);
|
|
237
|
-
freezeObjectInDev(value);
|
|
238
|
-
return clone;
|
|
239
|
-
}
|
|
240
|
-
function cloneRemoteFunction(value) {
|
|
241
|
-
const clone = WorkletsModule.createSerializableFunction(value);
|
|
242
|
-
serializableMappingCache.set(value, clone);
|
|
243
|
-
serializableMappingCache.set(clone);
|
|
244
|
-
freezeObjectInDev(value);
|
|
245
|
-
return clone;
|
|
246
|
-
}
|
|
247
|
-
function cloneHostObject(value) {
|
|
248
|
-
// for host objects we pass the reference to the object as serializable and
|
|
249
|
-
// then recreate new host object wrapping the same instance on the UI thread.
|
|
250
|
-
// there is no point of iterating over keys as we do for regular objects.
|
|
251
|
-
const clone = WorkletsModule.createSerializableHostObject(value);
|
|
252
|
-
serializableMappingCache.set(value, clone);
|
|
253
|
-
serializableMappingCache.set(clone);
|
|
254
|
-
return clone;
|
|
255
|
-
}
|
|
256
|
-
function cloneWorklet(value, shouldPersistRemote, depth) {
|
|
257
|
-
if (__DEV__) {
|
|
258
|
-
const babelVersion = value.__pluginVersion;
|
|
259
|
-
if (babelVersion !== undefined && babelVersion !== jsVersion) {
|
|
260
|
-
throw new WorkletsError(`Mismatch between JavaScript code version and Worklets Babel plugin version (${jsVersion} vs. ${babelVersion}).
|
|
261
|
-
See \`https://docs.swmansion.com/react-native-worklets/docs/guides/troubleshooting#mismatch-between-javascript-code-version-and-worklets-babel-plugin-version\` for more details.
|
|
262
|
-
Offending code was: \`${getWorkletCode(value)}\``);
|
|
263
|
-
}
|
|
264
|
-
registerWorkletStackDetails(value.__workletHash, value.__stackDetails);
|
|
265
|
-
}
|
|
266
|
-
if (value.__stackDetails) {
|
|
267
|
-
// `Error` type of value cannot be copied to the UI thread, so we
|
|
268
|
-
// remove it after we handled it in dev mode or delete it to ignore it in production mode.
|
|
269
|
-
// Not removing this would cause an infinite loop in production mode and it just
|
|
270
|
-
// seems more elegant to handle it this way.
|
|
271
|
-
delete value.__stackDetails;
|
|
272
|
-
}
|
|
273
|
-
const clonedProps = cloneObjectProperties(value, true, depth);
|
|
274
|
-
// to save on transferring static __initData field of worklet structure
|
|
275
|
-
// we request serializable value to persist its UI counterpart. This means
|
|
276
|
-
// that the __initData field that contains long strings representing the
|
|
277
|
-
// worklet code, source map, and location, will always be
|
|
278
|
-
// serialized/deserialized once.
|
|
279
|
-
clonedProps.__initData = createSerializable(value.__initData, true, depth + 1);
|
|
280
|
-
const clone = WorkletsModule.createSerializableWorklet(clonedProps,
|
|
281
|
-
// TODO: Check after refactor if we can remove shouldPersistRemote parameter (imho it's redundant here since worklets are always persistent)
|
|
282
|
-
// retain all worklets
|
|
283
|
-
true);
|
|
284
|
-
serializableMappingCache.set(value, clone);
|
|
285
|
-
serializableMappingCache.set(clone);
|
|
286
|
-
freezeObjectInDev(value);
|
|
287
|
-
return clone;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* TurboModuleLike objects are JS objects that have a TurboModule as their
|
|
292
|
-
* prototype.
|
|
293
|
-
*/
|
|
294
|
-
function cloneTurboModuleLike(value, shouldPersistRemote, depth) {
|
|
295
|
-
const proto = Object.getPrototypeOf(value);
|
|
296
|
-
const clonedProps = cloneObjectProperties(value, shouldPersistRemote, depth);
|
|
297
|
-
const clone = WorkletsModule.createSerializableTurboModuleLike(clonedProps, proto);
|
|
298
|
-
return clone;
|
|
299
|
-
}
|
|
300
|
-
function cloneContextObject(value) {
|
|
301
|
-
const workletContextObjectFactory = value.__workletContextObjectFactory;
|
|
302
|
-
const handle = cloneInitializer({
|
|
303
|
-
__init: () => {
|
|
304
|
-
'worklet';
|
|
305
|
-
|
|
306
|
-
return workletContextObjectFactory();
|
|
307
|
-
}
|
|
308
|
-
});
|
|
309
|
-
serializableMappingCache.set(value, handle);
|
|
310
|
-
return handle;
|
|
311
|
-
}
|
|
312
|
-
function clonePlainJSObject(value, shouldPersistRemote, depth) {
|
|
313
|
-
const clonedProps = cloneObjectProperties(value, shouldPersistRemote, depth);
|
|
314
|
-
const clone = WorkletsModule.createSerializableObject(clonedProps, shouldPersistRemote, value);
|
|
315
|
-
serializableMappingCache.set(value, clone);
|
|
316
|
-
serializableMappingCache.set(clone);
|
|
317
|
-
freezeObjectInDev(value);
|
|
318
|
-
return clone;
|
|
319
|
-
}
|
|
320
|
-
function cloneMap(value) {
|
|
321
|
-
const clonedKeys = [];
|
|
322
|
-
const clonedValues = [];
|
|
323
|
-
for (const [key, element] of value.entries()) {
|
|
324
|
-
clonedKeys.push(createSerializable(key));
|
|
325
|
-
clonedValues.push(createSerializable(element));
|
|
326
|
-
}
|
|
327
|
-
const clone = WorkletsModule.createSerializableMap(clonedKeys, clonedValues);
|
|
328
|
-
serializableMappingCache.set(value, clone);
|
|
329
|
-
serializableMappingCache.set(clone);
|
|
330
|
-
freezeObjectInDev(value);
|
|
331
|
-
return clone;
|
|
332
|
-
}
|
|
333
|
-
function cloneSet(value) {
|
|
334
|
-
const clonedElements = [];
|
|
335
|
-
for (const element of value) {
|
|
336
|
-
clonedElements.push(createSerializable(element));
|
|
337
|
-
}
|
|
338
|
-
const clone = WorkletsModule.createSerializableSet(clonedElements);
|
|
339
|
-
serializableMappingCache.set(value, clone);
|
|
340
|
-
serializableMappingCache.set(clone);
|
|
341
|
-
freezeObjectInDev(value);
|
|
342
|
-
return clone;
|
|
343
|
-
}
|
|
344
|
-
function cloneRegExp(value) {
|
|
345
|
-
const pattern = value.source;
|
|
346
|
-
const flags = value.flags;
|
|
347
|
-
const handle = cloneInitializer({
|
|
348
|
-
__init: () => {
|
|
349
|
-
'worklet';
|
|
350
|
-
|
|
351
|
-
return new RegExp(pattern, flags);
|
|
352
|
-
}
|
|
353
|
-
});
|
|
354
|
-
serializableMappingCache.set(value, handle);
|
|
355
|
-
return handle;
|
|
356
|
-
}
|
|
357
|
-
function cloneError(value) {
|
|
358
|
-
const {
|
|
359
|
-
name,
|
|
360
|
-
message,
|
|
361
|
-
stack
|
|
362
|
-
} = value;
|
|
363
|
-
const handle = cloneInitializer({
|
|
364
|
-
__init: () => {
|
|
365
|
-
'worklet';
|
|
366
|
-
|
|
367
|
-
// eslint-disable-next-line reanimated/use-worklets-error
|
|
368
|
-
const error = new Error();
|
|
369
|
-
error.name = name;
|
|
370
|
-
error.message = message;
|
|
371
|
-
error.stack = stack;
|
|
372
|
-
return error;
|
|
373
|
-
}
|
|
374
|
-
});
|
|
375
|
-
serializableMappingCache.set(value, handle);
|
|
376
|
-
return handle;
|
|
377
|
-
}
|
|
378
|
-
function cloneArrayBuffer(value, shouldPersistRemote) {
|
|
379
|
-
const clone = WorkletsModule.createSerializable(value, shouldPersistRemote, value);
|
|
380
|
-
serializableMappingCache.set(value, clone);
|
|
381
|
-
serializableMappingCache.set(clone);
|
|
382
|
-
return clone;
|
|
383
|
-
}
|
|
384
|
-
function cloneArrayBufferView(value) {
|
|
385
|
-
const buffer = value.buffer;
|
|
386
|
-
const typeName = value.constructor.name;
|
|
387
|
-
const handle = cloneInitializer({
|
|
388
|
-
__init: () => {
|
|
389
|
-
'worklet';
|
|
390
|
-
|
|
391
|
-
if (!VALID_ARRAY_VIEWS_NAMES.includes(typeName)) {
|
|
392
|
-
throw new WorkletsError(`Invalid array view name \`${typeName}\`.`);
|
|
393
|
-
}
|
|
394
|
-
const constructor = global[typeName];
|
|
395
|
-
if (constructor === undefined) {
|
|
396
|
-
throw new WorkletsError(`Constructor for \`${typeName}\` not found.`);
|
|
397
|
-
}
|
|
398
|
-
return new constructor(buffer);
|
|
399
|
-
}
|
|
400
|
-
});
|
|
401
|
-
serializableMappingCache.set(value, handle);
|
|
402
|
-
return handle;
|
|
403
|
-
}
|
|
404
|
-
function cloneSynchronizable(value) {
|
|
405
|
-
serializableMappingCache.set(value);
|
|
406
|
-
return value;
|
|
407
|
-
}
|
|
408
|
-
function cloneImport(value) {
|
|
409
|
-
const {
|
|
410
|
-
source,
|
|
411
|
-
imported
|
|
412
|
-
} = value.__bundleData;
|
|
413
|
-
const clone = WorkletsModule.createSerializableImport(source, imported);
|
|
414
|
-
serializableMappingCache.set(value, clone);
|
|
415
|
-
serializableMappingCache.set(clone);
|
|
416
|
-
return clone;
|
|
417
|
-
}
|
|
418
|
-
function inaccessibleObject(value) {
|
|
419
|
-
// This is reached for object types that are not of plain Object.prototype.
|
|
420
|
-
// We don't support such objects from being transferred as serializables to
|
|
421
|
-
// the UI runtime and hence we replace them with "inaccessible object"
|
|
422
|
-
// which is implemented as a Proxy object that throws on any attempt
|
|
423
|
-
// of accessing its fields. We argue that such objects can sometimes leak
|
|
424
|
-
// as attributes of objects being captured by worklets but should never
|
|
425
|
-
// be used on the UI runtime regardless. If they are being accessed, the user
|
|
426
|
-
// will get an appropriate error message.
|
|
427
|
-
const clone = createSerializable(INACCESSIBLE_OBJECT);
|
|
428
|
-
serializableMappingCache.set(value, clone);
|
|
429
|
-
return clone;
|
|
430
|
-
}
|
|
431
|
-
const WORKLET_CODE_THRESHOLD = 255;
|
|
432
|
-
function getWorkletCode(value) {
|
|
433
|
-
const code = value?.__initData?.code;
|
|
434
|
-
if (!code) {
|
|
435
|
-
return 'unknown';
|
|
436
|
-
}
|
|
437
|
-
if (code.length > WORKLET_CODE_THRESHOLD) {
|
|
438
|
-
return `${code.substring(0, WORKLET_CODE_THRESHOLD)}...`;
|
|
439
|
-
}
|
|
440
|
-
return code;
|
|
441
|
-
}
|
|
442
|
-
function isRemoteFunction(value) {
|
|
443
|
-
'worklet';
|
|
444
|
-
|
|
445
|
-
return !!value.__remoteFunction;
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
/**
|
|
449
|
-
* We freeze
|
|
450
|
-
*
|
|
451
|
-
* - Arrays,
|
|
452
|
-
* - Remote functions,
|
|
453
|
-
* - Plain JS objects,
|
|
454
|
-
*
|
|
455
|
-
* That are transformed to a serializable with a meaningful warning. This should
|
|
456
|
-
* help detect issues when someone modifies data after it's been converted.
|
|
457
|
-
* Meaning that they may be doing a faulty assumption in their code expecting
|
|
458
|
-
* that the updates are going to automatically propagate to the object sent to
|
|
459
|
-
* the UI thread. If the user really wants some objects to be mutable they
|
|
460
|
-
* should use shared values instead.
|
|
461
|
-
*/
|
|
462
|
-
function freezeObjectInDev(value) {
|
|
463
|
-
if (!__DEV__) {
|
|
464
|
-
return;
|
|
465
|
-
}
|
|
466
|
-
Object.entries(value).forEach(([key, element]) => {
|
|
467
|
-
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
468
|
-
if (!descriptor.configurable) {
|
|
469
|
-
return;
|
|
470
|
-
}
|
|
471
|
-
Object.defineProperty(value, key, {
|
|
472
|
-
get() {
|
|
473
|
-
return element;
|
|
474
|
-
},
|
|
475
|
-
set() {
|
|
476
|
-
logger.warn(`Tried to modify key \`${key}\` of an object which has been already passed to a worklet. See
|
|
477
|
-
https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#tried-to-modify-key-of-an-object-which-has-been-converted-to-a-serializable
|
|
478
|
-
for more details.`);
|
|
479
|
-
}
|
|
480
|
-
});
|
|
481
|
-
});
|
|
482
|
-
Object.preventExtensions(value);
|
|
483
|
-
}
|
|
484
|
-
function makeShareableCloneOnUIRecursiveLEGACY(value) {
|
|
485
|
-
'worklet';
|
|
486
|
-
|
|
487
|
-
if (SHOULD_BE_USE_WEB) {
|
|
488
|
-
// @ts-ignore web is an interesting place where we don't run a secondary VM on the UI thread
|
|
489
|
-
// see more details in the comment where USE_STUB_IMPLEMENTATION is defined.
|
|
490
|
-
return value;
|
|
491
|
-
}
|
|
492
|
-
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
493
|
-
function cloneRecursive(value) {
|
|
494
|
-
if (typeof value === 'object' && value !== null || typeof value === 'function') {
|
|
495
|
-
if (isHostObject(value)) {
|
|
496
|
-
// We call `_createSerializableClone` to wrap the provided HostObject
|
|
497
|
-
// inside SerializableJSRef.
|
|
498
|
-
return global._createSerializableHostObject(value);
|
|
499
|
-
}
|
|
500
|
-
if (isRemoteFunction(value)) {
|
|
501
|
-
// RemoteFunctions are created by us therefore they are
|
|
502
|
-
// a Serializable out of the box and there is no need to
|
|
503
|
-
// call `_createSerializableClone`.
|
|
504
|
-
return value.__remoteFunction;
|
|
505
|
-
}
|
|
506
|
-
if (Array.isArray(value)) {
|
|
507
|
-
return global._createSerializableArray(value.map(cloneRecursive));
|
|
508
|
-
}
|
|
509
|
-
if (value.__synchronizableRef) {
|
|
510
|
-
return global._createSerializableSynchronizable(value);
|
|
511
|
-
}
|
|
512
|
-
const toAdapt = {};
|
|
513
|
-
for (const [key, element] of Object.entries(value)) {
|
|
514
|
-
toAdapt[key] = cloneRecursive(element);
|
|
515
|
-
}
|
|
516
|
-
return global._createSerializable(toAdapt, value);
|
|
517
|
-
}
|
|
518
|
-
if (typeof value === 'string') {
|
|
519
|
-
return global._createSerializableString(value);
|
|
520
|
-
}
|
|
521
|
-
if (typeof value === 'number') {
|
|
522
|
-
return global._createSerializableNumber(value);
|
|
523
|
-
}
|
|
524
|
-
if (typeof value === 'boolean') {
|
|
525
|
-
return global._createSerializableBoolean(value);
|
|
526
|
-
}
|
|
527
|
-
if (typeof value === 'bigint') {
|
|
528
|
-
return global._createSerializableBigInt(value);
|
|
529
|
-
}
|
|
530
|
-
if (value === undefined) {
|
|
531
|
-
return global._createSerializableUndefined();
|
|
532
|
-
}
|
|
533
|
-
if (value === null) {
|
|
534
|
-
return global._createSerializableNull();
|
|
535
|
-
}
|
|
536
|
-
return global._createSerializable(value, undefined);
|
|
537
|
-
}
|
|
538
|
-
return cloneRecursive(value);
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
/** @deprecated This function is no longer supported. */
|
|
542
|
-
export const makeShareableCloneOnUIRecursive = globalThis._WORKLETS_BUNDLE_MODE ? createSerializable : makeShareableCloneOnUIRecursiveLEGACY;
|
|
543
|
-
function makeShareableJS(value) {
|
|
544
|
-
return value;
|
|
545
|
-
}
|
|
546
|
-
function makeShareableNative(value) {
|
|
547
|
-
if (serializableMappingCache.get(value)) {
|
|
548
|
-
return value;
|
|
549
|
-
}
|
|
550
|
-
const handle = createSerializable({
|
|
551
|
-
__init: () => {
|
|
552
|
-
'worklet';
|
|
553
|
-
|
|
554
|
-
return value;
|
|
555
|
-
}
|
|
556
|
-
});
|
|
557
|
-
serializableMappingCache.set(value, handle);
|
|
558
|
-
return value;
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
/**
|
|
562
|
-
* This function creates a value on UI with persistent state - changes to it on
|
|
563
|
-
* the UI thread will be seen by all worklets. Use it when you want to create a
|
|
564
|
-
* value that is read and written only on the UI thread.
|
|
565
|
-
*/
|
|
566
|
-
/** @deprecated This function is no longer supported. */
|
|
567
|
-
export const makeShareable = SHOULD_BE_USE_WEB ? makeShareableJS : makeShareableNative;
|
|
568
|
-
//# sourceMappingURL=serializable.js.map
|