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,71 @@
|
|
|
1
|
+
#include <jsi/jsi.h>
|
|
2
|
+
#include <worklets/AnimationFrameQueue/AnimationFrameBatchinator.h>
|
|
3
|
+
#include <worklets/SharedItems/Shareables.h>
|
|
4
|
+
|
|
5
|
+
#include <atomic>
|
|
6
|
+
#include <functional>
|
|
7
|
+
#include <memory>
|
|
8
|
+
#include <mutex>
|
|
9
|
+
#include <utility>
|
|
10
|
+
#include <vector>
|
|
11
|
+
|
|
12
|
+
namespace worklets {
|
|
13
|
+
|
|
14
|
+
void AnimationFrameBatchinator::addToBatch(
|
|
15
|
+
const facebook::jsi::Value &callback) {
|
|
16
|
+
{
|
|
17
|
+
std::lock_guard<std::mutex> lock(callbacksMutex_);
|
|
18
|
+
callbacks_.push_back(
|
|
19
|
+
std::make_shared<const facebook::jsi::Value>(*uiRuntime_, callback));
|
|
20
|
+
}
|
|
21
|
+
flush();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
AnimationFrameBatchinator::JsiRequestAnimationFrame
|
|
25
|
+
AnimationFrameBatchinator::getJsiRequestAnimationFrame() {
|
|
26
|
+
return [weakThis = weak_from_this()](
|
|
27
|
+
facebook::jsi::Runtime &rt, const facebook::jsi::Value &callback) {
|
|
28
|
+
const auto strongThis = weakThis.lock();
|
|
29
|
+
if (!strongThis) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
strongThis->addToBatch(callback);
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
void AnimationFrameBatchinator::flush() {
|
|
38
|
+
if (flushRequested_.exchange(true)) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
requestAnimationFrame_([weakThis = weak_from_this()](double timestampMs) {
|
|
43
|
+
const auto strongThis = weakThis.lock();
|
|
44
|
+
if (!strongThis) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
auto callbacks = strongThis->pullCallbacks();
|
|
49
|
+
strongThis->flushRequested_ = false;
|
|
50
|
+
|
|
51
|
+
auto &uiRuntime = *(strongThis->uiRuntime_);
|
|
52
|
+
for (const auto &callback : callbacks) {
|
|
53
|
+
runOnRuntimeGuarded(uiRuntime, *callback, timestampMs);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
std::vector<std::shared_ptr<const facebook::jsi::Value>>
|
|
59
|
+
AnimationFrameBatchinator::pullCallbacks() {
|
|
60
|
+
std::lock_guard<std::mutex> lock(callbacksMutex_);
|
|
61
|
+
return std::move(callbacks_);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
AnimationFrameBatchinator::AnimationFrameBatchinator(
|
|
65
|
+
facebook::jsi::Runtime &uiRuntime,
|
|
66
|
+
std::function<void(std::function<void(const double)>)>
|
|
67
|
+
&&forwardedRequestAnimationFrame)
|
|
68
|
+
: uiRuntime_(&uiRuntime),
|
|
69
|
+
requestAnimationFrame_(std::move(forwardedRequestAnimationFrame)) {}
|
|
70
|
+
|
|
71
|
+
} // namespace worklets
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <jsi/jsi.h>
|
|
4
|
+
#include <worklets/SharedItems/Shareables.h>
|
|
5
|
+
#include <atomic>
|
|
6
|
+
#include <functional>
|
|
7
|
+
#include <memory>
|
|
8
|
+
#include <mutex>
|
|
9
|
+
#include <vector>
|
|
10
|
+
|
|
11
|
+
namespace worklets {
|
|
12
|
+
|
|
13
|
+
class AnimationFrameBatchinator
|
|
14
|
+
: public std::enable_shared_from_this<AnimationFrameBatchinator> {
|
|
15
|
+
public:
|
|
16
|
+
using JsiRequestAnimationFrame = std::function<
|
|
17
|
+
void(facebook::jsi::Runtime &, const facebook::jsi::Value &)>;
|
|
18
|
+
|
|
19
|
+
void addToBatch(const facebook::jsi::Value &callback);
|
|
20
|
+
JsiRequestAnimationFrame getJsiRequestAnimationFrame();
|
|
21
|
+
|
|
22
|
+
AnimationFrameBatchinator(
|
|
23
|
+
facebook::jsi::Runtime &uiRuntime,
|
|
24
|
+
std::function<void(std::function<void(const double)>)>
|
|
25
|
+
&&forwardedRequestAnimationFrame);
|
|
26
|
+
|
|
27
|
+
private:
|
|
28
|
+
void flush();
|
|
29
|
+
std::vector<std::shared_ptr<const facebook::jsi::Value>> pullCallbacks();
|
|
30
|
+
|
|
31
|
+
std::vector<std::shared_ptr<const facebook::jsi::Value>> callbacks_{};
|
|
32
|
+
std::mutex callbacksMutex_{};
|
|
33
|
+
std::atomic_bool flushRequested_{};
|
|
34
|
+
facebook::jsi::Runtime *uiRuntime_;
|
|
35
|
+
std::function<void(std::function<void(const double)>)> requestAnimationFrame_;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
} // namespace worklets
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
#include <memory>
|
|
2
|
+
#include <string>
|
|
3
|
+
#include <utility>
|
|
4
|
+
|
|
5
|
+
#include <react/renderer/uimanager/UIManagerBinding.h>
|
|
6
|
+
#include <react/renderer/uimanager/primitives.h>
|
|
7
|
+
|
|
8
|
+
#include <worklets/NativeModules/WorkletsModuleProxy.h>
|
|
9
|
+
#include <worklets/SharedItems/Shareables.h>
|
|
10
|
+
#include <worklets/Tools/Defs.h>
|
|
11
|
+
#include <worklets/WorkletRuntime/UIRuntimeDecorator.h>
|
|
12
|
+
|
|
13
|
+
#ifdef __ANDROID__
|
|
14
|
+
#include <fbjni/fbjni.h>
|
|
15
|
+
#endif // __ANDROID__
|
|
16
|
+
|
|
17
|
+
#include <jsi/jsi.h>
|
|
18
|
+
|
|
19
|
+
using namespace facebook;
|
|
20
|
+
|
|
21
|
+
namespace worklets {
|
|
22
|
+
|
|
23
|
+
WorkletsModuleProxy::WorkletsModuleProxy(
|
|
24
|
+
jsi::Runtime &rnRuntime,
|
|
25
|
+
const std::string &valueUnpackerCode,
|
|
26
|
+
const std::shared_ptr<MessageQueueThread> &jsQueue,
|
|
27
|
+
const std::shared_ptr<CallInvoker> &jsCallInvoker,
|
|
28
|
+
const std::shared_ptr<JSScheduler> &jsScheduler,
|
|
29
|
+
const std::shared_ptr<UIScheduler> &uiScheduler,
|
|
30
|
+
std::function<void(std::function<void(const double)>)>
|
|
31
|
+
&&forwardedRequestAnimationFrame)
|
|
32
|
+
: WorkletsModuleProxySpec(jsCallInvoker),
|
|
33
|
+
valueUnpackerCode_(valueUnpackerCode),
|
|
34
|
+
jsQueue_(jsQueue),
|
|
35
|
+
jsScheduler_(jsScheduler),
|
|
36
|
+
uiScheduler_(uiScheduler),
|
|
37
|
+
uiWorkletRuntime_(std::make_shared<WorkletRuntime>(
|
|
38
|
+
rnRuntime,
|
|
39
|
+
jsQueue,
|
|
40
|
+
jsScheduler,
|
|
41
|
+
"Reanimated UI runtime",
|
|
42
|
+
true /* supportsLocking */,
|
|
43
|
+
valueUnpackerCode_)),
|
|
44
|
+
animationFrameBatchinator_(std::make_shared<AnimationFrameBatchinator>(
|
|
45
|
+
uiWorkletRuntime_->getJSIRuntime(),
|
|
46
|
+
std::move(forwardedRequestAnimationFrame))) {
|
|
47
|
+
UIRuntimeDecorator::decorate(
|
|
48
|
+
uiWorkletRuntime_->getJSIRuntime(),
|
|
49
|
+
animationFrameBatchinator_->getJsiRequestAnimationFrame());
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
WorkletsModuleProxy::~WorkletsModuleProxy() {
|
|
53
|
+
animationFrameBatchinator_.reset();
|
|
54
|
+
jsQueue_->quitSynchronous();
|
|
55
|
+
uiWorkletRuntime_.reset();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
jsi::Value WorkletsModuleProxy::makeShareableClone(
|
|
59
|
+
jsi::Runtime &rt,
|
|
60
|
+
const jsi::Value &value,
|
|
61
|
+
const jsi::Value &shouldRetainRemote,
|
|
62
|
+
const jsi::Value &nativeStateSource) {
|
|
63
|
+
// TODO: It might be a good idea to rename one of these methods to avoid
|
|
64
|
+
// confusion.
|
|
65
|
+
return worklets::makeShareableClone(
|
|
66
|
+
rt, value, shouldRetainRemote, nativeStateSource);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
void WorkletsModuleProxy::scheduleOnUI(
|
|
70
|
+
jsi::Runtime &rt,
|
|
71
|
+
const jsi::Value &worklet) {
|
|
72
|
+
auto shareableWorklet = extractShareableOrThrow<ShareableWorklet>(
|
|
73
|
+
rt, worklet, "[Worklets] Only worklets can be scheduled to run on UI.");
|
|
74
|
+
uiScheduler_->scheduleOnUI([shareableWorklet, weakThis = weak_from_this()] {
|
|
75
|
+
// This callback can outlive the WorkletsModuleProxy object during the
|
|
76
|
+
// invalidation of React Native. This happens when WorkletsModuleProxy
|
|
77
|
+
// destructor is called on the JS thread and the UI thread is executing
|
|
78
|
+
// callbacks from the `scheduleOnUI` queue. Therefore, we need to
|
|
79
|
+
// make sure it's still alive before we try to access it.
|
|
80
|
+
auto strongThis = weakThis.lock();
|
|
81
|
+
if (!strongThis) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
auto uiWorkletRuntime = strongThis->getUIWorkletRuntime();
|
|
86
|
+
|
|
87
|
+
#if JS_RUNTIME_HERMES
|
|
88
|
+
// JSI's scope defined here allows for JSI-objects to be cleared up
|
|
89
|
+
// after each runtime loop. Within these loops we typically create some
|
|
90
|
+
// temporary JSI objects and hence it allows for such objects to be
|
|
91
|
+
// garbage collected much sooner. Apparently the scope API is only
|
|
92
|
+
// supported on Hermes at the moment.
|
|
93
|
+
const auto scope = jsi::Scope(uiWorkletRuntime->getJSIRuntime());
|
|
94
|
+
#endif // JS_RUNTIME_HERMES
|
|
95
|
+
|
|
96
|
+
uiWorkletRuntime->runGuarded(shareableWorklet);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
jsi::Value WorkletsModuleProxy::executeOnUIRuntimeSync(
|
|
101
|
+
jsi::Runtime &rt,
|
|
102
|
+
const jsi::Value &worklet) {
|
|
103
|
+
return uiWorkletRuntime_->executeSync(rt, worklet);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
jsi::Value WorkletsModuleProxy::createWorkletRuntime(
|
|
107
|
+
jsi::Runtime &rt,
|
|
108
|
+
const jsi::Value &name,
|
|
109
|
+
const jsi::Value &initializer) {
|
|
110
|
+
auto workletRuntime = std::make_shared<WorkletRuntime>(
|
|
111
|
+
rt,
|
|
112
|
+
jsQueue_,
|
|
113
|
+
jsScheduler_,
|
|
114
|
+
name.asString(rt).utf8(rt),
|
|
115
|
+
true /* supportsLocking */,
|
|
116
|
+
valueUnpackerCode_);
|
|
117
|
+
auto initializerShareable = extractShareableOrThrow<ShareableWorklet>(
|
|
118
|
+
rt, initializer, "[Worklets] Initializer must be a worklet.");
|
|
119
|
+
workletRuntime->runGuarded(initializerShareable);
|
|
120
|
+
return jsi::Object::createFromHostObject(rt, workletRuntime);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
jsi::Value WorkletsModuleProxy::scheduleOnRuntime(
|
|
124
|
+
jsi::Runtime &rt,
|
|
125
|
+
const jsi::Value &workletRuntimeValue,
|
|
126
|
+
const jsi::Value &shareableWorkletValue) {
|
|
127
|
+
worklets::scheduleOnRuntime(rt, workletRuntimeValue, shareableWorkletValue);
|
|
128
|
+
return jsi::Value::undefined();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
} // namespace worklets
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <cxxreact/MessageQueueThread.h>
|
|
4
|
+
#include <worklets/AnimationFrameQueue/AnimationFrameBatchinator.h>
|
|
5
|
+
#include <worklets/NativeModules/WorkletsModuleProxySpec.h>
|
|
6
|
+
#include <worklets/Tools/JSScheduler.h>
|
|
7
|
+
#include <worklets/Tools/SingleInstanceChecker.h>
|
|
8
|
+
#include <worklets/Tools/UIScheduler.h>
|
|
9
|
+
#include <worklets/WorkletRuntime/WorkletRuntime.h>
|
|
10
|
+
|
|
11
|
+
#include <memory>
|
|
12
|
+
#include <string>
|
|
13
|
+
|
|
14
|
+
namespace worklets {
|
|
15
|
+
|
|
16
|
+
class WorkletsModuleProxy
|
|
17
|
+
: public WorkletsModuleProxySpec,
|
|
18
|
+
public std::enable_shared_from_this<WorkletsModuleProxy> {
|
|
19
|
+
public:
|
|
20
|
+
explicit WorkletsModuleProxy(
|
|
21
|
+
jsi::Runtime &rnRuntime,
|
|
22
|
+
const std::string &valueUnpackerCode,
|
|
23
|
+
const std::shared_ptr<MessageQueueThread> &jsQueue,
|
|
24
|
+
const std::shared_ptr<CallInvoker> &jsCallInvoker,
|
|
25
|
+
const std::shared_ptr<JSScheduler> &jsScheduler,
|
|
26
|
+
const std::shared_ptr<UIScheduler> &uiScheduler,
|
|
27
|
+
std::function<void(std::function<void(const double)>)>
|
|
28
|
+
&&forwardedRequestAnimationFrame);
|
|
29
|
+
|
|
30
|
+
~WorkletsModuleProxy() override;
|
|
31
|
+
|
|
32
|
+
jsi::Value makeShareableClone(
|
|
33
|
+
jsi::Runtime &rt,
|
|
34
|
+
const jsi::Value &value,
|
|
35
|
+
const jsi::Value &shouldRetainRemote,
|
|
36
|
+
const jsi::Value &nativeStateSource) override;
|
|
37
|
+
|
|
38
|
+
void scheduleOnUI(jsi::Runtime &rt, const jsi::Value &worklet) override;
|
|
39
|
+
|
|
40
|
+
jsi::Value executeOnUIRuntimeSync(jsi::Runtime &rt, const jsi::Value &worklet)
|
|
41
|
+
override;
|
|
42
|
+
|
|
43
|
+
jsi::Value createWorkletRuntime(
|
|
44
|
+
jsi::Runtime &rt,
|
|
45
|
+
const jsi::Value &name,
|
|
46
|
+
const jsi::Value &initializer) override;
|
|
47
|
+
|
|
48
|
+
jsi::Value scheduleOnRuntime(
|
|
49
|
+
jsi::Runtime &rt,
|
|
50
|
+
const jsi::Value &workletRuntimeValue,
|
|
51
|
+
const jsi::Value &shareableWorkletValue) override;
|
|
52
|
+
|
|
53
|
+
[[nodiscard]] inline std::shared_ptr<MessageQueueThread> getJSQueue() const {
|
|
54
|
+
return jsQueue_;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
[[nodiscard]] inline std::shared_ptr<JSScheduler> getJSScheduler() const {
|
|
58
|
+
return jsScheduler_;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
[[nodiscard]] inline std::shared_ptr<UIScheduler> getUIScheduler() const {
|
|
62
|
+
return uiScheduler_;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
[[nodiscard]] inline std::shared_ptr<WorkletRuntime> getUIWorkletRuntime()
|
|
66
|
+
const {
|
|
67
|
+
return uiWorkletRuntime_;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private:
|
|
71
|
+
const std::string valueUnpackerCode_;
|
|
72
|
+
const std::shared_ptr<MessageQueueThread> jsQueue_;
|
|
73
|
+
const std::shared_ptr<JSScheduler> jsScheduler_;
|
|
74
|
+
const std::shared_ptr<UIScheduler> uiScheduler_;
|
|
75
|
+
std::shared_ptr<WorkletRuntime> uiWorkletRuntime_;
|
|
76
|
+
std::shared_ptr<AnimationFrameBatchinator> animationFrameBatchinator_;
|
|
77
|
+
#ifndef NDEBUG
|
|
78
|
+
SingleInstanceChecker<WorkletsModuleProxy> singleInstanceChecker_;
|
|
79
|
+
#endif // NDEBUG
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
} // namespace worklets
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
#include <worklets/NativeModules/WorkletsModuleProxySpec.h>
|
|
2
|
+
|
|
3
|
+
#include <utility>
|
|
4
|
+
|
|
5
|
+
#define WORKLETS_SPEC_PREFIX(FN_NAME) \
|
|
6
|
+
__hostFunction_WorkletsModuleProxySpec_##FN_NAME
|
|
7
|
+
|
|
8
|
+
namespace worklets {
|
|
9
|
+
|
|
10
|
+
static jsi::Value WORKLETS_SPEC_PREFIX(makeShareableClone)(
|
|
11
|
+
jsi::Runtime &rt,
|
|
12
|
+
TurboModule &turboModule,
|
|
13
|
+
const jsi::Value *args,
|
|
14
|
+
size_t) {
|
|
15
|
+
return static_cast<WorkletsModuleProxySpec *>(&turboModule)
|
|
16
|
+
->makeShareableClone(
|
|
17
|
+
rt, std::move(args[0]), std::move(args[1]), std::move(args[2]));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
static jsi::Value WORKLETS_SPEC_PREFIX(scheduleOnUI)(
|
|
21
|
+
jsi::Runtime &rt,
|
|
22
|
+
TurboModule &turboModule,
|
|
23
|
+
const jsi::Value *args,
|
|
24
|
+
size_t) {
|
|
25
|
+
static_cast<WorkletsModuleProxySpec *>(&turboModule)
|
|
26
|
+
->scheduleOnUI(rt, std::move(args[0]));
|
|
27
|
+
return jsi::Value::undefined();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
static jsi::Value WORKLETS_SPEC_PREFIX(executeOnUIRuntimeSync)(
|
|
31
|
+
jsi::Runtime &rt,
|
|
32
|
+
TurboModule &turboModule,
|
|
33
|
+
const jsi::Value *args,
|
|
34
|
+
size_t) {
|
|
35
|
+
return static_cast<WorkletsModuleProxySpec *>(&turboModule)
|
|
36
|
+
->executeOnUIRuntimeSync(rt, std::move(args[0]));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static jsi::Value WORKLETS_SPEC_PREFIX(createWorkletRuntime)(
|
|
40
|
+
jsi::Runtime &rt,
|
|
41
|
+
TurboModule &turboModule,
|
|
42
|
+
const jsi::Value *args,
|
|
43
|
+
size_t) {
|
|
44
|
+
return static_cast<WorkletsModuleProxySpec *>(&turboModule)
|
|
45
|
+
->createWorkletRuntime(rt, std::move(args[0]), std::move(args[1]));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
static jsi::Value WORKLETS_SPEC_PREFIX(scheduleOnRuntime)(
|
|
49
|
+
jsi::Runtime &rt,
|
|
50
|
+
TurboModule &turboModule,
|
|
51
|
+
const jsi::Value *args,
|
|
52
|
+
size_t) {
|
|
53
|
+
return static_cast<WorkletsModuleProxySpec *>(&turboModule)
|
|
54
|
+
->scheduleOnRuntime(rt, std::move(args[0]), std::move(args[1]));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
WorkletsModuleProxySpec::WorkletsModuleProxySpec(
|
|
58
|
+
const std::shared_ptr<CallInvoker> jsInvoker)
|
|
59
|
+
: TurboModule("NativeWorklets", jsInvoker) {
|
|
60
|
+
methodMap_["makeShareableClone"] =
|
|
61
|
+
MethodMetadata{2, WORKLETS_SPEC_PREFIX(makeShareableClone)};
|
|
62
|
+
methodMap_["scheduleOnUI"] =
|
|
63
|
+
MethodMetadata{1, WORKLETS_SPEC_PREFIX(scheduleOnUI)};
|
|
64
|
+
methodMap_["executeOnUIRuntimeSync"] =
|
|
65
|
+
MethodMetadata{1, WORKLETS_SPEC_PREFIX(executeOnUIRuntimeSync)};
|
|
66
|
+
methodMap_["createWorkletRuntime"] =
|
|
67
|
+
MethodMetadata{2, WORKLETS_SPEC_PREFIX(createWorkletRuntime)};
|
|
68
|
+
methodMap_["scheduleOnRuntime"] =
|
|
69
|
+
MethodMetadata{2, WORKLETS_SPEC_PREFIX(scheduleOnRuntime)};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
} // namespace worklets
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <ReactCommon/CallInvoker.h>
|
|
4
|
+
#include <ReactCommon/TurboModule.h>
|
|
5
|
+
#include <memory>
|
|
6
|
+
|
|
7
|
+
using namespace facebook;
|
|
8
|
+
using namespace react;
|
|
9
|
+
|
|
10
|
+
namespace worklets {
|
|
11
|
+
|
|
12
|
+
class JSI_EXPORT WorkletsModuleProxySpec : public TurboModule {
|
|
13
|
+
protected:
|
|
14
|
+
explicit WorkletsModuleProxySpec(
|
|
15
|
+
const std::shared_ptr<CallInvoker> jsInvoker);
|
|
16
|
+
|
|
17
|
+
public:
|
|
18
|
+
// Shareables
|
|
19
|
+
virtual jsi::Value makeShareableClone(
|
|
20
|
+
jsi::Runtime &rt,
|
|
21
|
+
const jsi::Value &value,
|
|
22
|
+
const jsi::Value &shouldRetainRemote,
|
|
23
|
+
const jsi::Value &nativeStateSource) = 0;
|
|
24
|
+
|
|
25
|
+
// Scheduling
|
|
26
|
+
virtual void scheduleOnUI(jsi::Runtime &rt, const jsi::Value &worklet) = 0;
|
|
27
|
+
|
|
28
|
+
virtual jsi::Value executeOnUIRuntimeSync(
|
|
29
|
+
jsi::Runtime &rt,
|
|
30
|
+
const jsi::Value &worklet) = 0;
|
|
31
|
+
|
|
32
|
+
// Worklet runtime
|
|
33
|
+
virtual jsi::Value createWorkletRuntime(
|
|
34
|
+
jsi::Runtime &rt,
|
|
35
|
+
const jsi::Value &name,
|
|
36
|
+
const jsi::Value &initializer) = 0;
|
|
37
|
+
|
|
38
|
+
virtual jsi::Value scheduleOnRuntime(
|
|
39
|
+
jsi::Runtime &rt,
|
|
40
|
+
const jsi::Value &workletRuntimeValue,
|
|
41
|
+
const jsi::Value &shareableWorkletValue) = 0;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
} // namespace worklets
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
#include <worklets/Registries/EventHandlerRegistry.h>
|
|
2
|
+
#include <worklets/Tools/WorkletEventHandler.h>
|
|
3
|
+
|
|
4
|
+
#include <utility>
|
|
5
|
+
#include <vector>
|
|
6
|
+
|
|
7
|
+
namespace worklets {
|
|
8
|
+
|
|
9
|
+
void EventHandlerRegistry::registerEventHandler(
|
|
10
|
+
const std::shared_ptr<WorkletEventHandler> &eventHandler) {
|
|
11
|
+
const std::lock_guard<std::mutex> lock(instanceMutex);
|
|
12
|
+
const auto &eventName = eventHandler->getEventName();
|
|
13
|
+
auto handlerId = eventHandler->getHandlerId();
|
|
14
|
+
|
|
15
|
+
if (eventHandler->shouldIgnoreEmitterReactTag()) {
|
|
16
|
+
eventMappingsWithoutTag[eventName][handlerId] = eventHandler;
|
|
17
|
+
} else {
|
|
18
|
+
const auto emitterReactTag = eventHandler->getEmitterReactTag();
|
|
19
|
+
const auto eventHash = std::make_pair(emitterReactTag, eventName);
|
|
20
|
+
eventMappingsWithTag[eventHash][handlerId] = eventHandler;
|
|
21
|
+
}
|
|
22
|
+
eventHandlers[handlerId] = eventHandler;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
void EventHandlerRegistry::unregisterEventHandler(const uint64_t id) {
|
|
26
|
+
const std::lock_guard<std::mutex> lock(instanceMutex);
|
|
27
|
+
auto handlerIt = eventHandlers.find(id);
|
|
28
|
+
if (handlerIt != eventHandlers.end()) {
|
|
29
|
+
const auto &eventHandler = handlerIt->second;
|
|
30
|
+
const auto &eventName = eventHandler->getEventName();
|
|
31
|
+
|
|
32
|
+
if (eventHandler->shouldIgnoreEmitterReactTag()) {
|
|
33
|
+
const auto eventMappingIt = eventMappingsWithoutTag.find(eventName);
|
|
34
|
+
auto &handlersMap = eventMappingIt->second;
|
|
35
|
+
handlersMap.erase(id);
|
|
36
|
+
if (handlersMap.empty()) {
|
|
37
|
+
eventMappingsWithoutTag.erase(eventMappingIt);
|
|
38
|
+
}
|
|
39
|
+
} else {
|
|
40
|
+
const auto emitterReactTag = eventHandler->getEmitterReactTag();
|
|
41
|
+
const auto eventHash = std::make_pair(emitterReactTag, eventName);
|
|
42
|
+
const auto eventMappingIt = eventMappingsWithTag.find(eventHash);
|
|
43
|
+
auto &handlersMap = eventMappingIt->second;
|
|
44
|
+
handlersMap.erase(id);
|
|
45
|
+
if (handlersMap.empty()) {
|
|
46
|
+
eventMappingsWithTag.erase(eventMappingIt);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
eventHandlers.erase(handlerIt);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
void EventHandlerRegistry::processEvent(
|
|
54
|
+
const std::shared_ptr<WorkletRuntime> &uiWorkletRuntime,
|
|
55
|
+
const double eventTimestamp,
|
|
56
|
+
const std::string &eventName,
|
|
57
|
+
const int emitterReactTag,
|
|
58
|
+
const jsi::Value &eventPayload) {
|
|
59
|
+
std::vector<std::shared_ptr<WorkletEventHandler>> handlersForEvent;
|
|
60
|
+
{
|
|
61
|
+
const std::lock_guard<std::mutex> lock(instanceMutex);
|
|
62
|
+
auto handlersIt = eventMappingsWithoutTag.find(eventName);
|
|
63
|
+
if (handlersIt != eventMappingsWithoutTag.end()) {
|
|
64
|
+
for (auto handler : handlersIt->second) {
|
|
65
|
+
handlersForEvent.push_back(handler.second);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const auto eventHash = std::make_pair(emitterReactTag, eventName);
|
|
69
|
+
auto handlersWithTagIt = eventMappingsWithTag.find(eventHash);
|
|
70
|
+
if (handlersWithTagIt != eventMappingsWithTag.end()) {
|
|
71
|
+
for (auto handler : handlersWithTagIt->second) {
|
|
72
|
+
handlersForEvent.push_back(handler.second);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
jsi::Runtime &rt = uiWorkletRuntime->getJSIRuntime();
|
|
78
|
+
eventPayload.asObject(rt).setProperty(
|
|
79
|
+
rt, "eventName", jsi::String::createFromUtf8(rt, eventName));
|
|
80
|
+
for (auto handler : handlersForEvent) {
|
|
81
|
+
handler->process(uiWorkletRuntime, eventTimestamp, eventPayload);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
bool EventHandlerRegistry::isAnyHandlerWaitingForEvent(
|
|
86
|
+
const std::string &eventName,
|
|
87
|
+
const int emitterReactTag) {
|
|
88
|
+
const std::lock_guard<std::mutex> lock(instanceMutex);
|
|
89
|
+
const auto eventHash = std::make_pair(emitterReactTag, eventName);
|
|
90
|
+
auto it = eventMappingsWithTag.find(eventHash);
|
|
91
|
+
return it != eventMappingsWithTag.end() && !it->second.empty();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
} // namespace worklets
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <worklets/WorkletRuntime/WorkletRuntime.h>
|
|
4
|
+
|
|
5
|
+
#include <jsi/jsi.h>
|
|
6
|
+
|
|
7
|
+
#include <map>
|
|
8
|
+
#include <memory>
|
|
9
|
+
#include <mutex>
|
|
10
|
+
#include <string>
|
|
11
|
+
#include <unordered_map>
|
|
12
|
+
#include <utility>
|
|
13
|
+
|
|
14
|
+
using namespace facebook;
|
|
15
|
+
|
|
16
|
+
namespace worklets {
|
|
17
|
+
|
|
18
|
+
class WorkletEventHandler;
|
|
19
|
+
|
|
20
|
+
class EventHandlerRegistry {
|
|
21
|
+
std::map<
|
|
22
|
+
std::pair<int, std::string>,
|
|
23
|
+
std::unordered_map<uint64_t, std::shared_ptr<WorkletEventHandler>>>
|
|
24
|
+
eventMappingsWithTag;
|
|
25
|
+
std::map<
|
|
26
|
+
std::string,
|
|
27
|
+
std::unordered_map<uint64_t, std::shared_ptr<WorkletEventHandler>>>
|
|
28
|
+
eventMappingsWithoutTag;
|
|
29
|
+
std::map<uint64_t, std::shared_ptr<WorkletEventHandler>> eventHandlers;
|
|
30
|
+
std::mutex instanceMutex;
|
|
31
|
+
|
|
32
|
+
public:
|
|
33
|
+
void registerEventHandler(
|
|
34
|
+
const std::shared_ptr<WorkletEventHandler> &eventHandler);
|
|
35
|
+
void unregisterEventHandler(const uint64_t id);
|
|
36
|
+
|
|
37
|
+
void processEvent(
|
|
38
|
+
const std::shared_ptr<WorkletRuntime> &uiWorkletRuntime,
|
|
39
|
+
const double eventTimestamp,
|
|
40
|
+
const std::string &eventName,
|
|
41
|
+
const int emitterReactTag,
|
|
42
|
+
const jsi::Value &eventPayload);
|
|
43
|
+
|
|
44
|
+
bool isAnyHandlerWaitingForEvent(
|
|
45
|
+
const std::string &eventName,
|
|
46
|
+
const int emitterReactTag);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
} // namespace worklets
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <jsi/jsi.h>
|
|
4
|
+
|
|
5
|
+
#include <mutex>
|
|
6
|
+
#include <set>
|
|
7
|
+
|
|
8
|
+
using namespace facebook;
|
|
9
|
+
|
|
10
|
+
namespace worklets {
|
|
11
|
+
|
|
12
|
+
class WorkletRuntimeRegistry {
|
|
13
|
+
private:
|
|
14
|
+
static std::set<jsi::Runtime *> registry_;
|
|
15
|
+
static std::mutex mutex_; // Protects `registry_`.
|
|
16
|
+
|
|
17
|
+
WorkletRuntimeRegistry() {} // private ctor
|
|
18
|
+
|
|
19
|
+
static void registerRuntime(jsi::Runtime &runtime) {
|
|
20
|
+
std::lock_guard<std::mutex> lock(mutex_);
|
|
21
|
+
registry_.insert(&runtime);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
static void unregisterRuntime(jsi::Runtime &runtime) {
|
|
25
|
+
std::lock_guard<std::mutex> lock(mutex_);
|
|
26
|
+
registry_.erase(&runtime);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
friend class WorkletRuntimeCollector;
|
|
30
|
+
|
|
31
|
+
public:
|
|
32
|
+
static bool isRuntimeAlive(jsi::Runtime *runtime) {
|
|
33
|
+
assert(runtime != nullptr);
|
|
34
|
+
std::lock_guard<std::mutex> lock(mutex_);
|
|
35
|
+
return registry_.find(runtime) != registry_.end();
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
} // namespace worklets
|