Skip to content

Commit db3625a

Browse files
RSNarafacebook-github-bot
authored andcommitted
Refactor: Move RuntimeExecutor into Instance.cpp
Summary: RuntimeExecutor is currently declared inside NativeToJsBridge. It doesn't need to be: Instance.cpp can use NativeToJsBridge::runOnExecutorQueue to schedule work on the JS Thread. So, this diff moves RuntimeExecutor out of NativeToJsBridge into Instance.cpp. Now, both the JS CallInvoker and the RuntimeExecutor are declared in the same file. Changelog: [Internal] Reviewed By: JoshuaGross Differential Revision: D27975840 fbshipit-source-id: aa06f479fa24bb7a15bfd21712df5414a183266c
1 parent c68c554 commit db3625a

File tree

3 files changed

+21
-28
lines changed

3 files changed

+21
-28
lines changed

ReactCommon/cxxreact/Instance.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "Instance.h"
99

10+
#include "ErrorUtils.h"
1011
#include "JSBigString.h"
1112
#include "JSBundleType.h"
1213
#include "JSExecutor.h"
@@ -245,8 +246,27 @@ std::shared_ptr<CallInvoker> Instance::getJSCallInvoker() {
245246
return std::static_pointer_cast<CallInvoker>(jsCallInvoker_);
246247
}
247248

249+
// TODO: Unify with JS CallInvoker
248250
RuntimeExecutor Instance::getRuntimeExecutor() {
249-
return nativeToJsBridge_->getRuntimeExecutor();
251+
std::weak_ptr<NativeToJsBridge> weakNativeToJsBridge = nativeToJsBridge_;
252+
253+
auto runtimeExecutor =
254+
[weakNativeToJsBridge](
255+
std::function<void(jsi::Runtime & runtime)> &&callback) {
256+
if (auto strongNativeToJsBridge = weakNativeToJsBridge.lock()) {
257+
strongNativeToJsBridge->runOnExecutorQueue(
258+
[callback = std::move(callback)](JSExecutor *executor) {
259+
jsi::Runtime *runtime =
260+
(jsi::Runtime *)executor->getJavaScriptContext();
261+
try {
262+
callback(*runtime);
263+
} catch (jsi::JSError &originalError) {
264+
handleJSError(*runtime, originalError, true);
265+
}
266+
});
267+
}
268+
};
269+
return runtimeExecutor;
250270
}
251271

252272
std::shared_ptr<CallInvoker> Instance::getDecoratedNativeCallInvoker(

ReactCommon/cxxreact/NativeToJsBridge.cpp

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -340,26 +340,5 @@ std::shared_ptr<CallInvoker> NativeToJsBridge::getDecoratedNativeCallInvoker(
340340
return std::make_shared<NativeCallInvoker>(m_delegate, nativeInvoker);
341341
}
342342

343-
RuntimeExecutor NativeToJsBridge::getRuntimeExecutor() {
344-
auto runtimeExecutor =
345-
[this, isDestroyed = m_destroyed](
346-
std::function<void(jsi::Runtime & runtime)> &&callback) {
347-
if (*isDestroyed) {
348-
return;
349-
}
350-
runOnExecutorQueue(
351-
[callback = std::move(callback)](JSExecutor *executor) {
352-
jsi::Runtime *runtime =
353-
(jsi::Runtime *)executor->getJavaScriptContext();
354-
try {
355-
callback(*runtime);
356-
} catch (jsi::JSError &originalError) {
357-
handleJSError(*runtime, originalError, true);
358-
}
359-
});
360-
};
361-
return runtimeExecutor;
362-
}
363-
364343
} // namespace react
365344
} // namespace facebook

ReactCommon/cxxreact/NativeToJsBridge.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,6 @@ class NativeToJsBridge {
107107
std::shared_ptr<CallInvoker> getDecoratedNativeCallInvoker(
108108
std::shared_ptr<CallInvoker> nativeInvoker);
109109

110-
/**
111-
* RuntimeExecutor is used on Android to access the jsi::Runtime from Fabric
112-
* and TurboModules
113-
*/
114-
RuntimeExecutor getRuntimeExecutor();
115-
116110
private:
117111
// This is used to avoid a race condition where a proxyCallback gets queued
118112
// after ~NativeToJsBridge(), on the same thread. In that case, the callback

0 commit comments

Comments
 (0)