-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
WorkletRuntimeCollector.h
36 lines (26 loc) · 1.02 KB
/
WorkletRuntimeCollector.h
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
#pragma once
#include "WorkletRuntimeRegistry.h"
#include <jsi/jsi.h>
#include <memory>
namespace reanimated {
class WorkletRuntimeCollector : public jsi::HostObject {
// When worklet runtime is created, we inject an instance of this class as a
// `jsi::HostObject` into the global object. When worklet runtime is
// terminated, the object is garbage-collected, which runs the C++ destructor.
// In the destructor, we unregister the worklet runtime from the registry.
public:
explicit WorkletRuntimeCollector(jsi::Runtime &runtime) : runtime_(runtime) {
WorkletRuntimeRegistry::registerRuntime(runtime_);
}
~WorkletRuntimeCollector() {
WorkletRuntimeRegistry::unregisterRuntime(runtime_);
}
static void install(jsi::Runtime &rt) {
auto collector = std::make_shared<WorkletRuntimeCollector>(rt);
auto object = jsi::Object::createFromHostObject(rt, collector);
rt.global().setProperty(rt, "__workletRuntimeCollector", object);
}
private:
jsi::Runtime &runtime_;
};
} // namespace reanimated