@depup/react-native-worklets 0.11.3-depup.0

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 (457) hide show
  1. package/.worklets/dummy.md +1 -0
  2. package/Common/cpp/worklets/AnimationFrameQueue/AnimationFrameBatchinator.cpp +64 -0
  3. package/Common/cpp/worklets/AnimationFrameQueue/AnimationFrameBatchinator.h +38 -0
  4. package/Common/cpp/worklets/Compat/Holders.h +25 -0
  5. package/Common/cpp/worklets/Compat/StableApi.cpp +145 -0
  6. package/Common/cpp/worklets/Compat/StableApi.h +128 -0
  7. package/Common/cpp/worklets/NativeModules/JSIWorkletsModuleProxy.cpp +729 -0
  8. package/Common/cpp/worklets/NativeModules/JSIWorkletsModuleProxy.h +126 -0
  9. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.cpp +81 -0
  10. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.h +76 -0
  11. package/Common/cpp/worklets/Registries/EventHandlerRegistry.cpp +95 -0
  12. package/Common/cpp/worklets/Registries/EventHandlerRegistry.h +44 -0
  13. package/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.cpp +10 -0
  14. package/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.h +43 -0
  15. package/Common/cpp/worklets/RunLoop/AsyncQueue.h +14 -0
  16. package/Common/cpp/worklets/RunLoop/AsyncQueueImpl.cpp +69 -0
  17. package/Common/cpp/worklets/RunLoop/AsyncQueueImpl.h +48 -0
  18. package/Common/cpp/worklets/RunLoop/EventLoop.cpp +103 -0
  19. package/Common/cpp/worklets/RunLoop/EventLoop.h +52 -0
  20. package/Common/cpp/worklets/SharedItems/MemoryManager.cpp +60 -0
  21. package/Common/cpp/worklets/SharedItems/MemoryManager.h +29 -0
  22. package/Common/cpp/worklets/SharedItems/Serializable.cpp +373 -0
  23. package/Common/cpp/worklets/SharedItems/Serializable.h +413 -0
  24. package/Common/cpp/worklets/SharedItems/SerializableFactory.cpp +172 -0
  25. package/Common/cpp/worklets/SharedItems/SerializableFactory.h +85 -0
  26. package/Common/cpp/worklets/SharedItems/SerializableRemoteFunction.cpp +71 -0
  27. package/Common/cpp/worklets/SharedItems/SerializableRemoteFunction.h +75 -0
  28. package/Common/cpp/worklets/SharedItems/Shareable.cpp +84 -0
  29. package/Common/cpp/worklets/SharedItems/Shareable.h +43 -0
  30. package/Common/cpp/worklets/SharedItems/Synchronizable.cpp +59 -0
  31. package/Common/cpp/worklets/SharedItems/Synchronizable.h +57 -0
  32. package/Common/cpp/worklets/SharedItems/SynchronizableAccess.cpp +78 -0
  33. package/Common/cpp/worklets/SharedItems/SynchronizableAccess.h +45 -0
  34. package/Common/cpp/worklets/SharedItems/UnpackerLoader.h +78 -0
  35. package/Common/cpp/worklets/Tools/FeatureFlags.cpp +18 -0
  36. package/Common/cpp/worklets/Tools/FeatureFlags.h +44 -0
  37. package/Common/cpp/worklets/Tools/JSISerializer.cpp +312 -0
  38. package/Common/cpp/worklets/Tools/JSISerializer.h +42 -0
  39. package/Common/cpp/worklets/Tools/JSLogger.cpp +92 -0
  40. package/Common/cpp/worklets/Tools/JSLogger.h +48 -0
  41. package/Common/cpp/worklets/Tools/JSScheduler.cpp +21 -0
  42. package/Common/cpp/worklets/Tools/JSScheduler.h +35 -0
  43. package/Common/cpp/worklets/Tools/PlatformLogger.h +16 -0
  44. package/Common/cpp/worklets/Tools/RNRuntimeStatus.h +27 -0
  45. package/Common/cpp/worklets/Tools/ScriptBuffer.h +56 -0
  46. package/Common/cpp/worklets/Tools/SingleInstanceChecker.h +75 -0
  47. package/Common/cpp/worklets/Tools/ThreadSafeQueue.h +49 -0
  48. package/Common/cpp/worklets/Tools/UIScheduler.cpp +19 -0
  49. package/Common/cpp/worklets/Tools/UIScheduler.h +22 -0
  50. package/Common/cpp/worklets/Tools/VersionUtils.cpp +61 -0
  51. package/Common/cpp/worklets/Tools/VersionUtils.h +24 -0
  52. package/Common/cpp/worklets/Tools/WorkletEventHandler.cpp +34 -0
  53. package/Common/cpp/worklets/Tools/WorkletEventHandler.h +44 -0
  54. package/Common/cpp/worklets/Tools/WorkletsJSIUtils.cpp +30 -0
  55. package/Common/cpp/worklets/Tools/WorkletsJSIUtils.h +222 -0
  56. package/Common/cpp/worklets/Tools/WorkletsSystraceSection.h +116 -0
  57. package/Common/cpp/worklets/Tools/WorkletsVersion.cpp +46 -0
  58. package/Common/cpp/worklets/Tools/WorkletsVersion.h +18 -0
  59. package/Common/cpp/worklets/WorkletRuntime/BundleModeConfig.h +17 -0
  60. package/Common/cpp/worklets/WorkletRuntime/HermesProfiling.cpp +45 -0
  61. package/Common/cpp/worklets/WorkletRuntime/HermesProfiling.h +12 -0
  62. package/Common/cpp/worklets/WorkletRuntime/RNRuntimeWorkletDecorator.cpp +68 -0
  63. package/Common/cpp/worklets/WorkletRuntime/RNRuntimeWorkletDecorator.h +24 -0
  64. package/Common/cpp/worklets/WorkletRuntime/RuntimeBindings.h +59 -0
  65. package/Common/cpp/worklets/WorkletRuntime/RuntimeData.h +38 -0
  66. package/Common/cpp/worklets/WorkletRuntime/RuntimeHolder.h +19 -0
  67. package/Common/cpp/worklets/WorkletRuntime/RuntimeManager.cpp +122 -0
  68. package/Common/cpp/worklets/WorkletRuntime/RuntimeManager.h +75 -0
  69. package/Common/cpp/worklets/WorkletRuntime/ScriptLoader.h +116 -0
  70. package/Common/cpp/worklets/WorkletRuntime/UIRuntimeDecorator.cpp +14 -0
  71. package/Common/cpp/worklets/WorkletRuntime/UIRuntimeDecorator.h +15 -0
  72. package/Common/cpp/worklets/WorkletRuntime/WorkletHermesRuntime.cpp +58 -0
  73. package/Common/cpp/worklets/WorkletRuntime/WorkletHermesRuntime.h +100 -0
  74. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp +327 -0
  75. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h +256 -0
  76. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeCollector.h +36 -0
  77. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.cpp +320 -0
  78. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.h +36 -0
  79. package/LICENSE +20 -0
  80. package/README.md +43 -0
  81. package/RNWorklets.podspec +94 -0
  82. package/android/CMakeLists.txt +108 -0
  83. package/android/build.gradle.kts +369 -0
  84. package/android/fix-prefab.gradle.kts +56 -0
  85. package/android/generate-stub-pch.gradle.kts +84 -0
  86. package/android/gradle.properties +25 -0
  87. package/android/proguard-rules.pro +3 -0
  88. package/android/settings.gradle.kts +21 -0
  89. package/android/src/main/AndroidManifest.xml +2 -0
  90. package/android/src/main/cpp/WorkletsPCH.h +26 -0
  91. package/android/src/main/cpp/worklets/android/AndroidUIScheduler.cpp +66 -0
  92. package/android/src/main/cpp/worklets/android/AndroidUIScheduler.h +38 -0
  93. package/android/src/main/cpp/worklets/android/AnimationFrameCallback.h +30 -0
  94. package/android/src/main/cpp/worklets/android/JScriptBufferWrapper.cpp +66 -0
  95. package/android/src/main/cpp/worklets/android/JScriptBufferWrapper.h +48 -0
  96. package/android/src/main/cpp/worklets/android/JWorkletRuntimeWrapper.cpp +51 -0
  97. package/android/src/main/cpp/worklets/android/JWorkletRuntimeWrapper.h +43 -0
  98. package/android/src/main/cpp/worklets/android/PlatformLogger.cpp +31 -0
  99. package/android/src/main/cpp/worklets/android/WorkletsModule.cpp +203 -0
  100. package/android/src/main/cpp/worklets/android/WorkletsModule.h +75 -0
  101. package/android/src/main/cpp/worklets/android/WorkletsOnLoad.cpp +19 -0
  102. package/android/src/main/java/com/swmansion/worklets/AndroidUIScheduler.kt +56 -0
  103. package/android/src/main/java/com/swmansion/worklets/ScriptBufferWrapper.kt +103 -0
  104. package/android/src/main/java/com/swmansion/worklets/WorkletsPackage.kt +42 -0
  105. package/android/src/main/java/com/swmansion/worklets/runloop/AnimationFrameCallback.kt +14 -0
  106. package/android/src/main/java/com/swmansion/worklets/runloop/AnimationFrameQueue.kt +112 -0
  107. package/android/src/networking/com/swmansion/worklets/WorkletRuntimeWrapper.kt +27 -0
  108. package/android/src/networking/com/swmansion/worklets/WorkletsHeaderUtil.kt +30 -0
  109. package/android/src/networking/com/swmansion/worklets/WorkletsModule.kt +187 -0
  110. package/android/src/networking/com/swmansion/worklets/WorkletsNetworkEventUtil.kt +272 -0
  111. package/android/src/networking/com/swmansion/worklets/WorkletsNetworking.kt +1124 -0
  112. package/android/src/networking/com/swmansion/worklets/WorkletsOkHttpCallUtil.kt +40 -0
  113. package/android/src/networking/com/swmansion/worklets/WorkletsProgressListener.kt +13 -0
  114. package/android/src/networking/com/swmansion/worklets/WorkletsProgressRequestBody.kt +98 -0
  115. package/android/src/networking/com/swmansion/worklets/WorkletsProgressResponseBody.kt +60 -0
  116. package/android/src/networking/com/swmansion/worklets/WorkletsProgressiveStringDecoder.kt +87 -0
  117. package/android/src/networking/com/swmansion/worklets/WorkletsRequestBodyUtil.kt +181 -0
  118. package/android/src/no-networking/com/swmansion/worklets/WorkletsModule.kt +143 -0
  119. package/apple/worklets/apple/AnimationFrameQueue.h +15 -0
  120. package/apple/worklets/apple/AnimationFrameQueue.mm +145 -0
  121. package/apple/worklets/apple/AssertJavaScriptQueue.h +14 -0
  122. package/apple/worklets/apple/AssertTurboModuleManagerQueue.h +12 -0
  123. package/apple/worklets/apple/IOSUIScheduler.h +14 -0
  124. package/apple/worklets/apple/IOSUIScheduler.mm +29 -0
  125. package/apple/worklets/apple/Networking/WorkletsNetworking.h +25 -0
  126. package/apple/worklets/apple/Networking/WorkletsNetworking.mm +707 -0
  127. package/apple/worklets/apple/PlatformLogger.mm +31 -0
  128. package/apple/worklets/apple/SlowAnimations.h +8 -0
  129. package/apple/worklets/apple/SlowAnimations.mm +45 -0
  130. package/apple/worklets/apple/WorkletsDisplayLink.h +21 -0
  131. package/apple/worklets/apple/WorkletsModule.h +28 -0
  132. package/apple/worklets/apple/WorkletsModule.mm +177 -0
  133. package/bundleMode/index.d.ts +6 -0
  134. package/bundleMode/index.js +182 -0
  135. package/bundleMode/shims/reactNativeShim.js +21 -0
  136. package/bundleMode/shims/turboModuleRegistryShim.js +47 -0
  137. package/changes.json +58 -0
  138. package/compatibility.json +29 -0
  139. package/jest/pluginTestUtils.ts +16 -0
  140. package/jest/resolver.js +18 -0
  141. package/lib/module/WorkletsModule/NativeWorklets.js +4 -0
  142. package/lib/module/WorkletsModule/NativeWorklets.js.map +1 -0
  143. package/lib/module/WorkletsModule/NativeWorklets.native.js +199 -0
  144. package/lib/module/WorkletsModule/NativeWorklets.native.js.map +1 -0
  145. package/lib/module/WorkletsModule/workletsModuleProxy.js +4 -0
  146. package/lib/module/WorkletsModule/workletsModuleProxy.js.map +1 -0
  147. package/lib/module/bundleMode/metroOverrides.native.js +26 -0
  148. package/lib/module/bundleMode/metroOverrides.native.js.map +1 -0
  149. package/lib/module/bundleMode/network.native.js +44 -0
  150. package/lib/module/bundleMode/network.native.js.map +1 -0
  151. package/lib/module/debug/bundleMode.js +6 -0
  152. package/lib/module/debug/bundleMode.js.map +1 -0
  153. package/lib/module/debug/bundleMode.native.js +11 -0
  154. package/lib/module/debug/bundleMode.native.js.map +1 -0
  155. package/lib/module/debug/checkCppVersion.js +29 -0
  156. package/lib/module/debug/checkCppVersion.js.map +1 -0
  157. package/lib/module/debug/errors.native.js +67 -0
  158. package/lib/module/debug/errors.native.js.map +1 -0
  159. package/lib/module/debug/jsVersion.js +9 -0
  160. package/lib/module/debug/jsVersion.js.map +1 -0
  161. package/lib/module/debug/logger.js +15 -0
  162. package/lib/module/debug/logger.js.map +1 -0
  163. package/lib/module/debug/slowAnimations.js +7 -0
  164. package/lib/module/debug/slowAnimations.js.map +1 -0
  165. package/lib/module/debug/slowAnimations.native.js +17 -0
  166. package/lib/module/debug/slowAnimations.native.js.map +1 -0
  167. package/lib/module/deprecated.js +27 -0
  168. package/lib/module/deprecated.js.map +1 -0
  169. package/lib/module/featureFlags/featureFlags.js +12 -0
  170. package/lib/module/featureFlags/featureFlags.js.map +1 -0
  171. package/lib/module/featureFlags/featureFlags.native.js +51 -0
  172. package/lib/module/featureFlags/featureFlags.native.js.map +1 -0
  173. package/lib/module/featureFlags/staticFlags.json +6 -0
  174. package/lib/module/featureFlags/types.js +16 -0
  175. package/lib/module/featureFlags/types.js.map +1 -0
  176. package/lib/module/guardImplementation.native.js +17 -0
  177. package/lib/module/guardImplementation.native.js.map +1 -0
  178. package/lib/module/index.js +26 -0
  179. package/lib/module/index.js.map +1 -0
  180. package/lib/module/initializers/initializers.js +24 -0
  181. package/lib/module/initializers/initializers.js.map +1 -0
  182. package/lib/module/initializers/initializers.native.js +195 -0
  183. package/lib/module/initializers/initializers.native.js.map +1 -0
  184. package/lib/module/memory/bundleUnpacker.native.js +38 -0
  185. package/lib/module/memory/bundleUnpacker.native.js.map +1 -0
  186. package/lib/module/memory/customSerializableUnpacker.native.js +20 -0
  187. package/lib/module/memory/customSerializableUnpacker.native.js.map +1 -0
  188. package/lib/module/memory/isShareable.js +8 -0
  189. package/lib/module/memory/isShareable.js.map +1 -0
  190. package/lib/module/memory/isSynchronizable.js +8 -0
  191. package/lib/module/memory/isSynchronizable.js.map +1 -0
  192. package/lib/module/memory/remoteFunctionUnpacker.native.js +18 -0
  193. package/lib/module/memory/remoteFunctionUnpacker.native.js.map +1 -0
  194. package/lib/module/memory/serializable.js +18 -0
  195. package/lib/module/memory/serializable.js.map +1 -0
  196. package/lib/module/memory/serializable.native.js +619 -0
  197. package/lib/module/memory/serializable.native.js.map +1 -0
  198. package/lib/module/memory/serializableMappingCache.js +11 -0
  199. package/lib/module/memory/serializableMappingCache.js.map +1 -0
  200. package/lib/module/memory/serializableMappingCache.native.js +29 -0
  201. package/lib/module/memory/serializableMappingCache.native.js.map +1 -0
  202. package/lib/module/memory/shareable.js +18 -0
  203. package/lib/module/memory/shareable.js.map +1 -0
  204. package/lib/module/memory/shareable.native.js +42 -0
  205. package/lib/module/memory/shareable.native.js.map +1 -0
  206. package/lib/module/memory/shareableGuestUnpacker.native.js +105 -0
  207. package/lib/module/memory/shareableGuestUnpacker.native.js.map +1 -0
  208. package/lib/module/memory/shareableHostUnpacker.native.js +20 -0
  209. package/lib/module/memory/shareableHostUnpacker.native.js.map +1 -0
  210. package/lib/module/memory/synchronizable.js +6 -0
  211. package/lib/module/memory/synchronizable.js.map +1 -0
  212. package/lib/module/memory/synchronizable.native.js +9 -0
  213. package/lib/module/memory/synchronizable.native.js.map +1 -0
  214. package/lib/module/memory/synchronizableUnpacker.native.js +47 -0
  215. package/lib/module/memory/synchronizableUnpacker.native.js.map +1 -0
  216. package/lib/module/memory/types.js +12 -0
  217. package/lib/module/memory/types.js.map +1 -0
  218. package/lib/module/memory/valueUnpacker.native.js +52 -0
  219. package/lib/module/memory/valueUnpacker.native.js.map +1 -0
  220. package/lib/module/mock.js +85 -0
  221. package/lib/module/mock.js.map +1 -0
  222. package/lib/module/package.json +1 -0
  223. package/lib/module/platformChecker.js +4 -0
  224. package/lib/module/platformChecker.js.map +1 -0
  225. package/lib/module/platformChecker.native.js +4 -0
  226. package/lib/module/platformChecker.native.js.map +1 -0
  227. package/lib/module/privateGlobals.d.js +7 -0
  228. package/lib/module/privateGlobals.d.js.map +1 -0
  229. package/lib/module/runLoop/common/setImmediatePolyfill.js +15 -0
  230. package/lib/module/runLoop/common/setImmediatePolyfill.js.map +1 -0
  231. package/lib/module/runLoop/common/setIntervalPolyfill.js +26 -0
  232. package/lib/module/runLoop/common/setIntervalPolyfill.js.map +1 -0
  233. package/lib/module/runLoop/uiRuntime/mockedRequestAnimationFrame.js +10 -0
  234. package/lib/module/runLoop/uiRuntime/mockedRequestAnimationFrame.js.map +1 -0
  235. package/lib/module/runLoop/uiRuntime/requestAnimationFrame.js +71 -0
  236. package/lib/module/runLoop/uiRuntime/requestAnimationFrame.js.map +1 -0
  237. package/lib/module/runLoop/uiRuntime/setTimeoutPolyfill.js +32 -0
  238. package/lib/module/runLoop/uiRuntime/setTimeoutPolyfill.js.map +1 -0
  239. package/lib/module/runLoop/workletRuntime/index.js +19 -0
  240. package/lib/module/runLoop/workletRuntime/index.js.map +1 -0
  241. package/lib/module/runLoop/workletRuntime/queueMicrotask.js +11 -0
  242. package/lib/module/runLoop/workletRuntime/queueMicrotask.js.map +1 -0
  243. package/lib/module/runLoop/workletRuntime/requestAnimationFramePolyfill.js +48 -0
  244. package/lib/module/runLoop/workletRuntime/requestAnimationFramePolyfill.js.map +1 -0
  245. package/lib/module/runLoop/workletRuntime/setTimeout.js +28 -0
  246. package/lib/module/runLoop/workletRuntime/setTimeout.js.map +1 -0
  247. package/lib/module/runLoop/workletRuntime/taskQueue.js +37 -0
  248. package/lib/module/runLoop/workletRuntime/taskQueue.js.map +1 -0
  249. package/lib/module/runtimeKind.js +92 -0
  250. package/lib/module/runtimeKind.js.map +1 -0
  251. package/lib/module/runtimes.js +36 -0
  252. package/lib/module/runtimes.js.map +1 -0
  253. package/lib/module/runtimes.native.js +379 -0
  254. package/lib/module/runtimes.native.js.map +1 -0
  255. package/lib/module/specs/NativeWorkletsModule.js +5 -0
  256. package/lib/module/specs/NativeWorkletsModule.js.map +1 -0
  257. package/lib/module/specs/index.js +10 -0
  258. package/lib/module/specs/index.js.map +1 -0
  259. package/lib/module/threads.js +60 -0
  260. package/lib/module/threads.js.map +1 -0
  261. package/lib/module/threads.native.js +295 -0
  262. package/lib/module/threads.native.js.map +1 -0
  263. package/lib/module/types.js +5 -0
  264. package/lib/module/types.js.map +1 -0
  265. package/lib/module/workletFunction.js +36 -0
  266. package/lib/module/workletFunction.js.map +1 -0
  267. package/lib/typescript/WorkletsModule/NativeWorklets.d.ts +3 -0
  268. package/lib/typescript/WorkletsModule/NativeWorklets.d.ts.map +1 -0
  269. package/lib/typescript/WorkletsModule/NativeWorklets.native.d.ts +3 -0
  270. package/lib/typescript/WorkletsModule/NativeWorklets.native.d.ts.map +1 -0
  271. package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts +59 -0
  272. package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts.map +1 -0
  273. package/lib/typescript/bundleMode/metroOverrides.native.d.ts +9 -0
  274. package/lib/typescript/bundleMode/metroOverrides.native.d.ts.map +1 -0
  275. package/lib/typescript/bundleMode/network.native.d.ts +7 -0
  276. package/lib/typescript/bundleMode/network.native.d.ts.map +1 -0
  277. package/lib/typescript/debug/bundleMode.d.ts +2 -0
  278. package/lib/typescript/debug/bundleMode.d.ts.map +1 -0
  279. package/lib/typescript/debug/bundleMode.native.d.ts +2 -0
  280. package/lib/typescript/debug/bundleMode.native.d.ts.map +1 -0
  281. package/lib/typescript/debug/checkCppVersion.d.ts +3 -0
  282. package/lib/typescript/debug/checkCppVersion.d.ts.map +1 -0
  283. package/lib/typescript/debug/errors.native.d.ts +13 -0
  284. package/lib/typescript/debug/errors.native.d.ts.map +1 -0
  285. package/lib/typescript/debug/jsVersion.d.ts +7 -0
  286. package/lib/typescript/debug/jsVersion.d.ts.map +1 -0
  287. package/lib/typescript/debug/logger.d.ts +5 -0
  288. package/lib/typescript/debug/logger.d.ts.map +1 -0
  289. package/lib/typescript/debug/slowAnimations.d.ts +2 -0
  290. package/lib/typescript/debug/slowAnimations.d.ts.map +1 -0
  291. package/lib/typescript/debug/slowAnimations.native.d.ts +11 -0
  292. package/lib/typescript/debug/slowAnimations.native.d.ts.map +1 -0
  293. package/lib/typescript/deprecated.d.ts +19 -0
  294. package/lib/typescript/deprecated.d.ts.map +1 -0
  295. package/lib/typescript/featureFlags/featureFlags.d.ts +5 -0
  296. package/lib/typescript/featureFlags/featureFlags.d.ts.map +1 -0
  297. package/lib/typescript/featureFlags/featureFlags.native.d.ts +6 -0
  298. package/lib/typescript/featureFlags/featureFlags.native.d.ts.map +1 -0
  299. package/lib/typescript/featureFlags/types.d.ts +23 -0
  300. package/lib/typescript/featureFlags/types.d.ts.map +1 -0
  301. package/lib/typescript/guardImplementation.native.d.ts +3 -0
  302. package/lib/typescript/guardImplementation.native.d.ts.map +1 -0
  303. package/lib/typescript/index.d.ts +19 -0
  304. package/lib/typescript/index.d.ts.map +1 -0
  305. package/lib/typescript/initializers/initializers.d.ts +2 -0
  306. package/lib/typescript/initializers/initializers.d.ts.map +1 -0
  307. package/lib/typescript/initializers/initializers.native.d.ts +19 -0
  308. package/lib/typescript/initializers/initializers.native.d.ts.map +1 -0
  309. package/lib/typescript/memory/bundleUnpacker.native.d.ts +7 -0
  310. package/lib/typescript/memory/bundleUnpacker.native.d.ts.map +1 -0
  311. package/lib/typescript/memory/customSerializableUnpacker.native.d.ts +3 -0
  312. package/lib/typescript/memory/customSerializableUnpacker.native.d.ts.map +1 -0
  313. package/lib/typescript/memory/isShareable.d.ts +3 -0
  314. package/lib/typescript/memory/isShareable.d.ts.map +1 -0
  315. package/lib/typescript/memory/isSynchronizable.d.ts +3 -0
  316. package/lib/typescript/memory/isSynchronizable.d.ts.map +1 -0
  317. package/lib/typescript/memory/remoteFunctionUnpacker.native.d.ts +3 -0
  318. package/lib/typescript/memory/remoteFunctionUnpacker.native.d.ts.map +1 -0
  319. package/lib/typescript/memory/serializable.d.ts +7 -0
  320. package/lib/typescript/memory/serializable.d.ts.map +1 -0
  321. package/lib/typescript/memory/serializable.native.d.ts +37 -0
  322. package/lib/typescript/memory/serializable.native.d.ts.map +1 -0
  323. package/lib/typescript/memory/serializableMappingCache.d.ts +6 -0
  324. package/lib/typescript/memory/serializableMappingCache.d.ts.map +1 -0
  325. package/lib/typescript/memory/serializableMappingCache.native.d.ts +13 -0
  326. package/lib/typescript/memory/serializableMappingCache.native.d.ts.map +1 -0
  327. package/lib/typescript/memory/shareable.d.ts +15 -0
  328. package/lib/typescript/memory/shareable.d.ts.map +1 -0
  329. package/lib/typescript/memory/shareable.native.d.ts +15 -0
  330. package/lib/typescript/memory/shareable.native.d.ts.map +1 -0
  331. package/lib/typescript/memory/shareableGuestUnpacker.native.d.ts +4 -0
  332. package/lib/typescript/memory/shareableGuestUnpacker.native.d.ts.map +1 -0
  333. package/lib/typescript/memory/shareableHostUnpacker.native.d.ts +4 -0
  334. package/lib/typescript/memory/shareableHostUnpacker.native.d.ts.map +1 -0
  335. package/lib/typescript/memory/synchronizable.d.ts +3 -0
  336. package/lib/typescript/memory/synchronizable.d.ts.map +1 -0
  337. package/lib/typescript/memory/synchronizable.native.d.ts +3 -0
  338. package/lib/typescript/memory/synchronizable.native.d.ts.map +1 -0
  339. package/lib/typescript/memory/synchronizableUnpacker.native.d.ts +4 -0
  340. package/lib/typescript/memory/synchronizableUnpacker.native.d.ts.map +1 -0
  341. package/lib/typescript/memory/types.d.ts +136 -0
  342. package/lib/typescript/memory/types.d.ts.map +1 -0
  343. package/lib/typescript/memory/valueUnpacker.native.d.ts +7 -0
  344. package/lib/typescript/memory/valueUnpacker.native.d.ts.map +1 -0
  345. package/lib/typescript/mock.d.ts +2 -0
  346. package/lib/typescript/mock.d.ts.map +1 -0
  347. package/lib/typescript/package.json +1 -0
  348. package/lib/typescript/platformChecker.d.ts +2 -0
  349. package/lib/typescript/platformChecker.d.ts.map +1 -0
  350. package/lib/typescript/platformChecker.native.d.ts +2 -0
  351. package/lib/typescript/platformChecker.native.d.ts.map +1 -0
  352. package/lib/typescript/runLoop/common/setImmediatePolyfill.d.ts +2 -0
  353. package/lib/typescript/runLoop/common/setImmediatePolyfill.d.ts.map +1 -0
  354. package/lib/typescript/runLoop/common/setIntervalPolyfill.d.ts +2 -0
  355. package/lib/typescript/runLoop/common/setIntervalPolyfill.d.ts.map +1 -0
  356. package/lib/typescript/runLoop/uiRuntime/mockedRequestAnimationFrame.d.ts +6 -0
  357. package/lib/typescript/runLoop/uiRuntime/mockedRequestAnimationFrame.d.ts.map +1 -0
  358. package/lib/typescript/runLoop/uiRuntime/requestAnimationFrame.d.ts +2 -0
  359. package/lib/typescript/runLoop/uiRuntime/requestAnimationFrame.d.ts.map +1 -0
  360. package/lib/typescript/runLoop/uiRuntime/setTimeoutPolyfill.d.ts +2 -0
  361. package/lib/typescript/runLoop/uiRuntime/setTimeoutPolyfill.d.ts.map +1 -0
  362. package/lib/typescript/runLoop/workletRuntime/index.d.ts +2 -0
  363. package/lib/typescript/runLoop/workletRuntime/index.d.ts.map +1 -0
  364. package/lib/typescript/runLoop/workletRuntime/queueMicrotask.d.ts +2 -0
  365. package/lib/typescript/runLoop/workletRuntime/queueMicrotask.d.ts.map +1 -0
  366. package/lib/typescript/runLoop/workletRuntime/requestAnimationFramePolyfill.d.ts +2 -0
  367. package/lib/typescript/runLoop/workletRuntime/requestAnimationFramePolyfill.d.ts.map +1 -0
  368. package/lib/typescript/runLoop/workletRuntime/setTimeout.d.ts +2 -0
  369. package/lib/typescript/runLoop/workletRuntime/setTimeout.d.ts.map +1 -0
  370. package/lib/typescript/runLoop/workletRuntime/taskQueue.d.ts +10 -0
  371. package/lib/typescript/runLoop/workletRuntime/taskQueue.d.ts.map +1 -0
  372. package/lib/typescript/runtimeKind.d.ts +56 -0
  373. package/lib/typescript/runtimeKind.d.ts.map +1 -0
  374. package/lib/typescript/runtimes.d.ts +15 -0
  375. package/lib/typescript/runtimes.d.ts.map +1 -0
  376. package/lib/typescript/runtimes.native.d.ts +155 -0
  377. package/lib/typescript/runtimes.native.d.ts.map +1 -0
  378. package/lib/typescript/specs/NativeWorkletsModule.d.ts +9 -0
  379. package/lib/typescript/specs/NativeWorkletsModule.d.ts.map +1 -0
  380. package/lib/typescript/specs/index.d.ts +3 -0
  381. package/lib/typescript/specs/index.d.ts.map +1 -0
  382. package/lib/typescript/threads.d.ts +8 -0
  383. package/lib/typescript/threads.d.ts.map +1 -0
  384. package/lib/typescript/threads.native.d.ts +125 -0
  385. package/lib/typescript/threads.native.d.ts.map +1 -0
  386. package/lib/typescript/types.d.ts +136 -0
  387. package/lib/typescript/types.d.ts.map +1 -0
  388. package/lib/typescript/workletFunction.d.ts +26 -0
  389. package/lib/typescript/workletFunction.d.ts.map +1 -0
  390. package/package.json +220 -0
  391. package/plugin/index.d.ts +168 -0
  392. package/plugin/index.js +2188 -0
  393. package/scripts/validate-react-native-version.js +43 -0
  394. package/scripts/worklets_utils.rb +94 -0
  395. package/src/WorkletsModule/NativeWorklets.native.ts +461 -0
  396. package/src/WorkletsModule/NativeWorklets.ts +5 -0
  397. package/src/WorkletsModule/workletsModuleProxy.ts +247 -0
  398. package/src/bundleMode/metroOverrides.native.ts +33 -0
  399. package/src/bundleMode/network.native.ts +63 -0
  400. package/src/debug/bundleMode.native.ts +18 -0
  401. package/src/debug/bundleMode.ts +5 -0
  402. package/src/debug/checkCppVersion.ts +34 -0
  403. package/src/debug/errors.native.ts +74 -0
  404. package/src/debug/jsVersion.ts +8 -0
  405. package/src/debug/logger.ts +16 -0
  406. package/src/debug/slowAnimations.native.ts +15 -0
  407. package/src/debug/slowAnimations.ts +6 -0
  408. package/src/deprecated.ts +38 -0
  409. package/src/featureFlags/featureFlags.native.ts +70 -0
  410. package/src/featureFlags/featureFlags.ts +20 -0
  411. package/src/featureFlags/staticFlags.json +6 -0
  412. package/src/featureFlags/types.ts +32 -0
  413. package/src/guardImplementation.native.ts +26 -0
  414. package/src/index.ts +95 -0
  415. package/src/initializers/initializers.native.ts +218 -0
  416. package/src/initializers/initializers.ts +24 -0
  417. package/src/memory/bundleUnpacker.native.ts +59 -0
  418. package/src/memory/customSerializableUnpacker.native.ts +30 -0
  419. package/src/memory/isShareable.ts +16 -0
  420. package/src/memory/isSynchronizable.ts +14 -0
  421. package/src/memory/remoteFunctionUnpacker.native.ts +27 -0
  422. package/src/memory/serializable.native.ts +889 -0
  423. package/src/memory/serializable.ts +36 -0
  424. package/src/memory/serializableMappingCache.native.ts +37 -0
  425. package/src/memory/serializableMappingCache.ts +12 -0
  426. package/src/memory/shareable.native.ts +68 -0
  427. package/src/memory/shareable.ts +27 -0
  428. package/src/memory/shareableGuestUnpacker.native.ts +168 -0
  429. package/src/memory/shareableHostUnpacker.native.ts +31 -0
  430. package/src/memory/synchronizable.native.ts +17 -0
  431. package/src/memory/synchronizable.ts +9 -0
  432. package/src/memory/synchronizableUnpacker.native.ts +65 -0
  433. package/src/memory/types.ts +191 -0
  434. package/src/memory/valueUnpacker.native.ts +79 -0
  435. package/src/mock.ts +111 -0
  436. package/src/platformChecker.native.ts +3 -0
  437. package/src/platformChecker.ts +4 -0
  438. package/src/privateGlobals.d.ts +127 -0
  439. package/src/runLoop/common/setImmediatePolyfill.ts +20 -0
  440. package/src/runLoop/common/setIntervalPolyfill.ts +38 -0
  441. package/src/runLoop/uiRuntime/mockedRequestAnimationFrame.ts +11 -0
  442. package/src/runLoop/uiRuntime/requestAnimationFrame.ts +87 -0
  443. package/src/runLoop/uiRuntime/setTimeoutPolyfill.ts +40 -0
  444. package/src/runLoop/workletRuntime/index.ts +18 -0
  445. package/src/runLoop/workletRuntime/queueMicrotask.ts +13 -0
  446. package/src/runLoop/workletRuntime/requestAnimationFramePolyfill.ts +61 -0
  447. package/src/runLoop/workletRuntime/setTimeout.ts +37 -0
  448. package/src/runLoop/workletRuntime/taskQueue.ts +44 -0
  449. package/src/runtimeKind.ts +85 -0
  450. package/src/runtimes.native.ts +572 -0
  451. package/src/runtimes.ts +107 -0
  452. package/src/specs/NativeWorkletsModule.ts +11 -0
  453. package/src/specs/index.ts +13 -0
  454. package/src/threads.native.ts +408 -0
  455. package/src/threads.ts +110 -0
  456. package/src/types.ts +164 -0
  457. package/src/workletFunction.ts +41 -0
@@ -0,0 +1,889 @@
1
+ 'use strict';
2
+
3
+ import { isBundleModeEnabled } from '../debug/bundleMode';
4
+ import { registerWorkletStackDetails } from '../debug/errors';
5
+ import { jsVersion } from '../debug/jsVersion';
6
+ import { logger } from '../debug/logger';
7
+ import { getRuntimeKind, RuntimeKind } from '../runtimeKind';
8
+ import type { WorkletFunction, WorkletImport } from '../types';
9
+ import { isWorkletFunction } from '../workletFunction';
10
+ import { WorkletsModule } from '../WorkletsModule/NativeWorklets';
11
+ import { isSynchronizable } from './isSynchronizable';
12
+ import {
13
+ serializableMappingCache,
14
+ serializableMappingFlag,
15
+ } from './serializableMappingCache';
16
+ import type {
17
+ FlatSerializableRef,
18
+ RegistrationData,
19
+ RemoteFunction,
20
+ SerializableRef,
21
+ SerializationData,
22
+ Synchronizable,
23
+ } from './types';
24
+
25
+ const MAGIC_KEY = 'REANIMATED_MAGIC_KEY';
26
+
27
+ function isHostObject(value: NonNullable<object>) {
28
+ 'worklet';
29
+ // We could use JSI to determine whether an object is a host object, however
30
+ // the below workaround works well and is way faster than an additional JSI call.
31
+ // We use the fact that host objects have broken implementation of `hasOwnProperty`
32
+ // and hence return true for all `in` checks regardless of the key we ask for.
33
+ return MAGIC_KEY in value;
34
+ }
35
+
36
+ export function isSerializableRef<TValue = unknown>(
37
+ value: unknown
38
+ ): value is SerializableRef<TValue> {
39
+ 'worklet';
40
+ return (
41
+ typeof value === 'object' &&
42
+ value !== null &&
43
+ '__serializableRef' in value &&
44
+ value.__serializableRef === true
45
+ );
46
+ }
47
+
48
+ function isPlainJSObject(object: object): object is Record<string, unknown> {
49
+ 'worklet';
50
+ return Object.getPrototypeOf(object) === Object.prototype;
51
+ }
52
+
53
+ /**
54
+ * RN has introduced a new representation of TurboModules as a JS object whose
55
+ * prototype is the host object. More details:
56
+ * https://github.com/facebook/react-native/blob/main/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.cpp#L182
57
+ */
58
+ function isTurboModuleLike(object: object): object is Record<string, unknown> {
59
+ const proto = Object.getPrototypeOf(object);
60
+ return proto !== null && isHostObject(proto);
61
+ }
62
+
63
+ function getFromCache(value: object) {
64
+ const cached = serializableMappingCache.get(value);
65
+ if (cached === serializableMappingFlag) {
66
+ // This means that `value` was already a clone and we should return it as is.
67
+ return value;
68
+ }
69
+ return cached;
70
+ }
71
+
72
+ const VALID_ARRAY_VIEWS_NAMES = [
73
+ 'Int8Array',
74
+ 'Uint8Array',
75
+ 'Uint8ClampedArray',
76
+ 'Int16Array',
77
+ 'Uint16Array',
78
+ 'Int32Array',
79
+ 'Uint32Array',
80
+ 'Float32Array',
81
+ 'Float64Array',
82
+ 'BigInt64Array',
83
+ 'BigUint64Array',
84
+ 'DataView',
85
+ ];
86
+
87
+ const DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD = 30;
88
+ // Below variable stores object that we process in createSerializable at the specified depth.
89
+ // We use it to check if later on the function reenters with the same object
90
+ let processedObjectAtThresholdDepth: unknown;
91
+
92
+ export function createSerializable<TValue>(
93
+ value: TValue,
94
+ shouldPersistRemote = false,
95
+ depth = 0
96
+ ): SerializableRef<TValue> {
97
+ detectCyclicObject(value, depth);
98
+
99
+ const isFunction = typeof value === 'function';
100
+
101
+ if (typeof value === 'string') {
102
+ return cloneString(value) as SerializableRef<TValue>;
103
+ }
104
+
105
+ if (typeof value === 'number') {
106
+ return cloneNumber(value) as SerializableRef<TValue>;
107
+ }
108
+
109
+ if (typeof value === 'boolean') {
110
+ return cloneBoolean(value) as SerializableRef<TValue>;
111
+ }
112
+
113
+ if (typeof value === 'bigint') {
114
+ return cloneBigInt(value) as SerializableRef<TValue>;
115
+ }
116
+
117
+ if (typeof value === 'symbol') {
118
+ return cloneString(String(value)) as SerializableRef<TValue>;
119
+ }
120
+
121
+ if (value === undefined) {
122
+ return cloneUndefined() as SerializableRef<TValue>;
123
+ }
124
+
125
+ if (value === null) {
126
+ return cloneNull() as SerializableRef<TValue>;
127
+ }
128
+
129
+ const cached = getFromCache(value);
130
+ if (cached !== undefined) {
131
+ if (globalThis.WeakRef && cached instanceof WeakRef) {
132
+ // WeakRef is installed on runtimes only with Hermes microtaskQueue enabled.
133
+ const deref = cached.deref();
134
+ if (deref !== undefined) {
135
+ return deref as SerializableRef<TValue>;
136
+ }
137
+ } else {
138
+ return cached as SerializableRef<TValue>;
139
+ }
140
+ }
141
+
142
+ if (Array.isArray(value)) {
143
+ return cloneArray(value, shouldPersistRemote, depth);
144
+ }
145
+ if (isFunction) {
146
+ if (globalThis._WORKLETS_BUNDLE_MODE_ENABLED) {
147
+ if ((value as unknown as WorkletImport).__bundleData) {
148
+ return cloneImport(
149
+ value as unknown as WorkletImport
150
+ ) as SerializableRef<TValue>;
151
+ }
152
+ }
153
+ if ((value as unknown as RemoteFunction).__remoteFunction) {
154
+ // Remote functions are already serialized.
155
+ return value as unknown as SerializableRef<TValue>;
156
+ }
157
+ if (!isWorkletFunction(value)) {
158
+ return cloneNonWorkletFunction(
159
+ value as () => unknown
160
+ ) as SerializableRef<TValue>;
161
+ }
162
+ }
163
+ if (isTurboModuleLike(value)) {
164
+ return cloneTurboModuleLike(value, shouldPersistRemote, depth);
165
+ }
166
+ if (isHostObject(value)) {
167
+ return cloneHostObject(value);
168
+ }
169
+ if (isPlainJSObject(value) && value.__init) {
170
+ return cloneInitializer(
171
+ value,
172
+ shouldPersistRemote,
173
+ depth
174
+ ) as SerializableRef<TValue>;
175
+ }
176
+ if (isPlainJSObject(value) && value.__workletContextObjectFactory) {
177
+ return cloneContextObject(value);
178
+ }
179
+ if ((isPlainJSObject(value) || isFunction) && isWorkletFunction(value)) {
180
+ return cloneWorklet(value, shouldPersistRemote, depth);
181
+ }
182
+ if (isSynchronizable(value)) {
183
+ return cloneSynchronizable(value) as SerializableRef<TValue>;
184
+ }
185
+ if (isPlainJSObject(value) || isFunction) {
186
+ return clonePlainJSObject(value, shouldPersistRemote, depth);
187
+ }
188
+ if (value instanceof Set) {
189
+ return cloneSet(value) as SerializableRef<TValue>;
190
+ }
191
+ if (value instanceof Map) {
192
+ return cloneMap(value) as SerializableRef<TValue>;
193
+ }
194
+ if (value instanceof RegExp) {
195
+ return cloneRegExp(value) as SerializableRef<TValue>;
196
+ }
197
+ if (value instanceof Error) {
198
+ return cloneError(value) as SerializableRef<TValue>;
199
+ }
200
+ if (value instanceof ArrayBuffer) {
201
+ return cloneArrayBuffer(value) as SerializableRef<TValue>;
202
+ }
203
+ if (ArrayBuffer.isView(value)) {
204
+ // typed array (e.g. Int32Array, Uint8ClampedArray) or DataView
205
+ return cloneArrayBufferView(value);
206
+ }
207
+ for (let i = 0; i < customSerializationRegistry.length; i++) {
208
+ const { determine, pack } = customSerializationRegistry[i];
209
+ if (determine(value)) {
210
+ return cloneCustom(value, pack, i) as SerializableRef<TValue>;
211
+ }
212
+ }
213
+
214
+ return cloneUndefined() as SerializableRef<TValue>;
215
+ }
216
+
217
+ if (isBundleModeEnabled()) {
218
+ // TODO: Do it programmatically.
219
+ createSerializable.__bundleData = {
220
+ imported: 'createSerializable',
221
+ source: require.resolveWeak('react-native-worklets'),
222
+ };
223
+ }
224
+
225
+ if (!globalThis.__customSerializationRegistry) {
226
+ globalThis.__customSerializationRegistry =
227
+ [] as typeof globalThis.__customSerializationRegistry;
228
+ }
229
+ const customSerializationRegistry = globalThis.__customSerializationRegistry;
230
+
231
+ /**
232
+ * `registerCustomSerializable` lets you register your own pre-serialization and
233
+ * post-deserialization logic. This is necessary for objects with prototypes
234
+ * different than just `Object.prototype` or some other built-in prototypes like
235
+ * `Map` etc. Worklets can't handle such objects by default to convert into
236
+ * [Serializables](https://docs.swmansion.com/react-native-worklets/docs/memory/serializable)
237
+ * hence you need to register them as **Custom Serializables**. This way you can
238
+ * tell Worklets how to transfer your custom data structures between different
239
+ * Runtimes without manually serializing and deserializing them every time.
240
+ *
241
+ * @param registrationData - The registration data for the custom serializable -
242
+ * {@link RegistrationData}
243
+ * @see https://docs.swmansion.com/react-native-worklets/docs/memory/registerCustomSerializable/
244
+ */
245
+ export function registerCustomSerializable<
246
+ TValue extends object,
247
+ TPacked extends object,
248
+ >(registrationData: RegistrationData<TValue, TPacked>) {
249
+ if (__DEV__ && getRuntimeKind() !== RuntimeKind.ReactNative) {
250
+ throw new Error(
251
+ '[Worklets] registerCustomSerializable can be used only on React Native runtime.'
252
+ );
253
+ }
254
+
255
+ const { name, determine, pack, unpack } = registrationData;
256
+
257
+ if (__DEV__) {
258
+ verifyRegistrationData(determine, pack, unpack);
259
+ }
260
+ if (customSerializationRegistry.some((data) => data.name === name)) {
261
+ if (__DEV__) {
262
+ console.warn(
263
+ `Custom serializable with name "${name}" is already registered. Duplicate registration is ignored.`
264
+ );
265
+ }
266
+ return;
267
+ }
268
+
269
+ customSerializationRegistry.push(
270
+ registrationData as unknown as SerializationData<object, unknown>
271
+ );
272
+
273
+ WorkletsModule.registerCustomSerializable(
274
+ createSerializable(determine),
275
+ createSerializable(pack),
276
+ createSerializable(unpack),
277
+ customSerializationRegistry.length - 1
278
+ );
279
+ }
280
+
281
+ function verifyRegistrationData(
282
+ determine: unknown,
283
+ pack: unknown,
284
+ unpack: unknown
285
+ ) {
286
+ if (!isWorkletFunction(determine)) {
287
+ throw new Error(
288
+ '[Worklets] The "determine" function provided to registerCustomSerializable must be a worklet.'
289
+ );
290
+ }
291
+ if (!isWorkletFunction(pack)) {
292
+ throw new Error(
293
+ '[Worklets] The "pack" function provided to registerCustomSerializable must be a worklet.'
294
+ );
295
+ }
296
+ if (!isWorkletFunction(unpack)) {
297
+ throw new Error(
298
+ '[Worklets] The "unpack" function provided to registerCustomSerializable must be a worklet.'
299
+ );
300
+ }
301
+ }
302
+
303
+ function detectCyclicObject(value: unknown, depth: number) {
304
+ if (depth >= DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD) {
305
+ // if we reach certain recursion depth we suspect that we are dealing with a cyclic object.
306
+ // this type of objects are not supported and cannot be transferred as serializable, so we
307
+ // implement a simple detection mechanism that remembers the value at a given depth and
308
+ // tests whether we try reenter this method later on with the same value. If that happens
309
+ // we throw an appropriate error.
310
+ if (depth === DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD) {
311
+ processedObjectAtThresholdDepth = value;
312
+ } else if (value === processedObjectAtThresholdDepth) {
313
+ throw new Error(
314
+ '[Worklets] Trying to convert a cyclic object to a serializable. This is not supported.'
315
+ );
316
+ }
317
+ } else {
318
+ processedObjectAtThresholdDepth = undefined;
319
+ }
320
+ }
321
+
322
+ function cloneString(value: string): SerializableRef<string> {
323
+ return WorkletsModule.createSerializableString(value);
324
+ }
325
+
326
+ function cloneNumber(value: number): SerializableRef<number> {
327
+ return WorkletsModule.createSerializableNumber(value);
328
+ }
329
+
330
+ function cloneBoolean(value: boolean): SerializableRef<boolean> {
331
+ return WorkletsModule.createSerializableBoolean(value);
332
+ }
333
+
334
+ function cloneBigInt(value: bigint): SerializableRef<bigint> {
335
+ return WorkletsModule.createSerializableBigInt(value);
336
+ }
337
+
338
+ function cloneUndefined(): SerializableRef<undefined> {
339
+ return WorkletsModule.createSerializableUndefined();
340
+ }
341
+
342
+ function cloneNull(): SerializableRef<null> {
343
+ return WorkletsModule.createSerializableNull();
344
+ }
345
+
346
+ function cloneObjectProperties<T extends object>(
347
+ value: T,
348
+ shouldPersistRemote: boolean,
349
+ depth: number
350
+ ): Record<string, unknown> {
351
+ const clonedProps: Record<string, unknown> = {};
352
+ for (const [key, element] of Object.entries(value)) {
353
+ // We don't need to clone __initData field as it contains long strings
354
+ // representing the worklet code, source map, and location, and we will
355
+ // serialize/deserialize it once.
356
+ if (key === '__initData' && clonedProps.__initData !== undefined) {
357
+ continue;
358
+ }
359
+ clonedProps[key] = createSerializable(
360
+ element,
361
+ shouldPersistRemote,
362
+ depth + 1
363
+ );
364
+ }
365
+ return clonedProps;
366
+ }
367
+
368
+ function cloneInitializer(
369
+ value: object,
370
+ shouldPersistRemote = false,
371
+ depth = 0
372
+ ): SerializableRef<object> {
373
+ const clonedProps: Record<string, unknown> = cloneObjectProperties(
374
+ value,
375
+ shouldPersistRemote,
376
+ depth
377
+ );
378
+ return WorkletsModule.createSerializableInitializer(clonedProps);
379
+ }
380
+
381
+ function cloneArray<T extends unknown[]>(
382
+ value: T,
383
+ shouldPersistRemote: boolean,
384
+ depth: number
385
+ ): SerializableRef<T> {
386
+ const clonedElements = value.map((element) =>
387
+ createSerializable(element, shouldPersistRemote, depth + 1)
388
+ );
389
+ const clone = WorkletsModule.createSerializableArray(
390
+ clonedElements,
391
+ shouldPersistRemote
392
+ ) as SerializableRef<T>;
393
+ serializableMappingCache.set(value, clone);
394
+ serializableMappingCache.set(clone);
395
+
396
+ freezeObjectInDev(value);
397
+ return clone;
398
+ }
399
+
400
+ function cloneNonWorkletFunction<TArgs extends unknown[], TReturn>(
401
+ fun: (...args: TArgs) => TReturn
402
+ ): SerializableRef<(...args: TArgs) => TReturn> {
403
+ const clone = WorkletsModule.createSerializableNonWorkletFunction(
404
+ fun,
405
+ __DEV__ ? fun.name : undefined
406
+ ) as SerializableRef<(...args: TArgs) => TReturn>;
407
+
408
+ if (globalThis.WeakRef) {
409
+ // WeakRef is installed on runtimes only with Hermes microtaskQueue enabled.
410
+ serializableMappingCache.set(fun, new WeakRef(clone));
411
+ }
412
+ serializableMappingCache.set(clone);
413
+ freezeObjectInDev(fun);
414
+
415
+ return clone;
416
+ }
417
+
418
+ function cloneHostObject<T extends object>(value: T): SerializableRef<T> {
419
+ // for host objects we pass the reference to the object as serializable and
420
+ // then recreate new host object wrapping the same instance on the UI thread.
421
+ // there is no point of iterating over keys as we do for regular objects.
422
+ const clone = WorkletsModule.createSerializableHostObject(value);
423
+ serializableMappingCache.set(value, clone);
424
+ serializableMappingCache.set(clone);
425
+
426
+ return clone;
427
+ }
428
+
429
+ function cloneWorklet<TValue extends WorkletFunction>(
430
+ value: TValue,
431
+ shouldPersistRemote: boolean,
432
+ depth: number
433
+ ): SerializableRef<TValue> {
434
+ if (__DEV__) {
435
+ const babelVersion = (value as WorkletFunction).__pluginVersion;
436
+ if (babelVersion !== undefined && babelVersion !== jsVersion) {
437
+ throw new Error(
438
+ `[Worklets] Mismatch between JavaScript code version and Worklets Babel plugin version (${jsVersion} vs. ${babelVersion}).
439
+ See \`https://docs.swmansion.com/react-native-worklets/docs/guides/troubleshooting#mismatch-between-javascript-code-version-and-worklets-babel-plugin-version\` for more details.
440
+ Offending code was: \`${getWorkletCode(value)}\``
441
+ );
442
+ }
443
+ registerWorkletStackDetails(
444
+ value.__workletHash,
445
+ (value as WorkletFunction).__stackDetails!
446
+ );
447
+ }
448
+ if ((value as WorkletFunction).__stackDetails) {
449
+ // `Error` type of value cannot be copied to the UI thread, so we
450
+ // remove it after we handled it in dev mode or delete it to ignore it in production mode.
451
+ // Not removing this would cause an infinite loop in production mode and it just
452
+ // seems more elegant to handle it this way.
453
+ delete (value as WorkletFunction).__stackDetails;
454
+ }
455
+ const clonedProps: Record<string, unknown> = cloneObjectProperties(
456
+ value,
457
+ shouldPersistRemote,
458
+ depth
459
+ );
460
+ // to save on transferring static __initData field of worklet structure
461
+ // we request serializable value to persist its UI counterpart. This means
462
+ // that the __initData field that contains long strings representing the
463
+ // worklet code, source map, and location, will always be
464
+ // serialized/deserialized once.
465
+ clonedProps.__initData = createSerializable(
466
+ value.__initData,
467
+ true,
468
+ depth + 1
469
+ );
470
+
471
+ const clone = WorkletsModule.createSerializableWorklet(
472
+ clonedProps,
473
+ // TODO: Check after refactor if we can remove shouldPersistRemote parameter (imho it's redundant here since worklets are always persistent)
474
+ // retain all worklets
475
+ true
476
+ ) as SerializableRef<TValue>;
477
+ serializableMappingCache.set(value, clone);
478
+ serializableMappingCache.set(clone);
479
+
480
+ freezeObjectInDev(value);
481
+ return clone;
482
+ }
483
+
484
+ /**
485
+ * TurboModuleLike objects are JS objects that have a TurboModule as their
486
+ * prototype.
487
+ */
488
+ function cloneTurboModuleLike<TValue extends object>(
489
+ value: TValue,
490
+ shouldPersistRemote: boolean,
491
+ depth: number
492
+ ): SerializableRef<TValue> {
493
+ const proto = Object.getPrototypeOf(value);
494
+ const clonedProps = cloneObjectProperties(value, shouldPersistRemote, depth);
495
+ const clone = WorkletsModule.createSerializableTurboModuleLike(
496
+ clonedProps,
497
+ proto
498
+ ) as SerializableRef<TValue>;
499
+ return clone;
500
+ }
501
+
502
+ function cloneContextObject<TValue extends object>(
503
+ value: TValue
504
+ ): SerializableRef<TValue> {
505
+ const workletContextObjectFactory = (value as Record<string, unknown>)
506
+ .__workletContextObjectFactory as () => TValue;
507
+ const handle = cloneInitializer({
508
+ __init: () => {
509
+ 'worklet';
510
+ return workletContextObjectFactory();
511
+ },
512
+ });
513
+ serializableMappingCache.set(value, handle);
514
+ return handle as SerializableRef<TValue>;
515
+ }
516
+
517
+ function clonePlainJSObject<TValue extends object>(
518
+ value: TValue,
519
+ shouldPersistRemote: boolean,
520
+ depth: number
521
+ ): SerializableRef<TValue> {
522
+ const clonedProps: Record<string, unknown> = cloneObjectProperties(
523
+ value,
524
+ shouldPersistRemote,
525
+ depth
526
+ );
527
+ const clone = WorkletsModule.createSerializableObject(
528
+ clonedProps,
529
+ shouldPersistRemote,
530
+ value
531
+ ) as SerializableRef<TValue>;
532
+ serializableMappingCache.set(value, clone);
533
+ serializableMappingCache.set(clone);
534
+
535
+ freezeObjectInDev(value);
536
+ return clone;
537
+ }
538
+
539
+ function cloneMap(
540
+ value: Map<unknown, unknown>
541
+ ): SerializableRef<Map<unknown, unknown>> {
542
+ const clonedKeys: unknown[] = [];
543
+ const clonedValues: unknown[] = [];
544
+ for (const [key, element] of value.entries()) {
545
+ clonedKeys.push(createSerializable(key));
546
+ clonedValues.push(createSerializable(element));
547
+ }
548
+ const clone = WorkletsModule.createSerializableMap(clonedKeys, clonedValues);
549
+ serializableMappingCache.set(value, clone);
550
+ serializableMappingCache.set(clone);
551
+
552
+ freezeObjectInDev(value);
553
+ return clone;
554
+ }
555
+
556
+ function cloneSet(value: Set<unknown>): SerializableRef<Set<unknown>> {
557
+ const clonedElements: unknown[] = [];
558
+ for (const element of value) {
559
+ clonedElements.push(createSerializable(element));
560
+ }
561
+ const clone = WorkletsModule.createSerializableSet(clonedElements);
562
+ serializableMappingCache.set(value, clone);
563
+ serializableMappingCache.set(clone);
564
+
565
+ freezeObjectInDev(value);
566
+ return clone;
567
+ }
568
+
569
+ function cloneRegExp(value: RegExp): SerializableRef<RegExp> {
570
+ const clone = WorkletsModule.createSerializableRegExp(
571
+ value.source,
572
+ value.flags
573
+ );
574
+ serializableMappingCache.set(value, clone);
575
+ return clone;
576
+ }
577
+
578
+ function cloneError(value: Error): SerializableRef<Error> {
579
+ const { name, message, stack } = value;
580
+ const clone = WorkletsModule.createSerializableError(name, message, stack);
581
+ serializableMappingCache.set(value, clone);
582
+ return clone;
583
+ }
584
+
585
+ function cloneArrayBuffer(
586
+ arrayBuffer: ArrayBuffer
587
+ ): SerializableRef<ArrayBuffer> {
588
+ const clone = WorkletsModule.createSerializableArrayBuffer(arrayBuffer);
589
+ serializableMappingCache.set(arrayBuffer, clone);
590
+ serializableMappingCache.set(clone);
591
+
592
+ return clone;
593
+ }
594
+
595
+ function cloneArrayBufferView<TValue extends ArrayBufferView>(
596
+ value: TValue
597
+ ): SerializableRef<TValue> {
598
+ const typeName = value.constructor.name;
599
+ if (!VALID_ARRAY_VIEWS_NAMES.includes(typeName)) {
600
+ throw new Error(`[Worklets] Invalid array view name \`${typeName}\`.`);
601
+ }
602
+ const length =
603
+ typeName === 'DataView'
604
+ ? value.byteLength
605
+ : (value as unknown as { length: number }).length;
606
+ const clone = WorkletsModule.createSerializableArrayBufferView<TValue>(
607
+ typeName,
608
+ value.buffer as ArrayBuffer,
609
+ value.byteOffset,
610
+ length
611
+ );
612
+ serializableMappingCache.set(value, clone);
613
+ return clone;
614
+ }
615
+
616
+ function cloneSynchronizable<TValue>(
617
+ value: Synchronizable<TValue>
618
+ ): SerializableRef<TValue> {
619
+ serializableMappingCache.set(value);
620
+ return value;
621
+ }
622
+
623
+ function cloneImport<TValue extends WorkletImport>(
624
+ value: TValue
625
+ ): SerializableRef<TValue> {
626
+ const { source, imported } = value.__bundleData;
627
+ const clone = WorkletsModule.createSerializableImport(source, imported);
628
+
629
+ serializableMappingCache.set(value, clone);
630
+ serializableMappingCache.set(clone);
631
+
632
+ return clone as SerializableRef<TValue>;
633
+ }
634
+
635
+ function cloneCustom<TValue extends object, TPacked = unknown>(
636
+ data: TValue,
637
+ pack: (data: TValue) => TPacked,
638
+ typeId: number
639
+ ): SerializableRef<TValue> {
640
+ const packedData = pack(data);
641
+ const serialized = createSerializable(packedData);
642
+
643
+ return WorkletsModule.createCustomSerializable(
644
+ serialized,
645
+ typeId
646
+ ) as SerializableRef<TValue>;
647
+ }
648
+
649
+ const WORKLET_CODE_THRESHOLD = 255;
650
+
651
+ function getWorkletCode(value: WorkletFunction) {
652
+ const code = value?.__initData?.code;
653
+ if (!code) {
654
+ return 'unknown';
655
+ }
656
+ if (code.length > WORKLET_CODE_THRESHOLD) {
657
+ return `${code.substring(0, WORKLET_CODE_THRESHOLD)}...`;
658
+ }
659
+ return code;
660
+ }
661
+
662
+ /**
663
+ * We freeze
664
+ *
665
+ * - Arrays,
666
+ * - Remote functions,
667
+ * - Plain JS objects,
668
+ *
669
+ * That are transformed to a serializable with a meaningful warning. This should
670
+ * help detect issues when someone modifies data after it's been converted.
671
+ * Meaning that they may be doing a faulty assumption in their code expecting
672
+ * that the updates are going to automatically propagate to the object sent to
673
+ * the UI thread. If the user really wants some objects to be mutable they
674
+ * should use shared values instead.
675
+ */
676
+ function freezeObjectInDev<TValue extends object>(value: TValue) {
677
+ if (!__DEV__ || globalThis.__RUNTIME_KIND !== RuntimeKind.ReactNative) {
678
+ return;
679
+ }
680
+ Object.entries(value).forEach(([key, element]) => {
681
+ const descriptor = Object.getOwnPropertyDescriptor(value, key)!;
682
+ if (!descriptor.configurable) {
683
+ return;
684
+ }
685
+ Object.defineProperty(value, key, {
686
+ get() {
687
+ return element;
688
+ },
689
+ set() {
690
+ logger.warn(
691
+ `Tried to modify key \`${key}\` of an object which has been already passed to a worklet. See
692
+ https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#tried-to-modify-key-of-an-object-which-has-been-converted-to-a-serializable
693
+ for more details.`
694
+ );
695
+ },
696
+ });
697
+ });
698
+ Object.preventExtensions(value);
699
+ }
700
+
701
+ function makeShareableCloneOnUIRecursiveLEGACY<TValue>(
702
+ value: TValue
703
+ ): FlatSerializableRef<TValue> {
704
+ 'worklet';
705
+ // eslint-disable-next-line @typescript-eslint/no-shadow
706
+ function cloneRecursive(value: TValue): FlatSerializableRef<TValue> {
707
+ if (
708
+ (typeof value === 'object' && value !== null) ||
709
+ typeof value === 'function'
710
+ ) {
711
+ if (isHostObject(value)) {
712
+ // We call `_createSerializableClone` to wrap the provided HostObject
713
+ // inside SerializableJSRef.
714
+ return globalThis._createSerializableHostObject(
715
+ value
716
+ ) as FlatSerializableRef<TValue>;
717
+ }
718
+ if ((value as Record<string, unknown>).__remoteFunction) {
719
+ // RemoteFunctions are created by us therefore they are
720
+ // a Serializable out of the box and there is no need to
721
+ // call `_createSerializableClone`.
722
+ return value as FlatSerializableRef<TValue>;
723
+ }
724
+ if (Array.isArray(value)) {
725
+ return globalThis._createSerializableArray(
726
+ value.map(cloneRecursive)
727
+ ) as FlatSerializableRef<TValue>;
728
+ }
729
+ if ((value as Record<string, unknown>).__synchronizableRef) {
730
+ return globalThis._createSerializableSynchronizable(
731
+ value
732
+ ) as FlatSerializableRef<TValue>;
733
+ }
734
+ if ((value as Record<string, unknown>).__serializableRef) {
735
+ return value as FlatSerializableRef<TValue>;
736
+ }
737
+ if (value instanceof Set) {
738
+ const values: unknown[] = [];
739
+ value.forEach((element) => {
740
+ values.push(cloneRecursive(element));
741
+ });
742
+ return globalThis.__workletsModuleProxy.createSerializableSet(
743
+ values
744
+ ) as FlatSerializableRef<TValue>;
745
+ }
746
+ if (value instanceof Error) {
747
+ const { name, message, stack } = value;
748
+ return globalThis.__workletsModuleProxy.createSerializableError(
749
+ name,
750
+ message,
751
+ stack
752
+ ) as FlatSerializableRef<TValue>;
753
+ }
754
+ if (value instanceof RegExp) {
755
+ return globalThis.__workletsModuleProxy.createSerializableRegExp(
756
+ value.source,
757
+ value.flags
758
+ ) as FlatSerializableRef<TValue>;
759
+ }
760
+ if (value instanceof ArrayBuffer) {
761
+ return globalThis.__workletsModuleProxy.createSerializableArrayBuffer(
762
+ value
763
+ ) as FlatSerializableRef<TValue>;
764
+ }
765
+ if (ArrayBuffer.isView(value)) {
766
+ const typeName = value.constructor.name;
767
+ if (!VALID_ARRAY_VIEWS_NAMES.includes(typeName)) {
768
+ throw new Error(
769
+ `[Worklets] Invalid array view name \`${typeName}\`.`
770
+ );
771
+ }
772
+ const length =
773
+ typeName === 'DataView'
774
+ ? value.byteLength
775
+ : (value as unknown as { length: number }).length;
776
+ return globalThis.__workletsModuleProxy.createSerializableArrayBufferView(
777
+ typeName,
778
+ value.buffer as ArrayBuffer,
779
+ value.byteOffset,
780
+ length
781
+ ) as FlatSerializableRef<TValue>;
782
+ }
783
+ if (value instanceof Map) {
784
+ const keys: unknown[] = [];
785
+ const values: unknown[] = [];
786
+ value.forEach((element, key) => {
787
+ keys.push(cloneRecursive(key));
788
+ values.push(cloneRecursive(element));
789
+ });
790
+ return globalThis.__workletsModuleProxy.createSerializableMap(
791
+ keys,
792
+ values
793
+ ) as FlatSerializableRef<TValue>;
794
+ }
795
+ if (Object.getPrototypeOf(value) !== Object.prototype) {
796
+ const length = globalThis.__customSerializationRegistry.length;
797
+ for (let i = 0; i < length; i++) {
798
+ const { determine, pack } =
799
+ globalThis.__customSerializationRegistry[i];
800
+ if (determine(value)) {
801
+ const packedData = pack(value);
802
+ return globalThis.__workletsModuleProxy.createCustomSerializable(
803
+ cloneRecursive(packedData as TValue) as SerializableRef<object>,
804
+ i
805
+ ) as FlatSerializableRef<TValue>;
806
+ }
807
+ }
808
+ }
809
+ if (__DEV__ && value instanceof Promise) {
810
+ throw new Error(
811
+ '[Worklets] Promises cannot be converted to serializable.'
812
+ );
813
+ }
814
+ const toAdapt: Record<string, FlatSerializableRef<TValue>> = {};
815
+ for (const [key, element] of Object.entries(value)) {
816
+ toAdapt[key] = cloneRecursive(element);
817
+ }
818
+ return globalThis.__workletsModuleProxy.createSerializableLEGACY(
819
+ toAdapt,
820
+ value
821
+ ) as FlatSerializableRef<TValue>;
822
+ }
823
+
824
+ if (typeof value === 'string') {
825
+ return globalThis._createSerializableString(value);
826
+ }
827
+
828
+ if (typeof value === 'number') {
829
+ return globalThis._createSerializableNumber(value);
830
+ }
831
+
832
+ if (typeof value === 'boolean') {
833
+ return globalThis._createSerializableBoolean(value);
834
+ }
835
+
836
+ if (typeof value === 'bigint') {
837
+ return globalThis._createSerializableBigInt(value);
838
+ }
839
+
840
+ if (typeof value === 'symbol') {
841
+ // TODO: add native support
842
+ return globalThis._createSerializableString(
843
+ String(value)
844
+ ) as FlatSerializableRef<TValue>;
845
+ }
846
+
847
+ if (value === undefined) {
848
+ return globalThis._createSerializableUndefined();
849
+ }
850
+
851
+ if (value === null) {
852
+ return globalThis._createSerializableNull();
853
+ }
854
+
855
+ return globalThis.__workletsModuleProxy.createSerializableLEGACY(
856
+ value,
857
+ undefined
858
+ ) as FlatSerializableRef<TValue>;
859
+ }
860
+ return cloneRecursive(value);
861
+ }
862
+
863
+ /** @deprecated This function is no longer supported. */
864
+ export const makeShareableCloneOnUIRecursive = (
865
+ isBundleModeEnabled()
866
+ ? createSerializable
867
+ : makeShareableCloneOnUIRecursiveLEGACY
868
+ ) as typeof makeShareableCloneOnUIRecursiveLEGACY;
869
+
870
+ /**
871
+ * This function creates a value on UI with persistent state - changes to it on
872
+ * the UI thread will be seen by all worklets. Use it when you want to create a
873
+ * value that is read and written only on the UI thread.
874
+ *
875
+ * @deprecated This function is no longer supported.
876
+ */
877
+ export function makeShareable<TValue extends object>(value: TValue): TValue {
878
+ if (serializableMappingCache.get(value)) {
879
+ return value;
880
+ }
881
+ const handle = createSerializable({
882
+ __init: () => {
883
+ 'worklet';
884
+ return value;
885
+ },
886
+ });
887
+ serializableMappingCache.set(value, handle);
888
+ return value;
889
+ }