Skip to content

Commit ac2dd4c

Browse files
committed
fix: Fix reload crash by also listening to Main JS Runtime in RuntimeAwareCache
1 parent 8565e29 commit ac2dd4c

File tree

3 files changed

+17
-56
lines changed

3 files changed

+17
-56
lines changed

cpp/WKTJsiWorkletContext.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ void JsiWorkletContext::initialize(
7272
std::function<void(std::function<void()> &&)> jsCallInvoker,
7373
std::function<void(std::function<void()> &&)> workletCallInvoker) {
7474

75-
// Register main runtime
76-
BaseRuntimeAwareCache::setMainJsRuntime(jsRuntime);
77-
7875
_name = name;
7976
_jsRuntime = jsRuntime;
8077
_jsCallInvoker = jsCallInvoker;

cpp/base/WKTRuntimeAwareCache.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
#include "WKTRuntimeAwareCache.h"
22

3-
namespace RNWorklet {
4-
5-
jsi::Runtime *BaseRuntimeAwareCache::_mainRuntime = nullptr;
6-
7-
} // namespace RNWorklet
3+
namespace RNWorklet {} // namespace RNWorklet

cpp/base/WKTRuntimeAwareCache.h

+16-48
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,6 @@ namespace RNWorklet {
1212

1313
namespace jsi = facebook::jsi;
1414

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-
3215
/**
3316
* Provides a way to keep data specific to a jsi::Runtime instance that gets
3417
* cleaned up when that runtime is destroyed. This is necessary because JSI does
@@ -51,51 +34,36 @@ class BaseRuntimeAwareCache {
5134
* related to tracking runtime lifecycle when only a single runtime is used.
5235
*/
5336
template <typename T>
54-
class RuntimeAwareCache : public BaseRuntimeAwareCache,
55-
public RuntimeLifecycleListener {
37+
class RuntimeAwareCache : public RuntimeLifecycleListener {
5638

5739
public:
5840
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);
6343
}
6444

6545
~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);
6949
}
7050
}
7151

7252
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));
9261
}
93-
return _secondaryRuntimeCaches.at(&rt);
62+
return _runtimeCaches.at(&rt);
9463
}
9564

9665
private:
97-
std::unordered_map<void *, T> _secondaryRuntimeCaches;
98-
T _primaryCache;
66+
std::unordered_map<jsi::Runtime *, T> _runtimeCaches;
9967
};
10068

10169
} // namespace RNWorklet

0 commit comments

Comments
 (0)