Skip to content

Commit fe837e5

Browse files
committed
Stored call invokers by napi_env
1 parent f489338 commit fe837e5

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

packages/host/cpp/CxxNodeApiHostModule.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ CxxNodeApiHostModule::CxxNodeApiHostModule(
1212
methodMap_["requireNodeAddon"] =
1313
MethodMetadata{1, &CxxNodeApiHostModule::requireNodeAddon};
1414

15-
callstack::nodeapihost::setCallInvoker(jsInvoker);
15+
callInvoker_ = std::move(jsInvoker);
1616
}
1717

1818
jsi::Value
@@ -127,6 +127,7 @@ bool CxxNodeApiHostModule::initializeNodeModule(jsi::Runtime &rt,
127127
napi_set_named_property(env, global, addon.generatedName.data(), exports);
128128
assert(status == napi_ok);
129129

130+
callstack::nodeapihost::setCallInvoker(env, callInvoker_);
130131
return true;
131132
}
132133

packages/host/cpp/CxxNodeApiHostModule.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class JSI_EXPORT CxxNodeApiHostModule : public facebook::react::TurboModule {
2828
std::string generatedName;
2929
};
3030
std::unordered_map<std::string, NodeAddon> nodeAddons_;
31+
std::shared_ptr<facebook::react::CallInvoker> callInvoker_;
32+
3133
using LoaderPolicy = PosixLoader; // FIXME: HACK: This is temporary workaround
3234
// for my lazyness (work on iOS and Android)
3335

packages/host/cpp/RuntimeNodeApiAsync.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,21 @@ class AsyncWorkRegistry {
8080
std::unordered_map<IdType, std::shared_ptr<AsyncJob>> jobs_;
8181
};
8282

83-
static std::weak_ptr<facebook::react::CallInvoker> callInvoker;
83+
static std::unordered_map<napi_env, std::weak_ptr<facebook::react::CallInvoker>>
84+
callInvokers;
8485
static AsyncWorkRegistry asyncWorkRegistry;
8586

8687
namespace callstack::nodeapihost {
8788

88-
void setCallInvoker(
89+
void setCallInvoker(napi_env env,
8990
const std::shared_ptr<facebook::react::CallInvoker>& invoker) {
90-
callInvoker = invoker;
91+
callInvokers[env] = invoker;
92+
}
93+
94+
std::weak_ptr<facebook::react::CallInvoker> getCallInvoker(napi_env env) {
95+
return callInvokers.contains(env)
96+
? callInvokers[env]
97+
: std::weak_ptr<facebook::react::CallInvoker>{};
9198
}
9299

93100
napi_status napi_create_async_work(napi_env env,
@@ -116,7 +123,7 @@ napi_status napi_queue_async_work(
116123
return napi_invalid_arg;
117124
}
118125

119-
const auto invoker = callInvoker.lock();
126+
const auto invoker = getCallInvoker(env).lock();
120127
if (!invoker) {
121128
log_debug("Error: No CallInvoker available for async work");
122129
return napi_invalid_arg;

packages/host/cpp/RuntimeNodeApiAsync.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace callstack::nodeapihost {
88
void setCallInvoker(
9-
const std::shared_ptr<facebook::react::CallInvoker>& invoker);
9+
napi_env env, const std::shared_ptr<facebook::react::CallInvoker>& invoker);
1010

1111
napi_status napi_create_async_work(napi_env env,
1212
napi_value async_resource,

0 commit comments

Comments
 (0)