Skip to content

Commit

Permalink
Injecting js_call invoker only if microtasks are disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
kraenhansen committed Jun 19, 2024
1 parent 9a30fb1 commit 1446644
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.turbomodule.core.CallInvokerHolderImpl;
import com.facebook.soloader.SoLoader;
Expand Down Expand Up @@ -74,9 +75,12 @@ public boolean injectModuleIntoJSGlobal() {
throw new IllegalStateException(e);
}


CallInvokerHolderImpl jsCallInvokerHolder = (CallInvokerHolderImpl) reactContext.getCatalystInstance().getJSCallInvokerHolder();
injectCallInvoker(jsCallInvokerHolder);
// Since https://github.com/facebook/react-native/pull/43396 this should only be needed when bridgeless is not enabled.
// We can use the enablement of "microtasks" to avoid the overhead of calling the invokeAsync on every call from C++ into JS.
if (!ReactNativeFeatureFlags.enableMicrotasks()) {
CallInvokerHolderImpl jsCallInvokerHolder = (CallInvokerHolderImpl) reactContext.getCatalystInstance().getJSCallInvokerHolder();
injectCallInvoker(jsCallInvokerHolder);
}

// Get the javascript runtime and inject our native module with it
JavaScriptContextHolder jsContext = reactContext.getJavaScriptContextHolder();
Expand Down Expand Up @@ -105,7 +109,6 @@ public boolean injectModuleIntoJSGlobal() {
* Passes the React Native jsCallInvokerHolder over to C++ so we can setup our UI queue flushing.
* This is needed as a workaround for https://github.com/facebook/react-native/issues/33006
* where we call the invokeAsync method to flush the React Native UI queue whenever we call from C++ to JS.
* NOTE: Since https://github.com/facebook/react-native/pull/43396 this should only be needed when bridgeless is not enabled.
*/
private native void injectCallInvoker(CallInvokerHolderImpl callInvoker);

Expand Down
6 changes: 4 additions & 2 deletions packages/realm/binding/apple/RealmReactModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#import <React/RCTBridge+Private.h>
#import <React/RCTInvalidating.h>
#import <ReactCommon/CallInvoker.h>
#import <React-featureflags/react/featureflags/ReactNativeFeatureFlags.h>
#import <jsi/jsi.h>

#import <arpa/inet.h>
Expand All @@ -35,7 +36,6 @@
// the part of the RCTCxxBridge private class we care about
@interface RCTBridge (Realm_RCTCxxBridge)
- (void *)runtime;
// Expose the CallInvoker so that we can call `invokeAsync`
- (std::shared_ptr<facebook::react::CallInvoker>)jsCallInvoker;
@end

Expand Down Expand Up @@ -74,7 +74,9 @@ - (void)dealloc {
RCTBridge* bridge = [RCTBridge currentBridge];
auto &rt = *static_cast<facebook::jsi::Runtime *>(bridge.runtime);

realm::js::flush_ui_workaround::inject_js_call_invoker([bridge jsCallInvoker]);
if (!facebook::react::ReactNativeFeatureFlags::enableMicrotasks()) {
realm::js::flush_ui_workaround::inject_js_call_invoker([bridge jsCallInvoker]);
}

auto exports = jsi::Object(rt);
realm_jsi_init(rt, exports);
Expand Down

0 comments on commit 1446644

Please sign in to comment.