Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lift isSyncModule_ check outside of isMethodSync #39590

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,23 @@ id convertJSIValueToObjCObject(jsi::Runtime &runtime, const jsi::Value &value, s

bool ObjCTurboModule::isMethodSync(TurboModuleMethodValueKind returnType)
{
return isSyncModule_ || !(returnType == VoidKind || returnType == PromiseKind);
if (isSyncModule_) {
return true;
}

if (returnType == VoidKind && shouldVoidMethodsExecuteSync_) {
return true;
}

return !(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 All @@ -702,15 +711,17 @@ id convertJSIValueToObjCObject(jsi::Runtime &runtime, const jsi::Value &value, s
const char *moduleName = name_.c_str();
const char *methodName = methodNameStr.c_str();

if (isMethodSync(returnType)) {
bool isSyncInvocation = isMethodSync(returnType);

if (isSyncInvocation) {
TurboModulePerfLogger::syncMethodCallStart(moduleName, methodName);
} else {
TurboModulePerfLogger::asyncMethodCallStart(moduleName, methodName);
}

NSMutableArray *retainedObjectsForInvocation = [NSMutableArray arrayWithCapacity:count + 2];
NSInvocation *inv = createMethodInvocation(
runtime, isMethodSync(returnType), methodName, selector, args, count, retainedObjectsForInvocation);
runtime, isSyncInvocation, methodName, selector, args, count, retainedObjectsForInvocation);

jsi::Value returnValue = jsi::Value::undefined();

Expand All @@ -725,24 +736,23 @@ id convertJSIValueToObjCObject(jsi::Runtime &runtime, const jsi::Value &value, s
[retainedObjectsForInvocation addObject:resolveCopy];
[retainedObjectsForInvocation addObject:rejectCopy];
// The return type becomes void in the ObjC side.
performMethodInvocation(runtime, isMethodSync(VoidKind), methodName, inv, retainedObjectsForInvocation);
performMethodInvocation(runtime, isSyncInvocation, methodName, inv, retainedObjectsForInvocation);
});
} else {
id result =
performMethodInvocation(runtime, isMethodSync(returnType), methodName, inv, retainedObjectsForInvocation);
id result = performMethodInvocation(runtime, isSyncInvocation, methodName, inv, retainedObjectsForInvocation);

if (isMethodSync(returnType)) {
if (isSyncInvocation) {
TurboModulePerfLogger::syncMethodCallReturnConversionStart(moduleName, methodName);
}

returnValue = convertReturnIdToJSIValue(runtime, methodName, returnType, result);

if (isMethodSync(returnType)) {
if (isSyncInvocation) {
TurboModulePerfLogger::syncMethodCallReturnConversionEnd(moduleName, methodName);
}
}

if (isMethodSync(returnType)) {
if (isSyncInvocation) {
TurboModulePerfLogger::syncMethodCallEnd(moduleName, methodName);
} else {
TurboModulePerfLogger::asyncMethodCallEnd(moduleName, methodName);
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