@amazon-devices/react-native-worklets 1.0.1 → 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.
Files changed (95) hide show
  1. package/kepler-compatibility.json +2 -1
  2. package/lib/module/index.js +0 -25
  3. package/package.json +1 -1
  4. package/src/index.ts +0 -52
  5. package/bundleMode/index.js +0 -59
  6. package/lib/module/PlatformChecker/PlatformChecker.js +0 -8
  7. package/lib/module/PlatformChecker/index.js +0 -18
  8. package/lib/module/WorkletsError.js +0 -29
  9. package/lib/module/WorkletsModule/JSWorklets.js +0 -111
  10. package/lib/module/WorkletsModule/NativeWorklets.js +0 -126
  11. package/lib/module/WorkletsModule/index.js +0 -4
  12. package/lib/module/WorkletsModule/workletsModuleInstance.js +0 -7
  13. package/lib/module/WorkletsModule/workletsModuleInstance.web.js +0 -5
  14. package/lib/module/WorkletsModule/workletsModuleProxy.js +0 -4
  15. package/lib/module/bundleUnpacker.js +0 -47
  16. package/lib/module/callGuard.js +0 -30
  17. package/lib/module/deprecated.js +0 -20
  18. package/lib/module/errors.js +0 -69
  19. package/lib/module/featureFlags/index.js +0 -48
  20. package/lib/module/initializers.js +0 -221
  21. package/lib/module/isSynchronizable.js +0 -6
  22. package/lib/module/logger.js +0 -15
  23. package/lib/module/privateGlobals.d.js +0 -7
  24. package/lib/module/publicGlobals.js +0 -6
  25. package/lib/module/runLoop/common/setImmediatePolyfill.js +0 -15
  26. package/lib/module/runLoop/common/setIntervalPolyfill.js +0 -26
  27. package/lib/module/runLoop/uiRuntime/mockedRequestAnimationFrame.js +0 -10
  28. package/lib/module/runLoop/uiRuntime/requestAnimationFrame.js +0 -52
  29. package/lib/module/runLoop/uiRuntime/setTimeoutPolyfill.js +0 -32
  30. package/lib/module/runLoop/workletRuntime/index.js +0 -19
  31. package/lib/module/runLoop/workletRuntime/queueMicrotask.js +0 -11
  32. package/lib/module/runLoop/workletRuntime/requestAnimationFramePolyfill.js +0 -48
  33. package/lib/module/runLoop/workletRuntime/setTimeout.js +0 -28
  34. package/lib/module/runLoop/workletRuntime/taskQueue.js +0 -37
  35. package/lib/module/runtimeKind.js +0 -39
  36. package/lib/module/runtimes.js +0 -100
  37. package/lib/module/serializable.js +0 -568
  38. package/lib/module/serializableMappingCache.js +0 -37
  39. package/lib/module/specs/NativeWorkletsModule.js +0 -5
  40. package/lib/module/specs/NativeWorkletsModule.kepler.js +0 -7
  41. package/lib/module/specs/index.js +0 -10
  42. package/lib/module/synchronizable.js +0 -9
  43. package/lib/module/synchronizableUnpacker.js +0 -42
  44. package/lib/module/threads.js +0 -347
  45. package/lib/module/utils/checkCppVersion.js +0 -30
  46. package/lib/module/utils/jsVersion.js +0 -9
  47. package/lib/module/valueUnpacker.js +0 -60
  48. package/lib/module/workletFunction.js +0 -37
  49. package/lib/module/workletRuntimeEntry.js +0 -32
  50. package/lib/module/workletTypes.js +0 -12
  51. package/scripts/validate-react-native-version.js +0 -43
  52. package/src/PlatformChecker/PlatformChecker.ts +0 -7
  53. package/src/PlatformChecker/index.ts +0 -30
  54. package/src/WorkletsError.ts +0 -36
  55. package/src/WorkletsModule/JSWorklets.ts +0 -200
  56. package/src/WorkletsModule/NativeWorklets.ts +0 -256
  57. package/src/WorkletsModule/index.ts +0 -7
  58. package/src/WorkletsModule/workletsModuleInstance.ts +0 -9
  59. package/src/WorkletsModule/workletsModuleInstance.web.ts +0 -5
  60. package/src/WorkletsModule/workletsModuleProxy.ts +0 -126
  61. package/src/bundleUnpacker.ts +0 -75
  62. package/src/callGuard.ts +0 -33
  63. package/src/deprecated.ts +0 -32
  64. package/src/errors.ts +0 -80
  65. package/src/featureFlags/index.ts +0 -72
  66. package/src/initializers.ts +0 -262
  67. package/src/isSynchronizable.ts +0 -13
  68. package/src/logger.ts +0 -16
  69. package/src/publicGlobals.ts +0 -39
  70. package/src/runLoop/common/setImmediatePolyfill.ts +0 -20
  71. package/src/runLoop/common/setIntervalPolyfill.ts +0 -38
  72. package/src/runLoop/uiRuntime/mockedRequestAnimationFrame.ts +0 -11
  73. package/src/runLoop/uiRuntime/requestAnimationFrame.ts +0 -66
  74. package/src/runLoop/uiRuntime/setTimeoutPolyfill.ts +0 -40
  75. package/src/runLoop/workletRuntime/index.ts +0 -18
  76. package/src/runLoop/workletRuntime/queueMicrotask.ts +0 -13
  77. package/src/runLoop/workletRuntime/requestAnimationFramePolyfill.ts +0 -61
  78. package/src/runLoop/workletRuntime/setTimeout.ts +0 -37
  79. package/src/runLoop/workletRuntime/taskQueue.ts +0 -44
  80. package/src/runtimeKind.ts +0 -37
  81. package/src/runtimes.ts +0 -193
  82. package/src/serializable.ts +0 -801
  83. package/src/serializableMappingCache.ts +0 -42
  84. package/src/specs/NativeWorkletsModule.kepler.ts +0 -11
  85. package/src/specs/NativeWorkletsModule.ts +0 -9
  86. package/src/specs/index.ts +0 -13
  87. package/src/synchronizable.ts +0 -32
  88. package/src/synchronizableUnpacker.ts +0 -67
  89. package/src/threads.ts +0 -452
  90. package/src/utils/checkCppVersion.ts +0 -34
  91. package/src/utils/jsVersion.ts +0 -7
  92. package/src/valueUnpacker.ts +0 -90
  93. package/src/workletFunction.ts +0 -42
  94. package/src/workletRuntimeEntry.ts +0 -35
  95. package/src/workletTypes.ts +0 -81
@@ -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
@@ -1,37 +0,0 @@
1
- 'use strict';
2
-
3
- import { SHOULD_BE_USE_WEB } from './PlatformChecker';
4
- /**
5
- * This symbol is used to represent a mapping from the value to itself.
6
- *
7
- * It's used to prevent converting a serializable that's already converted - for
8
- * example a Shared Value that's in worklet's closure.
9
- */
10
- export const serializableMappingFlag = Symbol('serializable flag');
11
-
12
- /*
13
- During a fast refresh, React holds the same instance of a Mutable
14
- (that's guaranteed by `useRef`) but `serializableCache` gets regenerated and thus
15
- becoming empty. This happens when editing the file that contains the definition of this cache.
16
-
17
- Because of it, `createSerializable` can't find given mapping
18
- in `serializableCache` for the Mutable and tries to clone it as if it was a regular JS object.
19
- During cloning we use `Object.entries` to iterate over the keys which throws an error on accessing `_value`.
20
- For convenience we moved this cache to a separate file so it doesn't scare us with red squiggles.
21
- */
22
-
23
- const cache = SHOULD_BE_USE_WEB ? null : new WeakMap();
24
- export const serializableMappingCache = SHOULD_BE_USE_WEB ? {
25
- set() {
26
- // NOOP
27
- },
28
- get() {
29
- return null;
30
- }
31
- } : {
32
- set(serializable, serializableRef) {
33
- cache.set(serializable, serializableRef || serializableMappingFlag);
34
- },
35
- get: cache.get.bind(cache)
36
- };
37
- //# sourceMappingURL=serializableMappingCache.js.map
@@ -1,5 +0,0 @@
1
- 'use strict';
2
-
3
- import { TurboModuleRegistry } from 'react-native';
4
- export default TurboModuleRegistry.get('WorkletsModule');
5
- //# sourceMappingURL=NativeWorkletsModule.js.map
@@ -1,7 +0,0 @@
1
- 'use strict';
2
-
3
- // @ts-ignore No type definitions for KeplerTurboModule
4
- // @ts-ignore No type definitions for TurboModuleRegistry
5
- import * as TurboModuleRegistry from '@amazon-devices/react-native-kepler/Libraries/TurboModule/TurboModuleRegistry';
6
- export default TurboModuleRegistry.get('WorkletsModule');
7
- //# sourceMappingURL=NativeWorkletsModule.kepler.js.map
@@ -1,10 +0,0 @@
1
- 'use strict';
2
-
3
- import { RuntimeKind } from '../runtimeKind';
4
- import RNWorkletsTurboModule from './NativeWorkletsModule';
5
- export const WorkletsTurboModule = globalThis.__RUNTIME_KIND === RuntimeKind.ReactNative ? RNWorkletsTurboModule :
6
- // In Bundle Mode, on Worklet Runtimes `RNWorkletsTurboModule` isn't
7
- // available and shouldn't be accessed. We return null here
8
- // to keep the same codebase for the Bundle Mode and legacy behavior.
9
- null;
10
- //# sourceMappingURL=index.js.map
@@ -1,9 +0,0 @@
1
- 'use strict';
2
-
3
- import { createSerializable } from './serializable';
4
- import { WorkletsModule } from './WorkletsModule';
5
- export function createSynchronizable(initialValue) {
6
- const synchronizableRef = WorkletsModule.createSynchronizable(createSerializable(initialValue));
7
- return globalThis.__synchronizableUnpacker(synchronizableRef);
8
- }
9
- //# sourceMappingURL=synchronizable.js.map
@@ -1,42 +0,0 @@
1
- 'use strict';
2
-
3
- import { createSerializable } from './serializable';
4
- export function __installUnpacker() {
5
- // TODO: Add cache for synchronizables.
6
- const serializer = !globalThis._WORKLET || globalThis._WORKLETS_BUNDLE_MODE ? (value, _) => createSerializable(value) : globalThis._createSerializable;
7
- function synchronizableUnpacker(synchronizableRef) {
8
- const synchronizable = synchronizableRef;
9
- const proxy = globalThis.__workletsModuleProxy;
10
- synchronizable.__synchronizableRef = true;
11
- synchronizable.getDirty = () => {
12
- return proxy.synchronizableGetDirty(synchronizable);
13
- };
14
- synchronizable.getBlocking = () => {
15
- return proxy.synchronizableGetBlocking(synchronizable);
16
- };
17
- synchronizable.setBlocking = valueOrFunction => {
18
- let newValue;
19
- if (typeof valueOrFunction === 'function') {
20
- const func = valueOrFunction;
21
- synchronizable.lock();
22
- const prev = synchronizable.getBlocking();
23
- newValue = func(prev);
24
- proxy.synchronizableSetBlocking(synchronizable, serializer(newValue, undefined));
25
- synchronizable.unlock();
26
- } else {
27
- const value = valueOrFunction;
28
- newValue = value;
29
- proxy.synchronizableSetBlocking(synchronizable, serializer(newValue, undefined));
30
- }
31
- };
32
- synchronizable.lock = () => {
33
- proxy.synchronizableLock(synchronizable);
34
- };
35
- synchronizable.unlock = () => {
36
- proxy.synchronizableUnlock(synchronizable);
37
- };
38
- return synchronizable;
39
- }
40
- globalThis.__synchronizableUnpacker = synchronizableUnpacker;
41
- }
42
- //# sourceMappingURL=synchronizableUnpacker.js.map