Skip to content

Commit

Permalink
introduce flag to determine if turbomodule void method should run sync (
Browse files Browse the repository at this point in the history
facebook#39589)

Summary:

Changelog: [Internal]

currently, turbomodule void methods run async by default, unless the consumer returns `RCTJSThread` in the `methodQueue` API.

we're looking to update this, so i'm introducing a flag at the module level that allows us to configure this behavior.

Reviewed By: RSNara

Differential Revision: D49521864
  • Loading branch information
philIip authored and facebook-github-bot committed Oct 3, 2023
1 parent 4da0d44 commit 55f97ea
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class JSI_EXPORT ObjCTurboModule : public TurboModule {
std::shared_ptr<CallInvoker> jsInvoker;
std::shared_ptr<NativeMethodCallInvoker> nativeMethodCallInvoker;
bool isSyncModule;
bool shouldVoidMethodsExecuteSync;
};

ObjCTurboModule(const InitParams &params);
Expand Down Expand Up @@ -112,6 +113,9 @@ class JSI_EXPORT ObjCTurboModule : public TurboModule {
// Does the NativeModule dispatch async methods to the JS thread?
const bool isSyncModule_;

// Should void methods execute synchronously?
const bool shouldVoidMethodsExecuteSync_;

/**
* TODO(ramanpreet):
* Investigate an optimization that'll let us get rid of this NSMutableDictionary.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -680,14 +680,19 @@ id convertJSIValueToObjCObject(jsi::Runtime &runtime, const jsi::Value &value, s

bool ObjCTurboModule::isMethodSync(TurboModuleMethodValueKind returnType)
{
if (returnType == VoidKind && shouldVoidMethodsExecuteSync_) {
return true;
}

return isSyncModule_ || !(returnType == VoidKind || returnType == PromiseKind);
}

ObjCTurboModule::ObjCTurboModule(const InitParams &params)
: TurboModule(params.moduleName, params.jsInvoker),
instance_(params.instance),
nativeMethodCallInvoker_(params.nativeMethodCallInvoker),
isSyncModule_(params.isSyncModule)
isSyncModule_(params.isSyncModule),
shouldVoidMethodsExecuteSync_(params.shouldVoidMethodsExecuteSync)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ - (instancetype)initWithBridgeProxy:(RCTBridgeProxy *)bridgeProxy
.jsInvoker = _jsInvoker,
.nativeMethodCallInvoker = nativeMethodCallInvoker,
.isSyncModule = methodQueue == RCTJSThread,
.shouldVoidMethodsExecuteSync = false,
};

auto turboModule = [(id<RCTTurboModule>)module getTurboModule:params];
Expand Down Expand Up @@ -437,6 +438,7 @@ - (instancetype)initWithBridgeProxy:(RCTBridgeProxy *)bridgeProxy
.jsInvoker = _jsInvoker,
.nativeMethodCallInvoker = std::move(nativeMethodCallInvoker),
.isSyncModule = methodQueue == RCTJSThread,
.shouldVoidMethodsExecuteSync = false,
};

auto turboModule = std::make_shared<ObjCInteropTurboModule>(params);
Expand Down

0 comments on commit 55f97ea

Please sign in to comment.