@@ -12,23 +12,6 @@ namespace RNWorklet {
12
12
13
13
namespace jsi = facebook::jsi;
14
14
15
- class BaseRuntimeAwareCache {
16
- public:
17
- static void setMainJsRuntime (jsi::Runtime *rt) { _mainRuntime = rt; }
18
-
19
- protected:
20
- static jsi::Runtime *getMainJsRuntime () {
21
- assert (_mainRuntime != nullptr &&
22
- " Expected main Javascript runtime to be set in the "
23
- " BaseRuntimeAwareCache class." );
24
-
25
- return _mainRuntime;
26
- }
27
-
28
- private:
29
- static jsi::Runtime *_mainRuntime;
30
- };
31
-
32
15
/* *
33
16
* Provides a way to keep data specific to a jsi::Runtime instance that gets
34
17
* cleaned up when that runtime is destroyed. This is necessary because JSI does
@@ -51,51 +34,36 @@ class BaseRuntimeAwareCache {
51
34
* related to tracking runtime lifecycle when only a single runtime is used.
52
35
*/
53
36
template <typename T>
54
- class RuntimeAwareCache : public BaseRuntimeAwareCache ,
55
- public RuntimeLifecycleListener {
37
+ class RuntimeAwareCache : public RuntimeLifecycleListener {
56
38
57
39
public:
58
40
void onRuntimeDestroyed (jsi::Runtime *rt) override {
59
- if (getMainJsRuntime () != rt) {
60
- // We are removing a secondary runtime
61
- _secondaryRuntimeCaches.erase (rt);
62
- }
41
+ // A runtime has been destroyed, so destroy the related cache.
42
+ _runtimeCaches.erase (rt);
63
43
}
64
44
65
45
~RuntimeAwareCache () {
66
- for (auto &cache : _secondaryRuntimeCaches ) {
67
- RuntimeLifecycleMonitor::removeListener (
68
- * static_cast <jsi::Runtime *> (cache.first ) , this );
46
+ for (auto &cache : _runtimeCaches ) {
47
+ // remove all `onRuntimeDestroyed` listeners.
48
+ RuntimeLifecycleMonitor::removeListener (cache.first , this );
69
49
}
70
50
}
71
51
72
52
T &get (jsi::Runtime &rt) {
73
- // We check if we're accessing the main runtime - this is the happy path
74
- // to avoid us having to lookup by runtime for caches that only has a single
75
- // runtime
76
- if (getMainJsRuntime () == &rt) {
77
- return _primaryCache;
78
- } else {
79
- if (_secondaryRuntimeCaches.count (&rt) == 0 ) {
80
- // we only add listener when the secondary runtime is used, this assumes
81
- // that the secondary runtime is terminated first. This lets us avoid
82
- // additional complexity for the majority of cases when objects are not
83
- // shared between runtimes. Otherwise we'd have to register all objects
84
- // with the RuntimeMonitor as opposed to only registering ones that are
85
- // used in secondary runtime. Note that we can't register listener here
86
- // with the primary runtime as it may run on a separate thread.
87
- RuntimeLifecycleMonitor::addListener (rt, this );
88
-
89
- T cache;
90
- _secondaryRuntimeCaches.emplace (&rt, std::move (cache));
91
- }
53
+ if (_runtimeCaches.count (&rt) == 0 ) {
54
+ // This is the first time this Runtime has been accessed,
55
+ // set up a `onRuntimeDestroyed` listener for it and
56
+ // initialize the cache map.
57
+ RuntimeLifecycleMonitor::addListener (rt, this );
58
+
59
+ T cache;
60
+ _runtimeCaches.emplace (&rt, std::move (cache));
92
61
}
93
- return _secondaryRuntimeCaches .at (&rt);
62
+ return _runtimeCaches .at (&rt);
94
63
}
95
64
96
65
private:
97
- std::unordered_map<void *, T> _secondaryRuntimeCaches;
98
- T _primaryCache;
66
+ std::unordered_map<jsi::Runtime *, T> _runtimeCaches;
99
67
};
100
68
101
69
} // namespace RNWorklet
0 commit comments