-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathWorkletsModuleProxy.cpp
More file actions
77 lines (66 loc) · 2.8 KB
/
Copy pathWorkletsModuleProxy.cpp
File metadata and controls
77 lines (66 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <react/renderer/uimanager/UIManagerBinding.h>
#include <react/renderer/uimanager/primitives.h>
#include <worklets/NativeModules/WorkletsModuleProxy.h>
#include <worklets/RunLoop/AsyncQueueImpl.h>
#include <worklets/SharedItems/Serializable.h>
#include <worklets/Tools/Defs.h>
#include <worklets/Tools/ScriptBuffer.h>
#include <worklets/WorkletRuntime/RuntimeBindings.h>
#include <worklets/WorkletRuntime/UIRuntimeDecorator.h>
#include <memory>
#include <utility>
using namespace facebook;
namespace worklets {
bool isDevBundleFromRNRuntime(jsi::Runtime &rnRuntime);
WorkletsModuleProxy::WorkletsModuleProxy(
jsi::Runtime &rnRuntime,
const std::shared_ptr<MessageQueueThread> &jsQueue,
const std::shared_ptr<CallInvoker> &jsCallInvoker,
const std::shared_ptr<UIScheduler> &uiScheduler,
std::function<bool()> &&isJavaScriptThread,
const std::shared_ptr<RuntimeBindings> &runtimeBindings,
const BundleModeConfig &bundleModeConfig)
: isDevBundle_(isDevBundleFromRNRuntime(rnRuntime)),
jsQueue_(jsQueue),
jsScheduler_(std::make_shared<JSScheduler>(rnRuntime, jsCallInvoker, std::move(isJavaScriptThread))),
uiScheduler_(uiScheduler),
jsLogger_(std::make_shared<JSLogger>(jsScheduler_)),
runtimeBindings_(runtimeBindings),
bundleModeConfig_(bundleModeConfig),
memoryManager_(std::make_shared<MemoryManager>()),
runtimeManager_(std::make_shared<RuntimeManager>()),
uiWorkletRuntime_(
runtimeManager_->createUninitializedUIRuntime(jsQueue_, std::make_shared<AsyncQueueUI>(uiScheduler_))) {
/**
* We call additional `init` method here because
* JSIWorkletsModuleProxy needs a weak_ptr to the UI Runtime.
*/
uiWorkletRuntime_->init(createJSIWorkletsModuleProxy());
animationFrameBatchinator_ =
std::make_shared<AnimationFrameBatchinator>(uiWorkletRuntime_, runtimeBindings_->requestAnimationFrame);
UIRuntimeDecorator::decorate(
uiWorkletRuntime_->getJSIRuntime(), animationFrameBatchinator_->getJsiRequestAnimationFrame());
}
std::shared_ptr<JSIWorkletsModuleProxy> WorkletsModuleProxy::createJSIWorkletsModuleProxy() const {
assert(uiWorkletRuntime_ && "UI Worklet Runtime must be initialized before creating JSI proxy.");
return std::make_shared<JSIWorkletsModuleProxy>(
isDevBundle_,
jsQueue_,
jsScheduler_,
uiScheduler_,
memoryManager_,
runtimeManager_,
uiWorkletRuntime_,
runtimeBindings_,
bundleModeConfig_);
}
WorkletsModuleProxy::~WorkletsModuleProxy() {
animationFrameBatchinator_.reset();
jsQueue_->quitSynchronous();
uiWorkletRuntime_.reset();
}
auto isDevBundleFromRNRuntime(jsi::Runtime &rnRuntime) -> bool {
const auto rtDev = rnRuntime.global().getProperty(rnRuntime, "__DEV__");
return rtDev.isBool() && rtDev.asBool();
}
} // namespace worklets