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,155 @@
1
+ 'use strict';
2
+ import type { LogBoxLogLevel, LogData } from './LogBox';
3
+ import { addLogBoxLog } from './LogBox';
4
+
5
+ const DOCS_URL =
6
+ 'https://docs.swmansion.com/react-native-reanimated/docs/debugging/logger-configuration';
7
+ 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.`;
8
+
9
+ type LogFunction = (data: LogData) => void;
10
+
11
+ export enum LogLevel {
12
+ warn = 1,
13
+ error = 2,
14
+ }
15
+
16
+ export type LoggerConfig = {
17
+ level?: LogLevel;
18
+ strict?: boolean;
19
+ };
20
+
21
+ export type LoggerConfigInternal = {
22
+ logFunction: LogFunction;
23
+ } & Required<LoggerConfig>;
24
+
25
+ function logToConsole(data: LogData) {
26
+ 'worklet';
27
+ switch (data.level) {
28
+ case 'warn':
29
+ console.warn(data.message.content);
30
+ break;
31
+ case 'error':
32
+ case 'fatal':
33
+ case 'syntax':
34
+ console.error(data.message.content);
35
+ break;
36
+ }
37
+ }
38
+
39
+ export const DEFAULT_LOGGER_CONFIG: LoggerConfigInternal = {
40
+ logFunction: logToConsole,
41
+ level: LogLevel.warn,
42
+ strict: true,
43
+ };
44
+
45
+ function formatMessage(message: string) {
46
+ 'worklet';
47
+ return `[Worklets] ${message}`;
48
+ }
49
+
50
+ function createLog(level: LogBoxLogLevel, message: string): LogData {
51
+ 'worklet';
52
+ const formattedMessage = formatMessage(message);
53
+
54
+ return {
55
+ level,
56
+ message: {
57
+ content: formattedMessage,
58
+ substitutions: [],
59
+ },
60
+ category: formattedMessage,
61
+ componentStack: [],
62
+ componentStackType: null,
63
+ // eslint-disable-next-line reanimated/use-worklets-error
64
+ stack: new Error().stack,
65
+ };
66
+ }
67
+
68
+ /**
69
+ * Function that logs to LogBox and console. Used to replace the default console
70
+ * logging with logging to LogBox on the UI thread when runOnJS is available.
71
+ *
72
+ * @param data - The details of the log.
73
+ */
74
+ export function logToLogBoxAndConsole(data: LogData) {
75
+ addLogBoxLog(data);
76
+ logToConsole(data);
77
+ }
78
+
79
+ /**
80
+ * Registers the logger configuration. use it only for Worklet runtimes.
81
+ *
82
+ * @param config - The config to register.
83
+ */
84
+ export function registerLoggerConfig(config: LoggerConfigInternal) {
85
+ 'worklet';
86
+ global.__workletsLoggerConfig = config;
87
+ }
88
+
89
+ /**
90
+ * Replaces the default log function with a custom implementation.
91
+ *
92
+ * @param logFunction - The custom log function.
93
+ */
94
+ export function replaceLoggerImplementation(logFunction: LogFunction) {
95
+ 'worklet';
96
+ registerLoggerConfig({ ...global.__workletsLoggerConfig, logFunction });
97
+ }
98
+
99
+ /**
100
+ * Updates logger configuration.
101
+ *
102
+ * @param options - The new logger configuration to apply.
103
+ *
104
+ * - Level: The minimum log level to display.
105
+ * - Strict: Whether to log warnings and errors that are not strict. Defaults to
106
+ * false.
107
+ */
108
+ export function updateLoggerConfig(options?: Partial<LoggerConfig>) {
109
+ 'worklet';
110
+ registerLoggerConfig({
111
+ ...global.__workletsLoggerConfig,
112
+ // Don't reuse previous level and strict values from the global config
113
+ level: options?.level ?? DEFAULT_LOGGER_CONFIG.level,
114
+ strict: options?.strict ?? DEFAULT_LOGGER_CONFIG.strict,
115
+ });
116
+ }
117
+
118
+ type LogOptions = {
119
+ strict?: boolean;
120
+ };
121
+
122
+ function handleLog(
123
+ level: Exclude<LogBoxLogLevel, 'syntax' | 'fatal'>,
124
+ message: string,
125
+ options: LogOptions
126
+ ) {
127
+ 'worklet';
128
+ const config = global.__workletsLoggerConfig;
129
+ if (
130
+ // Don't log if the log is marked as strict-only and the config doesn't
131
+ // enable strict logging
132
+ (options.strict && !config.strict) ||
133
+ // Don't log if the log level is below the minimum configured level
134
+ LogLevel[level] < config.level
135
+ ) {
136
+ return;
137
+ }
138
+
139
+ if (options.strict) {
140
+ message += `\n\n${DOCS_REFERENCE}`;
141
+ }
142
+
143
+ config.logFunction(createLog(level, message));
144
+ }
145
+
146
+ export const logger = {
147
+ warn(message: string, options: LogOptions = {}) {
148
+ 'worklet';
149
+ handleLog('warn', message, options);
150
+ },
151
+ error(message: string, options: LogOptions = {}) {
152
+ 'worklet';
153
+ handleLog('error', message, options);
154
+ },
155
+ };
@@ -0,0 +1,41 @@
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
+ import type { callGuardDEV } from './initializers';
8
+ import type { WorkletsModuleProxy } from './WorkletsModule';
9
+
10
+ declare global {
11
+ var __workletsCache: Map<string, any>;
12
+ var __handleCache: WeakMap<object, any>;
13
+ var evalWithSourceMap:
14
+ | ((js: string, sourceURL: string, sourceMap: string) => any)
15
+ | undefined;
16
+ var evalWithSourceUrl: ((js: string, sourceURL: string) => any) | undefined;
17
+ var _toString: (value: unknown) => string;
18
+ var __workletsModuleProxy: WorkletsModuleProxy | undefined;
19
+ var _WORKLET: boolean | undefined;
20
+ var _makeShareableClone: <T>(
21
+ value: T,
22
+ nativeStateSource?: object
23
+ ) => FlatShareableRef<T>;
24
+ var __callMicrotasks: () => void;
25
+ var _scheduleHostFunctionOnJS: (fun: (...args: A) => R, args?: A) => void;
26
+ var _scheduleRemoteFunctionOnJS: (fun: (...args: A) => R, args?: A) => void;
27
+ var __ErrorUtils: {
28
+ reportFatalError: (error: Error) => void;
29
+ };
30
+ var __callGuardDEV: typeof callGuardDEV | undefined;
31
+ var __flushAnimationFrame: (timestamp: number) => void;
32
+ var __frameTimestamp: number | undefined;
33
+ var __workletsLoggerConfig: LoggerConfigInternal;
34
+ var _log: (value: unknown) => void;
35
+ var _getAnimationTimestamp: () => number;
36
+ var _scheduleOnRuntime: (
37
+ runtime: WorkletRuntime,
38
+ worklet: ShareableRef<() => void>
39
+ ) => void;
40
+ var _microtaskQueueFinalizers: (() => void)[];
41
+ }
@@ -0,0 +1,92 @@
1
+ 'use strict';
2
+
3
+ import { setupCallGuard, setupConsole } from './initializers';
4
+ import { registerLoggerConfig } from './logger';
5
+ import { shouldBeUseWeb } from './PlatformChecker';
6
+ import {
7
+ makeShareableCloneOnUIRecursive,
8
+ makeShareableCloneRecursive,
9
+ } from './shareables';
10
+ import { isWorkletFunction } from './workletFunction';
11
+ import { registerWorkletsError, WorkletsError } from './WorkletsError';
12
+ import { WorkletsModule } from './WorkletsModule';
13
+ import type { WorkletFunction, WorkletRuntime } from './workletTypes';
14
+
15
+ const SHOULD_BE_USE_WEB = shouldBeUseWeb();
16
+
17
+ /**
18
+ * Lets you create a new JS runtime which can be used to run worklets possibly
19
+ * on different threads than JS or UI thread.
20
+ *
21
+ * @param name - A name used to identify the runtime which will appear in
22
+ * devices list in Chrome DevTools.
23
+ * @param initializer - An optional worklet that will be run synchronously on
24
+ * the same thread immediately after the runtime is created.
25
+ * @returns WorkletRuntime which is a
26
+ * `jsi::HostObject<worklets::WorkletRuntime>` - {@link WorkletRuntime}
27
+ * @see https://docs.swmansion.com/react-native-reanimated/docs/threading/createWorkletRuntime
28
+ */
29
+ // @ts-expect-error Check `runOnUI` overload.
30
+ export function createWorkletRuntime(
31
+ name: string,
32
+ initializer?: () => void
33
+ ): WorkletRuntime;
34
+
35
+ export function createWorkletRuntime(
36
+ name: string,
37
+ initializer?: WorkletFunction<[], void>
38
+ ): WorkletRuntime {
39
+ // Assign to a different variable as __workletsLoggerConfig is not a captured
40
+ // identifier in the Worklet runtime.
41
+ const config = __workletsLoggerConfig;
42
+ return WorkletsModule.createWorkletRuntime(
43
+ name,
44
+ makeShareableCloneRecursive(() => {
45
+ 'worklet';
46
+ registerWorkletsError();
47
+ registerLoggerConfig(config);
48
+ setupCallGuard();
49
+ setupConsole();
50
+ initializer?.();
51
+ })
52
+ );
53
+ }
54
+
55
+ // @ts-expect-error Check `runOnUI` overload.
56
+ export function runOnRuntime<Args extends unknown[], ReturnValue>(
57
+ workletRuntime: WorkletRuntime,
58
+ worklet: (...args: Args) => ReturnValue
59
+ ): WorkletFunction<Args, ReturnValue>;
60
+ /** Schedule a worklet to execute on the background queue. */
61
+ export function runOnRuntime<Args extends unknown[], ReturnValue>(
62
+ workletRuntime: WorkletRuntime,
63
+ worklet: WorkletFunction<Args, ReturnValue>
64
+ ): (...args: Args) => void {
65
+ 'worklet';
66
+ if (__DEV__ && !SHOULD_BE_USE_WEB && !isWorkletFunction(worklet)) {
67
+ throw new WorkletsError(
68
+ 'The function passed to `runOnRuntime` is not a worklet.' +
69
+ (_WORKLET
70
+ ? ' Please make sure that `processNestedWorklets` option in Reanimated Babel plugin is enabled.'
71
+ : '')
72
+ );
73
+ }
74
+ if (_WORKLET) {
75
+ return (...args) =>
76
+ global._scheduleOnRuntime(
77
+ workletRuntime,
78
+ makeShareableCloneOnUIRecursive(() => {
79
+ 'worklet';
80
+ worklet(...args);
81
+ })
82
+ );
83
+ }
84
+ return (...args) =>
85
+ WorkletsModule.scheduleOnRuntime(
86
+ workletRuntime,
87
+ makeShareableCloneRecursive(() => {
88
+ 'worklet';
89
+ worklet(...args);
90
+ })
91
+ );
92
+ }
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+ import { shouldBeUseWeb } from './PlatformChecker';
3
+ import type { ShareableRef } from './workletTypes';
4
+
5
+ const SHOULD_BE_USE_WEB = shouldBeUseWeb();
6
+
7
+ /**
8
+ * This symbol is used to represent a mapping from the value to itself.
9
+ *
10
+ * It's used to prevent converting a shareable that's already converted - for
11
+ * example a Shared Value that's in worklet's closure.
12
+ */
13
+ export const shareableMappingFlag = Symbol('shareable flag');
14
+
15
+ /*
16
+ During a fast refresh, React holds the same instance of a Mutable
17
+ (that's guaranteed by `useRef`) but `shareableCache` gets regenerated and thus
18
+ becoming empty. This happens when editing the file that contains the definition of this cache.
19
+
20
+ Because of it, `makeShareableCloneRecursive` can't find given mapping
21
+ in `shareableCache` for the Mutable and tries to clone it as if it was a regular JS object.
22
+ During cloning we use `Object.entries` to iterate over the keys which throws an error on accessing `_value`.
23
+ For convenience we moved this cache to a separate file so it doesn't scare us with red squiggles.
24
+ */
25
+
26
+ const cache = SHOULD_BE_USE_WEB
27
+ ? null
28
+ : new WeakMap<object, ShareableRef | symbol>();
29
+
30
+ export const shareableMappingCache = SHOULD_BE_USE_WEB
31
+ ? {
32
+ set() {
33
+ // NOOP
34
+ },
35
+ get() {
36
+ return null;
37
+ },
38
+ }
39
+ : {
40
+ set(shareable: object, shareableRef?: ShareableRef): void {
41
+ cache!.set(shareable, shareableRef || shareableMappingFlag);
42
+ },
43
+ get: cache!.get.bind(cache),
44
+ };