react-native-worklets 0.10.0 → 0.10.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 (48) hide show
  1. package/Common/cpp/worklets/NativeModules/JSIWorkletsModuleProxy.cpp +8 -11
  2. package/Common/cpp/worklets/NativeModules/JSIWorkletsModuleProxy.h +5 -0
  3. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.cpp +4 -1
  4. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.h +4 -1
  5. package/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.cpp +1 -1
  6. package/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.h +11 -8
  7. package/Common/cpp/worklets/SharedItems/Serializable.h +9 -3
  8. package/Common/cpp/worklets/SharedItems/SerializableFactory.cpp +17 -8
  9. package/Common/cpp/worklets/SharedItems/SerializableFactory.h +8 -5
  10. package/Common/cpp/worklets/SharedItems/SerializableRemoteFunction.cpp +34 -76
  11. package/Common/cpp/worklets/SharedItems/SerializableRemoteFunction.h +40 -113
  12. package/Common/cpp/worklets/Tools/RNRuntimeStatus.h +27 -0
  13. package/android/src/main/cpp/worklets/android/WorkletsModule.cpp +6 -2
  14. package/android/src/main/cpp/worklets/android/WorkletsModule.h +2 -0
  15. package/android/src/main/java/com/swmansion/worklets/ScriptBufferWrapper.kt +4 -0
  16. package/android/src/networking/com/swmansion/worklets/WorkletsModule.kt +0 -3
  17. package/android/src/no-networking/com/swmansion/worklets/WorkletsModule.kt +0 -3
  18. package/apple/worklets/apple/WorkletsModule.mm +6 -4
  19. package/bundleMode/index.js +2 -1
  20. package/lib/module/WorkletsModule/NativeWorklets.native.js +2 -2
  21. package/lib/module/WorkletsModule/NativeWorklets.native.js.map +1 -1
  22. package/lib/module/debug/jsVersion.js +1 -1
  23. package/lib/module/memory/serializable.native.js +16 -11
  24. package/lib/module/memory/serializable.native.js.map +1 -1
  25. package/lib/module/memory/serializableMappingCache.native.js.map +1 -1
  26. package/lib/typescript/WorkletsModule/NativeWorklets.native.d.ts.map +1 -1
  27. package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts +1 -1
  28. package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts.map +1 -1
  29. package/lib/typescript/debug/jsVersion.d.ts +1 -1
  30. package/lib/typescript/deprecated.d.ts +2 -2
  31. package/lib/typescript/memory/serializable.native.d.ts.map +1 -1
  32. package/lib/typescript/memory/serializableMappingCache.native.d.ts +2 -2
  33. package/lib/typescript/memory/serializableMappingCache.native.d.ts.map +1 -1
  34. package/lib/typescript/memory/types.d.ts +0 -4
  35. package/lib/typescript/memory/types.d.ts.map +1 -1
  36. package/package.json +1 -1
  37. package/src/WorkletsModule/NativeWorklets.native.ts +0 -2
  38. package/src/WorkletsModule/workletsModuleProxy.ts +0 -1
  39. package/src/debug/jsVersion.ts +1 -1
  40. package/src/memory/serializable.native.ts +17 -19
  41. package/src/memory/serializableMappingCache.native.ts +8 -2
  42. package/src/memory/types.ts +0 -5
  43. package/src/privateGlobals.d.ts +0 -1
  44. package/lib/module/memory/remoteFunctionRegistry.native.js +0 -12
  45. package/lib/module/memory/remoteFunctionRegistry.native.js.map +0 -1
  46. package/lib/typescript/memory/remoteFunctionRegistry.native.d.ts +0 -3
  47. package/lib/typescript/memory/remoteFunctionRegistry.native.d.ts.map +0 -1
  48. package/src/memory/remoteFunctionRegistry.native.ts +0 -14
@@ -8,17 +8,12 @@ import type { WorkletFunction, WorkletImport } from '../types';
8
8
  import { isWorkletFunction } from '../workletFunction';
9
9
  import { WorkletsModule } from '../WorkletsModule/NativeWorklets';
10
10
  import { isSynchronizable } from './isSynchronizable';
11
- import {
12
- nextRemoteFunctionId,
13
- registerRemoteFunction,
14
- } from './remoteFunctionRegistry';
15
11
  import {
16
12
  serializableMappingCache,
17
13
  serializableMappingFlag,
18
14
  } from './serializableMappingCache';
19
15
  import type {
20
16
  FlatSerializableRef,
21
- RegisteredRemoteFunction,
22
17
  RegistrationData,
23
18
  RemoteFunction,
24
19
  SerializableRef,
@@ -132,7 +127,15 @@ export function createSerializable<TValue>(
132
127
 
133
128
  const cached = getFromCache(value);
134
129
  if (cached !== undefined) {
135
- return cached as SerializableRef<TValue>;
130
+ if (globalThis.WeakRef && cached instanceof WeakRef) {
131
+ // WeakRef is installed on runtimes only with Hermes microtaskQueue enabled.
132
+ const deref = cached.deref();
133
+ if (deref !== undefined) {
134
+ return deref as SerializableRef<TValue>;
135
+ }
136
+ } else {
137
+ return cached as SerializableRef<TValue>;
138
+ }
136
139
  }
137
140
 
138
141
  if (Array.isArray(value)) {
@@ -206,12 +209,8 @@ export function createSerializable<TValue>(
206
209
  return cloneCustom(value, pack, i) as SerializableRef<TValue>;
207
210
  }
208
211
  }
209
- const constructorName =
210
- (value as { constructor?: { name?: string } })?.constructor?.name ||
211
- 'unknown';
212
- throw new Error(
213
- `[Worklets] Cannot copy value of type \`${constructorName}\`.`
214
- );
212
+
213
+ return cloneUndefined() as SerializableRef<TValue>;
215
214
  }
216
215
 
217
216
  if (globalThis._WORKLETS_BUNDLE_MODE_ENABLED) {
@@ -400,19 +399,18 @@ function cloneArray<T extends unknown[]>(
400
399
  function cloneNonWorkletFunction<TArgs extends unknown[], TReturn>(
401
400
  fun: (...args: TArgs) => TReturn
402
401
  ): SerializableRef<(...args: TArgs) => TReturn> {
403
- const functionId = nextRemoteFunctionId;
404
402
  const clone = WorkletsModule.createSerializableNonWorkletFunction(
405
403
  fun,
406
- functionId,
407
404
  __DEV__ ? fun.name : undefined
408
405
  ) as SerializableRef<(...args: TArgs) => TReturn>;
409
- if ((clone as RegisteredRemoteFunction).__keepAlive) {
410
- registerRemoteFunction(fun);
406
+
407
+ if (globalThis.WeakRef) {
408
+ // WeakRef is installed on runtimes only with Hermes microtaskQueue enabled.
409
+ serializableMappingCache.set(fun, new WeakRef(clone));
410
+ serializableMappingCache.set(clone);
411
+ freezeObjectInDev(fun);
411
412
  }
412
- serializableMappingCache.set(fun, clone);
413
- serializableMappingCache.set(clone);
414
413
 
415
- freezeObjectInDev(fun);
416
414
  return clone;
417
415
  }
418
416
 
@@ -21,10 +21,16 @@ During cloning we use `Object.entries` to iterate over the keys which throws an
21
21
  For convenience we moved this cache to a separate file so it doesn't scare us with red squiggles.
22
22
  */
23
23
 
24
- const cache = new WeakMap<object, SerializableRef | symbol>();
24
+ const cache = new WeakMap<
25
+ object,
26
+ SerializableRef | WeakRef<SerializableRef> | symbol
27
+ >();
25
28
 
26
29
  export const serializableMappingCache = {
27
- set(serializable: object, serializableRef?: SerializableRef): void {
30
+ set(
31
+ serializable: object,
32
+ serializableRef?: SerializableRef | WeakRef<SerializableRef>
33
+ ): void {
28
34
  cache.set(serializable, serializableRef || serializableMappingFlag);
29
35
  },
30
36
  get: cache.get.bind(cache),
@@ -189,8 +189,3 @@ export interface RemoteFunction extends SerializableRef {
189
189
  /** Defined when extracted on a Guest Runtime. */
190
190
  __remoteFunction: true | undefined;
191
191
  }
192
-
193
- export interface RegisteredRemoteFunction extends SerializableRef {
194
- /** Defined when created on the Host Runtime. */
195
- __keepAlive: true | undefined;
196
- }
@@ -112,7 +112,6 @@ declare global {
112
112
  unknown,
113
113
  unknown
114
114
  >;
115
- var __remoteFunctionRegistry: Map<number, Function>;
116
115
  /** Only in Bundle Mode on Worklet Runtimes. */
117
116
  var TurboModules: Map<string, unknown>;
118
117
  /**
@@ -1,12 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-unsafe-function-type */
2
- 'use strict';
3
-
4
- const remoteFunctionRegistry = new Map();
5
- export let nextRemoteFunctionId = 1;
6
- export function registerRemoteFunction(fun) {
7
- const id = nextRemoteFunctionId++;
8
- remoteFunctionRegistry.set(id, fun);
9
- return id;
10
- }
11
- globalThis.__remoteFunctionRegistry = remoteFunctionRegistry;
12
- //# sourceMappingURL=remoteFunctionRegistry.native.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["remoteFunctionRegistry","Map","nextRemoteFunctionId","registerRemoteFunction","fun","id","set","globalThis","__remoteFunctionRegistry"],"sourceRoot":"../../../src","sources":["memory/remoteFunctionRegistry.native.ts"],"mappings":"AAAA;AACA,YAAY;;AAEZ,MAAMA,sBAAsB,GAAG,IAAIC,GAAG,CAAmB,CAAC;AAE1D,OAAO,IAAIC,oBAAoB,GAAG,CAAC;AAEnC,OAAO,SAASC,sBAAsBA,CAACC,GAAa,EAAU;EAC5D,MAAMC,EAAE,GAAGH,oBAAoB,EAAE;EACjCF,sBAAsB,CAACM,GAAG,CAACD,EAAE,EAAED,GAAG,CAAC;EACnC,OAAOC,EAAE;AACX;AAEAE,UAAU,CAACC,wBAAwB,GAAGR,sBAAsB","ignoreList":[]}
@@ -1,3 +0,0 @@
1
- export declare let nextRemoteFunctionId: number;
2
- export declare function registerRemoteFunction(fun: Function): number;
3
- //# sourceMappingURL=remoteFunctionRegistry.native.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"remoteFunctionRegistry.native.d.ts","sourceRoot":"","sources":["../../../src/memory/remoteFunctionRegistry.native.ts"],"names":[],"mappings":"AAKA,eAAO,IAAI,oBAAoB,QAAI,CAAC;AAEpC,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,QAAQ,GAAG,MAAM,CAI5D"}
@@ -1,14 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-unsafe-function-type */
2
- 'use strict';
3
-
4
- const remoteFunctionRegistry = new Map<number, Function>();
5
-
6
- export let nextRemoteFunctionId = 1;
7
-
8
- export function registerRemoteFunction(fun: Function): number {
9
- const id = nextRemoteFunctionId++;
10
- remoteFunctionRegistry.set(id, fun);
11
- return id;
12
- }
13
-
14
- globalThis.__remoteFunctionRegistry = remoteFunctionRegistry;