Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,44 @@ const ModuleDeclarationTemplate = ({
structDeclarations: string,
eventEmitters: string,
protocolMethods: string,
}>) => `${structDeclarations}
}>) => {
// Split methods into required and optional groups
const methodLines = protocolMethods.split('\n');
const requiredMethods = [];
const optionalMethods = [];

let isOptional = false;
for (const line of methodLines) {
switch (line.trim()) {
case '@optional':
isOptional = true;
continue;
case '@required':
isOptional = false;
continue;
case '':
continue;
default:
if (isOptional) {
optionalMethods.push(line);
} else {
requiredMethods.push(line);
}
}
}
let protocolMethodsBody = requiredMethods.join('\n');
if (optionalMethods.length > 0) {
if (protocolMethodsBody === '') {
protocolMethodsBody += optionalMethods.join('\n');
} else {
protocolMethodsBody += `\n\n@optional\n${optionalMethods.join('\n')}`;
}
}

return `${structDeclarations}
@protocol ${hasteModuleName}Spec <RCTBridgeModule, RCTTurboModule>

${protocolMethods}
${protocolMethodsBody}

@end

Expand All @@ -57,6 +91,7 @@ namespace facebook::react {
${hasteModuleName}SpecJSI(const ObjCTurboModule::InitParams &params);
};
} // namespace facebook::react`;
};

const HeaderFileTemplate = ({
headerFileName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ const ProtocolMethodTemplate = ({
returnObjCType,
methodName,
params,
isOptional,
}: $ReadOnly<{
returnObjCType: string,
methodName: string,
params: string,
}>) => `- (${returnObjCType})${methodName}${params};`;
isOptional: boolean,
}>) =>
`${
isOptional ? '@optional\n' : '@required\n'
}- (${returnObjCType})${methodName}${params};`;

export type StructParameterRecord = $ReadOnly<{
paramIndex: number,
Expand Down Expand Up @@ -139,6 +144,7 @@ function serializeMethod(
methodName,
returnObjCType,
params: objCParams,
isOptional: property.optional === true,
});

/**
Expand Down Expand Up @@ -517,6 +523,7 @@ function serializeConstantsProtocolMethods(
methodName,
returnObjCType,
params: '',
isOptional: property.optional === true,
});

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,16 @@ namespace JS {

- (NSDictionary *)difficult:(JS::NativeSampleTurboModule::SpecDifficultA &)A;
- (void)optionals:(JS::NativeSampleTurboModule::SpecOptionalsA &)A;
- (void)optionalMethod:(NSDictionary *)options
callback:(RCTResponseSenderBlock)callback
extras:(NSArray *)extras;
- (void)getArrays:(JS::NativeSampleTurboModule::SpecGetArraysOptions &)options;
- (NSDictionary * _Nullable)getNullableObject;
- (NSDictionary * _Nullable)getNullableGenericObject;
- (NSArray<id<NSObject>> * _Nullable)getNullableArray;

@optional
- (void)optionalMethod:(NSDictionary *)options
callback:(RCTResponseSenderBlock)callback
extras:(NSArray *)extras;

@end

@interface NativeSampleTurboModuleSpecBase : NSObject {
Expand Down Expand Up @@ -838,10 +840,12 @@ namespace JS {
- (void)reportSoftException:(NSString *)message
stack:(NSArray *)stack
exceptionId:(double)exceptionId;
- (void)reportException:(JS::NativeExceptionsManager::ExceptionData &)data;
- (void)updateExceptionMessage:(NSString *)message
stack:(NSArray *)stack
exceptionId:(double)exceptionId;

@optional
- (void)reportException:(JS::NativeExceptionsManager::ExceptionData &)data;
- (void)dismissRedbox;

@end
Expand Down