react-native-worklets 0.0.1-alpha → 0.1.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 (246) hide show
  1. package/Common/cpp/worklets/AnimationFrameQueue/AnimationFrameBatchinator.cpp +71 -0
  2. package/Common/cpp/worklets/AnimationFrameQueue/AnimationFrameBatchinator.h +38 -0
  3. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.cpp +131 -0
  4. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.h +82 -0
  5. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxySpec.cpp +72 -0
  6. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxySpec.h +44 -0
  7. package/Common/cpp/worklets/Registries/EventHandlerRegistry.cpp +94 -0
  8. package/Common/cpp/worklets/Registries/EventHandlerRegistry.h +49 -0
  9. package/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.cpp +8 -0
  10. package/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.h +39 -0
  11. package/Common/cpp/worklets/SharedItems/Shareables.cpp +326 -0
  12. package/Common/cpp/worklets/SharedItems/Shareables.h +345 -0
  13. package/Common/cpp/worklets/Tools/AsyncQueue.cpp +52 -0
  14. package/Common/cpp/worklets/Tools/AsyncQueue.h +35 -0
  15. package/Common/cpp/worklets/Tools/Defs.h +10 -0
  16. package/Common/cpp/worklets/Tools/JSISerializer.cpp +342 -0
  17. package/Common/cpp/worklets/Tools/JSISerializer.h +47 -0
  18. package/Common/cpp/worklets/Tools/JSLogger.cpp +16 -0
  19. package/Common/cpp/worklets/Tools/JSLogger.h +20 -0
  20. package/Common/cpp/worklets/Tools/JSScheduler.cpp +10 -0
  21. package/Common/cpp/worklets/Tools/JSScheduler.h +29 -0
  22. package/Common/cpp/worklets/Tools/PlatformLogger.h +16 -0
  23. package/Common/cpp/worklets/Tools/SingleInstanceChecker.h +72 -0
  24. package/Common/cpp/worklets/Tools/ThreadSafeQueue.h +49 -0
  25. package/Common/cpp/worklets/Tools/UIScheduler.cpp +19 -0
  26. package/Common/cpp/worklets/Tools/UIScheduler.h +22 -0
  27. package/Common/cpp/worklets/Tools/WorkletEventHandler.cpp +29 -0
  28. package/Common/cpp/worklets/Tools/WorkletEventHandler.h +41 -0
  29. package/Common/cpp/worklets/Tools/WorkletsJSIUtils.cpp +26 -0
  30. package/Common/cpp/worklets/Tools/WorkletsJSIUtils.h +199 -0
  31. package/Common/cpp/worklets/WorkletRuntime/RNRuntimeWorkletDecorator.cpp +20 -0
  32. package/Common/cpp/worklets/WorkletRuntime/RNRuntimeWorkletDecorator.h +19 -0
  33. package/Common/cpp/worklets/WorkletRuntime/RuntimeInitialization.md +191 -0
  34. package/Common/cpp/worklets/WorkletRuntime/UIRuntimeDecorator.cpp +19 -0
  35. package/Common/cpp/worklets/WorkletRuntime/UIRuntimeDecorator.h +16 -0
  36. package/Common/cpp/worklets/WorkletRuntime/WorkletHermesRuntime.cpp +108 -0
  37. package/Common/cpp/worklets/WorkletRuntime/WorkletHermesRuntime.h +127 -0
  38. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp +183 -0
  39. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h +90 -0
  40. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeCollector.h +36 -0
  41. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.cpp +179 -0
  42. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.h +22 -0
  43. package/LICENSE +20 -0
  44. package/README.md +27 -0
  45. package/RNWorklets.podspec +70 -0
  46. package/android/CMakeLists.txt +56 -0
  47. package/android/build.gradle +313 -0
  48. package/android/gradle.properties +5 -0
  49. package/android/proguard-rules.pro +3 -0
  50. package/android/spotless.gradle +9 -0
  51. package/android/src/main/AndroidManifest.xml +2 -0
  52. package/android/src/main/cpp/worklets/CMakeLists.txt +85 -0
  53. package/android/src/main/cpp/worklets/android/AndroidUIScheduler.cpp +63 -0
  54. package/android/src/main/cpp/worklets/android/AndroidUIScheduler.h +41 -0
  55. package/android/src/main/cpp/worklets/android/AnimationFrameCallback.h +32 -0
  56. package/android/src/main/cpp/worklets/android/PlatformLogger.cpp +29 -0
  57. package/android/src/main/cpp/worklets/android/WorkletsModule.cpp +83 -0
  58. package/android/src/main/cpp/worklets/android/WorkletsModule.h +63 -0
  59. package/android/src/main/cpp/worklets/android/WorkletsOnLoad.cpp +13 -0
  60. package/android/src/main/java/com/swmansion/worklets/AndroidUIScheduler.java +60 -0
  61. package/android/src/main/java/com/swmansion/worklets/AnimationFrameQueue/AnimationFrameCallback.java +20 -0
  62. package/android/src/main/java/com/swmansion/worklets/AnimationFrameQueue/AnimationFrameQueue.java +113 -0
  63. package/android/src/main/java/com/swmansion/worklets/JSCallInvokerResolver.java +27 -0
  64. package/android/src/main/java/com/swmansion/worklets/WorkletsMessageQueueThread.java +16 -0
  65. package/android/src/main/java/com/swmansion/worklets/WorkletsMessageQueueThreadBase.java +72 -0
  66. package/android/src/main/java/com/swmansion/worklets/WorkletsModule.java +106 -0
  67. package/android/src/main/java/com/swmansion/worklets/WorkletsPackage.java +49 -0
  68. package/android/src/paper/com/swmansion/worklets/NativeWorkletsModuleSpec.java +26 -0
  69. package/apple/worklets/apple/AnimationFrameQueue.h +15 -0
  70. package/apple/worklets/apple/AnimationFrameQueue.mm +81 -0
  71. package/apple/worklets/apple/AssertJavaScriptQueue.h +14 -0
  72. package/apple/worklets/apple/AssertTurboModuleManagerQueue.h +16 -0
  73. package/apple/worklets/apple/IOSUIScheduler.h +14 -0
  74. package/apple/worklets/apple/IOSUIScheduler.mm +24 -0
  75. package/apple/worklets/apple/PlatformLogger.mm +31 -0
  76. package/apple/worklets/apple/SlowAnimations.h +8 -0
  77. package/apple/worklets/apple/SlowAnimations.mm +47 -0
  78. package/apple/worklets/apple/WorkletsDisplayLink.h +21 -0
  79. package/apple/worklets/apple/WorkletsMessageThread.h +16 -0
  80. package/apple/worklets/apple/WorkletsMessageThread.mm +32 -0
  81. package/apple/worklets/apple/WorkletsModule.h +10 -0
  82. package/apple/worklets/apple/WorkletsModule.mm +85 -0
  83. package/lib/module/PlatformChecker.js +35 -0
  84. package/lib/module/PlatformChecker.js.map +1 -0
  85. package/lib/module/WorkletsError.js +13 -0
  86. package/lib/module/WorkletsError.js.map +1 -0
  87. package/lib/module/WorkletsModule/JSWorklets.js +36 -0
  88. package/lib/module/WorkletsModule/JSWorklets.js.map +1 -0
  89. package/lib/module/WorkletsModule/NativeWorklets.js +39 -0
  90. package/lib/module/WorkletsModule/NativeWorklets.js.map +1 -0
  91. package/lib/module/WorkletsModule/index.js +4 -0
  92. package/lib/module/WorkletsModule/index.js.map +1 -0
  93. package/lib/module/WorkletsModule/workletsModuleInstance.js +7 -0
  94. package/lib/module/WorkletsModule/workletsModuleInstance.js.map +1 -0
  95. package/lib/module/WorkletsModule/workletsModuleInstance.web.js +5 -0
  96. package/lib/module/WorkletsModule/workletsModuleInstance.web.js.map +1 -0
  97. package/lib/module/WorkletsModule/workletsModuleProxy.js +4 -0
  98. package/lib/module/WorkletsModule/workletsModuleProxy.js.map +1 -0
  99. package/lib/module/animationFrameQueue/mockedRequestAnimationFrame.js +10 -0
  100. package/lib/module/animationFrameQueue/mockedRequestAnimationFrame.js.map +1 -0
  101. package/lib/module/animationFrameQueue/requestAnimationFrame.js +36 -0
  102. package/lib/module/animationFrameQueue/requestAnimationFrame.js.map +1 -0
  103. package/lib/module/errors.js +78 -0
  104. package/lib/module/errors.js.map +1 -0
  105. package/lib/module/index.js +17 -0
  106. package/lib/module/index.js.map +1 -0
  107. package/lib/module/initializers.js +158 -0
  108. package/lib/module/initializers.js.map +1 -0
  109. package/lib/module/logger/LogBox.js +15 -0
  110. package/lib/module/logger/LogBox.js.map +1 -0
  111. package/lib/module/logger/index.js +5 -0
  112. package/lib/module/logger/index.js.map +1 -0
  113. package/lib/module/logger/logger.js +137 -0
  114. package/lib/module/logger/logger.js.map +1 -0
  115. package/lib/module/privateGlobals.d.js +8 -0
  116. package/lib/module/privateGlobals.d.js.map +1 -0
  117. package/lib/module/runtimes.js +63 -0
  118. package/lib/module/runtimes.js.map +1 -0
  119. package/lib/module/shareableMappingCache.js +39 -0
  120. package/lib/module/shareableMappingCache.js.map +1 -0
  121. package/lib/module/shareables.js +417 -0
  122. package/lib/module/shareables.js.map +1 -0
  123. package/lib/module/specs/NativeWorkletsModule.js +5 -0
  124. package/lib/module/specs/NativeWorkletsModule.js.map +1 -0
  125. package/lib/module/specs/index.js +5 -0
  126. package/lib/module/specs/index.js.map +1 -0
  127. package/lib/module/threads.js +204 -0
  128. package/lib/module/threads.js.map +1 -0
  129. package/lib/module/valueUnpacker.js +83 -0
  130. package/lib/module/valueUnpacker.js.map +1 -0
  131. package/lib/module/workletFunction.js +37 -0
  132. package/lib/module/workletFunction.js.map +1 -0
  133. package/lib/module/workletTypes.js +12 -0
  134. package/lib/module/workletTypes.js.map +1 -0
  135. package/lib/typescript/PlatformChecker.d.ts +7 -0
  136. package/lib/typescript/PlatformChecker.d.ts.map +1 -0
  137. package/lib/typescript/WorkletsError.d.ts +3 -0
  138. package/lib/typescript/WorkletsError.d.ts.map +1 -0
  139. package/lib/typescript/WorkletsModule/JSWorklets.d.ts +3 -0
  140. package/lib/typescript/WorkletsModule/JSWorklets.d.ts.map +1 -0
  141. package/lib/typescript/WorkletsModule/NativeWorklets.d.ts +5 -0
  142. package/lib/typescript/WorkletsModule/NativeWorklets.d.ts.map +1 -0
  143. package/lib/typescript/WorkletsModule/index.d.ts +3 -0
  144. package/lib/typescript/WorkletsModule/index.d.ts.map +1 -0
  145. package/lib/typescript/WorkletsModule/workletsModuleInstance.d.ts +2 -0
  146. package/lib/typescript/WorkletsModule/workletsModuleInstance.d.ts.map +1 -0
  147. package/lib/typescript/WorkletsModule/workletsModuleInstance.web.d.ts +2 -0
  148. package/lib/typescript/WorkletsModule/workletsModuleInstance.web.d.ts.map +1 -0
  149. package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts +12 -0
  150. package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts.map +1 -0
  151. package/lib/typescript/animationFrameQueue/mockedRequestAnimationFrame.d.ts +6 -0
  152. package/lib/typescript/animationFrameQueue/mockedRequestAnimationFrame.d.ts.map +1 -0
  153. package/lib/typescript/animationFrameQueue/requestAnimationFrame.d.ts +2 -0
  154. package/lib/typescript/animationFrameQueue/requestAnimationFrame.d.ts.map +1 -0
  155. package/lib/typescript/errors.d.ts +19 -0
  156. package/lib/typescript/errors.d.ts.map +1 -0
  157. package/lib/typescript/index.d.ts +13 -0
  158. package/lib/typescript/index.d.ts.map +1 -0
  159. package/lib/typescript/initializers.d.ts +6 -0
  160. package/lib/typescript/initializers.d.ts.map +1 -0
  161. package/lib/typescript/logger/LogBox.d.ts +32 -0
  162. package/lib/typescript/logger/LogBox.d.ts.map +1 -0
  163. package/lib/typescript/logger/index.d.ts +3 -0
  164. package/lib/typescript/logger/index.d.ts.map +1 -0
  165. package/lib/typescript/logger/logger.d.ts +52 -0
  166. package/lib/typescript/logger/logger.d.ts.map +1 -0
  167. package/lib/typescript/runtimes.d.ts +16 -0
  168. package/lib/typescript/runtimes.d.ts.map +1 -0
  169. package/lib/typescript/shareableMappingCache.d.ts +16 -0
  170. package/lib/typescript/shareableMappingCache.d.ts.map +1 -0
  171. package/lib/typescript/shareables.d.ts +15 -0
  172. package/lib/typescript/shareables.d.ts.map +1 -0
  173. package/lib/typescript/specs/NativeWorkletsModule.d.ts +7 -0
  174. package/lib/typescript/specs/NativeWorkletsModule.d.ts.map +1 -0
  175. package/lib/typescript/specs/index.d.ts +3 -0
  176. package/lib/typescript/specs/index.d.ts.map +1 -0
  177. package/lib/typescript/threads.d.ts +49 -0
  178. package/lib/typescript/threads.d.ts.map +1 -0
  179. package/lib/typescript/valueUnpacker.d.ts +2 -0
  180. package/lib/typescript/valueUnpacker.d.ts.map +1 -0
  181. package/lib/typescript/workletFunction.d.ts +27 -0
  182. package/lib/typescript/workletFunction.d.ts.map +1 -0
  183. package/lib/typescript/workletTypes.d.ts +49 -0
  184. package/lib/typescript/workletTypes.d.ts.map +1 -0
  185. package/package.json +106 -8
  186. package/plugin/index.js +3 -0
  187. package/scripts/worklets_utils.rb +53 -0
  188. package/src/PlatformChecker.ts +43 -0
  189. package/src/WorkletsError.ts +13 -0
  190. package/src/WorkletsModule/JSWorklets.ts +57 -0
  191. package/src/WorkletsModule/NativeWorklets.ts +68 -0
  192. package/src/WorkletsModule/index.ts +7 -0
  193. package/src/WorkletsModule/workletsModuleInstance.ts +9 -0
  194. package/src/WorkletsModule/workletsModuleInstance.web.ts +5 -0
  195. package/src/WorkletsModule/workletsModuleProxy.ts +30 -0
  196. package/src/animationFrameQueue/mockedRequestAnimationFrame.ts +11 -0
  197. package/src/animationFrameQueue/requestAnimationFrame.ts +41 -0
  198. package/src/errors.ts +103 -0
  199. package/src/index.ts +42 -0
  200. package/src/initializers.ts +175 -0
  201. package/src/logger/LogBox.ts +55 -0
  202. package/src/logger/index.ts +3 -0
  203. package/src/logger/logger.ts +155 -0
  204. package/src/privateGlobals.d.ts +41 -0
  205. package/src/runtimes.ts +92 -0
  206. package/src/shareableMappingCache.ts +44 -0
  207. package/src/shareables.ts +577 -0
  208. package/src/specs/NativeWorkletsModule.ts +9 -0
  209. package/src/specs/index.ts +5 -0
  210. package/src/threads.ts +275 -0
  211. package/src/valueUnpacker.ts +110 -0
  212. package/src/workletFunction.ts +47 -0
  213. package/src/workletTypes.ts +76 -0
  214. package/Animated.js +0 -13
  215. package/AnimatedEvent.js +0 -167
  216. package/AnimatedImplementation.js +0 -666
  217. package/CoreAnimated.js +0 -43
  218. package/Easing.js +0 -236
  219. package/NativeAnimatedHelper.js +0 -226
  220. package/SpringConfig.js +0 -79
  221. package/animations/Animation.js +0 -36
  222. package/animations/DecayAnimation.js +0 -70
  223. package/animations/SpringAnimation.js +0 -125
  224. package/animations/TimingAnimation.js +0 -70
  225. package/bezier.js +0 -128
  226. package/createAnimatedComponent.js +0 -188
  227. package/nodes/AnimatedBlock.js +0 -19
  228. package/nodes/AnimatedClock.js +0 -76
  229. package/nodes/AnimatedCond.js +0 -23
  230. package/nodes/AnimatedDetach.js +0 -15
  231. package/nodes/AnimatedInterpolation.js +0 -338
  232. package/nodes/AnimatedNode.js +0 -97
  233. package/nodes/AnimatedOnChange.js +0 -28
  234. package/nodes/AnimatedOp.js +0 -17
  235. package/nodes/AnimatedProps.js +0 -154
  236. package/nodes/AnimatedSet.js +0 -19
  237. package/nodes/AnimatedStartClock.js +0 -21
  238. package/nodes/AnimatedStopClock.js +0 -21
  239. package/nodes/AnimatedStyle.js +0 -89
  240. package/nodes/AnimatedTracking.js +0 -36
  241. package/nodes/AnimatedTransform.js +0 -93
  242. package/nodes/AnimatedValue.js +0 -271
  243. package/nodes/AnimatedWithInput.js +0 -21
  244. package/nodes/SpringNode.js +0 -106
  245. package/nodes/TimingStep.js +0 -44
  246. package/utils.js +0 -28
@@ -0,0 +1,137 @@
1
+ 'use strict';
2
+
3
+ import { addLogBoxLog } from "./LogBox.js";
4
+ const DOCS_URL = 'https://docs.swmansion.com/react-native-reanimated/docs/debugging/logger-configuration';
5
+ const DOCS_REFERENCE = `If you don't want to see this message, you can disable the \`strict\` mode. Refer to:\n${DOCS_URL} for more details.`;
6
+ export let LogLevel = /*#__PURE__*/function (LogLevel) {
7
+ LogLevel[LogLevel["warn"] = 1] = "warn";
8
+ LogLevel[LogLevel["error"] = 2] = "error";
9
+ return LogLevel;
10
+ }({});
11
+ function logToConsole(data) {
12
+ 'worklet';
13
+
14
+ switch (data.level) {
15
+ case 'warn':
16
+ console.warn(data.message.content);
17
+ break;
18
+ case 'error':
19
+ case 'fatal':
20
+ case 'syntax':
21
+ console.error(data.message.content);
22
+ break;
23
+ }
24
+ }
25
+ export const DEFAULT_LOGGER_CONFIG = {
26
+ logFunction: logToConsole,
27
+ level: LogLevel.warn,
28
+ strict: true
29
+ };
30
+ function formatMessage(message) {
31
+ 'worklet';
32
+
33
+ return `[Worklets] ${message}`;
34
+ }
35
+ function createLog(level, message) {
36
+ 'worklet';
37
+
38
+ const formattedMessage = formatMessage(message);
39
+ return {
40
+ level,
41
+ message: {
42
+ content: formattedMessage,
43
+ substitutions: []
44
+ },
45
+ category: formattedMessage,
46
+ componentStack: [],
47
+ componentStackType: null,
48
+ // eslint-disable-next-line reanimated/use-worklets-error
49
+ stack: new Error().stack
50
+ };
51
+ }
52
+
53
+ /**
54
+ * Function that logs to LogBox and console. Used to replace the default console
55
+ * logging with logging to LogBox on the UI thread when runOnJS is available.
56
+ *
57
+ * @param data - The details of the log.
58
+ */
59
+ export function logToLogBoxAndConsole(data) {
60
+ addLogBoxLog(data);
61
+ logToConsole(data);
62
+ }
63
+
64
+ /**
65
+ * Registers the logger configuration. use it only for Worklet runtimes.
66
+ *
67
+ * @param config - The config to register.
68
+ */
69
+ export function registerLoggerConfig(config) {
70
+ 'worklet';
71
+
72
+ global.__workletsLoggerConfig = config;
73
+ }
74
+
75
+ /**
76
+ * Replaces the default log function with a custom implementation.
77
+ *
78
+ * @param logFunction - The custom log function.
79
+ */
80
+ export function replaceLoggerImplementation(logFunction) {
81
+ 'worklet';
82
+
83
+ registerLoggerConfig({
84
+ ...global.__workletsLoggerConfig,
85
+ logFunction
86
+ });
87
+ }
88
+
89
+ /**
90
+ * Updates logger configuration.
91
+ *
92
+ * @param options - The new logger configuration to apply.
93
+ *
94
+ * - Level: The minimum log level to display.
95
+ * - Strict: Whether to log warnings and errors that are not strict. Defaults to
96
+ * false.
97
+ */
98
+ export function updateLoggerConfig(options) {
99
+ 'worklet';
100
+
101
+ registerLoggerConfig({
102
+ ...global.__workletsLoggerConfig,
103
+ // Don't reuse previous level and strict values from the global config
104
+ level: options?.level ?? DEFAULT_LOGGER_CONFIG.level,
105
+ strict: options?.strict ?? DEFAULT_LOGGER_CONFIG.strict
106
+ });
107
+ }
108
+ function handleLog(level, message, options) {
109
+ 'worklet';
110
+
111
+ const config = global.__workletsLoggerConfig;
112
+ if (
113
+ // Don't log if the log is marked as strict-only and the config doesn't
114
+ // enable strict logging
115
+ options.strict && !config.strict ||
116
+ // Don't log if the log level is below the minimum configured level
117
+ LogLevel[level] < config.level) {
118
+ return;
119
+ }
120
+ if (options.strict) {
121
+ message += `\n\n${DOCS_REFERENCE}`;
122
+ }
123
+ config.logFunction(createLog(level, message));
124
+ }
125
+ export const logger = {
126
+ warn(message, options = {}) {
127
+ 'worklet';
128
+
129
+ handleLog('warn', message, options);
130
+ },
131
+ error(message, options = {}) {
132
+ 'worklet';
133
+
134
+ handleLog('error', message, options);
135
+ }
136
+ };
137
+ //# sourceMappingURL=logger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["addLogBoxLog","DOCS_URL","DOCS_REFERENCE","LogLevel","logToConsole","data","level","console","warn","message","content","error","DEFAULT_LOGGER_CONFIG","logFunction","strict","formatMessage","createLog","formattedMessage","substitutions","category","componentStack","componentStackType","stack","Error","logToLogBoxAndConsole","registerLoggerConfig","config","global","__workletsLoggerConfig","replaceLoggerImplementation","updateLoggerConfig","options","handleLog","logger"],"sourceRoot":"../../../src","sources":["logger/logger.ts"],"mappings":"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","ignoreList":[]}
@@ -0,0 +1,8 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ /* eslint-disable no-var */
3
+ 'use strict';
4
+
5
+ // This file works by accident - currently Builder Bob doesn't move `.d.ts` files to output types.
6
+ // If it ever breaks, we should address it so we'd not pollute the user's global namespace.
7
+ export {};
8
+ //# sourceMappingURL=privateGlobals.d.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sourceRoot":"../../src","sources":["privateGlobals.d.ts"],"mappings":"AAAA;AACA;AACA,YAAY;;AAEZ;AACA;AAAA","ignoreList":[]}
@@ -0,0 +1,63 @@
1
+ 'use strict';
2
+
3
+ import { setupCallGuard, setupConsole } from "./initializers.js";
4
+ import { registerLoggerConfig } from "./logger/index.js";
5
+ import { shouldBeUseWeb } from "./PlatformChecker.js";
6
+ import { makeShareableCloneOnUIRecursive, makeShareableCloneRecursive } from "./shareables.js";
7
+ import { isWorkletFunction } from "./workletFunction.js";
8
+ import { registerWorkletsError, WorkletsError } from "./WorkletsError.js";
9
+ import { WorkletsModule } from "./WorkletsModule/index.js";
10
+ const SHOULD_BE_USE_WEB = shouldBeUseWeb();
11
+
12
+ /**
13
+ * Lets you create a new JS runtime which can be used to run worklets possibly
14
+ * on different threads than JS or UI thread.
15
+ *
16
+ * @param name - A name used to identify the runtime which will appear in
17
+ * devices list in Chrome DevTools.
18
+ * @param initializer - An optional worklet that will be run synchronously on
19
+ * the same thread immediately after the runtime is created.
20
+ * @returns WorkletRuntime which is a
21
+ * `jsi::HostObject<worklets::WorkletRuntime>` - {@link WorkletRuntime}
22
+ * @see https://docs.swmansion.com/react-native-reanimated/docs/threading/createWorkletRuntime
23
+ */
24
+ // @ts-expect-error Check `runOnUI` overload.
25
+
26
+ export function createWorkletRuntime(name, initializer) {
27
+ // Assign to a different variable as __workletsLoggerConfig is not a captured
28
+ // identifier in the Worklet runtime.
29
+ const config = __workletsLoggerConfig;
30
+ return WorkletsModule.createWorkletRuntime(name, makeShareableCloneRecursive(() => {
31
+ 'worklet';
32
+
33
+ registerWorkletsError();
34
+ registerLoggerConfig(config);
35
+ setupCallGuard();
36
+ setupConsole();
37
+ initializer?.();
38
+ }));
39
+ }
40
+
41
+ // @ts-expect-error Check `runOnUI` overload.
42
+
43
+ /** Schedule a worklet to execute on the background queue. */
44
+ export function runOnRuntime(workletRuntime, worklet) {
45
+ 'worklet';
46
+
47
+ if (__DEV__ && !SHOULD_BE_USE_WEB && !isWorkletFunction(worklet)) {
48
+ throw new WorkletsError('The function passed to `runOnRuntime` is not a worklet.' + (_WORKLET ? ' Please make sure that `processNestedWorklets` option in Reanimated Babel plugin is enabled.' : ''));
49
+ }
50
+ if (_WORKLET) {
51
+ return (...args) => global._scheduleOnRuntime(workletRuntime, makeShareableCloneOnUIRecursive(() => {
52
+ 'worklet';
53
+
54
+ worklet(...args);
55
+ }));
56
+ }
57
+ return (...args) => WorkletsModule.scheduleOnRuntime(workletRuntime, makeShareableCloneRecursive(() => {
58
+ 'worklet';
59
+
60
+ worklet(...args);
61
+ }));
62
+ }
63
+ //# sourceMappingURL=runtimes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["setupCallGuard","setupConsole","registerLoggerConfig","shouldBeUseWeb","makeShareableCloneOnUIRecursive","makeShareableCloneRecursive","isWorkletFunction","registerWorkletsError","WorkletsError","WorkletsModule","SHOULD_BE_USE_WEB","createWorkletRuntime","name","initializer","config","__workletsLoggerConfig","runOnRuntime","workletRuntime","worklet","__DEV__","_WORKLET","args","global","_scheduleOnRuntime","scheduleOnRuntime"],"sourceRoot":"../../src","sources":["runtimes.ts"],"mappings":"AAAA,YAAY;;AAEZ,SAASA,cAAc,EAAEC,YAAY,QAAQ,mBAAgB;AAC7D,SAASC,oBAAoB,QAAQ,mBAAU;AAC/C,SAASC,cAAc,QAAQ,sBAAmB;AAClD,SACEC,+BAA+B,EAC/BC,2BAA2B,QACtB,iBAAc;AACrB,SAASC,iBAAiB,QAAQ,sBAAmB;AACrD,SAASC,qBAAqB,EAAEC,aAAa,QAAQ,oBAAiB;AACtE,SAASC,cAAc,QAAQ,2BAAkB;AAGjD,MAAMC,iBAAiB,GAAGP,cAAc,CAAC,CAAC;;AAE1C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAMA,OAAO,SAASQ,oBAAoBA,CAClCC,IAAY,EACZC,WAAuC,EACvB;EAChB;EACA;EACA,MAAMC,MAAM,GAAGC,sBAAsB;EACrC,OAAON,cAAc,CAACE,oBAAoB,CACxCC,IAAI,EACJP,2BAA2B,CAAC,MAAM;IAChC,SAAS;;IACTE,qBAAqB,CAAC,CAAC;IACvBL,oBAAoB,CAACY,MAAM,CAAC;IAC5Bd,cAAc,CAAC,CAAC;IAChBC,YAAY,CAAC,CAAC;IACdY,WAAW,GAAG,CAAC;EACjB,CAAC,CACH,CAAC;AACH;;AAEA;;AAKA;AACA,OAAO,SAASG,YAAYA,CAC1BC,cAA8B,EAC9BC,OAA2C,EAClB;EACzB,SAAS;;EACT,IAAIC,OAAO,IAAI,CAACT,iBAAiB,IAAI,CAACJ,iBAAiB,CAACY,OAAO,CAAC,EAAE;IAChE,MAAM,IAAIV,aAAa,CACrB,yDAAyD,IACtDY,QAAQ,GACL,8FAA8F,GAC9F,EAAE,CACV,CAAC;EACH;EACA,IAAIA,QAAQ,EAAE;IACZ,OAAO,CAAC,GAAGC,IAAI,KACbC,MAAM,CAACC,kBAAkB,CACvBN,cAAc,EACdb,+BAA+B,CAAC,MAAM;MACpC,SAAS;;MACTc,OAAO,CAAC,GAAGG,IAAI,CAAC;IAClB,CAAC,CACH,CAAC;EACL;EACA,OAAO,CAAC,GAAGA,IAAI,KACbZ,cAAc,CAACe,iBAAiB,CAC9BP,cAAc,EACdZ,2BAA2B,CAAC,MAAM;IAChC,SAAS;;IACTa,OAAO,CAAC,GAAGG,IAAI,CAAC;EAClB,CAAC,CACH,CAAC;AACL","ignoreList":[]}
@@ -0,0 +1,39 @@
1
+ 'use strict';
2
+
3
+ import { shouldBeUseWeb } from "./PlatformChecker.js";
4
+ const SHOULD_BE_USE_WEB = shouldBeUseWeb();
5
+
6
+ /**
7
+ * This symbol is used to represent a mapping from the value to itself.
8
+ *
9
+ * It's used to prevent converting a shareable that's already converted - for
10
+ * example a Shared Value that's in worklet's closure.
11
+ */
12
+ export const shareableMappingFlag = Symbol('shareable flag');
13
+
14
+ /*
15
+ During a fast refresh, React holds the same instance of a Mutable
16
+ (that's guaranteed by `useRef`) but `shareableCache` gets regenerated and thus
17
+ becoming empty. This happens when editing the file that contains the definition of this cache.
18
+
19
+ Because of it, `makeShareableCloneRecursive` can't find given mapping
20
+ in `shareableCache` for the Mutable and tries to clone it as if it was a regular JS object.
21
+ During cloning we use `Object.entries` to iterate over the keys which throws an error on accessing `_value`.
22
+ For convenience we moved this cache to a separate file so it doesn't scare us with red squiggles.
23
+ */
24
+
25
+ const cache = SHOULD_BE_USE_WEB ? null : new WeakMap();
26
+ export const shareableMappingCache = SHOULD_BE_USE_WEB ? {
27
+ set() {
28
+ // NOOP
29
+ },
30
+ get() {
31
+ return null;
32
+ }
33
+ } : {
34
+ set(shareable, shareableRef) {
35
+ cache.set(shareable, shareableRef || shareableMappingFlag);
36
+ },
37
+ get: cache.get.bind(cache)
38
+ };
39
+ //# sourceMappingURL=shareableMappingCache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["shouldBeUseWeb","SHOULD_BE_USE_WEB","shareableMappingFlag","Symbol","cache","WeakMap","shareableMappingCache","set","get","shareable","shareableRef","bind"],"sourceRoot":"../../src","sources":["shareableMappingCache.ts"],"mappings":"AAAA,YAAY;;AACZ,SAASA,cAAc,QAAQ,sBAAmB;AAGlD,MAAMC,iBAAiB,GAAGD,cAAc,CAAC,CAAC;;AAE1C;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAME,oBAAoB,GAAGC,MAAM,CAAC,gBAAgB,CAAC;;AAE5D;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,MAAMC,KAAK,GAAGH,iBAAiB,GAC3B,IAAI,GACJ,IAAII,OAAO,CAAgC,CAAC;AAEhD,OAAO,MAAMC,qBAAqB,GAAGL,iBAAiB,GAClD;EACEM,GAAGA,CAAA,EAAG;IACJ;EAAA,CACD;EACDC,GAAGA,CAAA,EAAG;IACJ,OAAO,IAAI;EACb;AACF,CAAC,GACD;EACED,GAAGA,CAACE,SAAiB,EAAEC,YAA2B,EAAQ;IACxDN,KAAK,CAAEG,GAAG,CAACE,SAAS,EAAEC,YAAY,IAAIR,oBAAoB,CAAC;EAC7D,CAAC;EACDM,GAAG,EAAEJ,KAAK,CAAEI,GAAG,CAACG,IAAI,CAACP,KAAK;AAC5B,CAAC","ignoreList":[]}
@@ -0,0 +1,417 @@
1
+ 'use strict';
2
+
3
+ import { registerWorkletStackDetails } from "./errors.js";
4
+ import { logger } from "./logger/index.js";
5
+ import { shouldBeUseWeb } from "./PlatformChecker.js";
6
+ import { shareableMappingCache, shareableMappingFlag } from "./shareableMappingCache.js";
7
+ import { isWorkletFunction } from "./workletFunction.js";
8
+ import { WorkletsError } from "./WorkletsError.js";
9
+ import { WorkletsModule } from "./WorkletsModule/index.js";
10
+ // for web/chrome debugger/jest environments this file provides a stub implementation
11
+ // where no shareable references are used. Instead, the objects themselves are used
12
+ // instead of shareable references, because of the fact that we don't have to deal with
13
+ // running the code on separate VMs.
14
+ const SHOULD_BE_USE_WEB = shouldBeUseWeb();
15
+ const MAGIC_KEY = 'REANIMATED_MAGIC_KEY';
16
+ function isHostObject(value) {
17
+ 'worklet';
18
+
19
+ // We could use JSI to determine whether an object is a host object, however
20
+ // the below workaround works well and is way faster than an additional JSI call.
21
+ // We use the fact that host objects have broken implementation of `hasOwnProperty`
22
+ // and hence return true for all `in` checks regardless of the key we ask for.
23
+ return MAGIC_KEY in value;
24
+ }
25
+ function isPlainJSObject(object) {
26
+ return Object.getPrototypeOf(object) === Object.prototype;
27
+ }
28
+ function getFromCache(value) {
29
+ const cached = shareableMappingCache.get(value);
30
+ if (cached === shareableMappingFlag) {
31
+ // This means that `value` was already a clone and we should return it as is.
32
+ return value;
33
+ }
34
+ return cached;
35
+ }
36
+
37
+ // The below object is used as a replacement for objects that cannot be transferred
38
+ // as shareable values. In makeShareableCloneRecursive we detect if an object is of
39
+ // a plain Object.prototype and only allow such objects to be transferred. This lets
40
+ // us avoid all sorts of react internals from leaking into the UI runtime. To make it
41
+ // possible to catch errors when someone actually tries to access such object on the UI
42
+ // runtime, we use the below Proxy object which is instantiated on the UI runtime and
43
+ // throws whenever someone tries to access its fields.
44
+ const INACCESSIBLE_OBJECT = {
45
+ __init: () => {
46
+ 'worklet';
47
+
48
+ return new Proxy({}, {
49
+ get: (_, prop) => {
50
+ if (prop === '_isReanimatedSharedValue' || prop === '__remoteFunction') {
51
+ // not very happy about this check here, but we need to allow for
52
+ // "inaccessible" objects to be tested with isSharedValue check
53
+ // as it is being used in the mappers when extracting inputs recursively
54
+ // as well as with isRemoteFunction when cloning objects recursively.
55
+ // Apparently we can't check if a key exists there as HostObjects always
56
+ // return true for such tests, so the only possibility for us is to
57
+ // actually access that key and see if it is set to true. We therefore
58
+ // need to allow for this key to be accessed here.
59
+ return false;
60
+ }
61
+ throw new WorkletsError(`Trying to access property \`${String(prop)}\` of an object which cannot be sent to the UI runtime.`);
62
+ },
63
+ set: () => {
64
+ throw new WorkletsError('Trying to write to an object which cannot be sent to the UI runtime.');
65
+ }
66
+ });
67
+ }
68
+ };
69
+ const VALID_ARRAY_VIEWS_NAMES = ['Int8Array', 'Uint8Array', 'Uint8ClampedArray', 'Int16Array', 'Uint16Array', 'Int32Array', 'Uint32Array', 'Float32Array', 'Float64Array', 'BigInt64Array', 'BigUint64Array', 'DataView'];
70
+ const DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD = 30;
71
+ // Below variable stores object that we process in makeShareableCloneRecursive at the specified depth.
72
+ // We use it to check if later on the function reenters with the same object
73
+ let processedObjectAtThresholdDepth;
74
+ function makeShareableCloneRecursiveWeb(value) {
75
+ return value;
76
+ }
77
+ function makeShareableCloneRecursiveNative(value, shouldPersistRemote = false, depth = 0) {
78
+ detectCyclicObject(value, depth);
79
+ const isObject = typeof value === 'object';
80
+ const isFunction = typeof value === 'function';
81
+ if (!isObject && !isFunction || value === null) {
82
+ return clonePrimitive(value, shouldPersistRemote);
83
+ }
84
+ const cached = getFromCache(value);
85
+ if (cached !== undefined) {
86
+ return cached;
87
+ }
88
+ if (Array.isArray(value)) {
89
+ return cloneArray(value, shouldPersistRemote, depth);
90
+ }
91
+ if (isFunction && !isWorkletFunction(value)) {
92
+ return cloneRemoteFunction(value, shouldPersistRemote);
93
+ }
94
+ if (isHostObject(value)) {
95
+ return cloneHostObject(value, shouldPersistRemote);
96
+ }
97
+ if (isPlainJSObject(value) && value.__workletContextObjectFactory) {
98
+ return cloneContextObject(value);
99
+ }
100
+ if ((isPlainJSObject(value) || isFunction) && isWorkletFunction(value)) {
101
+ return cloneWorklet(value, shouldPersistRemote, depth);
102
+ }
103
+ if (isPlainJSObject(value) || isFunction) {
104
+ return clonePlainJSObject(value, shouldPersistRemote, depth);
105
+ }
106
+ if (value instanceof RegExp) {
107
+ return cloneRegExp(value);
108
+ }
109
+ if (value instanceof Error) {
110
+ return cloneError(value);
111
+ }
112
+ if (value instanceof ArrayBuffer) {
113
+ return cloneArrayBuffer(value, shouldPersistRemote);
114
+ }
115
+ if (ArrayBuffer.isView(value)) {
116
+ // typed array (e.g. Int32Array, Uint8ClampedArray) or DataView
117
+ return cloneArrayBufferView(value);
118
+ }
119
+ return inaccessibleObject(value);
120
+ }
121
+ export const makeShareableCloneRecursive = SHOULD_BE_USE_WEB ? makeShareableCloneRecursiveWeb : makeShareableCloneRecursiveNative;
122
+ function detectCyclicObject(value, depth) {
123
+ if (depth >= DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD) {
124
+ // if we reach certain recursion depth we suspect that we are dealing with a cyclic object.
125
+ // this type of objects are not supported and cannot be transferred as shareable, so we
126
+ // implement a simple detection mechanism that remembers the value at a given depth and
127
+ // tests whether we try reenter this method later on with the same value. If that happens
128
+ // we throw an appropriate error.
129
+ if (depth === DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD) {
130
+ processedObjectAtThresholdDepth = value;
131
+ } else if (value === processedObjectAtThresholdDepth) {
132
+ throw new WorkletsError('Trying to convert a cyclic object to a shareable. This is not supported.');
133
+ }
134
+ } else {
135
+ processedObjectAtThresholdDepth = undefined;
136
+ }
137
+ }
138
+ function clonePrimitive(value, shouldPersistRemote) {
139
+ return WorkletsModule.makeShareableClone(value, shouldPersistRemote);
140
+ }
141
+ function cloneArray(value, shouldPersistRemote, depth) {
142
+ const clonedElements = value.map(element => makeShareableCloneRecursive(element, shouldPersistRemote, depth + 1));
143
+ const clone = WorkletsModule.makeShareableClone(clonedElements, shouldPersistRemote, value);
144
+ shareableMappingCache.set(value, clone);
145
+ shareableMappingCache.set(clone);
146
+ freezeObjectInDev(value);
147
+ return clone;
148
+ }
149
+ function cloneRemoteFunction(value, shouldPersistRemote) {
150
+ const clone = WorkletsModule.makeShareableClone(value, shouldPersistRemote, value);
151
+ shareableMappingCache.set(value, clone);
152
+ shareableMappingCache.set(clone);
153
+ freezeObjectInDev(value);
154
+ return clone;
155
+ }
156
+ function cloneHostObject(value, shouldPersistRemote) {
157
+ // for host objects we pass the reference to the object as shareable and
158
+ // then recreate new host object wrapping the same instance on the UI thread.
159
+ // there is no point of iterating over keys as we do for regular objects.
160
+ const clone = WorkletsModule.makeShareableClone(value, shouldPersistRemote, value);
161
+ shareableMappingCache.set(value, clone);
162
+ shareableMappingCache.set(clone);
163
+ return clone;
164
+ }
165
+ function cloneWorklet(value, shouldPersistRemote, depth) {
166
+ if (__DEV__) {
167
+ // TODO: Restore this once we reimplement JS version checking
168
+ // const babelVersion = (value as WorkletFunctionDev).__initData.version;
169
+ // if (babelVersion !== undefined && babelVersion !== jsVersion) {
170
+ // throw new Error(`[Reanimated] Mismatch between JavaScript code version and Reanimated Babel plugin version (${jsVersion} vs. ${babelVersion}).
171
+ // See \`https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#mismatch-between-javascript-code-version-and-reanimated-babel-plugin-version\` for more details.
172
+ // Offending code was: \`${getWorkletCode(value)}\``);
173
+ // }
174
+ registerWorkletStackDetails(value.__workletHash, value.__stackDetails);
175
+ }
176
+ if (value.__stackDetails) {
177
+ // `Error` type of value cannot be copied to the UI thread, so we
178
+ // remove it after we handled it in dev mode or delete it to ignore it in production mode.
179
+ // Not removing this would cause an infinite loop in production mode and it just
180
+ // seems more elegant to handle it this way.
181
+ delete value.__stackDetails;
182
+ }
183
+ // to save on transferring static __initData field of worklet structure
184
+ // we request shareable value to persist its UI counterpart. This means
185
+ // that the __initData field that contains long strings represeting the
186
+ // worklet code, source map, and location, will always be
187
+ // serialized/deserialized once.
188
+ const clonedProps = {};
189
+ clonedProps.__initData = makeShareableCloneRecursive(value.__initData, true, depth + 1);
190
+ for (const [key, element] of Object.entries(value)) {
191
+ if (key === '__initData' && clonedProps.__initData !== undefined) {
192
+ continue;
193
+ }
194
+ clonedProps[key] = makeShareableCloneRecursive(element, shouldPersistRemote, depth + 1);
195
+ }
196
+ const clone = WorkletsModule.makeShareableClone(clonedProps,
197
+ // retain all worklets
198
+ true, value);
199
+ shareableMappingCache.set(value, clone);
200
+ shareableMappingCache.set(clone);
201
+ freezeObjectInDev(value);
202
+ return clone;
203
+ }
204
+ function cloneContextObject(value) {
205
+ const workletContextObjectFactory = value.__workletContextObjectFactory;
206
+ const handle = makeShareableCloneRecursive({
207
+ __init: () => {
208
+ 'worklet';
209
+
210
+ return workletContextObjectFactory();
211
+ }
212
+ });
213
+ shareableMappingCache.set(value, handle);
214
+ return handle;
215
+ }
216
+ function clonePlainJSObject(value, shouldPersistRemote, depth) {
217
+ const clonedProps = {};
218
+ for (const [key, element] of Object.entries(value)) {
219
+ if (key === '__initData' && clonedProps.__initData !== undefined) {
220
+ continue;
221
+ }
222
+ clonedProps[key] = makeShareableCloneRecursive(element, shouldPersistRemote, depth + 1);
223
+ }
224
+ const clone = WorkletsModule.makeShareableClone(clonedProps, shouldPersistRemote, value);
225
+ shareableMappingCache.set(value, clone);
226
+ shareableMappingCache.set(clone);
227
+ freezeObjectInDev(value);
228
+ return clone;
229
+ }
230
+ function cloneRegExp(value) {
231
+ const pattern = value.source;
232
+ const flags = value.flags;
233
+ const handle = makeShareableCloneRecursive({
234
+ __init: () => {
235
+ 'worklet';
236
+
237
+ return new RegExp(pattern, flags);
238
+ }
239
+ });
240
+ shareableMappingCache.set(value, handle);
241
+ return handle;
242
+ }
243
+ function cloneError(value) {
244
+ const {
245
+ name,
246
+ message,
247
+ stack
248
+ } = value;
249
+ const handle = makeShareableCloneRecursive({
250
+ __init: () => {
251
+ 'worklet';
252
+
253
+ // eslint-disable-next-line reanimated/use-worklets-error
254
+ const error = new Error();
255
+ error.name = name;
256
+ error.message = message;
257
+ error.stack = stack;
258
+ return error;
259
+ }
260
+ });
261
+ shareableMappingCache.set(value, handle);
262
+ return handle;
263
+ }
264
+ function cloneArrayBuffer(value, shouldPersistRemote) {
265
+ const clone = WorkletsModule.makeShareableClone(value, shouldPersistRemote, value);
266
+ shareableMappingCache.set(value, clone);
267
+ shareableMappingCache.set(clone);
268
+ return clone;
269
+ }
270
+ function cloneArrayBufferView(value) {
271
+ const buffer = value.buffer;
272
+ const typeName = value.constructor.name;
273
+ const handle = makeShareableCloneRecursive({
274
+ __init: () => {
275
+ 'worklet';
276
+
277
+ if (!VALID_ARRAY_VIEWS_NAMES.includes(typeName)) {
278
+ throw new WorkletsError(`Invalid array view name \`${typeName}\`.`);
279
+ }
280
+ const constructor = global[typeName];
281
+ if (constructor === undefined) {
282
+ throw new WorkletsError(`Constructor for \`${typeName}\` not found.`);
283
+ }
284
+ return new constructor(buffer);
285
+ }
286
+ });
287
+ shareableMappingCache.set(value, handle);
288
+ return handle;
289
+ }
290
+ function inaccessibleObject(value) {
291
+ // This is reached for object types that are not of plain Object.prototype.
292
+ // We don't support such objects from being transferred as shareables to
293
+ // the UI runtime and hence we replace them with "inaccessible object"
294
+ // which is implemented as a Proxy object that throws on any attempt
295
+ // of accessing its fields. We argue that such objects can sometimes leak
296
+ // as attributes of objects being captured by worklets but should never
297
+ // be used on the UI runtime regardless. If they are being accessed, the user
298
+ // will get an appropriate error message.
299
+ const clone = makeShareableCloneRecursive(INACCESSIBLE_OBJECT);
300
+ shareableMappingCache.set(value, clone);
301
+ return clone;
302
+ }
303
+ const WORKLET_CODE_THRESHOLD = 255;
304
+
305
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
306
+ function getWorkletCode(value) {
307
+ const code = value?.__initData?.code;
308
+ if (!code) {
309
+ return 'unknown';
310
+ }
311
+ if (code.length > WORKLET_CODE_THRESHOLD) {
312
+ return `${code.substring(0, WORKLET_CODE_THRESHOLD)}...`;
313
+ }
314
+ return code;
315
+ }
316
+ function isRemoteFunction(value) {
317
+ 'worklet';
318
+
319
+ return !!value.__remoteFunction;
320
+ }
321
+
322
+ /**
323
+ * We freeze
324
+ *
325
+ * - Arrays,
326
+ * - Remote functions,
327
+ * - Plain JS objects,
328
+ *
329
+ * That are transformed to a shareable with a meaningful warning. This should
330
+ * help detect issues when someone modifies data after it's been converted.
331
+ * Meaning that they may be doing a faulty assumption in their code expecting
332
+ * that the updates are going to automatically propagate to the object sent to
333
+ * the UI thread. If the user really wants some objects to be mutable they
334
+ * should use shared values instead.
335
+ */
336
+ function freezeObjectInDev(value) {
337
+ if (!__DEV__) {
338
+ return;
339
+ }
340
+ Object.entries(value).forEach(([key, element]) => {
341
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
342
+ if (!descriptor.configurable) {
343
+ return;
344
+ }
345
+ Object.defineProperty(value, key, {
346
+ get() {
347
+ return element;
348
+ },
349
+ set() {
350
+ logger.warn(`Tried to modify key \`${key}\` of an object which has been already passed to a worklet. See
351
+ https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#tried-to-modify-key-of-an-object-which-has-been-converted-to-a-shareable
352
+ for more details.`);
353
+ }
354
+ });
355
+ });
356
+ Object.preventExtensions(value);
357
+ }
358
+ export function makeShareableCloneOnUIRecursive(value) {
359
+ 'worklet';
360
+
361
+ if (SHOULD_BE_USE_WEB) {
362
+ // @ts-ignore web is an interesting place where we don't run a secondary VM on the UI thread
363
+ // see more details in the comment where USE_STUB_IMPLEMENTATION is defined.
364
+ return value;
365
+ }
366
+ // eslint-disable-next-line @typescript-eslint/no-shadow
367
+ function cloneRecursive(value) {
368
+ if (typeof value === 'object' && value !== null || typeof value === 'function') {
369
+ if (isHostObject(value)) {
370
+ // We call `_makeShareableClone` to wrap the provided HostObject
371
+ // inside ShareableJSRef.
372
+ return global._makeShareableClone(value, undefined);
373
+ }
374
+ if (isRemoteFunction(value)) {
375
+ // RemoteFunctions are created by us therefore they are
376
+ // a Shareable out of the box and there is no need to
377
+ // call `_makeShareableClone`.
378
+ return value.__remoteFunction;
379
+ }
380
+ if (Array.isArray(value)) {
381
+ return global._makeShareableClone(value.map(cloneRecursive), undefined);
382
+ }
383
+ const toAdapt = {};
384
+ for (const [key, element] of Object.entries(value)) {
385
+ toAdapt[key] = cloneRecursive(element);
386
+ }
387
+ return global._makeShareableClone(toAdapt, value);
388
+ }
389
+ return global._makeShareableClone(value, undefined);
390
+ }
391
+ return cloneRecursive(value);
392
+ }
393
+ function makeShareableJS(value) {
394
+ return value;
395
+ }
396
+ function makeShareableNative(value) {
397
+ if (shareableMappingCache.get(value)) {
398
+ return value;
399
+ }
400
+ const handle = makeShareableCloneRecursive({
401
+ __init: () => {
402
+ 'worklet';
403
+
404
+ return value;
405
+ }
406
+ });
407
+ shareableMappingCache.set(value, handle);
408
+ return value;
409
+ }
410
+
411
+ /**
412
+ * This function creates a value on UI with persistent state - changes to it on
413
+ * the UI thread will be seen by all worklets. Use it when you want to create a
414
+ * value that is read and written only on the UI thread.
415
+ */
416
+ export const makeShareable = SHOULD_BE_USE_WEB ? makeShareableJS : makeShareableNative;
417
+ //# sourceMappingURL=shareables.js.map