react-native-reanimated 3.5.1 → 3.5.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/lib/module/reanimated2/platform-specific/jsVersion.js +1 -1
- package/lib/module/reanimated2/platform-specific/jsVersion.js.map +1 -1
- package/lib/module/reanimated2/shareables.js +1 -1
- package/lib/module/reanimated2/shareables.js.map +1 -1
- package/lib/module/reanimated2/threads.js +2 -2
- package/lib/module/reanimated2/threads.js.map +1 -1
- package/lib/module/reanimated2/valueUnpacker.js +0 -2
- package/lib/module/reanimated2/valueUnpacker.js.map +1 -1
- package/lib/typescript/reanimated2/platform-specific/jsVersion.d.ts +1 -1
- package/package.json +1 -1
- package/src/reanimated2/platform-specific/jsVersion.ts +1 -1
- package/src/reanimated2/shareables.ts +4 -2
- package/src/reanimated2/threads.ts +5 -3
- package/src/reanimated2/valueUnpacker.ts +8 -4
|
@@ -3,5 +3,5 @@
|
|
|
3
3
|
* with the version used to build the native part of the library in runtime.
|
|
4
4
|
* Remember to keep this in sync with the version declared in `package.json`
|
|
5
5
|
*/
|
|
6
|
-
export const jsVersion = '3.5.
|
|
6
|
+
export const jsVersion = '3.5.2';
|
|
7
7
|
//# sourceMappingURL=jsVersion.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["jsVersion"],"sources":["jsVersion.ts"],"sourcesContent":["/**\n * We hardcode the version of Reanimated here in order to compare it\n * with the version used to build the native part of the library in runtime.\n * Remember to keep this in sync with the version declared in `package.json`\n */\nexport const jsVersion = '3.5.
|
|
1
|
+
{"version":3,"names":["jsVersion"],"sources":["jsVersion.ts"],"sourcesContent":["/**\n * We hardcode the version of Reanimated here in order to compare it\n * with the version used to build the native part of the library in runtime.\n * Remember to keep this in sync with the version declared in `package.json`\n */\nexport const jsVersion = '3.5.2';\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMA,SAAS,GAAG,OAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["NativeReanimatedModule","shouldBeUseWeb","registerWorkletStackDetails","jsVersion","USE_STUB_IMPLEMENTATION","_shareableCache","WeakMap","_shareableFlag","Symbol","MAGIC_KEY","isHostObject","value","registerShareableMapping","shareable","shareableRef","set","isPlainJSObject","object","Object","getPrototypeOf","prototype","INACCESSIBLE_OBJECT","__init","Proxy","get","_","prop","Error","String","DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD","processedObjectAtThresholdDepth","makeShareableCloneRecursive","shouldPersistRemote","arguments","length","undefined","depth","type","isTypeObject","isTypeFunction","cached","toAdapt","Array","isArray","map","element","__workletHash","__DEV__","babelVersion","__initData","version","getWorkletCode","__stackDetails","key","entries","RegExp","pattern","source","flags","handle","inaccessibleObject","freeze","adopted","makeShareableClone","WORKLET_CODE_THRESHOLD","_value$__initData","code","substring","isRemoteFunction","makeShareableCloneOnUIRecursive","cloneRecursive","_makeShareableClone","__remoteFunction","makeShareable"],"sources":["shareables.ts"],"sourcesContent":["import NativeReanimatedModule from './NativeReanimated';\nimport type {\n ShareableRef,\n FlatShareableRef,\n __WorkletFunction,\n} from './commonTypes';\nimport { shouldBeUseWeb } from './PlatformChecker';\nimport { registerWorkletStackDetails } from './errors';\nimport { jsVersion } from './platform-specific/jsVersion';\n\n// for web/chrome debugger/jest environments this file provides a stub implementation\n// where no shareable references are used. Instead, the objects themselves are used\n// instead of shareable references, because of the fact that we don't have to deal with\n// runnning the code on separate VMs.\nconst USE_STUB_IMPLEMENTATION = shouldBeUseWeb();\n\nconst _shareableCache = new WeakMap<\n Record<string, unknown>,\n ShareableRef<any> | symbol\n>();\n// the below symbol is used to represent a mapping from the value to itself\n// this is used to allow for a converted shareable to be passed to makeShareableClone\nconst _shareableFlag = Symbol('shareable flag');\n\nconst MAGIC_KEY = 'REANIMATED_MAGIC_KEY';\n\nfunction isHostObject(value: NonNullable<object>) {\n 'worklet';\n // We could use JSI to determine whether an object is a host object, however\n // the below workaround works well and is way faster than an additional JSI call.\n // We use the fact that host objects have broken implementation of `hasOwnProperty`\n // and hence return true for all `in` checks regardless of the key we ask for.\n return MAGIC_KEY in value;\n}\n\nexport function registerShareableMapping(\n shareable: any,\n shareableRef?: ShareableRef<any>\n): void {\n if (USE_STUB_IMPLEMENTATION) {\n return;\n }\n _shareableCache.set(shareable, shareableRef || _shareableFlag);\n}\n\nfunction isPlainJSObject(object: object) {\n return Object.getPrototypeOf(object) === Object.prototype;\n}\n\n// The below object is used as a replacement for objects that cannot be transferred\n// as shareable values. In makeShareableCloneRecursive we detect if an object is of\n// a plain Object.prototype and only allow such objects to be transferred. This lets\n// us avoid all sorts of react internals from leaking into the UI runtime. To make it\n// possible to catch errors when someone actually tries to access such object on the UI\n// runtime, we use the below Proxy object which is instantiated on the UI runtime and\n// throws whenever someone tries to access its fields.\nconst INACCESSIBLE_OBJECT = {\n __init: () => {\n 'worklet';\n return new Proxy(\n {},\n {\n get: (_: any, prop: string | symbol) => {\n if (prop === '_isReanimatedSharedValue') {\n // not very happy about this check here, but we need to allow for\n // \"inaccessible\" objects to be tested with isSharedValue check\n // as it is being used in the mappers when extracing inputs recursively.\n // Apparently we can't check if a key exists there as HostObjects always\n // return true for such tests, so the only possibility for us is to\n // actually access that key and see if it is set to true. We therefore\n // need to allow for this key to be accessed here.\n return false;\n }\n throw new Error(\n `[Reanimated] Trying to access property \\`${String(\n prop\n )}\\` of an object which cannot be sent to the UI runtime.`\n );\n },\n set: () => {\n throw new Error(\n '[Reanimated] Trying to write to an object which cannot be sent to the UI runtime.'\n );\n },\n }\n );\n },\n};\n\nconst DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD = 30;\n// Below variable stores object that we process in makeShareableCloneRecursive at the specified depth.\n// We use it to check if later on the function reenters with the same object\nlet processedObjectAtThresholdDepth: any;\n\nexport function makeShareableCloneRecursive<T>(\n value: any,\n shouldPersistRemote = false,\n depth = 0\n): ShareableRef<T> {\n if (USE_STUB_IMPLEMENTATION) {\n return value;\n }\n if (depth >= DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD) {\n // if we reach certain recursion depth we suspect that we are dealing with a cyclic object.\n // this type of objects are not supported and cannot be trasferred as shareable, so we\n // implement a simple detection mechanism that remembers the value at a given depth and\n // tests whether we try reenter this method later on with the same value. If that happens\n // we throw an appropriate error.\n if (depth === DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD) {\n processedObjectAtThresholdDepth = value;\n } else if (value === processedObjectAtThresholdDepth) {\n throw new Error(\n '[Reanimated] Trying to convert a cyclic object to a shareable. This is not supported.'\n );\n }\n } else {\n processedObjectAtThresholdDepth = undefined;\n }\n // This one actually may be worth to be moved to c++, we also need similar logic to run on the UI thread\n const type = typeof value;\n const isTypeObject = type === 'object';\n const isTypeFunction = type === 'function';\n if ((isTypeObject || isTypeFunction) && value !== null) {\n const cached = _shareableCache.get(value);\n if (cached === _shareableFlag) {\n return value;\n } else if (cached !== undefined) {\n return cached as ShareableRef<T>;\n } else {\n let toAdapt: any;\n if (Array.isArray(value)) {\n toAdapt = value.map((element) =>\n makeShareableCloneRecursive(element, shouldPersistRemote, depth + 1)\n );\n } else if (isTypeFunction && value.__workletHash === undefined) {\n // this is a remote function\n toAdapt = value;\n } else if (isHostObject(value)) {\n // for host objects we pass the reference to the object as shareable and\n // then recreate new host object wrapping the same instance on the UI thread.\n // there is no point of iterating over keys as we do for regular objects.\n toAdapt = value;\n } else if (isPlainJSObject(value) || isTypeFunction) {\n toAdapt = {};\n if (value.__workletHash !== undefined) {\n // we are converting a worklet\n if (__DEV__) {\n const babelVersion = value.__initData.version;\n if (babelVersion === undefined) {\n throw new Error(`[Reanimated] Unknown version of Reanimated Babel plugin.\nSee \\`https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#unknown-version-of-reanimated-babel-plugin\\` for more details. \nOffending code was: \\`${getWorkletCode(value)}\\``);\n } else if (babelVersion !== jsVersion) {\n throw new Error(`[Reanimated] Mismatch between JavaScript code version and Reanimated Babel plugin version (${jsVersion} vs. ${babelVersion}). \nSee \\`https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#mismatch-between-javascript-code-version-and-reanimated-babel-plugin-version\\` for more details.\nOffending code was: \\`${getWorkletCode(value)}\\``);\n }\n registerWorkletStackDetails(\n value.__workletHash,\n value.__stackDetails\n );\n delete value.__stackDetails;\n } else if (value.__stackDetails) {\n // Detected debug version of the worklet in release bundle. This\n // might lead to unexpected issues or errors. Probably one of user\n // dependencies provided transpiled code with debug version of the\n // Reanimated plugin.\n throw new Error(\n `[Reanimated] Using dev bundle in a release app build is not supported.\nSee \\`https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#using-dev-bundle-in-a-release-app-build-is-not-supported\\` for more details.`\n );\n }\n // to save on transferring static __initData field of worklet structure\n // we request shareable value to persist its UI counterpart. This means\n // that the __initData field that contains long strings represeting the\n // worklet code, source map, and location, will always be\n // serialized/deserialized once.\n toAdapt.__initData = makeShareableCloneRecursive(\n value.__initData,\n true,\n depth + 1\n );\n }\n\n for (const [key, element] of Object.entries(value)) {\n toAdapt[key] = makeShareableCloneRecursive(\n element,\n shouldPersistRemote,\n depth + 1\n );\n }\n } else if (value instanceof RegExp) {\n const pattern = value.source;\n const flags = value.flags;\n const handle = makeShareableCloneRecursive({\n __init: () => {\n 'worklet';\n return new RegExp(pattern, flags);\n },\n });\n registerShareableMapping(value, handle);\n return handle as ShareableRef<T>;\n } else {\n // This is reached for object types that are not of plain Object.prototype.\n // We don't support such objects from being transferred as shareables to\n // the UI runtime and hence we replace them with \"inaccessible object\"\n // which is implemented as a Proxy object that throws on any attempt\n // of accessing its fields. We argue that such objects can sometimes leak\n // as attributes of objects being captured by worklets but should never\n // be used on the UI runtime regardless. If they are being accessed, the user\n // will get an appropriate error message.\n const inaccessibleObject =\n makeShareableCloneRecursive<T>(INACCESSIBLE_OBJECT);\n _shareableCache.set(value, inaccessibleObject);\n return inaccessibleObject;\n }\n if (__DEV__) {\n // we freeze objects that are transformed to shareable. This should help\n // detect issues when someone modifies data after it's been converted to\n // shareable. Meaning that they may be doing a faulty assumption in their\n // code expecting that the updates are going to automatically populate to\n // the object sent to the UI thread. If the user really wants some objects\n // to be mutable they should use shared values instead.\n Object.freeze(value);\n }\n const adopted = NativeReanimatedModule.makeShareableClone(\n toAdapt,\n shouldPersistRemote\n );\n _shareableCache.set(value, adopted);\n _shareableCache.set(adopted, _shareableFlag);\n return adopted;\n }\n }\n return NativeReanimatedModule.makeShareableClone(value, shouldPersistRemote);\n}\n\nconst WORKLET_CODE_THRESHOLD = 255;\n\nfunction getWorkletCode(value: __WorkletFunction) {\n // @ts-ignore this is fine\n const code = value?.__initData?.code;\n if (!code) {\n return 'unknown';\n }\n if (code.length > WORKLET_CODE_THRESHOLD) {\n return `${code.substring(0, WORKLET_CODE_THRESHOLD)}...`;\n }\n return code;\n}\n\ntype RemoteFunction<T> = {\n __remoteFunction: FlatShareableRef<T>;\n};\n\nfunction isRemoteFunction<T>(value: object): value is RemoteFunction<T> {\n 'worklet';\n return '__remoteFunction' in value;\n}\n\nexport function makeShareableCloneOnUIRecursive<T>(\n value: T\n): FlatShareableRef<T> {\n 'worklet';\n if (USE_STUB_IMPLEMENTATION) {\n // @ts-ignore web is an interesting place where we don't run a secondary VM on the UI thread\n // see more details in the comment where USE_STUB_IMPLEMENTATION is defined.\n return value;\n }\n function cloneRecursive<T>(value: T): FlatShareableRef<T> {\n if (\n (typeof value === 'object' && value !== null) ||\n typeof value === 'function'\n ) {\n if (isHostObject(value)) {\n // We call `_makeShareableClone` to wrap the provided HostObject\n // inside ShareableJSRef.\n return _makeShareableClone(value) as FlatShareableRef<T>;\n }\n if (isRemoteFunction<T>(value)) {\n // RemoteFunctions are created by us therefore they are\n // a Shareable out of the box and there is no need to\n // call `_makeShareableClone`.\n return value.__remoteFunction;\n }\n if (Array.isArray(value)) {\n return _makeShareableClone(\n value.map(cloneRecursive)\n ) as FlatShareableRef<T>;\n }\n const toAdapt: Record<string, FlatShareableRef<T>> = {};\n for (const [key, element] of Object.entries(value)) {\n toAdapt[key] = cloneRecursive<T>(element);\n }\n return _makeShareableClone(toAdapt) as FlatShareableRef<T>;\n }\n return _makeShareableClone(value);\n }\n return cloneRecursive(value);\n}\n\nexport function makeShareable<T>(value: T): T {\n if (USE_STUB_IMPLEMENTATION) {\n return value;\n }\n const handle = makeShareableCloneRecursive({\n __init: () => {\n 'worklet';\n return value;\n },\n });\n registerShareableMapping(value, handle);\n return value;\n}\n"],"mappings":"AAAA,OAAOA,sBAAsB,MAAM,oBAAoB;AAMvD,SAASC,cAAc,QAAQ,mBAAmB;AAClD,SAASC,2BAA2B,QAAQ,UAAU;AACtD,SAASC,SAAS,QAAQ,+BAA+B;;AAEzD;AACA;AACA;AACA;AACA,MAAMC,uBAAuB,GAAGH,cAAc,EAAE;AAEhD,MAAMI,eAAe,GAAG,IAAIC,OAAO,EAGhC;AACH;AACA;AACA,MAAMC,cAAc,GAAGC,MAAM,CAAC,gBAAgB,CAAC;AAE/C,MAAMC,SAAS,GAAG,sBAAsB;AAExC,SAASC,YAAYA,CAACC,KAA0B,EAAE;EAChD,SAAS;;EACT;EACA;EACA;EACA;EACA,OAAOF,SAAS,IAAIE,KAAK;AAC3B;AAEA,OAAO,SAASC,wBAAwBA,CACtCC,SAAc,EACdC,YAAgC,EAC1B;EACN,IAAIV,uBAAuB,EAAE;IAC3B;EACF;EACAC,eAAe,CAACU,GAAG,CAACF,SAAS,EAAEC,YAAY,IAAIP,cAAc,CAAC;AAChE;AAEA,SAASS,eAAeA,CAACC,MAAc,EAAE;EACvC,OAAOC,MAAM,CAACC,cAAc,CAACF,MAAM,CAAC,KAAKC,MAAM,CAACE,SAAS;AAC3D;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,mBAAmB,GAAG;EAC1BC,MAAM,EAAEA,CAAA,KAAM;IACZ,SAAS;;IACT,OAAO,IAAIC,KAAK,CACd,CAAC,CAAC,EACF;MACEC,GAAG,EAAEA,CAACC,C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AgB,CAAIzD,KAAK,CAAC,EAAE;QAC9B;QACA;QACA;QACA,OAAOA,KAAK,CAAC6D,gBAAgB;MAC/B;MACA,IAAI9B,KAAK,CAACC,OAAO,CAAChC,KAAK,CAAC,EAAE;QACxB,OAAO4D,mBAAmB,CACxB5D,KAAK,CAACiC,GAAG,CAAC0B,cAAc,CAAC,CAC1B;MACH;MACA,MAAM7B,OAA4C,GAAG,CAAC,CAAC;MACvD,KAAK,MAAM,CAACY,GAAG,EAAER,OAAO,CAAC,IAAI3B,MAAM,CAACoC,OAAO,CAAC3C,KAAK,CAAC,EAAE;QAClD8B,OAAO,CAACY,GAAG,CAAC,GAAGiB,cAAc,CAAIzB,OAAO,CAAC;MAC3C;MACA,OAAO0B,mBAAmB,CAAC9B,OAAO,CAAC;IACrC;IACA,OAAO8B,mBAAmB,CAAC5D,KAAK,CAAC;EACnC;EACA,OAAO2D,cAAc,CAAC3D,KAAK,CAAC;AAC9B;AAEA,OAAO,SAAS8D,aAAaA,CAAI9D,KAAQ,EAAK;EAC5C,IAAIP,uBAAuB,EAAE;IAC3B,OAAOO,KAAK;EACd;EACA,MAAMgD,MAAM,GAAG5B,2BAA2B,CAAC;IACzCT,MAAM,EAAEA,CAAA,KAAM;MACZ,SAAS;;MACT,OAAOX,KAAK;IACd;EACF,CAAC,CAAC;EACFC,wBAAwB,CAACD,KAAK,EAAEgD,MAAM,CAAC;EACvC,OAAOhD,KAAK;AACd"}
|
|
1
|
+
{"version":3,"names":["NativeReanimatedModule","shouldBeUseWeb","registerWorkletStackDetails","jsVersion","USE_STUB_IMPLEMENTATION","_shareableCache","WeakMap","_shareableFlag","Symbol","MAGIC_KEY","isHostObject","value","registerShareableMapping","shareable","shareableRef","set","isPlainJSObject","object","Object","getPrototypeOf","prototype","INACCESSIBLE_OBJECT","__init","Proxy","get","_","prop","Error","String","DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD","processedObjectAtThresholdDepth","makeShareableCloneRecursive","shouldPersistRemote","arguments","length","undefined","depth","type","isTypeObject","isTypeFunction","cached","toAdapt","Array","isArray","map","element","__workletHash","__DEV__","babelVersion","__initData","version","getWorkletCode","__stackDetails","key","entries","RegExp","pattern","source","flags","handle","inaccessibleObject","freeze","adopted","makeShareableClone","WORKLET_CODE_THRESHOLD","_value$__initData","code","substring","isRemoteFunction","__remoteFunction","makeShareableCloneOnUIRecursive","cloneRecursive","_makeShareableClone","makeShareable"],"sources":["shareables.ts"],"sourcesContent":["import NativeReanimatedModule from './NativeReanimated';\nimport type {\n ShareableRef,\n FlatShareableRef,\n __WorkletFunction,\n} from './commonTypes';\nimport { shouldBeUseWeb } from './PlatformChecker';\nimport { registerWorkletStackDetails } from './errors';\nimport { jsVersion } from './platform-specific/jsVersion';\n\n// for web/chrome debugger/jest environments this file provides a stub implementation\n// where no shareable references are used. Instead, the objects themselves are used\n// instead of shareable references, because of the fact that we don't have to deal with\n// runnning the code on separate VMs.\nconst USE_STUB_IMPLEMENTATION = shouldBeUseWeb();\n\nconst _shareableCache = new WeakMap<\n Record<string, unknown>,\n ShareableRef<any> | symbol\n>();\n// the below symbol is used to represent a mapping from the value to itself\n// this is used to allow for a converted shareable to be passed to makeShareableClone\nconst _shareableFlag = Symbol('shareable flag');\n\nconst MAGIC_KEY = 'REANIMATED_MAGIC_KEY';\n\nfunction isHostObject(value: NonNullable<object>) {\n 'worklet';\n // We could use JSI to determine whether an object is a host object, however\n // the below workaround works well and is way faster than an additional JSI call.\n // We use the fact that host objects have broken implementation of `hasOwnProperty`\n // and hence return true for all `in` checks regardless of the key we ask for.\n return MAGIC_KEY in value;\n}\n\nexport function registerShareableMapping(\n shareable: any,\n shareableRef?: ShareableRef<any>\n): void {\n if (USE_STUB_IMPLEMENTATION) {\n return;\n }\n _shareableCache.set(shareable, shareableRef || _shareableFlag);\n}\n\nfunction isPlainJSObject(object: object) {\n return Object.getPrototypeOf(object) === Object.prototype;\n}\n\n// The below object is used as a replacement for objects that cannot be transferred\n// as shareable values. In makeShareableCloneRecursive we detect if an object is of\n// a plain Object.prototype and only allow such objects to be transferred. This lets\n// us avoid all sorts of react internals from leaking into the UI runtime. To make it\n// possible to catch errors when someone actually tries to access such object on the UI\n// runtime, we use the below Proxy object which is instantiated on the UI runtime and\n// throws whenever someone tries to access its fields.\nconst INACCESSIBLE_OBJECT = {\n __init: () => {\n 'worklet';\n return new Proxy(\n {},\n {\n get: (_: any, prop: string | symbol) => {\n if (prop === '_isReanimatedSharedValue') {\n // not very happy about this check here, but we need to allow for\n // \"inaccessible\" objects to be tested with isSharedValue check\n // as it is being used in the mappers when extracing inputs recursively.\n // Apparently we can't check if a key exists there as HostObjects always\n // return true for such tests, so the only possibility for us is to\n // actually access that key and see if it is set to true. We therefore\n // need to allow for this key to be accessed here.\n return false;\n }\n throw new Error(\n `[Reanimated] Trying to access property \\`${String(\n prop\n )}\\` of an object which cannot be sent to the UI runtime.`\n );\n },\n set: () => {\n throw new Error(\n '[Reanimated] Trying to write to an object which cannot be sent to the UI runtime.'\n );\n },\n }\n );\n },\n};\n\nconst DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD = 30;\n// Below variable stores object that we process in makeShareableCloneRecursive at the specified depth.\n// We use it to check if later on the function reenters with the same object\nlet processedObjectAtThresholdDepth: any;\n\nexport function makeShareableCloneRecursive<T>(\n value: any,\n shouldPersistRemote = false,\n depth = 0\n): ShareableRef<T> {\n if (USE_STUB_IMPLEMENTATION) {\n return value;\n }\n if (depth >= DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD) {\n // if we reach certain recursion depth we suspect that we are dealing with a cyclic object.\n // this type of objects are not supported and cannot be trasferred as shareable, so we\n // implement a simple detection mechanism that remembers the value at a given depth and\n // tests whether we try reenter this method later on with the same value. If that happens\n // we throw an appropriate error.\n if (depth === DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD) {\n processedObjectAtThresholdDepth = value;\n } else if (value === processedObjectAtThresholdDepth) {\n throw new Error(\n '[Reanimated] Trying to convert a cyclic object to a shareable. This is not supported.'\n );\n }\n } else {\n processedObjectAtThresholdDepth = undefined;\n }\n // This one actually may be worth to be moved to c++, we also need similar logic to run on the UI thread\n const type = typeof value;\n const isTypeObject = type === 'object';\n const isTypeFunction = type === 'function';\n if ((isTypeObject || isTypeFunction) && value !== null) {\n const cached = _shareableCache.get(value);\n if (cached === _shareableFlag) {\n return value;\n } else if (cached !== undefined) {\n return cached as ShareableRef<T>;\n } else {\n let toAdapt: any;\n if (Array.isArray(value)) {\n toAdapt = value.map((element) =>\n makeShareableCloneRecursive(element, shouldPersistRemote, depth + 1)\n );\n } else if (isTypeFunction && value.__workletHash === undefined) {\n // this is a remote function\n toAdapt = value;\n } else if (isHostObject(value)) {\n // for host objects we pass the reference to the object as shareable and\n // then recreate new host object wrapping the same instance on the UI thread.\n // there is no point of iterating over keys as we do for regular objects.\n toAdapt = value;\n } else if (isPlainJSObject(value) || isTypeFunction) {\n toAdapt = {};\n if (value.__workletHash !== undefined) {\n // we are converting a worklet\n if (__DEV__) {\n const babelVersion = value.__initData.version;\n if (babelVersion === undefined) {\n throw new Error(`[Reanimated] Unknown version of Reanimated Babel plugin.\nSee \\`https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#unknown-version-of-reanimated-babel-plugin\\` for more details. \nOffending code was: \\`${getWorkletCode(value)}\\``);\n } else if (babelVersion !== jsVersion) {\n throw new Error(`[Reanimated] Mismatch between JavaScript code version and Reanimated Babel plugin version (${jsVersion} vs. ${babelVersion}). \nSee \\`https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#mismatch-between-javascript-code-version-and-reanimated-babel-plugin-version\\` for more details.\nOffending code was: \\`${getWorkletCode(value)}\\``);\n }\n registerWorkletStackDetails(\n value.__workletHash,\n value.__stackDetails\n );\n delete value.__stackDetails;\n } else if (value.__stackDetails) {\n // Detected debug version of the worklet in release bundle. This\n // might lead to unexpected issues or errors. Probably one of user\n // dependencies provided transpiled code with debug version of the\n // Reanimated plugin.\n throw new Error(\n `[Reanimated] Using dev bundle in a release app build is not supported.\nSee \\`https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#using-dev-bundle-in-a-release-app-build-is-not-supported\\` for more details.`\n );\n }\n // to save on transferring static __initData field of worklet structure\n // we request shareable value to persist its UI counterpart. This means\n // that the __initData field that contains long strings represeting the\n // worklet code, source map, and location, will always be\n // serialized/deserialized once.\n toAdapt.__initData = makeShareableCloneRecursive(\n value.__initData,\n true,\n depth + 1\n );\n }\n\n for (const [key, element] of Object.entries(value)) {\n toAdapt[key] = makeShareableCloneRecursive(\n element,\n shouldPersistRemote,\n depth + 1\n );\n }\n } else if (value instanceof RegExp) {\n const pattern = value.source;\n const flags = value.flags;\n const handle = makeShareableCloneRecursive({\n __init: () => {\n 'worklet';\n return new RegExp(pattern, flags);\n },\n });\n registerShareableMapping(value, handle);\n return handle as ShareableRef<T>;\n } else {\n // This is reached for object types that are not of plain Object.prototype.\n // We don't support such objects from being transferred as shareables to\n // the UI runtime and hence we replace them with \"inaccessible object\"\n // which is implemented as a Proxy object that throws on any attempt\n // of accessing its fields. We argue that such objects can sometimes leak\n // as attributes of objects being captured by worklets but should never\n // be used on the UI runtime regardless. If they are being accessed, the user\n // will get an appropriate error message.\n const inaccessibleObject =\n makeShareableCloneRecursive<T>(INACCESSIBLE_OBJECT);\n _shareableCache.set(value, inaccessibleObject);\n return inaccessibleObject;\n }\n if (__DEV__) {\n // we freeze objects that are transformed to shareable. This should help\n // detect issues when someone modifies data after it's been converted to\n // shareable. Meaning that they may be doing a faulty assumption in their\n // code expecting that the updates are going to automatically populate to\n // the object sent to the UI thread. If the user really wants some objects\n // to be mutable they should use shared values instead.\n Object.freeze(value);\n }\n const adopted = NativeReanimatedModule.makeShareableClone(\n toAdapt,\n shouldPersistRemote\n );\n _shareableCache.set(value, adopted);\n _shareableCache.set(adopted, _shareableFlag);\n return adopted;\n }\n }\n return NativeReanimatedModule.makeShareableClone(value, shouldPersistRemote);\n}\n\nconst WORKLET_CODE_THRESHOLD = 255;\n\nfunction getWorkletCode(value: __WorkletFunction) {\n // @ts-ignore this is fine\n const code = value?.__initData?.code;\n if (!code) {\n return 'unknown';\n }\n if (code.length > WORKLET_CODE_THRESHOLD) {\n return `${code.substring(0, WORKLET_CODE_THRESHOLD)}...`;\n }\n return code;\n}\n\ntype RemoteFunction<T> = {\n __remoteFunction: FlatShareableRef<T>;\n};\n\nfunction isRemoteFunction<T>(value: {\n __remoteFunction?: unknown;\n}): value is RemoteFunction<T> {\n 'worklet';\n return !!value.__remoteFunction;\n}\n\nexport function makeShareableCloneOnUIRecursive<T>(\n value: T\n): FlatShareableRef<T> {\n 'worklet';\n if (USE_STUB_IMPLEMENTATION) {\n // @ts-ignore web is an interesting place where we don't run a secondary VM on the UI thread\n // see more details in the comment where USE_STUB_IMPLEMENTATION is defined.\n return value;\n }\n function cloneRecursive<T>(value: T): FlatShareableRef<T> {\n if (\n (typeof value === 'object' && value !== null) ||\n typeof value === 'function'\n ) {\n if (isHostObject(value)) {\n // We call `_makeShareableClone` to wrap the provided HostObject\n // inside ShareableJSRef.\n return _makeShareableClone(value) as FlatShareableRef<T>;\n }\n if (isRemoteFunction<T>(value)) {\n // RemoteFunctions are created by us therefore they are\n // a Shareable out of the box and there is no need to\n // call `_makeShareableClone`.\n return value.__remoteFunction;\n }\n if (Array.isArray(value)) {\n return _makeShareableClone(\n value.map(cloneRecursive)\n ) as FlatShareableRef<T>;\n }\n const toAdapt: Record<string, FlatShareableRef<T>> = {};\n for (const [key, element] of Object.entries(value)) {\n toAdapt[key] = cloneRecursive<T>(element);\n }\n return _makeShareableClone(toAdapt) as FlatShareableRef<T>;\n }\n return _makeShareableClone(value);\n }\n return cloneRecursive(value);\n}\n\nexport function makeShareable<T>(value: T): T {\n if (USE_STUB_IMPLEMENTATION) {\n return value;\n }\n const handle = makeShareableCloneRecursive({\n __init: () => {\n 'worklet';\n return value;\n },\n });\n registerShareableMapping(value, handle);\n return value;\n}\n"],"mappings":"AAAA,OAAOA,sBAAsB,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AAC,IAAI3B,MAAM,CAACoC,OAAO,CAAC3C,KAAK,CAAC,EAAE;QAClD8B,OAAO,CAACY,GAAG,CAAC,GAAGkB,cAAc,CAAI1B,OAAO,CAAC;MAC3C;MACA,OAAO2B,mBAAmB,CAAC/B,OAAO,CAAC;IACrC;IACA,OAAO+B,mBAAmB,CAAC7D,KAAK,CAAC;EACnC;EACA,OAAO4D,cAAc,CAAC5D,KAAK,CAAC;AAC9B;AAEA,OAAO,SAAS8D,aAAaA,CAAI9D,KAAQ,EAAK;EAC5C,IAAIP,uBAAuB,EAAE;IAC3B,OAAOO,KAAK;EACd;EACA,MAAMgD,MAAM,GAAG5B,2BAA2B,CAAC;IACzCT,MAAM,EAAEA,CAAA,KAAM;MACZ,SAAS;;MACT,OAAOX,KAAK;IACd;EACF,CAAC,CAAC;EACFC,wBAAwB,CAACD,KAAK,EAAEgD,MAAM,CAAC;EACvC,OAAOhD,KAAK;AACd"}
|
|
@@ -165,7 +165,7 @@ export function runOnJS(fun) {
|
|
|
165
165
|
return queueMicrotask(args.length ? () => fun(...args) : fun);
|
|
166
166
|
};
|
|
167
167
|
}
|
|
168
|
-
if (
|
|
168
|
+
if (fun.__workletHash) {
|
|
169
169
|
// If `fun` is a worklet, we schedule a call of a remote function `runWorkletOnJS`
|
|
170
170
|
// and pass the worklet as a first argument followed by original arguments.
|
|
171
171
|
|
|
@@ -176,7 +176,7 @@ export function runOnJS(fun) {
|
|
|
176
176
|
return runOnJS(runWorkletOnJS)(fun, ...args);
|
|
177
177
|
};
|
|
178
178
|
}
|
|
179
|
-
if (
|
|
179
|
+
if (fun.__remoteFunction) {
|
|
180
180
|
// In development mode the function provided as `fun` throws an error message
|
|
181
181
|
// such that when someone accidentally calls it directly on the UI runtime, they
|
|
182
182
|
// see that they should use `runOnJS` instead. To facilitate that we put the
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["NativeReanimatedModule","isJest","shouldBeUseWeb","makeShareableCloneOnUIRecursive","makeShareableCloneRecursive","IS_JEST","IS_NATIVE","_runOnUIQueue","setupMicrotasks","microtasksQueue","isExecutingMicrotasksQueue","global","queueMicrotask","callback","push","__callMicrotasks","index","length","_maybeFlushUIUpdatesQueue","callMicrotasksOnUIThread","callMicrotasks","runOnUI","worklet","__DEV__","_WORKLET","Error","__workletHash","undefined","_len","arguments","args","Array","_key","scheduleOnUI","queue","forEach","_ref","runOnUIImmediately","_len2","_key2","f","runWorkletOnJS","_len3","_key3","runOnJS","fun","_len4","_key4","_len5","_key5","__remoteFunction","_len6","_key6","_scheduleOnJS"],"sources":["threads.ts"],"sourcesContent":["import NativeReanimatedModule from './NativeReanimated';\nimport { isJest, shouldBeUseWeb } from './PlatformChecker';\nimport type { WorkletFunction } from './commonTypes';\nimport {\n makeShareableCloneOnUIRecursive,\n makeShareableCloneRecursive,\n} from './shareables';\n\nconst IS_JEST = isJest();\nconst IS_NATIVE = !shouldBeUseWeb();\n\n/**\n * An array of [worklet, args] pairs.\n * */\nlet _runOnUIQueue: Array<[WorkletFunction<unknown[], unknown>, unknown[]]> = [];\n\nexport function setupMicrotasks() {\n 'worklet';\n\n let microtasksQueue: Array<() => void> = [];\n let isExecutingMicrotasksQueue = false;\n global.queueMicrotask = (callback: () => void) => {\n microtasksQueue.push(callback);\n };\n\n global.__callMicrotasks = () => {\n if (isExecutingMicrotasksQueue) {\n return;\n }\n try {\n isExecutingMicrotasksQueue = true;\n for (let index = 0; index < microtasksQueue.length; index += 1) {\n // we use classic 'for' loop because the size of the currentTasks array may change while executing some of the callbacks due to queueMicrotask calls\n microtasksQueue[index]();\n }\n microtasksQueue = [];\n global._maybeFlushUIUpdatesQueue();\n } finally {\n isExecutingMicrotasksQueue = false;\n }\n };\n}\n\nfunction callMicrotasksOnUIThread() {\n 'worklet';\n global.__callMicrotasks();\n}\n\nexport const callMicrotasks = IS_NATIVE\n ? callMicrotasksOnUIThread\n : () => {\n // on web flushing is a noop as immediates are handled by the browser\n };\n\n// @ts-expect-error This overload is correct since it's what user sees in his code\n// before it's transformed by Reanimated Babel plugin.\nexport function runOnUI<Args extends unknown[], ReturnValue>(\n worklet: (...args: Args) => ReturnValue\n): (...args: Args) => void;\n/**\n * Schedule a worklet to execute on the UI runtime. This method does not schedule the work immediately but instead\n * waits for other worklets to be scheduled within the same JS loop. It uses queueMicrotask to schedule all the worklets\n * at once making sure they will run within the same frame boundaries on the UI thread.\n */\nexport function runOnUI<Args extends unknown[], ReturnValue>(\n worklet: WorkletFunction<Args, ReturnValue>\n): (...args: Args) => void {\n 'worklet';\n if (__DEV__ && IS_NATIVE && _WORKLET) {\n throw new Error(\n '[Reanimated] `runOnUI` cannot be called on the UI runtime. Please call the function synchronously or use `queueMicrotask` or `requestAnimationFrame` instead.'\n );\n }\n if (__DEV__ && IS_NATIVE && worklet.__workletHash === undefined) {\n throw new Error('[Reanimated] `runOnUI` can only be used on worklets.');\n }\n return (...args) => {\n if (IS_JEST) {\n // Mocking time in Jest is tricky as both requestAnimationFrame and queueMicrotask\n // callbacks run on the same queue and can be interleaved. There is no way\n // to flush particular queue in Jest and the only control over mocked timers\n // is by using jest.advanceTimersByTime() method which advances all types\n // of timers including immediate and animation callbacks. Ideally we'd like\n // to have some way here to schedule work along with React updates, but\n // that's not possible, and hence in Jest environment instead of using scheduling\n // mechanism we just schedule the work ommiting the queue. This is ok for the\n // uses that we currently have but may not be ok for future tests that we write.\n NativeReanimatedModule.scheduleOnUI(\n makeShareableCloneRecursive(() => {\n 'worklet';\n worklet(...args);\n })\n );\n return;\n }\n if (__DEV__) {\n // in DEV mode we call shareable conversion here because in case the object\n // can't be converted, we will get a meaningful stack-trace as opposed to the\n // situation when conversion is only done via microtask queue. This does not\n // make the app particularily less efficient as converted objects are cached\n // and for a given worklet the conversion only happens once.\n makeShareableCloneRecursive(worklet);\n makeShareableCloneRecursive(args);\n }\n //\n _runOnUIQueue.push([worklet as WorkletFunction<unknown[], unknown>, args]);\n if (_runOnUIQueue.length === 1) {\n queueMicrotask(() => {\n const queue = _runOnUIQueue;\n _runOnUIQueue = [];\n NativeReanimatedModule.scheduleOnUI(\n makeShareableCloneRecursive(() => {\n 'worklet';\n queue.forEach(([worklet, args]) => {\n worklet(...args);\n });\n callMicrotasks();\n })\n );\n });\n }\n };\n}\n\n// @ts-expect-error Check `runOnUI` overload above.\nexport function runOnUIImmediately<Args extends unknown[], ReturnValue>(\n worklet: (...args: Args) => ReturnValue\n): WorkletFunction<Args, ReturnValue>;\n/**\n * Schedule a worklet to execute on the UI runtime skipping batching mechanism.\n */\nexport function runOnUIImmediately<Args extends unknown[], ReturnValue>(\n worklet: WorkletFunction<Args, ReturnValue>\n): (...args: Args) => void {\n 'worklet';\n if (__DEV__ && IS_NATIVE && _WORKLET) {\n throw new Error(\n '[Reanimated] `runOnUIImmediately` cannot be called on the UI runtime. Please call the function synchronously or use `queueMicrotask` or `requestAnimationFrame` instead.'\n );\n }\n if (__DEV__ && IS_NATIVE && worklet.__workletHash === undefined) {\n throw new Error(\n '[Reanimated] `runOnUIImmediately` can only be used on worklets.'\n );\n }\n return (...args) => {\n NativeReanimatedModule.scheduleOnUI(\n makeShareableCloneRecursive(() => {\n 'worklet';\n worklet(...args);\n })\n );\n };\n}\n\nif (__DEV__ && IS_NATIVE) {\n const f = (() => {\n 'worklet';\n }) as WorkletFunction<[], void>;\n if (f.__workletHash === undefined) {\n throw new Error(\n `[Reanimated] Failed to create a worklet. See \\`https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#failed-to-create-a-worklet\\` for more details.`\n );\n }\n}\n\ntype ReleaseRemoteFunction<Args extends unknown[], ReturnValue> = {\n (...args: Args): ReturnValue;\n};\n\ntype DevRemoteFunction<Args extends unknown[], ReturnValue> = {\n __remoteFunction: (...args: Args) => ReturnValue;\n};\n\ntype RemoteFunction<Args extends unknown[], ReturnValue> =\n | ReleaseRemoteFunction<Args, ReturnValue>\n | DevRemoteFunction<Args, ReturnValue>;\n\nfunction runWorkletOnJS<Args extends unknown[], ReturnValue>(\n worklet: WorkletFunction<Args, ReturnValue>,\n ...args: Args\n): void {\n // remote function that calls a worklet synchronously on the JS runtime\n worklet(...args);\n}\n\n/**\n * Returns a function that can be called to be executed asynchronously on both\n * UI and JS threads.\n */\nexport function runOnJS<Args extends unknown[], ReturnValue>(\n fun:\n | ((...args: Args) => ReturnValue)\n | RemoteFunction<Args, ReturnValue>\n | WorkletFunction<Args, ReturnValue>\n): (...args: Args) => void {\n 'worklet';\n if (!IS_NATIVE || !_WORKLET) {\n // if we are already on the JS thread, we just schedule the worklet on the JS queue\n return (...args) =>\n queueMicrotask(\n args.length\n ? () => (fun as (...args: Args) => ReturnValue)(...args)\n : (fun as () => ReturnValue)\n );\n }\n if ('__workletHash' in fun) {\n // If `fun` is a worklet, we schedule a call of a remote function `runWorkletOnJS`\n // and pass the worklet as a first argument followed by original arguments.\n\n return (...args) =>\n runOnJS(runWorkletOnJS<Args, ReturnValue>)(\n fun as WorkletFunction<Args, ReturnValue>,\n ...args\n );\n }\n if ('__remoteFunction' in fun) {\n // In development mode the function provided as `fun` throws an error message\n // such that when someone accidentally calls it directly on the UI runtime, they\n // see that they should use `runOnJS` instead. To facilitate that we put the\n // reference to the original remote function in the `__functionInDEV` property.\n fun = fun.__remoteFunction;\n }\n return (...args) => {\n _scheduleOnJS(\n fun as\n | ((...args: Args) => ReturnValue)\n | WorkletFunction<Args, ReturnValue>,\n args.length > 0\n ? // TODO TYPESCRIPT this cast is terrible but will be fixed\n (makeShareableCloneOnUIRecursive(args) as unknown as unknown[])\n : undefined\n );\n };\n}\n"],"mappings":"AAAA,OAAOA,sBAAsB,MAAM,oBAAoB;AACvD,SAASC,MAAM,EAAEC,cAAc,QAAQ,mBAAmB;AAE1D,SACEC,+BAA+B,EAC/BC,2BAA2B,QACtB,cAAc;AAErB,MAAMC,OAAO,GAAGJ,MAAM,EAAE;AACxB,MAAMK,SAAS,GAAG,CAACJ,cAAc,EAAE;;AAEnC;AACA;AACA;AACA,IAAIK,aAAsE,GAAG,EAAE;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AAI,CAAAiB,KAAA,IAAAlB,SAAA,CAAAkB,KAAA;MAAA;MAAA,OACbnC,cAAc,CACZkB,IAAI,CAACb,MAAM,GACP,MAAO4B,GAAG,CAAoC,GAAGf,IAAI,CAAC,GACrDe,GAAyB,CAC/B;IAAA;EACL;EACA,IAAI,eAAe,IAAIA,GAAG,EAAE;IAC1B;IACA;;IAEA,OAAO;MAAA,SAAAG,KAAA,GAAAnB,SAAA,CAAAZ,MAAA,EAAIa,IAAI,OAAAC,KAAA,CAAAiB,KAAA,GAAAC,KAAA,MAAAA,KAAA,GAAAD,KAAA,EAAAC,KAAA;QAAJnB,IAAI,CAAAmB,KAAA,IAAApB,SAAA,CAAAoB,KAAA;MAAA;MAAA,OACbL,OAAO,CAACH,cAAc,CAAoB,CACxCI,GAAG,EACH,GAAGf,IAAI,CACR;IAAA;EACL;EACA,IAAI,kBAAkB,IAAIe,GAAG,EAAE;IAC7B;IACA;IACA;IACA;IACAA,GAAG,GAAGA,GAAG,CAACK,gBAAgB;EAC5B;EACA,OAAO,YAAa;IAAA,SAAAC,KAAA,GAAAtB,SAAA,CAAAZ,MAAA,EAATa,IAAI,OAAAC,KAAA,CAAAoB,KAAA,GAAAC,KAAA,MAAAA,KAAA,GAAAD,KAAA,EAAAC,KAAA;MAAJtB,IAAI,CAAAsB,KAAA,IAAAvB,SAAA,CAAAuB,KAAA;IAAA;IACbC,aAAa,CACXR,GAAG,EAGHf,IAAI,CAACb,MAAM,GAAG,CAAC;IACX;IACCd,+BAA+B,CAAC2B,IAAI,CAAC,GACtCH,SAAS,CACd;EACH,CAAC;AACH"}
|
|
1
|
+
{"version":3,"names":["NativeReanimatedModule","isJest","shouldBeUseWeb","makeShareableCloneOnUIRecursive","makeShareableCloneRecursive","IS_JEST","IS_NATIVE","_runOnUIQueue","setupMicrotasks","microtasksQueue","isExecutingMicrotasksQueue","global","queueMicrotask","callback","push","__callMicrotasks","index","length","_maybeFlushUIUpdatesQueue","callMicrotasksOnUIThread","callMicrotasks","runOnUI","worklet","__DEV__","_WORKLET","Error","__workletHash","undefined","_len","arguments","args","Array","_key","scheduleOnUI","queue","forEach","_ref","runOnUIImmediately","_len2","_key2","f","runWorkletOnJS","_len3","_key3","runOnJS","fun","_len4","_key4","_len5","_key5","__remoteFunction","_len6","_key6","_scheduleOnJS"],"sources":["threads.ts"],"sourcesContent":["import NativeReanimatedModule from './NativeReanimated';\nimport { isJest, shouldBeUseWeb } from './PlatformChecker';\nimport type { WorkletFunction } from './commonTypes';\nimport {\n makeShareableCloneOnUIRecursive,\n makeShareableCloneRecursive,\n} from './shareables';\n\nconst IS_JEST = isJest();\nconst IS_NATIVE = !shouldBeUseWeb();\n\n/**\n * An array of [worklet, args] pairs.\n * */\nlet _runOnUIQueue: Array<[WorkletFunction<unknown[], unknown>, unknown[]]> = [];\n\nexport function setupMicrotasks() {\n 'worklet';\n\n let microtasksQueue: Array<() => void> = [];\n let isExecutingMicrotasksQueue = false;\n global.queueMicrotask = (callback: () => void) => {\n microtasksQueue.push(callback);\n };\n\n global.__callMicrotasks = () => {\n if (isExecutingMicrotasksQueue) {\n return;\n }\n try {\n isExecutingMicrotasksQueue = true;\n for (let index = 0; index < microtasksQueue.length; index += 1) {\n // we use classic 'for' loop because the size of the currentTasks array may change while executing some of the callbacks due to queueMicrotask calls\n microtasksQueue[index]();\n }\n microtasksQueue = [];\n global._maybeFlushUIUpdatesQueue();\n } finally {\n isExecutingMicrotasksQueue = false;\n }\n };\n}\n\nfunction callMicrotasksOnUIThread() {\n 'worklet';\n global.__callMicrotasks();\n}\n\nexport const callMicrotasks = IS_NATIVE\n ? callMicrotasksOnUIThread\n : () => {\n // on web flushing is a noop as immediates are handled by the browser\n };\n\n// @ts-expect-error This overload is correct since it's what user sees in his code\n// before it's transformed by Reanimated Babel plugin.\nexport function runOnUI<Args extends unknown[], ReturnValue>(\n worklet: (...args: Args) => ReturnValue\n): (...args: Args) => void;\n/**\n * Schedule a worklet to execute on the UI runtime. This method does not schedule the work immediately but instead\n * waits for other worklets to be scheduled within the same JS loop. It uses queueMicrotask to schedule all the worklets\n * at once making sure they will run within the same frame boundaries on the UI thread.\n */\nexport function runOnUI<Args extends unknown[], ReturnValue>(\n worklet: WorkletFunction<Args, ReturnValue>\n): (...args: Args) => void {\n 'worklet';\n if (__DEV__ && IS_NATIVE && _WORKLET) {\n throw new Error(\n '[Reanimated] `runOnUI` cannot be called on the UI runtime. Please call the function synchronously or use `queueMicrotask` or `requestAnimationFrame` instead.'\n );\n }\n if (__DEV__ && IS_NATIVE && worklet.__workletHash === undefined) {\n throw new Error('[Reanimated] `runOnUI` can only be used on worklets.');\n }\n return (...args) => {\n if (IS_JEST) {\n // Mocking time in Jest is tricky as both requestAnimationFrame and queueMicrotask\n // callbacks run on the same queue and can be interleaved. There is no way\n // to flush particular queue in Jest and the only control over mocked timers\n // is by using jest.advanceTimersByTime() method which advances all types\n // of timers including immediate and animation callbacks. Ideally we'd like\n // to have some way here to schedule work along with React updates, but\n // that's not possible, and hence in Jest environment instead of using scheduling\n // mechanism we just schedule the work ommiting the queue. This is ok for the\n // uses that we currently have but may not be ok for future tests that we write.\n NativeReanimatedModule.scheduleOnUI(\n makeShareableCloneRecursive(() => {\n 'worklet';\n worklet(...args);\n })\n );\n return;\n }\n if (__DEV__) {\n // in DEV mode we call shareable conversion here because in case the object\n // can't be converted, we will get a meaningful stack-trace as opposed to the\n // situation when conversion is only done via microtask queue. This does not\n // make the app particularily less efficient as converted objects are cached\n // and for a given worklet the conversion only happens once.\n makeShareableCloneRecursive(worklet);\n makeShareableCloneRecursive(args);\n }\n //\n _runOnUIQueue.push([worklet as WorkletFunction<unknown[], unknown>, args]);\n if (_runOnUIQueue.length === 1) {\n queueMicrotask(() => {\n const queue = _runOnUIQueue;\n _runOnUIQueue = [];\n NativeReanimatedModule.scheduleOnUI(\n makeShareableCloneRecursive(() => {\n 'worklet';\n queue.forEach(([worklet, args]) => {\n worklet(...args);\n });\n callMicrotasks();\n })\n );\n });\n }\n };\n}\n\n// @ts-expect-error Check `runOnUI` overload above.\nexport function runOnUIImmediately<Args extends unknown[], ReturnValue>(\n worklet: (...args: Args) => ReturnValue\n): WorkletFunction<Args, ReturnValue>;\n/**\n * Schedule a worklet to execute on the UI runtime skipping batching mechanism.\n */\nexport function runOnUIImmediately<Args extends unknown[], ReturnValue>(\n worklet: WorkletFunction<Args, ReturnValue>\n): (...args: Args) => void {\n 'worklet';\n if (__DEV__ && IS_NATIVE && _WORKLET) {\n throw new Error(\n '[Reanimated] `runOnUIImmediately` cannot be called on the UI runtime. Please call the function synchronously or use `queueMicrotask` or `requestAnimationFrame` instead.'\n );\n }\n if (__DEV__ && IS_NATIVE && worklet.__workletHash === undefined) {\n throw new Error(\n '[Reanimated] `runOnUIImmediately` can only be used on worklets.'\n );\n }\n return (...args) => {\n NativeReanimatedModule.scheduleOnUI(\n makeShareableCloneRecursive(() => {\n 'worklet';\n worklet(...args);\n })\n );\n };\n}\n\nif (__DEV__ && IS_NATIVE) {\n const f = (() => {\n 'worklet';\n }) as WorkletFunction<[], void>;\n if (f.__workletHash === undefined) {\n throw new Error(\n `[Reanimated] Failed to create a worklet. See \\`https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#failed-to-create-a-worklet\\` for more details.`\n );\n }\n}\n\ntype ReleaseRemoteFunction<Args extends unknown[], ReturnValue> = {\n (...args: Args): ReturnValue;\n};\n\ntype DevRemoteFunction<Args extends unknown[], ReturnValue> = {\n __remoteFunction: (...args: Args) => ReturnValue;\n};\n\ntype RemoteFunction<Args extends unknown[], ReturnValue> =\n | ReleaseRemoteFunction<Args, ReturnValue>\n | DevRemoteFunction<Args, ReturnValue>;\n\nfunction runWorkletOnJS<Args extends unknown[], ReturnValue>(\n worklet: WorkletFunction<Args, ReturnValue>,\n ...args: Args\n): void {\n // remote function that calls a worklet synchronously on the JS runtime\n worklet(...args);\n}\n\n/**\n * Returns a function that can be called to be executed asynchronously on both\n * UI and JS threads.\n */\nexport function runOnJS<Args extends unknown[], ReturnValue>(\n fun:\n | ((...args: Args) => ReturnValue)\n | RemoteFunction<Args, ReturnValue>\n | WorkletFunction<Args, ReturnValue>\n): (...args: Args) => void {\n 'worklet';\n type FunWorklet = Extract<typeof fun, WorkletFunction<Args, ReturnValue>>;\n type FunDevRemote = Extract<typeof fun, DevRemoteFunction<Args, ReturnValue>>;\n if (!IS_NATIVE || !_WORKLET) {\n // if we are already on the JS thread, we just schedule the worklet on the JS queue\n return (...args) =>\n queueMicrotask(\n args.length\n ? () => (fun as (...args: Args) => ReturnValue)(...args)\n : (fun as () => ReturnValue)\n );\n }\n if ((fun as FunWorklet).__workletHash) {\n // If `fun` is a worklet, we schedule a call of a remote function `runWorkletOnJS`\n // and pass the worklet as a first argument followed by original arguments.\n\n return (...args) =>\n runOnJS(runWorkletOnJS<Args, ReturnValue>)(\n fun as WorkletFunction<Args, ReturnValue>,\n ...args\n );\n }\n if ((fun as FunDevRemote).__remoteFunction) {\n // In development mode the function provided as `fun` throws an error message\n // such that when someone accidentally calls it directly on the UI runtime, they\n // see that they should use `runOnJS` instead. To facilitate that we put the\n // reference to the original remote function in the `__functionInDEV` property.\n fun = (fun as FunDevRemote).__remoteFunction;\n }\n return (...args) => {\n _scheduleOnJS(\n fun as\n | ((...args: Args) => ReturnValue)\n | WorkletFunction<Args, ReturnValue>,\n args.length > 0\n ? // TODO TYPESCRIPT this cast is terrible but will be fixed\n (makeShareableCloneOnUIRecursive(args) as unknown as unknown[])\n : undefined\n );\n };\n}\n"],"mappings":"AAAA,OAAOA,sBAAsB,MAAM,oBAAoB;AACvD,SAASC,MAAM,EAAEC,cAAc,QAAQ,mBAAmB;AAE1D,SACEC,+BAA+B,EAC/BC,2BAA2B,QACtB,cAAc;AAErB,MAAMC,OAAO,GAAGJ,MAAM,EAAE;AACxB,MAAMK,SAAS,GAAG,CAACJ,cAAc,EAAE;;AAEnC;AACA;AACA;AACA,IAAIK,aAAsE,GAAG,EAAE;AAE/E,OAAO,SAASC,eAAeA,CAA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AAA,CAAAkB,KAAA;MAAA;MAAA,OACbnC,cAAc,CACZkB,IAAI,CAACb,MAAM,GACP,MAAO4B,GAAG,CAAoC,GAAGf,IAAI,CAAC,GACrDe,GAAyB,CAC/B;IAAA;EACL;EACA,IAAKA,GAAG,CAAgBnB,aAAa,EAAE;IACrC;IACA;;IAEA,OAAO;MAAA,SAAAsB,KAAA,GAAAnB,SAAA,CAAAZ,MAAA,EAAIa,IAAI,OAAAC,KAAA,CAAAiB,KAAA,GAAAC,KAAA,MAAAA,KAAA,GAAAD,KAAA,EAAAC,KAAA;QAAJnB,IAAI,CAAAmB,KAAA,IAAApB,SAAA,CAAAoB,KAAA;MAAA;MAAA,OACbL,OAAO,CAACH,cAAc,CAAoB,CACxCI,GAAG,EACH,GAAGf,IAAI,CACR;IAAA;EACL;EACA,IAAKe,GAAG,CAAkBK,gBAAgB,EAAE;IAC1C;IACA;IACA;IACA;IACAL,GAAG,GAAIA,GAAG,CAAkBK,gBAAgB;EAC9C;EACA,OAAO,YAAa;IAAA,SAAAC,KAAA,GAAAtB,SAAA,CAAAZ,MAAA,EAATa,IAAI,OAAAC,KAAA,CAAAoB,KAAA,GAAAC,KAAA,MAAAA,KAAA,GAAAD,KAAA,EAAAC,KAAA;MAAJtB,IAAI,CAAAsB,KAAA,IAAAvB,SAAA,CAAAuB,KAAA;IAAA;IACbC,aAAa,CACXR,GAAG,EAGHf,IAAI,CAACb,MAAM,GAAG,CAAC;IACX;IACCd,+BAA+B,CAAC2B,IAAI,CAAC,GACtCH,SAAS,CACd;EACH,CAAC;AACH"}
|
|
@@ -59,14 +59,12 @@ if (__DEV__ && IS_NATIVE) {
|
|
|
59
59
|
if (!('__workletHash' in valueUnpacker)) {
|
|
60
60
|
throw new Error('[Reanimated] `valueUnpacker` is not a worklet');
|
|
61
61
|
}
|
|
62
|
-
// @ts-ignore TODO TYPESCRIPT
|
|
63
62
|
const closure = valueUnpacker.__closure;
|
|
64
63
|
if (closure !== undefined && Object.keys(closure).length !== 0) {
|
|
65
64
|
throw new Error('[Reanimated] `valueUnpacker` must have empty closure');
|
|
66
65
|
}
|
|
67
66
|
}
|
|
68
67
|
export function getValueUnpackerCode() {
|
|
69
|
-
// @ts-ignore TODO TYPESCRIPT
|
|
70
68
|
return valueUnpacker.__initData.code;
|
|
71
69
|
}
|
|
72
70
|
//# sourceMappingURL=valueUnpacker.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["shouldBeUseWeb","IS_NATIVE","valueUnpacker","objectToUnpack","category","workletsCache","global","__workletsCache","handleCache","__handleCache","undefined","Map","WeakMap","workletHash","__workletHash","workletFun","get","initData","__initData","evalWithSourceMap","code","location","sourceMap","evalWithSourceUrl","eval","set","functionInstance","bind","_recur","__init","value","fun","Error","__remoteFunction","__DEV__","closure","__closure","Object","keys","length","getValueUnpackerCode"],"sources":["valueUnpacker.ts"],"sourcesContent":["import { shouldBeUseWeb } from './PlatformChecker';\n\nconst IS_NATIVE = !shouldBeUseWeb();\n\nfunction valueUnpacker(objectToUnpack: any, category?: string): any {\n 'worklet';\n let workletsCache = global.__workletsCache;\n let handleCache = global.__handleCache;\n if (workletsCache === undefined) {\n // init\n workletsCache = global.__workletsCache = new Map();\n handleCache = global.__handleCache = new WeakMap();\n }\n const workletHash = objectToUnpack.__workletHash;\n if (workletHash !== undefined) {\n let workletFun = workletsCache.get(workletHash);\n if (workletFun === undefined) {\n const initData = objectToUnpack.__initData;\n if (global.evalWithSourceMap) {\n // if the runtime (hermes only for now) supports loading source maps\n // we want to use the proper filename for the location as it guarantees\n // that debugger understands and loads the source code of the file where\n // the worklet is defined.\n workletFun = global.evalWithSourceMap(\n '(' + initData.code + '\\n)',\n initData.location,\n initData.sourceMap\n ) as (...args: any[]) => any;\n } else if (global.evalWithSourceUrl) {\n // if the runtime doesn't support loading source maps, in dev mode we\n // can pass source url when evaluating the worklet. Now, instead of using\n // the actual file location we use worklet hash, as it the allows us to\n // properly symbolicate traces (see errors.ts for details)\n workletFun = global.evalWithSourceUrl(\n '(' + initData.code + '\\n)',\n `worklet_${workletHash}`\n ) as (...args: any[]) => any;\n } else {\n // in release we use the regular eval to save on JSI calls\n // eslint-disable-next-line no-eval\n workletFun = eval('(' + initData.code + '\\n)') as (\n ...args: any[]\n ) => any;\n }\n workletsCache.set(workletHash, workletFun);\n }\n const functionInstance = workletFun.bind(objectToUnpack);\n objectToUnpack._recur = functionInstance;\n return functionInstance;\n } else if (objectToUnpack.__init) {\n let value = handleCache!.get(objectToUnpack);\n if (value === undefined) {\n value = objectToUnpack.__init();\n handleCache!.set(objectToUnpack, value);\n }\n return value;\n } else if (category === 'RemoteFunction') {\n const fun = () => {\n throw new Error(`[Reanimated] Tried to synchronously call a non-worklet function on the UI thread.\nSee \\`https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#tried-to-synchronously-call-a-non-worklet-function-on-the-ui-thread\\` for more details.`);\n };\n fun.__remoteFunction = objectToUnpack;\n return fun;\n } else {\n throw new Error('[Reanimated] Data type not recognized by value unpacker.');\n }\n}\n\nif (__DEV__ && IS_NATIVE) {\n if (!('__workletHash' in valueUnpacker)) {\n throw new Error('[Reanimated] `valueUnpacker` is not a worklet');\n }\n
|
|
1
|
+
{"version":3,"names":["shouldBeUseWeb","IS_NATIVE","valueUnpacker","objectToUnpack","category","workletsCache","global","__workletsCache","handleCache","__handleCache","undefined","Map","WeakMap","workletHash","__workletHash","workletFun","get","initData","__initData","evalWithSourceMap","code","location","sourceMap","evalWithSourceUrl","eval","set","functionInstance","bind","_recur","__init","value","fun","Error","__remoteFunction","__DEV__","closure","__closure","Object","keys","length","getValueUnpackerCode"],"sources":["valueUnpacker.ts"],"sourcesContent":["import { shouldBeUseWeb } from './PlatformChecker';\nimport type { WorkletFunction } from './commonTypes';\n\nconst IS_NATIVE = !shouldBeUseWeb();\n\nfunction valueUnpacker(objectToUnpack: any, category?: string): any {\n 'worklet';\n let workletsCache = global.__workletsCache;\n let handleCache = global.__handleCache;\n if (workletsCache === undefined) {\n // init\n workletsCache = global.__workletsCache = new Map();\n handleCache = global.__handleCache = new WeakMap();\n }\n const workletHash = objectToUnpack.__workletHash;\n if (workletHash !== undefined) {\n let workletFun = workletsCache.get(workletHash);\n if (workletFun === undefined) {\n const initData = objectToUnpack.__initData;\n if (global.evalWithSourceMap) {\n // if the runtime (hermes only for now) supports loading source maps\n // we want to use the proper filename for the location as it guarantees\n // that debugger understands and loads the source code of the file where\n // the worklet is defined.\n workletFun = global.evalWithSourceMap(\n '(' + initData.code + '\\n)',\n initData.location,\n initData.sourceMap\n ) as (...args: any[]) => any;\n } else if (global.evalWithSourceUrl) {\n // if the runtime doesn't support loading source maps, in dev mode we\n // can pass source url when evaluating the worklet. Now, instead of using\n // the actual file location we use worklet hash, as it the allows us to\n // properly symbolicate traces (see errors.ts for details)\n workletFun = global.evalWithSourceUrl(\n '(' + initData.code + '\\n)',\n `worklet_${workletHash}`\n ) as (...args: any[]) => any;\n } else {\n // in release we use the regular eval to save on JSI calls\n // eslint-disable-next-line no-eval\n workletFun = eval('(' + initData.code + '\\n)') as (\n ...args: any[]\n ) => any;\n }\n workletsCache.set(workletHash, workletFun);\n }\n const functionInstance = workletFun.bind(objectToUnpack);\n objectToUnpack._recur = functionInstance;\n return functionInstance;\n } else if (objectToUnpack.__init) {\n let value = handleCache!.get(objectToUnpack);\n if (value === undefined) {\n value = objectToUnpack.__init();\n handleCache!.set(objectToUnpack, value);\n }\n return value;\n } else if (category === 'RemoteFunction') {\n const fun = () => {\n throw new Error(`[Reanimated] Tried to synchronously call a non-worklet function on the UI thread.\nSee \\`https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#tried-to-synchronously-call-a-non-worklet-function-on-the-ui-thread\\` for more details.`);\n };\n fun.__remoteFunction = objectToUnpack;\n return fun;\n } else {\n throw new Error('[Reanimated] Data type not recognized by value unpacker.');\n }\n}\n\ntype ValueUnpacker = WorkletFunction<\n [objectToUnpack: any, category?: string],\n any\n>;\n\nif (__DEV__ && IS_NATIVE) {\n if (!('__workletHash' in valueUnpacker)) {\n throw new Error('[Reanimated] `valueUnpacker` is not a worklet');\n }\n const closure = (valueUnpacker as ValueUnpacker).__closure;\n if (closure !== undefined && Object.keys(closure).length !== 0) {\n throw new Error('[Reanimated] `valueUnpacker` must have empty closure');\n }\n}\n\nexport function getValueUnpackerCode() {\n return (valueUnpacker as ValueUnpacker).__initData.code;\n}\n"],"mappings":"AAAA,SAASA,cAAc,QAAQ,mBAAmB;AAGlD,MAAMC,SAAS,GAAG,CAACD,cAAc,EAAE;AAEnC,SAASE,aAAaA,CAACC,cAAmB,EAAEC,QAAiB,EAAO;EAClE,SAAS;;EACT,IAAIC,aAAa,GAAGC,MAAM,CAACC,eAAe;EAC1C,IAAIC,WAAW,GAAGF,MAAM,CAACG,aAAa;EACtC,IAAIJ,aAAa,KAAKK,SAAS,EAAE;IAC/B;IACAL,aAAa,GAAGC,MAAM,CAACC,eAAe,GAAG,IAAII,GAAG,EAAE;IAClDH,WAAW,GAAGF,MAAM,CAACG,aAAa,GAAG,IAAIG,OAAO,EAAE;EACpD;EACA,MAAMC,WAAW,GAAGV,cAAc,CAACW,aAAa;EAChD,IAAID,WAAW,KAAKH,SAAS,EAAE;IAC7B,IAAIK,UAAU,GAAGV,aAAa,CAACW,GAAG,CAACH,WAAW,CAAC;IAC/C,IAAIE,UAAU,KAAKL,SAAS,EAAE;MAC5B,MAAMO,QAAQ,GAAGd,cAAc,CAACe,UAAU;MAC1C,IAAIZ,MAAM,CAACa,iBAAiB,EAAE;QAC5B;QACA;QACA;QACA;QACAJ,UAAU,GAAGT,MAAM,CAACa,iBAAiB,CACnC,GAAG,GAAGF,QAAQ,CAACG,IAAI,GAAG,KAAK,EAC3BH,QAAQ,CAACI,QAAQ,EACjBJ,QAAQ,CAACK,SAAS,CACQ;MAC9B,CAAC,MAAM,IAAIhB,MAAM,CAACiB,iBAAiB,EAAE;QACnC;QACA;QACA;QACA;QACAR,UAAU,GAAGT,MAAM,CAACiB,i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C,IAAI,CAACH,OAAO,CAAC,CAACI,MAAM,KAAK,CAAC,EAAE;IAC9D,MAAM,IAAIP,KAAK,CAAC,sDAAsD,CAAC;EACzE;AACF;AAEA,OAAO,SAASQ,oBAAoBA,CAAA,EAAG;EACrC,OAAQtC,aAAa,CAAmBgB,UAAU,CAACE,IAAI;AACzD"}
|
package/package.json
CHANGED
|
@@ -253,9 +253,11 @@ type RemoteFunction<T> = {
|
|
|
253
253
|
__remoteFunction: FlatShareableRef<T>;
|
|
254
254
|
};
|
|
255
255
|
|
|
256
|
-
function isRemoteFunction<T>(value:
|
|
256
|
+
function isRemoteFunction<T>(value: {
|
|
257
|
+
__remoteFunction?: unknown;
|
|
258
|
+
}): value is RemoteFunction<T> {
|
|
257
259
|
'worklet';
|
|
258
|
-
return
|
|
260
|
+
return !!value.__remoteFunction;
|
|
259
261
|
}
|
|
260
262
|
|
|
261
263
|
export function makeShareableCloneOnUIRecursive<T>(
|
|
@@ -195,6 +195,8 @@ export function runOnJS<Args extends unknown[], ReturnValue>(
|
|
|
195
195
|
| WorkletFunction<Args, ReturnValue>
|
|
196
196
|
): (...args: Args) => void {
|
|
197
197
|
'worklet';
|
|
198
|
+
type FunWorklet = Extract<typeof fun, WorkletFunction<Args, ReturnValue>>;
|
|
199
|
+
type FunDevRemote = Extract<typeof fun, DevRemoteFunction<Args, ReturnValue>>;
|
|
198
200
|
if (!IS_NATIVE || !_WORKLET) {
|
|
199
201
|
// if we are already on the JS thread, we just schedule the worklet on the JS queue
|
|
200
202
|
return (...args) =>
|
|
@@ -204,7 +206,7 @@ export function runOnJS<Args extends unknown[], ReturnValue>(
|
|
|
204
206
|
: (fun as () => ReturnValue)
|
|
205
207
|
);
|
|
206
208
|
}
|
|
207
|
-
if (
|
|
209
|
+
if ((fun as FunWorklet).__workletHash) {
|
|
208
210
|
// If `fun` is a worklet, we schedule a call of a remote function `runWorkletOnJS`
|
|
209
211
|
// and pass the worklet as a first argument followed by original arguments.
|
|
210
212
|
|
|
@@ -214,12 +216,12 @@ export function runOnJS<Args extends unknown[], ReturnValue>(
|
|
|
214
216
|
...args
|
|
215
217
|
);
|
|
216
218
|
}
|
|
217
|
-
if (
|
|
219
|
+
if ((fun as FunDevRemote).__remoteFunction) {
|
|
218
220
|
// In development mode the function provided as `fun` throws an error message
|
|
219
221
|
// such that when someone accidentally calls it directly on the UI runtime, they
|
|
220
222
|
// see that they should use `runOnJS` instead. To facilitate that we put the
|
|
221
223
|
// reference to the original remote function in the `__functionInDEV` property.
|
|
222
|
-
fun = fun.__remoteFunction;
|
|
224
|
+
fun = (fun as FunDevRemote).__remoteFunction;
|
|
223
225
|
}
|
|
224
226
|
return (...args) => {
|
|
225
227
|
_scheduleOnJS(
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { shouldBeUseWeb } from './PlatformChecker';
|
|
2
|
+
import type { WorkletFunction } from './commonTypes';
|
|
2
3
|
|
|
3
4
|
const IS_NATIVE = !shouldBeUseWeb();
|
|
4
5
|
|
|
@@ -66,18 +67,21 @@ See \`https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshoo
|
|
|
66
67
|
}
|
|
67
68
|
}
|
|
68
69
|
|
|
70
|
+
type ValueUnpacker = WorkletFunction<
|
|
71
|
+
[objectToUnpack: any, category?: string],
|
|
72
|
+
any
|
|
73
|
+
>;
|
|
74
|
+
|
|
69
75
|
if (__DEV__ && IS_NATIVE) {
|
|
70
76
|
if (!('__workletHash' in valueUnpacker)) {
|
|
71
77
|
throw new Error('[Reanimated] `valueUnpacker` is not a worklet');
|
|
72
78
|
}
|
|
73
|
-
|
|
74
|
-
const closure = valueUnpacker.__closure;
|
|
79
|
+
const closure = (valueUnpacker as ValueUnpacker).__closure;
|
|
75
80
|
if (closure !== undefined && Object.keys(closure).length !== 0) {
|
|
76
81
|
throw new Error('[Reanimated] `valueUnpacker` must have empty closure');
|
|
77
82
|
}
|
|
78
83
|
}
|
|
79
84
|
|
|
80
85
|
export function getValueUnpackerCode() {
|
|
81
|
-
|
|
82
|
-
return valueUnpacker.__initData.code as string;
|
|
86
|
+
return (valueUnpacker as ValueUnpacker).__initData.code;
|
|
83
87
|
}
|