@amazon-devices/react-native-worklets 1.0.0-rn-83
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/JSIWorkletsModuleProxy.cpp +712 -0
- package/Common/cpp/worklets/NativeModules/JSIWorkletsModuleProxy.h +88 -0
- package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.cpp +89 -0
- package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.h +80 -0
- package/Common/cpp/worklets/Public/AsyncQueue.h +14 -0
- package/Common/cpp/worklets/Registries/EventHandlerRegistry.cpp +94 -0
- package/Common/cpp/worklets/Registries/EventHandlerRegistry.h +51 -0
- package/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.cpp +8 -0
- package/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.h +40 -0
- package/Common/cpp/worklets/Resources/SynchronizableUnpacker.cpp +49 -0
- package/Common/cpp/worklets/Resources/Unpackers.h +6 -0
- package/Common/cpp/worklets/Resources/ValueUnpacker.cpp +53 -0
- package/Common/cpp/worklets/RunLoop/AsyncQueueImpl.cpp +62 -0
- package/Common/cpp/worklets/RunLoop/AsyncQueueImpl.h +46 -0
- package/Common/cpp/worklets/RunLoop/EventLoop.cpp +112 -0
- package/Common/cpp/worklets/RunLoop/EventLoop.h +52 -0
- package/Common/cpp/worklets/SharedItems/Serializable.cpp +545 -0
- package/Common/cpp/worklets/SharedItems/Serializable.h +473 -0
- package/Common/cpp/worklets/SharedItems/Synchronizable.cpp +67 -0
- package/Common/cpp/worklets/SharedItems/Synchronizable.h +59 -0
- package/Common/cpp/worklets/SharedItems/SynchronizableAccess.cpp +81 -0
- package/Common/cpp/worklets/SharedItems/SynchronizableAccess.h +45 -0
- package/Common/cpp/worklets/Tools/Defs.h +10 -0
- package/Common/cpp/worklets/Tools/FeatureFlags.cpp +15 -0
- package/Common/cpp/worklets/Tools/FeatureFlags.h +44 -0
- package/Common/cpp/worklets/Tools/JSISerializer.cpp +342 -0
- package/Common/cpp/worklets/Tools/JSISerializer.h +51 -0
- package/Common/cpp/worklets/Tools/JSLogger.cpp +68 -0
- package/Common/cpp/worklets/Tools/JSLogger.h +37 -0
- package/Common/cpp/worklets/Tools/JSScheduler.cpp +24 -0
- package/Common/cpp/worklets/Tools/JSScheduler.h +39 -0
- package/Common/cpp/worklets/Tools/PlatformLogger.h +16 -0
- package/Common/cpp/worklets/Tools/SingleInstanceChecker.h +82 -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 +26 -0
- package/Common/cpp/worklets/Tools/VersionUtils.cpp +75 -0
- package/Common/cpp/worklets/Tools/VersionUtils.h +28 -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 +46 -0
- package/Common/cpp/worklets/Tools/WorkletsJSIUtils.h +208 -0
- package/Common/cpp/worklets/Tools/WorkletsSystraceSection.h +161 -0
- package/Common/cpp/worklets/Tools/WorkletsVersion.cpp +54 -0
- package/Common/cpp/worklets/Tools/WorkletsVersion.h +20 -0
- package/Common/cpp/worklets/WorkletRuntime/RNRuntimeWorkletDecorator.cpp +38 -0
- package/Common/cpp/worklets/WorkletRuntime/RNRuntimeWorkletDecorator.h +21 -0
- package/Common/cpp/worklets/WorkletRuntime/RuntimeData.cpp +3 -0
- package/Common/cpp/worklets/WorkletRuntime/RuntimeData.h +12 -0
- package/Common/cpp/worklets/WorkletRuntime/RuntimeInitialization.md +191 -0
- package/Common/cpp/worklets/WorkletRuntime/RuntimeKind.h +16 -0
- package/Common/cpp/worklets/WorkletRuntime/RuntimeManager.cpp +105 -0
- package/Common/cpp/worklets/WorkletRuntime/RuntimeManager.h +63 -0
- package/Common/cpp/worklets/WorkletRuntime/UIRuntimeDecorator.cpp +23 -0
- package/Common/cpp/worklets/WorkletRuntime/UIRuntimeDecorator.h +16 -0
- package/Common/cpp/worklets/WorkletRuntime/WorkletHermesRuntime.cpp +119 -0
- package/Common/cpp/worklets/WorkletRuntime/WorkletHermesRuntime.h +134 -0
- package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp +244 -0
- package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h +102 -0
- package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeCollector.h +36 -0
- package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.cpp +308 -0
- package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.h +26 -0
- package/LICENSE +7 -0
- package/README.md +31 -0
- package/RNWorklets.podspec +94 -0
- package/SystemBundles/cc12deac6e91c435b76698c8445aed85612f2f88e733218d14f88a35d06fb0f4.bundle +10512 -0
- package/SystemBundles/cc12deac6e91c435b76698c8445aed85612f2f88e733218d14f88a35d06fb0f4.map +3771 -0
- package/SystemModules/amzn__react-native-worklets-1.modules.txt +39 -0
- package/SystemModules/amzn__react-native-worklets-1.private.modules.txt +61 -0
- package/SystemModules/keplerscript-system-bundle-config.json +7 -0
- package/android/CMakeLists.txt +124 -0
- package/android/build.gradle +350 -0
- package/android/gradle.properties +5 -0
- package/android/proguard-rules.pro +3 -0
- package/android/spotless.gradle +9 -0
- package/android/src/experimentalBundling/com/swmansion/worklets/WorkletsModule.java +149 -0
- package/android/src/legacyBundling/com/swmansion/worklets/WorkletsModule.java +122 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/cpp/worklets/android/AndroidUIScheduler.cpp +63 -0
- package/android/src/main/cpp/worklets/android/AndroidUIScheduler.h +43 -0
- package/android/src/main/cpp/worklets/android/AnimationFrameCallback.h +34 -0
- package/android/src/main/cpp/worklets/android/PlatformLogger.cpp +29 -0
- package/android/src/main/cpp/worklets/android/WorkletsModule.cpp +115 -0
- package/android/src/main/cpp/worklets/android/WorkletsModule.h +78 -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/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 +73 -0
- package/android/src/main/java/com/swmansion/worklets/WorkletsPackage.java +48 -0
- package/android/src/main/java/com/swmansion/worklets/runloop/AnimationFrameCallback.java +19 -0
- package/android/src/main/java/com/swmansion/worklets/runloop/AnimationFrameQueue.java +113 -0
- package/apple/worklets/apple/AnimationFrameQueue.h +15 -0
- package/apple/worklets/apple/AnimationFrameQueue.mm +135 -0
- package/apple/worklets/apple/AssertJavaScriptQueue.h +14 -0
- package/apple/worklets/apple/AssertTurboModuleManagerQueue.h +16 -0
- package/apple/worklets/apple/IOSUIScheduler.h +15 -0
- package/apple/worklets/apple/IOSUIScheduler.mm +29 -0
- package/apple/worklets/apple/PlatformLogger.mm +31 -0
- package/apple/worklets/apple/SlowAnimations.h +8 -0
- package/apple/worklets/apple/SlowAnimations.mm +45 -0
- package/apple/worklets/apple/WorkletsDisplayLink.h +21 -0
- package/apple/worklets/apple/WorkletsMessageThread.h +16 -0
- package/apple/worklets/apple/WorkletsMessageThread.mm +31 -0
- package/apple/worklets/apple/WorkletsModule.h +28 -0
- package/apple/worklets/apple/WorkletsModule.mm +121 -0
- package/bundleMode/index.d.ts +3 -0
- package/bundleMode/index.js +59 -0
- package/compatibility.json +11 -0
- package/kepler-compatibility.json +5 -0
- package/kepler-transformed-package-info.json +3 -0
- package/lib/module/PlatformChecker/PlatformChecker.js +8 -0
- package/lib/module/PlatformChecker/PlatformChecker.js.map +1 -0
- package/lib/module/PlatformChecker/index.js +18 -0
- package/lib/module/PlatformChecker/index.js.map +1 -0
- package/lib/module/WorkletsError.js +29 -0
- package/lib/module/WorkletsError.js.map +1 -0
- package/lib/module/WorkletsModule/JSWorklets.js +111 -0
- package/lib/module/WorkletsModule/JSWorklets.js.map +1 -0
- package/lib/module/WorkletsModule/NativeWorklets.js +126 -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/bundleUnpacker.js +47 -0
- package/lib/module/bundleUnpacker.js.map +1 -0
- package/lib/module/callGuard.js +30 -0
- package/lib/module/callGuard.js.map +1 -0
- package/lib/module/deprecated.js +20 -0
- package/lib/module/deprecated.js.map +1 -0
- package/lib/module/errors.js +69 -0
- package/lib/module/errors.js.map +1 -0
- package/lib/module/featureFlags/index.js +48 -0
- package/lib/module/featureFlags/index.js.map +1 -0
- package/lib/module/featureFlags/staticFlags.json +4 -0
- package/lib/module/index.js +26 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/initializers.js +221 -0
- package/lib/module/initializers.js.map +1 -0
- package/lib/module/isSynchronizable.js +6 -0
- package/lib/module/isSynchronizable.js.map +1 -0
- package/lib/module/logger.js +15 -0
- package/lib/module/logger.js.map +1 -0
- package/lib/module/privateGlobals.d.js +7 -0
- package/lib/module/privateGlobals.d.js.map +1 -0
- package/lib/module/publicGlobals.js +6 -0
- package/lib/module/publicGlobals.js.map +1 -0
- package/lib/module/runLoop/common/setImmediatePolyfill.js +15 -0
- package/lib/module/runLoop/common/setImmediatePolyfill.js.map +1 -0
- package/lib/module/runLoop/common/setIntervalPolyfill.js +26 -0
- package/lib/module/runLoop/common/setIntervalPolyfill.js.map +1 -0
- package/lib/module/runLoop/uiRuntime/mockedRequestAnimationFrame.js +10 -0
- package/lib/module/runLoop/uiRuntime/mockedRequestAnimationFrame.js.map +1 -0
- package/lib/module/runLoop/uiRuntime/requestAnimationFrame.js +52 -0
- package/lib/module/runLoop/uiRuntime/requestAnimationFrame.js.map +1 -0
- package/lib/module/runLoop/uiRuntime/setTimeoutPolyfill.js +32 -0
- package/lib/module/runLoop/uiRuntime/setTimeoutPolyfill.js.map +1 -0
- package/lib/module/runLoop/workletRuntime/index.js +19 -0
- package/lib/module/runLoop/workletRuntime/index.js.map +1 -0
- package/lib/module/runLoop/workletRuntime/queueMicrotask.js +11 -0
- package/lib/module/runLoop/workletRuntime/queueMicrotask.js.map +1 -0
- package/lib/module/runLoop/workletRuntime/requestAnimationFramePolyfill.js +48 -0
- package/lib/module/runLoop/workletRuntime/requestAnimationFramePolyfill.js.map +1 -0
- package/lib/module/runLoop/workletRuntime/setTimeout.js +28 -0
- package/lib/module/runLoop/workletRuntime/setTimeout.js.map +1 -0
- package/lib/module/runLoop/workletRuntime/taskQueue.js +37 -0
- package/lib/module/runLoop/workletRuntime/taskQueue.js.map +1 -0
- package/lib/module/runtimeKind.js +39 -0
- package/lib/module/runtimeKind.js.map +1 -0
- package/lib/module/runtimes.js +100 -0
- package/lib/module/runtimes.js.map +1 -0
- package/lib/module/serializable.js +568 -0
- package/lib/module/serializable.js.map +1 -0
- package/lib/module/serializableMappingCache.js +37 -0
- package/lib/module/serializableMappingCache.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/NativeWorkletsModule.kepler.js +7 -0
- package/lib/module/specs/NativeWorkletsModule.kepler.js.map +1 -0
- package/lib/module/specs/index.js +10 -0
- package/lib/module/specs/index.js.map +1 -0
- package/lib/module/synchronizable.js +9 -0
- package/lib/module/synchronizable.js.map +1 -0
- package/lib/module/synchronizableUnpacker.js +42 -0
- package/lib/module/synchronizableUnpacker.js.map +1 -0
- package/lib/module/threads.js +347 -0
- package/lib/module/threads.js.map +1 -0
- package/lib/module/utils/checkCppVersion.js +30 -0
- package/lib/module/utils/checkCppVersion.js.map +1 -0
- package/lib/module/utils/jsVersion.js +9 -0
- package/lib/module/utils/jsVersion.js.map +1 -0
- package/lib/module/valueUnpacker.js +60 -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/workletRuntimeEntry.js +32 -0
- package/lib/module/workletRuntimeEntry.js.map +1 -0
- package/lib/module/workletTypes.js +12 -0
- package/lib/module/workletTypes.js.map +1 -0
- package/lib/typescript/PlatformChecker/PlatformChecker.d.ts +5 -0
- package/lib/typescript/PlatformChecker/PlatformChecker.d.ts.map +1 -0
- package/lib/typescript/PlatformChecker/index.d.ts +10 -0
- package/lib/typescript/PlatformChecker/index.d.ts.map +1 -0
- package/lib/typescript/WorkletsError.d.ts +15 -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 +3 -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 +37 -0
- package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts.map +1 -0
- package/lib/typescript/bundleUnpacker.d.ts +7 -0
- package/lib/typescript/bundleUnpacker.d.ts.map +1 -0
- package/lib/typescript/callGuard.d.ts +4 -0
- package/lib/typescript/callGuard.d.ts.map +1 -0
- package/lib/typescript/deprecated.d.ts +20 -0
- package/lib/typescript/deprecated.d.ts.map +1 -0
- package/lib/typescript/errors.d.ts +16 -0
- package/lib/typescript/errors.d.ts.map +1 -0
- package/lib/typescript/featureFlags/index.d.ts +23 -0
- package/lib/typescript/featureFlags/index.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +17 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/initializers.d.ts +17 -0
- package/lib/typescript/initializers.d.ts.map +1 -0
- package/lib/typescript/isSynchronizable.d.ts +3 -0
- package/lib/typescript/isSynchronizable.d.ts.map +1 -0
- package/lib/typescript/logger.d.ts +5 -0
- package/lib/typescript/logger.d.ts.map +1 -0
- package/lib/typescript/publicGlobals.d.ts +31 -0
- package/lib/typescript/publicGlobals.d.ts.map +1 -0
- package/lib/typescript/runLoop/common/setImmediatePolyfill.d.ts +2 -0
- package/lib/typescript/runLoop/common/setImmediatePolyfill.d.ts.map +1 -0
- package/lib/typescript/runLoop/common/setIntervalPolyfill.d.ts +2 -0
- package/lib/typescript/runLoop/common/setIntervalPolyfill.d.ts.map +1 -0
- package/lib/typescript/runLoop/uiRuntime/mockedRequestAnimationFrame.d.ts +6 -0
- package/lib/typescript/runLoop/uiRuntime/mockedRequestAnimationFrame.d.ts.map +1 -0
- package/lib/typescript/runLoop/uiRuntime/requestAnimationFrame.d.ts +2 -0
- package/lib/typescript/runLoop/uiRuntime/requestAnimationFrame.d.ts.map +1 -0
- package/lib/typescript/runLoop/uiRuntime/setTimeoutPolyfill.d.ts +2 -0
- package/lib/typescript/runLoop/uiRuntime/setTimeoutPolyfill.d.ts.map +1 -0
- package/lib/typescript/runLoop/workletRuntime/index.d.ts +2 -0
- package/lib/typescript/runLoop/workletRuntime/index.d.ts.map +1 -0
- package/lib/typescript/runLoop/workletRuntime/queueMicrotask.d.ts +2 -0
- package/lib/typescript/runLoop/workletRuntime/queueMicrotask.d.ts.map +1 -0
- package/lib/typescript/runLoop/workletRuntime/requestAnimationFramePolyfill.d.ts +2 -0
- package/lib/typescript/runLoop/workletRuntime/requestAnimationFramePolyfill.d.ts.map +1 -0
- package/lib/typescript/runLoop/workletRuntime/setTimeout.d.ts +2 -0
- package/lib/typescript/runLoop/workletRuntime/setTimeout.d.ts.map +1 -0
- package/lib/typescript/runLoop/workletRuntime/taskQueue.d.ts +10 -0
- package/lib/typescript/runLoop/workletRuntime/taskQueue.d.ts.map +1 -0
- package/lib/typescript/runtimeKind.d.ts +25 -0
- package/lib/typescript/runtimeKind.d.ts.map +1 -0
- package/lib/typescript/runtimes.d.ts +77 -0
- package/lib/typescript/runtimes.d.ts.map +1 -0
- package/lib/typescript/serializable.d.ts +20 -0
- package/lib/typescript/serializable.d.ts.map +1 -0
- package/lib/typescript/serializableMappingCache.d.ts +16 -0
- package/lib/typescript/serializableMappingCache.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/NativeWorkletsModule.kepler.d.ts +7 -0
- package/lib/typescript/specs/NativeWorkletsModule.kepler.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/synchronizable.d.ts +15 -0
- package/lib/typescript/synchronizable.d.ts.map +1 -0
- package/lib/typescript/synchronizableUnpacker.d.ts +4 -0
- package/lib/typescript/synchronizableUnpacker.d.ts.map +1 -0
- package/lib/typescript/threads.d.ts +138 -0
- package/lib/typescript/threads.d.ts.map +1 -0
- package/lib/typescript/utils/checkCppVersion.d.ts +3 -0
- package/lib/typescript/utils/checkCppVersion.d.ts.map +1 -0
- package/lib/typescript/utils/jsVersion.d.ts +7 -0
- package/lib/typescript/utils/jsVersion.d.ts.map +1 -0
- package/lib/typescript/valueUnpacker.d.ts +6 -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/workletRuntimeEntry.d.ts +14 -0
- package/lib/typescript/workletRuntimeEntry.d.ts.map +1 -0
- package/lib/typescript/workletTypes.d.ts +61 -0
- package/lib/typescript/workletTypes.d.ts.map +1 -0
- package/package.json +153 -0
- package/plugin/index.js +1761 -0
- package/react-native.config.js +21 -0
- package/scripts/validate-react-native-version.js +43 -0
- package/scripts/worklets_utils.rb +94 -0
- package/src/PlatformChecker/PlatformChecker.ts +7 -0
- package/src/PlatformChecker/index.ts +30 -0
- package/src/WorkletsError.ts +36 -0
- package/src/WorkletsModule/JSWorklets.ts +200 -0
- package/src/WorkletsModule/NativeWorklets.ts +256 -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 +126 -0
- package/src/bundleUnpacker.ts +75 -0
- package/src/callGuard.ts +33 -0
- package/src/deprecated.ts +32 -0
- package/src/errors.ts +80 -0
- package/src/featureFlags/index.ts +72 -0
- package/src/featureFlags/staticFlags.json +4 -0
- package/src/index.ts +53 -0
- package/src/initializers.ts +262 -0
- package/src/isSynchronizable.ts +13 -0
- package/src/logger.ts +16 -0
- package/src/privateGlobals.d.ts +79 -0
- package/src/publicGlobals.ts +39 -0
- package/src/runLoop/common/setImmediatePolyfill.ts +20 -0
- package/src/runLoop/common/setIntervalPolyfill.ts +38 -0
- package/src/runLoop/uiRuntime/mockedRequestAnimationFrame.ts +11 -0
- package/src/runLoop/uiRuntime/requestAnimationFrame.ts +66 -0
- package/src/runLoop/uiRuntime/setTimeoutPolyfill.ts +40 -0
- package/src/runLoop/workletRuntime/index.ts +18 -0
- package/src/runLoop/workletRuntime/queueMicrotask.ts +13 -0
- package/src/runLoop/workletRuntime/requestAnimationFramePolyfill.ts +61 -0
- package/src/runLoop/workletRuntime/setTimeout.ts +37 -0
- package/src/runLoop/workletRuntime/taskQueue.ts +44 -0
- package/src/runtimeKind.ts +37 -0
- package/src/runtimes.ts +193 -0
- package/src/serializable.ts +801 -0
- package/src/serializableMappingCache.ts +42 -0
- package/src/specs/NativeWorkletsModule.kepler.ts +11 -0
- package/src/specs/NativeWorkletsModule.ts +9 -0
- package/src/specs/index.ts +13 -0
- package/src/synchronizable.ts +32 -0
- package/src/synchronizableUnpacker.ts +67 -0
- package/src/threads.ts +452 -0
- package/src/utils/checkCppVersion.ts +34 -0
- package/src/utils/jsVersion.ts +7 -0
- package/src/valueUnpacker.ts +90 -0
- package/src/workletFunction.ts +42 -0
- package/src/workletRuntimeEntry.ts +35 -0
- package/src/workletTypes.ts +81 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
import { SHOULD_BE_USE_WEB } from './PlatformChecker';
|
|
3
|
+
import type { SerializableRef } from './workletTypes';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* This symbol is used to represent a mapping from the value to itself.
|
|
7
|
+
*
|
|
8
|
+
* It's used to prevent converting a serializable that's already converted - for
|
|
9
|
+
* example a Shared Value that's in worklet's closure.
|
|
10
|
+
*/
|
|
11
|
+
export const serializableMappingFlag = Symbol('serializable flag');
|
|
12
|
+
|
|
13
|
+
/*
|
|
14
|
+
During a fast refresh, React holds the same instance of a Mutable
|
|
15
|
+
(that's guaranteed by `useRef`) but `serializableCache` gets regenerated and thus
|
|
16
|
+
becoming empty. This happens when editing the file that contains the definition of this cache.
|
|
17
|
+
|
|
18
|
+
Because of it, `createSerializable` can't find given mapping
|
|
19
|
+
in `serializableCache` for the Mutable and tries to clone it as if it was a regular JS object.
|
|
20
|
+
During cloning we use `Object.entries` to iterate over the keys which throws an error on accessing `_value`.
|
|
21
|
+
For convenience we moved this cache to a separate file so it doesn't scare us with red squiggles.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
const cache = SHOULD_BE_USE_WEB
|
|
25
|
+
? null
|
|
26
|
+
: new WeakMap<object, SerializableRef | symbol>();
|
|
27
|
+
|
|
28
|
+
export const serializableMappingCache = SHOULD_BE_USE_WEB
|
|
29
|
+
? {
|
|
30
|
+
set() {
|
|
31
|
+
// NOOP
|
|
32
|
+
},
|
|
33
|
+
get() {
|
|
34
|
+
return null;
|
|
35
|
+
},
|
|
36
|
+
}
|
|
37
|
+
: {
|
|
38
|
+
set(serializable: object, serializableRef?: SerializableRef): void {
|
|
39
|
+
cache!.set(serializable, serializableRef || serializableMappingFlag);
|
|
40
|
+
},
|
|
41
|
+
get: cache!.get.bind(cache),
|
|
42
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// @ts-ignore No type definitions for KeplerTurboModule
|
|
3
|
+
import type { KeplerTurboModule } from '@amazon-devices/react-native-kepler/Libraries/TurboModule/KeplerTurboModule';
|
|
4
|
+
// @ts-ignore No type definitions for TurboModuleRegistry
|
|
5
|
+
import * as TurboModuleRegistry from '@amazon-devices/react-native-kepler/Libraries/TurboModule/TurboModuleRegistry';
|
|
6
|
+
|
|
7
|
+
interface Spec extends KeplerTurboModule {
|
|
8
|
+
readonly installTurboModule: () => boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default TurboModuleRegistry.get<Spec>('WorkletsModule');
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
import type { TurboModule } from 'react-native';
|
|
3
|
+
import { TurboModuleRegistry } from 'react-native';
|
|
4
|
+
|
|
5
|
+
export interface Spec extends TurboModule {
|
|
6
|
+
installTurboModule: () => boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export default TurboModuleRegistry.get<Spec>('WorkletsModule');
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import { RuntimeKind } from '../runtimeKind';
|
|
4
|
+
import type { Spec } from './NativeWorkletsModule';
|
|
5
|
+
import RNWorkletsTurboModule from './NativeWorkletsModule';
|
|
6
|
+
|
|
7
|
+
export const WorkletsTurboModule: Spec | null =
|
|
8
|
+
globalThis.__RUNTIME_KIND === RuntimeKind.ReactNative
|
|
9
|
+
? RNWorkletsTurboModule
|
|
10
|
+
: // In Bundle Mode, on Worklet Runtimes `RNWorkletsTurboModule` isn't
|
|
11
|
+
// available and shouldn't be accessed. We return null here
|
|
12
|
+
// to keep the same codebase for the Bundle Mode and legacy behavior.
|
|
13
|
+
null;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import { createSerializable } from './serializable';
|
|
4
|
+
import { WorkletsModule } from './WorkletsModule';
|
|
5
|
+
import type { SerializableRef } from './workletTypes';
|
|
6
|
+
|
|
7
|
+
export function createSynchronizable<TValue>(
|
|
8
|
+
initialValue: TValue
|
|
9
|
+
): Synchronizable<TValue> {
|
|
10
|
+
const synchronizableRef = WorkletsModule.createSynchronizable(
|
|
11
|
+
createSerializable(initialValue)
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
return globalThis.__synchronizableUnpacker(
|
|
15
|
+
synchronizableRef
|
|
16
|
+
) as unknown as Synchronizable<TValue>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type SynchronizableRef<TValue = unknown> = {
|
|
20
|
+
__synchronizableRef: true;
|
|
21
|
+
__nativeStateSynchronizableJSRef: TValue;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type Synchronizable<TValue = unknown> = SerializableRef<TValue> &
|
|
25
|
+
SynchronizableRef<TValue> & {
|
|
26
|
+
__synchronizableRef: true;
|
|
27
|
+
getDirty(): TValue;
|
|
28
|
+
getBlocking(): TValue;
|
|
29
|
+
setBlocking(value: TValue | ((prev: TValue) => TValue)): void;
|
|
30
|
+
lock(): void;
|
|
31
|
+
unlock(): void;
|
|
32
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import { createSerializable } from './serializable';
|
|
4
|
+
import type { Synchronizable, SynchronizableRef } from './synchronizable';
|
|
5
|
+
|
|
6
|
+
export function __installUnpacker() {
|
|
7
|
+
// TODO: Add cache for synchronizables.
|
|
8
|
+
const serializer =
|
|
9
|
+
!globalThis._WORKLET || globalThis._WORKLETS_BUNDLE_MODE
|
|
10
|
+
? (value: unknown, _: unknown) => createSerializable(value)
|
|
11
|
+
: globalThis._createSerializable;
|
|
12
|
+
|
|
13
|
+
function synchronizableUnpacker<TValue>(
|
|
14
|
+
synchronizableRef: SynchronizableRef<TValue>
|
|
15
|
+
): Synchronizable<TValue> {
|
|
16
|
+
const synchronizable =
|
|
17
|
+
synchronizableRef as unknown as Synchronizable<TValue>;
|
|
18
|
+
const proxy = globalThis.__workletsModuleProxy!;
|
|
19
|
+
|
|
20
|
+
synchronizable.__synchronizableRef = true;
|
|
21
|
+
synchronizable.getDirty = () => {
|
|
22
|
+
return proxy.synchronizableGetDirty(synchronizable);
|
|
23
|
+
};
|
|
24
|
+
synchronizable.getBlocking = () => {
|
|
25
|
+
return proxy.synchronizableGetBlocking(synchronizable);
|
|
26
|
+
};
|
|
27
|
+
synchronizable.setBlocking = (
|
|
28
|
+
valueOrFunction: TValue | ((prev: TValue) => TValue)
|
|
29
|
+
) => {
|
|
30
|
+
let newValue: TValue;
|
|
31
|
+
if (typeof valueOrFunction === 'function') {
|
|
32
|
+
const func = valueOrFunction as (prev: TValue) => TValue;
|
|
33
|
+
synchronizable.lock();
|
|
34
|
+
const prev = synchronizable.getBlocking();
|
|
35
|
+
newValue = func(prev);
|
|
36
|
+
|
|
37
|
+
proxy.synchronizableSetBlocking(
|
|
38
|
+
synchronizable,
|
|
39
|
+
serializer(newValue, undefined)
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
synchronizable.unlock();
|
|
43
|
+
} else {
|
|
44
|
+
const value = valueOrFunction;
|
|
45
|
+
newValue = value;
|
|
46
|
+
proxy.synchronizableSetBlocking(
|
|
47
|
+
synchronizable,
|
|
48
|
+
serializer(newValue, undefined)
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
synchronizable.lock = () => {
|
|
53
|
+
proxy.synchronizableLock(synchronizable);
|
|
54
|
+
};
|
|
55
|
+
synchronizable.unlock = () => {
|
|
56
|
+
proxy.synchronizableUnlock(synchronizable);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
return synchronizable;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
globalThis.__synchronizableUnpacker = synchronizableUnpacker;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export type SynchronizableUnpacker = <TValue>(
|
|
66
|
+
synchronizableRef: SynchronizableRef<TValue>
|
|
67
|
+
) => Synchronizable<TValue>;
|
package/src/threads.ts
ADDED
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
import { IS_JEST, SHOULD_BE_USE_WEB } from './PlatformChecker';
|
|
3
|
+
import { RuntimeKind } from './runtimeKind';
|
|
4
|
+
import {
|
|
5
|
+
createSerializable,
|
|
6
|
+
makeShareableCloneOnUIRecursive,
|
|
7
|
+
} from './serializable';
|
|
8
|
+
import { serializableMappingCache } from './serializableMappingCache';
|
|
9
|
+
import { isWorkletFunction } from './workletFunction';
|
|
10
|
+
import { WorkletsError } from './WorkletsError';
|
|
11
|
+
import { WorkletsModule } from './WorkletsModule';
|
|
12
|
+
import type { WorkletFunction, WorkletImport } from './workletTypes';
|
|
13
|
+
|
|
14
|
+
type UIJob<Args extends unknown[] = unknown[], ReturnValue = unknown> = [
|
|
15
|
+
worklet: WorkletFunction<Args, ReturnValue>,
|
|
16
|
+
args: Args,
|
|
17
|
+
resolve?: (value: ReturnValue) => void,
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
let runOnUIQueue: UIJob[] = [];
|
|
21
|
+
|
|
22
|
+
export function setupMicrotasks() {
|
|
23
|
+
'worklet';
|
|
24
|
+
|
|
25
|
+
let microtasksQueue: Array<() => void> = [];
|
|
26
|
+
let isExecutingMicrotasksQueue = false;
|
|
27
|
+
global.queueMicrotask = (callback: () => void) => {
|
|
28
|
+
microtasksQueue.push(callback);
|
|
29
|
+
};
|
|
30
|
+
global._microtaskQueueFinalizers = [];
|
|
31
|
+
|
|
32
|
+
global.__callMicrotasks = () => {
|
|
33
|
+
if (isExecutingMicrotasksQueue) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
isExecutingMicrotasksQueue = true;
|
|
38
|
+
for (let index = 0; index < microtasksQueue.length; index += 1) {
|
|
39
|
+
// we use classic 'for' loop because the size of the currentTasks array may change while executing some of the callbacks due to queueMicrotask calls
|
|
40
|
+
microtasksQueue[index]();
|
|
41
|
+
}
|
|
42
|
+
microtasksQueue = [];
|
|
43
|
+
global._microtaskQueueFinalizers.forEach((finalizer) => finalizer());
|
|
44
|
+
} finally {
|
|
45
|
+
isExecutingMicrotasksQueue = false;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function callMicrotasksOnUIThread() {
|
|
51
|
+
'worklet';
|
|
52
|
+
global.__callMicrotasks();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const callMicrotasks = SHOULD_BE_USE_WEB
|
|
56
|
+
? () => {
|
|
57
|
+
// on web flushing is a noop as immediates are handled by the browser
|
|
58
|
+
}
|
|
59
|
+
: callMicrotasksOnUIThread;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Lets you schedule a function to be executed on the [UI
|
|
63
|
+
* Runtime](https://docs.swmansion.com/react-native-worklets/docs/fundamentals/glossary#ui-runtime).
|
|
64
|
+
*
|
|
65
|
+
* - The callback executes asynchronously and doesn't return a value.
|
|
66
|
+
* - Passed function and args are automatically
|
|
67
|
+
* [workletized](https://docs.swmansion.com/react-native-worklets/docs/fundamentals/glossary#to-workletize)
|
|
68
|
+
* and serialized.
|
|
69
|
+
* - This function cannot be called from the [UI
|
|
70
|
+
* Runtime](https://docs.swmansion.com/react-native-worklets/docs/fundamentals/glossary#ui-runtime)
|
|
71
|
+
* or [Worker
|
|
72
|
+
* Runtime](https://docs.swmansion.com/react-native-worklets/docs/fundamentals/glossary#worker-worklet-runtime---worker-runtime),
|
|
73
|
+
* unless you have the [Bundle Mode](/docs/experimental/bundleMode) enabled.
|
|
74
|
+
*
|
|
75
|
+
* @param fun - A reference to a function you want to schedule on the [UI
|
|
76
|
+
* Runtime](https://docs.swmansion.com/react-native-worklets/docs/fundamentals/glossary#ui-runtime).
|
|
77
|
+
* @param args - Arguments to pass to the function.
|
|
78
|
+
* @see https://docs.swmansion.com/react-native-worklets/docs/threading/scheduleOnUI
|
|
79
|
+
*/
|
|
80
|
+
export function scheduleOnUI<Args extends unknown[], ReturnValue>(
|
|
81
|
+
worklet: (...args: Args) => ReturnValue,
|
|
82
|
+
...args: Args
|
|
83
|
+
): void {
|
|
84
|
+
runOnUI(worklet)(...args);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Lets you asynchronously run
|
|
89
|
+
* [workletized](https://docs.swmansion.com/react-native-worklets/docs/fundamentals/glossary#to-workletize)
|
|
90
|
+
* functions on the [UI
|
|
91
|
+
* thread](https://docs.swmansion.com/react-native-worklets/docs/threading/runOnUI/).
|
|
92
|
+
*
|
|
93
|
+
* This method does not schedule the work immediately but instead waits for
|
|
94
|
+
* other worklets to be scheduled within the same JS loop. It uses
|
|
95
|
+
* queueMicrotask to schedule all the worklets at once making sure they will run
|
|
96
|
+
* within the same frame boundaries on the UI thread.
|
|
97
|
+
*
|
|
98
|
+
* @param fun - A reference to a function you want to execute on the [UI
|
|
99
|
+
* thread](https://docs.swmansion.com/react-native-worklets/docs/threading/runOnUI/)
|
|
100
|
+
* from the [JavaScript
|
|
101
|
+
* thread](https://docs.swmansion.com/react-native-worklets/docs/threading/runOnUI/).
|
|
102
|
+
* @returns A function that accepts arguments for the function passed as the
|
|
103
|
+
* first argument.
|
|
104
|
+
* @see https://docs.swmansion.com/react-native-worklets/docs/threading/runOnUI @deprecated Use `scheduleOnUI` instead.
|
|
105
|
+
*/
|
|
106
|
+
// @ts-expect-error This overload is correct since it's what user sees in his code
|
|
107
|
+
// before it's transformed by Reanimated Babel plugin.
|
|
108
|
+
export function runOnUI<Args extends unknown[], ReturnValue>(
|
|
109
|
+
worklet: (...args: Args) => ReturnValue
|
|
110
|
+
): (...args: Args) => void;
|
|
111
|
+
|
|
112
|
+
export function runOnUI<Args extends unknown[], ReturnValue>(
|
|
113
|
+
worklet: WorkletFunction<Args, ReturnValue>
|
|
114
|
+
): (...args: Args) => void {
|
|
115
|
+
if (
|
|
116
|
+
__DEV__ &&
|
|
117
|
+
!SHOULD_BE_USE_WEB &&
|
|
118
|
+
!isWorkletFunction(worklet) &&
|
|
119
|
+
!(worklet as unknown as WorkletImport).__bundleData
|
|
120
|
+
) {
|
|
121
|
+
throw new WorkletsError('`runOnUI` can only be used with worklets.');
|
|
122
|
+
}
|
|
123
|
+
return (...args) => {
|
|
124
|
+
if (IS_JEST) {
|
|
125
|
+
// Mocking time in Jest is tricky as both requestAnimationFrame and queueMicrotask
|
|
126
|
+
// callbacks run on the same queue and can be interleaved. There is no way
|
|
127
|
+
// to flush particular queue in Jest and the only control over mocked timers
|
|
128
|
+
// is by using jest.advanceTimersByTime() method which advances all types
|
|
129
|
+
// of timers including immediate and animation callbacks. Ideally we'd like
|
|
130
|
+
// to have some way here to schedule work along with React updates, but
|
|
131
|
+
// that's not possible, and hence in Jest environment instead of using scheduling
|
|
132
|
+
// mechanism we just schedule the work ommiting the queue. This is ok for the
|
|
133
|
+
// uses that we currently have but may not be ok for future tests that we write.
|
|
134
|
+
WorkletsModule.scheduleOnUI(
|
|
135
|
+
createSerializable(() => {
|
|
136
|
+
'worklet';
|
|
137
|
+
worklet(...args);
|
|
138
|
+
})
|
|
139
|
+
);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (__DEV__) {
|
|
143
|
+
// in DEV mode we call serializable conversion here because in case the object
|
|
144
|
+
// can't be converted, we will get a meaningful stack-trace as opposed to the
|
|
145
|
+
// situation when conversion is only done via microtask queue. This does not
|
|
146
|
+
// make the app particularily less efficient as converted objects are cached
|
|
147
|
+
// and for a given worklet the conversion only happens once.
|
|
148
|
+
createSerializable(worklet);
|
|
149
|
+
createSerializable(args);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
enqueueUI(worklet, args);
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (__DEV__) {
|
|
157
|
+
function runOnUIWorklet(): void {
|
|
158
|
+
'worklet';
|
|
159
|
+
throw new WorkletsError(
|
|
160
|
+
'`runOnUI` cannot be called on the UI runtime. Please call the function synchronously or use `queueMicrotask` or `requestAnimationFrame` instead.'
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const serializableRunOnUIWorklet = createSerializable(runOnUIWorklet);
|
|
165
|
+
serializableMappingCache.set(runOnUI, serializableRunOnUIWorklet);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Lets you run a function synchronously on the [UI
|
|
170
|
+
* Runtime](https://docs.swmansion.com/react-native-worklets/docs/fundamentals/glossary#ui-runtime)
|
|
171
|
+
* from the [RN
|
|
172
|
+
* Runtime](https://docs.swmansion.com/react-native-worklets/docs/fundamentals/glossary#react-native-runtime-rn-runtime).
|
|
173
|
+
* Passed function and args are automatically
|
|
174
|
+
* [workletized](https://docs.swmansion.com/react-native-worklets/docs/fundamentals/glossary#to-workletize)
|
|
175
|
+
* and serialized.
|
|
176
|
+
*
|
|
177
|
+
* - This function cannot be called from the [UI
|
|
178
|
+
* Runtime](https://docs.swmansion.com/react-native-worklets/docs/fundamentals/glossary#ui-runtime).
|
|
179
|
+
* - This function cannot be called from a [Worker
|
|
180
|
+
* Runtime](https://docs.swmansion.com/react-native-worklets/docs/fundamentals/glossary#worker-worklet-runtime---worker-runtime).
|
|
181
|
+
*
|
|
182
|
+
* @param fun - A reference to a function you want to execute on the [UI
|
|
183
|
+
* Runtime](https://docs.swmansion.com/react-native-worklets/docs/fundamentals/glossary#ui-runtime).
|
|
184
|
+
* @param args - Arguments to pass to the function.
|
|
185
|
+
* @returns The return value of the function passed as the first argument.
|
|
186
|
+
* @see https://docs.swmansion.com/react-native-worklets/docs/threading/runOnUISync
|
|
187
|
+
*/
|
|
188
|
+
export function runOnUISync<Args extends unknown[], ReturnValue>(
|
|
189
|
+
worklet: (...args: Args) => ReturnValue,
|
|
190
|
+
...args: Args
|
|
191
|
+
): ReturnValue {
|
|
192
|
+
return executeOnUIRuntimeSync(worklet)(...args);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// @ts-expect-error Check `executeOnUIRuntimeSync` overload above.
|
|
196
|
+
export function executeOnUIRuntimeSync<Args extends unknown[], ReturnValue>(
|
|
197
|
+
worklet: (...args: Args) => ReturnValue
|
|
198
|
+
): (...args: Args) => ReturnValue;
|
|
199
|
+
|
|
200
|
+
export function executeOnUIRuntimeSync<Args extends unknown[], ReturnValue>(
|
|
201
|
+
worklet: WorkletFunction<Args, ReturnValue>
|
|
202
|
+
): (...args: Args) => ReturnValue {
|
|
203
|
+
return (...args) => {
|
|
204
|
+
return WorkletsModule.executeOnUIRuntimeSync(
|
|
205
|
+
createSerializable(() => {
|
|
206
|
+
'worklet';
|
|
207
|
+
const result = worklet(...args);
|
|
208
|
+
return makeShareableCloneOnUIRecursive(result);
|
|
209
|
+
})
|
|
210
|
+
);
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
type ReleaseRemoteFunction<Args extends unknown[], ReturnValue> = {
|
|
215
|
+
(...args: Args): ReturnValue;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
type DevRemoteFunction<Args extends unknown[], ReturnValue> = {
|
|
219
|
+
__remoteFunction: (...args: Args) => ReturnValue;
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
type RemoteFunction<Args extends unknown[], ReturnValue> =
|
|
223
|
+
| ReleaseRemoteFunction<Args, ReturnValue>
|
|
224
|
+
| DevRemoteFunction<Args, ReturnValue>;
|
|
225
|
+
|
|
226
|
+
function runWorkletOnJS<Args extends unknown[], ReturnValue>(
|
|
227
|
+
worklet: WorkletFunction<Args, ReturnValue>,
|
|
228
|
+
...args: Args
|
|
229
|
+
): void {
|
|
230
|
+
// remote function that calls a worklet synchronously on the JS runtime
|
|
231
|
+
worklet(...args);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Lets you asynchronously run
|
|
236
|
+
* non-[workletized](https://docs.swmansion.com/react-native-worklets/docs/fundamentals/glossary#to-workletize)
|
|
237
|
+
* functions that couldn't otherwise run on the [UI
|
|
238
|
+
* thread](https://docs.swmansion.com/react-native-worklets/docs/fundamentals/glossary#ui-thread).
|
|
239
|
+
* This applies to most external libraries as they don't have their functions
|
|
240
|
+
* marked with "worklet"; directive.
|
|
241
|
+
*
|
|
242
|
+
* @param fun - A reference to a function you want to execute on the JavaScript
|
|
243
|
+
* thread from the UI thread.
|
|
244
|
+
* @returns A function that accepts arguments for the function passed as the
|
|
245
|
+
* first argument.
|
|
246
|
+
* @see https://docs.swmansion.com/react-native-worklets/docs/threading/runOnJS
|
|
247
|
+
*/
|
|
248
|
+
/** @deprecated Use `scheduleOnRN` instead. */
|
|
249
|
+
export function runOnJS<Args extends unknown[], ReturnValue>(
|
|
250
|
+
fun:
|
|
251
|
+
| ((...args: Args) => ReturnValue)
|
|
252
|
+
| RemoteFunction<Args, ReturnValue>
|
|
253
|
+
| WorkletFunction<Args, ReturnValue>
|
|
254
|
+
): (...args: Args) => void {
|
|
255
|
+
'worklet';
|
|
256
|
+
type FunDevRemote = Extract<typeof fun, DevRemoteFunction<Args, ReturnValue>>;
|
|
257
|
+
if (
|
|
258
|
+
SHOULD_BE_USE_WEB ||
|
|
259
|
+
globalThis.__RUNTIME_KIND === RuntimeKind.ReactNative
|
|
260
|
+
) {
|
|
261
|
+
// if we are already on the JS thread, we just schedule the worklet on the JS queue
|
|
262
|
+
return (...args) =>
|
|
263
|
+
queueMicrotask(
|
|
264
|
+
args.length
|
|
265
|
+
? () => (fun as (...args: Args) => ReturnValue)(...args)
|
|
266
|
+
: (fun as () => ReturnValue)
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
if (isWorkletFunction<Args, ReturnValue>(fun)) {
|
|
270
|
+
// If `fun` is a worklet, we schedule a call of a remote function `runWorkletOnJS`
|
|
271
|
+
// and pass the worklet as a first argument followed by original arguments.
|
|
272
|
+
|
|
273
|
+
return (...args) =>
|
|
274
|
+
runOnJS(runWorkletOnJS<Args, ReturnValue>)(
|
|
275
|
+
fun as WorkletFunction<Args, ReturnValue>,
|
|
276
|
+
...args
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
if ((fun as FunDevRemote).__remoteFunction) {
|
|
280
|
+
// In development mode the function provided as `fun` throws an error message
|
|
281
|
+
// such that when someone accidentally calls it directly on the UI runtime, they
|
|
282
|
+
// see that they should use `runOnJS` instead. To facilitate that we put the
|
|
283
|
+
// reference to the original remote function in the `__remoteFunction` property.
|
|
284
|
+
fun = (fun as FunDevRemote).__remoteFunction;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const scheduleOnJS =
|
|
288
|
+
typeof fun === 'function'
|
|
289
|
+
? global._scheduleHostFunctionOnJS
|
|
290
|
+
: global._scheduleRemoteFunctionOnJS;
|
|
291
|
+
|
|
292
|
+
return (...args) => {
|
|
293
|
+
scheduleOnJS(
|
|
294
|
+
fun as
|
|
295
|
+
| ((...args: Args) => ReturnValue)
|
|
296
|
+
| WorkletFunction<Args, ReturnValue>,
|
|
297
|
+
args.length > 0 ? makeShareableCloneOnUIRecursive(args) : undefined
|
|
298
|
+
);
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Lets you schedule a function to be executed on the RN runtime from any
|
|
304
|
+
* runtime. Check
|
|
305
|
+
* {@link https://docs.swmansion.com/react-native-worklets/docs/fundamentals/runtimeKinds}
|
|
306
|
+
* for more information about the different runtime kinds.
|
|
307
|
+
*
|
|
308
|
+
* Scheduling function from the RN Runtime (we are already on RN Runtime) simply
|
|
309
|
+
* uses `queueMicrotask`.
|
|
310
|
+
*
|
|
311
|
+
* When functions need to be scheduled from the UI Runtime, first function and
|
|
312
|
+
* args are serialized and then the system passes the scheduling responsibility
|
|
313
|
+
* to the JSScheduler. The JSScheduler then uses the RN CallInvoker to schedule
|
|
314
|
+
* the function asynchronously on the JavaScript thread by calling
|
|
315
|
+
* `jsCallInvoker_->invokeAsync()`.
|
|
316
|
+
*
|
|
317
|
+
* When called from a Worker Runtime, it uses the same JSScheduler mechanism.
|
|
318
|
+
*
|
|
319
|
+
* @param fun - A function you want to schedule on the RN runtime. A function
|
|
320
|
+
* can be a worklet, a remote function or a regular function.
|
|
321
|
+
* @param args - Arguments to pass to the function.
|
|
322
|
+
* @see https://docs.swmansion.com/react-native-worklets/docs/threading/scheduleOnRN
|
|
323
|
+
*/
|
|
324
|
+
export function scheduleOnRN<Args extends unknown[], ReturnValue>(
|
|
325
|
+
fun:
|
|
326
|
+
| ((...args: Args) => ReturnValue)
|
|
327
|
+
| RemoteFunction<Args, ReturnValue>
|
|
328
|
+
| WorkletFunction<Args, ReturnValue>,
|
|
329
|
+
...args: Args
|
|
330
|
+
): void {
|
|
331
|
+
'worklet';
|
|
332
|
+
runOnJS(fun)(...args);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Lets you asynchronously run
|
|
337
|
+
* [workletized](https://docs.swmansion.com/react-native-worklets/docs/fundamentals/glossary#to-workletize)
|
|
338
|
+
* functions on the [UI
|
|
339
|
+
* Runtime](https://docs.swmansion.com/react-native-worklets/docs/fundamentals/glossary#ui-runtime).
|
|
340
|
+
*
|
|
341
|
+
* This method does not schedule the work immediately but instead waits for
|
|
342
|
+
* other worklets to be scheduled within the same JS loop. It uses
|
|
343
|
+
* queueMicrotask to schedule all the worklets at once making sure they will run
|
|
344
|
+
* within the same frame boundaries on the UI thread.
|
|
345
|
+
*
|
|
346
|
+
* @param fun - A reference to a function you want to execute on the [UI
|
|
347
|
+
* Runtime](https://docs.swmansion.com/react-native-worklets/docs/fundamentals/glossary#ui-runtime).
|
|
348
|
+
* from the [JavaScript
|
|
349
|
+
* Runtime](https://docs.swmansion.com/react-native-worklets/docs/fundamentals/glossary#javascript-runtime).
|
|
350
|
+
* @returns A promise that resolves to the return value of the function passed
|
|
351
|
+
* as the first argument.
|
|
352
|
+
* @see https://docs.swmansion.com/react-native-worklets/docs/threading/runOnUIAsync
|
|
353
|
+
*/
|
|
354
|
+
export function runOnUIAsync<Args extends unknown[], ReturnValue>(
|
|
355
|
+
worklet: (...args: Args) => ReturnValue
|
|
356
|
+
): (...args: Args) => Promise<ReturnValue> {
|
|
357
|
+
if (__DEV__ && !SHOULD_BE_USE_WEB && !isWorkletFunction(worklet)) {
|
|
358
|
+
throw new WorkletsError('`runOnUIAsync` can only be used with worklets.');
|
|
359
|
+
}
|
|
360
|
+
return (...args: Args) => {
|
|
361
|
+
return new Promise<ReturnValue>((resolve) => {
|
|
362
|
+
if (IS_JEST) {
|
|
363
|
+
// Mocking time in Jest is tricky as both requestAnimationFrame and queueMicrotask
|
|
364
|
+
// callbacks run on the same queue and can be interleaved. There is no way
|
|
365
|
+
// to flush particular queue in Jest and the only control over mocked timers
|
|
366
|
+
// is by using jest.advanceTimersByTime() method which advances all types
|
|
367
|
+
// of timers including immediate and animation callbacks. Ideally we'd like
|
|
368
|
+
// to have some way here to schedule work along with React updates, but
|
|
369
|
+
// that's not possible, and hence in Jest environment instead of using scheduling
|
|
370
|
+
// mechanism we just schedule the work ommiting the queue. This is ok for the
|
|
371
|
+
// uses that we currently have but may not be ok for future tests that we write.
|
|
372
|
+
WorkletsModule.scheduleOnUI(
|
|
373
|
+
createSerializable(() => {
|
|
374
|
+
'worklet';
|
|
375
|
+
worklet(...args);
|
|
376
|
+
})
|
|
377
|
+
);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
if (__DEV__) {
|
|
381
|
+
// in DEV mode we call serializable conversion here because in case the object
|
|
382
|
+
// can't be converted, we will get a meaningful stack-trace as opposed to the
|
|
383
|
+
// situation when conversion is only done via microtask queue. This does not
|
|
384
|
+
// make the app particularily less efficient as converted objects are cached
|
|
385
|
+
// and for a given worklet the conversion only happens once.
|
|
386
|
+
createSerializable(worklet);
|
|
387
|
+
createSerializable(args);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
enqueueUI(worklet as WorkletFunction<Args, ReturnValue>, args, resolve);
|
|
391
|
+
});
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (__DEV__) {
|
|
396
|
+
function runOnUIAsyncWorklet(): void {
|
|
397
|
+
'worklet';
|
|
398
|
+
throw new WorkletsError(
|
|
399
|
+
'`runOnUIAsync` cannot be called on the UI runtime. Please call the function synchronously or use `queueMicrotask` or `requestAnimationFrame` instead.'
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
const serializableRunOnUIAsyncWorklet =
|
|
404
|
+
createSerializable(runOnUIAsyncWorklet);
|
|
405
|
+
serializableMappingCache.set(runOnUIAsync, serializableRunOnUIAsyncWorklet);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function enqueueUI<Args extends unknown[], ReturnValue>(
|
|
409
|
+
worklet: WorkletFunction<Args, ReturnValue>,
|
|
410
|
+
args: Args,
|
|
411
|
+
resolve?: (value: ReturnValue) => void
|
|
412
|
+
): void {
|
|
413
|
+
const job = [worklet, args, resolve] as UIJob<Args, ReturnValue>;
|
|
414
|
+
runOnUIQueue.push(job as unknown as UIJob);
|
|
415
|
+
if (runOnUIQueue.length === 1) {
|
|
416
|
+
flushUIQueue();
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
function flushUIQueue(): void {
|
|
421
|
+
queueMicrotask(() => {
|
|
422
|
+
const queue = runOnUIQueue;
|
|
423
|
+
runOnUIQueue = [];
|
|
424
|
+
WorkletsModule.scheduleOnUI(
|
|
425
|
+
createSerializable(() => {
|
|
426
|
+
'worklet';
|
|
427
|
+
queue.forEach(([workletFunction, workletArgs, jobResolve]) => {
|
|
428
|
+
const result = workletFunction(...workletArgs);
|
|
429
|
+
if (jobResolve) {
|
|
430
|
+
runOnJS(jobResolve)(result);
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
callMicrotasks();
|
|
434
|
+
})
|
|
435
|
+
);
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Added temporarily for integration with `react-native-audio-api`. Don't depend
|
|
441
|
+
* on this API as it may change without notice.
|
|
442
|
+
*/
|
|
443
|
+
// eslint-disable-next-line camelcase
|
|
444
|
+
export function unstable_eventLoopTask<TArgs extends unknown[], TRet>(
|
|
445
|
+
worklet: (...args: TArgs) => TRet
|
|
446
|
+
) {
|
|
447
|
+
return (...args: TArgs) => {
|
|
448
|
+
'worklet';
|
|
449
|
+
worklet(...args);
|
|
450
|
+
callMicrotasks();
|
|
451
|
+
};
|
|
452
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
import { logger } from '../logger';
|
|
3
|
+
import { WorkletsError } from '../WorkletsError';
|
|
4
|
+
import { jsVersion } from './jsVersion';
|
|
5
|
+
|
|
6
|
+
export function checkCppVersion() {
|
|
7
|
+
const cppVersion = global._WORKLETS_VERSION_CPP;
|
|
8
|
+
if (cppVersion === undefined) {
|
|
9
|
+
logger.warn(
|
|
10
|
+
`Couldn't determine the version of the native part of Worklets.
|
|
11
|
+
See \`https://docs.swmansion.com/react-native-worklets/docs/guides/troubleshooting#couldnt-determine-the-version-of-the-native-part-of-worklets\` for more details.`
|
|
12
|
+
);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const ok = matchVersion(jsVersion, cppVersion);
|
|
16
|
+
if (!ok) {
|
|
17
|
+
throw new WorkletsError(
|
|
18
|
+
`Mismatch between JavaScript part and native part of Worklets (${jsVersion} vs ${cppVersion}).
|
|
19
|
+
See \`https://docs.swmansion.com/react-native-worklets/docs/guides/troubleshooting#mismatch-between-javascript-part-and-native-part-of-worklets\` for more details.`
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function matchVersion(version1: string, version2: string) {
|
|
25
|
+
if (version1.match(/^\d+\.\d+\.\d+$/) && version2.match(/^\d+\.\d+\.\d+$/)) {
|
|
26
|
+
// x.y.z, compare only major and minor, skip patch
|
|
27
|
+
const [major1, minor1] = version1.split('.');
|
|
28
|
+
const [major2, minor2] = version2.split('.');
|
|
29
|
+
return major1 === major2 && minor1 === minor2;
|
|
30
|
+
} else {
|
|
31
|
+
// alpha, beta or rc, compare everything
|
|
32
|
+
return version1 === version2;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* We hardcode the version of Worklets here in order to compare it with the
|
|
4
|
+
* version used to build the native part of the library in runtime. Remember to
|
|
5
|
+
* keep this in sync with the version declared in `package.json`
|
|
6
|
+
*/
|
|
7
|
+
export const jsVersion = '1.0.0+0.6.0';
|