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.
- package/Common/cpp/worklets/AnimationFrameQueue/AnimationFrameBatchinator.cpp +71 -0
- package/Common/cpp/worklets/AnimationFrameQueue/AnimationFrameBatchinator.h +38 -0
- package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.cpp +131 -0
- package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.h +82 -0
- package/Common/cpp/worklets/NativeModules/WorkletsModuleProxySpec.cpp +72 -0
- package/Common/cpp/worklets/NativeModules/WorkletsModuleProxySpec.h +44 -0
- package/Common/cpp/worklets/Registries/EventHandlerRegistry.cpp +94 -0
- package/Common/cpp/worklets/Registries/EventHandlerRegistry.h +49 -0
- package/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.cpp +8 -0
- package/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.h +39 -0
- package/Common/cpp/worklets/SharedItems/Shareables.cpp +326 -0
- package/Common/cpp/worklets/SharedItems/Shareables.h +345 -0
- package/Common/cpp/worklets/Tools/AsyncQueue.cpp +52 -0
- package/Common/cpp/worklets/Tools/AsyncQueue.h +35 -0
- package/Common/cpp/worklets/Tools/Defs.h +10 -0
- package/Common/cpp/worklets/Tools/JSISerializer.cpp +342 -0
- package/Common/cpp/worklets/Tools/JSISerializer.h +47 -0
- package/Common/cpp/worklets/Tools/JSLogger.cpp +16 -0
- package/Common/cpp/worklets/Tools/JSLogger.h +20 -0
- package/Common/cpp/worklets/Tools/JSScheduler.cpp +10 -0
- package/Common/cpp/worklets/Tools/JSScheduler.h +29 -0
- package/Common/cpp/worklets/Tools/PlatformLogger.h +16 -0
- package/Common/cpp/worklets/Tools/SingleInstanceChecker.h +72 -0
- package/Common/cpp/worklets/Tools/ThreadSafeQueue.h +49 -0
- package/Common/cpp/worklets/Tools/UIScheduler.cpp +19 -0
- package/Common/cpp/worklets/Tools/UIScheduler.h +22 -0
- package/Common/cpp/worklets/Tools/WorkletEventHandler.cpp +29 -0
- package/Common/cpp/worklets/Tools/WorkletEventHandler.h +41 -0
- package/Common/cpp/worklets/Tools/WorkletsJSIUtils.cpp +26 -0
- package/Common/cpp/worklets/Tools/WorkletsJSIUtils.h +199 -0
- package/Common/cpp/worklets/WorkletRuntime/RNRuntimeWorkletDecorator.cpp +20 -0
- package/Common/cpp/worklets/WorkletRuntime/RNRuntimeWorkletDecorator.h +19 -0
- package/Common/cpp/worklets/WorkletRuntime/RuntimeInitialization.md +191 -0
- package/Common/cpp/worklets/WorkletRuntime/UIRuntimeDecorator.cpp +19 -0
- package/Common/cpp/worklets/WorkletRuntime/UIRuntimeDecorator.h +16 -0
- package/Common/cpp/worklets/WorkletRuntime/WorkletHermesRuntime.cpp +108 -0
- package/Common/cpp/worklets/WorkletRuntime/WorkletHermesRuntime.h +127 -0
- package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp +183 -0
- package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h +90 -0
- package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeCollector.h +36 -0
- package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.cpp +179 -0
- package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.h +22 -0
- package/LICENSE +20 -0
- package/README.md +27 -0
- package/RNWorklets.podspec +70 -0
- package/android/CMakeLists.txt +56 -0
- package/android/build.gradle +313 -0
- package/android/gradle.properties +5 -0
- package/android/proguard-rules.pro +3 -0
- package/android/spotless.gradle +9 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/cpp/worklets/CMakeLists.txt +85 -0
- package/android/src/main/cpp/worklets/android/AndroidUIScheduler.cpp +63 -0
- package/android/src/main/cpp/worklets/android/AndroidUIScheduler.h +41 -0
- package/android/src/main/cpp/worklets/android/AnimationFrameCallback.h +32 -0
- package/android/src/main/cpp/worklets/android/PlatformLogger.cpp +29 -0
- package/android/src/main/cpp/worklets/android/WorkletsModule.cpp +83 -0
- package/android/src/main/cpp/worklets/android/WorkletsModule.h +63 -0
- package/android/src/main/cpp/worklets/android/WorkletsOnLoad.cpp +13 -0
- package/android/src/main/java/com/swmansion/worklets/AndroidUIScheduler.java +60 -0
- package/android/src/main/java/com/swmansion/worklets/AnimationFrameQueue/AnimationFrameCallback.java +20 -0
- package/android/src/main/java/com/swmansion/worklets/AnimationFrameQueue/AnimationFrameQueue.java +113 -0
- package/android/src/main/java/com/swmansion/worklets/JSCallInvokerResolver.java +27 -0
- package/android/src/main/java/com/swmansion/worklets/WorkletsMessageQueueThread.java +16 -0
- package/android/src/main/java/com/swmansion/worklets/WorkletsMessageQueueThreadBase.java +72 -0
- package/android/src/main/java/com/swmansion/worklets/WorkletsModule.java +106 -0
- package/android/src/main/java/com/swmansion/worklets/WorkletsPackage.java +49 -0
- package/android/src/paper/com/swmansion/worklets/NativeWorkletsModuleSpec.java +26 -0
- package/apple/worklets/apple/AnimationFrameQueue.h +15 -0
- package/apple/worklets/apple/AnimationFrameQueue.mm +81 -0
- package/apple/worklets/apple/AssertJavaScriptQueue.h +14 -0
- package/apple/worklets/apple/AssertTurboModuleManagerQueue.h +16 -0
- package/apple/worklets/apple/IOSUIScheduler.h +14 -0
- package/apple/worklets/apple/IOSUIScheduler.mm +24 -0
- package/apple/worklets/apple/PlatformLogger.mm +31 -0
- package/apple/worklets/apple/SlowAnimations.h +8 -0
- package/apple/worklets/apple/SlowAnimations.mm +47 -0
- package/apple/worklets/apple/WorkletsDisplayLink.h +21 -0
- package/apple/worklets/apple/WorkletsMessageThread.h +16 -0
- package/apple/worklets/apple/WorkletsMessageThread.mm +32 -0
- package/apple/worklets/apple/WorkletsModule.h +10 -0
- package/apple/worklets/apple/WorkletsModule.mm +85 -0
- package/lib/module/PlatformChecker.js +35 -0
- package/lib/module/PlatformChecker.js.map +1 -0
- package/lib/module/WorkletsError.js +13 -0
- package/lib/module/WorkletsError.js.map +1 -0
- package/lib/module/WorkletsModule/JSWorklets.js +36 -0
- package/lib/module/WorkletsModule/JSWorklets.js.map +1 -0
- package/lib/module/WorkletsModule/NativeWorklets.js +39 -0
- package/lib/module/WorkletsModule/NativeWorklets.js.map +1 -0
- package/lib/module/WorkletsModule/index.js +4 -0
- package/lib/module/WorkletsModule/index.js.map +1 -0
- package/lib/module/WorkletsModule/workletsModuleInstance.js +7 -0
- package/lib/module/WorkletsModule/workletsModuleInstance.js.map +1 -0
- package/lib/module/WorkletsModule/workletsModuleInstance.web.js +5 -0
- package/lib/module/WorkletsModule/workletsModuleInstance.web.js.map +1 -0
- package/lib/module/WorkletsModule/workletsModuleProxy.js +4 -0
- package/lib/module/WorkletsModule/workletsModuleProxy.js.map +1 -0
- package/lib/module/animationFrameQueue/mockedRequestAnimationFrame.js +10 -0
- package/lib/module/animationFrameQueue/mockedRequestAnimationFrame.js.map +1 -0
- package/lib/module/animationFrameQueue/requestAnimationFrame.js +36 -0
- package/lib/module/animationFrameQueue/requestAnimationFrame.js.map +1 -0
- package/lib/module/errors.js +78 -0
- package/lib/module/errors.js.map +1 -0
- package/lib/module/index.js +17 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/initializers.js +158 -0
- package/lib/module/initializers.js.map +1 -0
- package/lib/module/logger/LogBox.js +15 -0
- package/lib/module/logger/LogBox.js.map +1 -0
- package/lib/module/logger/index.js +5 -0
- package/lib/module/logger/index.js.map +1 -0
- package/lib/module/logger/logger.js +137 -0
- package/lib/module/logger/logger.js.map +1 -0
- package/lib/module/privateGlobals.d.js +8 -0
- package/lib/module/privateGlobals.d.js.map +1 -0
- package/lib/module/runtimes.js +63 -0
- package/lib/module/runtimes.js.map +1 -0
- package/lib/module/shareableMappingCache.js +39 -0
- package/lib/module/shareableMappingCache.js.map +1 -0
- package/lib/module/shareables.js +417 -0
- package/lib/module/shareables.js.map +1 -0
- package/lib/module/specs/NativeWorkletsModule.js +5 -0
- package/lib/module/specs/NativeWorkletsModule.js.map +1 -0
- package/lib/module/specs/index.js +5 -0
- package/lib/module/specs/index.js.map +1 -0
- package/lib/module/threads.js +204 -0
- package/lib/module/threads.js.map +1 -0
- package/lib/module/valueUnpacker.js +83 -0
- package/lib/module/valueUnpacker.js.map +1 -0
- package/lib/module/workletFunction.js +37 -0
- package/lib/module/workletFunction.js.map +1 -0
- package/lib/module/workletTypes.js +12 -0
- package/lib/module/workletTypes.js.map +1 -0
- package/lib/typescript/PlatformChecker.d.ts +7 -0
- package/lib/typescript/PlatformChecker.d.ts.map +1 -0
- package/lib/typescript/WorkletsError.d.ts +3 -0
- package/lib/typescript/WorkletsError.d.ts.map +1 -0
- package/lib/typescript/WorkletsModule/JSWorklets.d.ts +3 -0
- package/lib/typescript/WorkletsModule/JSWorklets.d.ts.map +1 -0
- package/lib/typescript/WorkletsModule/NativeWorklets.d.ts +5 -0
- package/lib/typescript/WorkletsModule/NativeWorklets.d.ts.map +1 -0
- package/lib/typescript/WorkletsModule/index.d.ts +3 -0
- package/lib/typescript/WorkletsModule/index.d.ts.map +1 -0
- package/lib/typescript/WorkletsModule/workletsModuleInstance.d.ts +2 -0
- package/lib/typescript/WorkletsModule/workletsModuleInstance.d.ts.map +1 -0
- package/lib/typescript/WorkletsModule/workletsModuleInstance.web.d.ts +2 -0
- package/lib/typescript/WorkletsModule/workletsModuleInstance.web.d.ts.map +1 -0
- package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts +12 -0
- package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts.map +1 -0
- package/lib/typescript/animationFrameQueue/mockedRequestAnimationFrame.d.ts +6 -0
- package/lib/typescript/animationFrameQueue/mockedRequestAnimationFrame.d.ts.map +1 -0
- package/lib/typescript/animationFrameQueue/requestAnimationFrame.d.ts +2 -0
- package/lib/typescript/animationFrameQueue/requestAnimationFrame.d.ts.map +1 -0
- package/lib/typescript/errors.d.ts +19 -0
- package/lib/typescript/errors.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +13 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/initializers.d.ts +6 -0
- package/lib/typescript/initializers.d.ts.map +1 -0
- package/lib/typescript/logger/LogBox.d.ts +32 -0
- package/lib/typescript/logger/LogBox.d.ts.map +1 -0
- package/lib/typescript/logger/index.d.ts +3 -0
- package/lib/typescript/logger/index.d.ts.map +1 -0
- package/lib/typescript/logger/logger.d.ts +52 -0
- package/lib/typescript/logger/logger.d.ts.map +1 -0
- package/lib/typescript/runtimes.d.ts +16 -0
- package/lib/typescript/runtimes.d.ts.map +1 -0
- package/lib/typescript/shareableMappingCache.d.ts +16 -0
- package/lib/typescript/shareableMappingCache.d.ts.map +1 -0
- package/lib/typescript/shareables.d.ts +15 -0
- package/lib/typescript/shareables.d.ts.map +1 -0
- package/lib/typescript/specs/NativeWorkletsModule.d.ts +7 -0
- package/lib/typescript/specs/NativeWorkletsModule.d.ts.map +1 -0
- package/lib/typescript/specs/index.d.ts +3 -0
- package/lib/typescript/specs/index.d.ts.map +1 -0
- package/lib/typescript/threads.d.ts +49 -0
- package/lib/typescript/threads.d.ts.map +1 -0
- package/lib/typescript/valueUnpacker.d.ts +2 -0
- package/lib/typescript/valueUnpacker.d.ts.map +1 -0
- package/lib/typescript/workletFunction.d.ts +27 -0
- package/lib/typescript/workletFunction.d.ts.map +1 -0
- package/lib/typescript/workletTypes.d.ts +49 -0
- package/lib/typescript/workletTypes.d.ts.map +1 -0
- package/package.json +106 -8
- package/plugin/index.js +3 -0
- package/scripts/worklets_utils.rb +53 -0
- package/src/PlatformChecker.ts +43 -0
- package/src/WorkletsError.ts +13 -0
- package/src/WorkletsModule/JSWorklets.ts +57 -0
- package/src/WorkletsModule/NativeWorklets.ts +68 -0
- package/src/WorkletsModule/index.ts +7 -0
- package/src/WorkletsModule/workletsModuleInstance.ts +9 -0
- package/src/WorkletsModule/workletsModuleInstance.web.ts +5 -0
- package/src/WorkletsModule/workletsModuleProxy.ts +30 -0
- package/src/animationFrameQueue/mockedRequestAnimationFrame.ts +11 -0
- package/src/animationFrameQueue/requestAnimationFrame.ts +41 -0
- package/src/errors.ts +103 -0
- package/src/index.ts +42 -0
- package/src/initializers.ts +175 -0
- package/src/logger/LogBox.ts +55 -0
- package/src/logger/index.ts +3 -0
- package/src/logger/logger.ts +155 -0
- package/src/privateGlobals.d.ts +41 -0
- package/src/runtimes.ts +92 -0
- package/src/shareableMappingCache.ts +44 -0
- package/src/shareables.ts +577 -0
- package/src/specs/NativeWorkletsModule.ts +9 -0
- package/src/specs/index.ts +5 -0
- package/src/threads.ts +275 -0
- package/src/valueUnpacker.ts +110 -0
- package/src/workletFunction.ts +47 -0
- package/src/workletTypes.ts +76 -0
- package/Animated.js +0 -13
- package/AnimatedEvent.js +0 -167
- package/AnimatedImplementation.js +0 -666
- package/CoreAnimated.js +0 -43
- package/Easing.js +0 -236
- package/NativeAnimatedHelper.js +0 -226
- package/SpringConfig.js +0 -79
- package/animations/Animation.js +0 -36
- package/animations/DecayAnimation.js +0 -70
- package/animations/SpringAnimation.js +0 -125
- package/animations/TimingAnimation.js +0 -70
- package/bezier.js +0 -128
- package/createAnimatedComponent.js +0 -188
- package/nodes/AnimatedBlock.js +0 -19
- package/nodes/AnimatedClock.js +0 -76
- package/nodes/AnimatedCond.js +0 -23
- package/nodes/AnimatedDetach.js +0 -15
- package/nodes/AnimatedInterpolation.js +0 -338
- package/nodes/AnimatedNode.js +0 -97
- package/nodes/AnimatedOnChange.js +0 -28
- package/nodes/AnimatedOp.js +0 -17
- package/nodes/AnimatedProps.js +0 -154
- package/nodes/AnimatedSet.js +0 -19
- package/nodes/AnimatedStartClock.js +0 -21
- package/nodes/AnimatedStopClock.js +0 -21
- package/nodes/AnimatedStyle.js +0 -89
- package/nodes/AnimatedTracking.js +0 -36
- package/nodes/AnimatedTransform.js +0 -93
- package/nodes/AnimatedValue.js +0 -271
- package/nodes/AnimatedWithInput.js +0 -21
- package/nodes/SpringNode.js +0 -106
- package/nodes/TimingStep.js +0 -44
- 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
|
+
}
|
package/src/runtimes.ts
ADDED
|
@@ -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
|
+
};
|