Skip to content

Commit

Permalink
async
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrinneal committed Aug 3, 2023
1 parent cab52fc commit 2d84e30
Show file tree
Hide file tree
Showing 21 changed files with 112 additions and 17 deletions.
40 changes: 30 additions & 10 deletions packages/pigeon/lib/objc_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ class ObjcHeaderGenerator extends StructuredGenerator<ObjcOptions> {
for (final Method func in api.methods) {
final _ObjcPtr returnType =
_objcTypeForDartType(generatorOptions.prefix, func.returnType);
final String callbackType = _callbackForType(func.returnType, returnType);
final String callbackType =
_callbackForType(root, func.returnType, returnType);
addDocumentationComments(
indent, func.documentationComments, _docCommentSpec);

Expand Down Expand Up @@ -350,13 +351,15 @@ class ObjcHeaderGenerator extends StructuredGenerator<ObjcOptions> {
String? returnType;
if (func.isAsynchronous) {
returnType = 'void';
lastArgName = 'completion';
if (func.returnType.isVoid) {
lastArgType = 'void (^)(FlutterError *_Nullable)';
lastArgName = 'completion';
} else if (isEnum(root, func.returnType)) {
lastArgType =
'void (^)(${returnTypeName.baseName}, FlutterError *_Nullable)';
} else {
lastArgType =
'void (^)(${returnTypeName.withPtr}_Nullable, FlutterError *_Nullable)';
lastArgName = 'completion';
}
} else {
if (func.returnType.isVoid) {
Expand Down Expand Up @@ -718,16 +721,28 @@ class ObjcSourceGenerator extends StructuredGenerator<ObjcOptions> {
}
} else {
const String callback = 'callback(wrapResult(output, error));';
String returnTypeString = '${returnType.withPtr}_Nullable output';
const String enumConvert =
'NSNumber *output = [NSNumber numberWithInteger:enumValue];';
if (isEnum(root, func.returnType)) {
returnTypeString = '${returnType.baseName} enumValue';
}
if (func.arguments.isEmpty) {
indent.writeScoped(
'[api ${selectorComponents.first}:^(${returnType.withPtr}_Nullable output, FlutterError *_Nullable error) {',
'[api ${selectorComponents.first}:^($returnTypeString, FlutterError *_Nullable error) {',
'}];', () {
if (isEnum(root, func.returnType)) {
indent.writeln(enumConvert);
}
indent.writeln(callback);
});
} else {
indent.writeScoped(
'[api $callSignature ${selectorComponents.last}:^(${returnType.withPtr}_Nullable output, FlutterError *_Nullable error) {',
'[api $callSignature ${selectorComponents.last}:^($returnTypeString, FlutterError *_Nullable error) {',
'}];', () {
if (isEnum(root, func.returnType)) {
indent.writeln(enumConvert);
}
indent.writeln(callback);
});
}
Expand Down Expand Up @@ -981,7 +996,8 @@ static id GetNullableObjectAtIndex(NSArray *array, NSInteger key) {
}) {
final _ObjcPtr returnType =
_objcTypeForDartType(languageOptions.prefix, func.returnType);
final String callbackType = _callbackForType(func.returnType, returnType);
final String callbackType =
_callbackForType(root, func.returnType, returnType);

String argNameFunc(int count, NamedType arg) => _getSafeArgName(count, arg);
final Iterable<String> argNames = indexMap(func.arguments, argNameFunc);
Expand Down Expand Up @@ -1070,10 +1086,14 @@ String _className(String? prefix, String className) {
}

/// Calculates callback block signature for async methods.
String _callbackForType(TypeDeclaration type, _ObjcPtr objcType) {
return type.isVoid
? 'void (^)(FlutterError *_Nullable)'
: 'void (^)(${objcType.withPtr}_Nullable, FlutterError *_Nullable)';
String _callbackForType(Root root, TypeDeclaration type, _ObjcPtr objcType) {
if (type.isVoid) {
return 'void (^)(FlutterError *_Nullable)';
} else if (isEnum(root, type)) {
return 'void (^)(${objcType.baseName}, FlutterError *_Nullable)';
} else {
return 'void (^)(${objcType.withPtr}_Nullable, FlutterError *_Nullable)';
}
}

/// Represents an ObjC pointer (ex 'id', 'NSString *').
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/pigeons/core_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ abstract class HostIntegrationCoreApi {
@SwiftFunction('echoAsync(_:)')
Map<String?, Object?> echoAsyncMap(Map<String?, Object?> aMap);

// /// Returns the passed enum, to test asynchronous serialization and deserialization.
/// Returns the passed enum, to test asynchronous serialization and deserialization.
// @async
// @ObjCSelector('echoAsyncEnum:')
// @SwiftFunction('echoAsync(_:)')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ public void echoAsyncMap(
result.success(aMap);
}

@Override
public void echoAsyncEnum(@NonNull AnEnum anEnum, @NonNull Result<AnEnum> result) {
result.success(anEnum);
}

@Override
public void echoAsyncNullableInt(@Nullable Long anInt, @NonNull Result<Long> result) {
result.success(anInt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,10 @@ AllNullableTypes sendMultipleNullableTypes(
/** Returns the passed map, to test asynchronous serialization and deserialization. */
void echoAsyncMap(
@NonNull Map<String, Object> aMap, @NonNull Result<Map<String, Object>> result);
/** Responds with an error from an async function returning a value. */
/**
* Returns the passed enum, to test asynchronous serialization and deserialization. Responds
* with an error from an async function returning a value.
*/
void throwAsyncError(@NonNull Result<Object> result);
/** Responds with an error from an async void function. */
void throwAsyncErrorFromVoid(@NonNull Result<Void> result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ - (void)echoAsyncMap:(NSDictionary<NSString *, id> *)aMap
completion(aMap, nil);
}

- (void)echoAsyncEnum:(AnEnum)anEnum
completion:(void (^)(AnEnum, FlutterError *_Nullable))completion {
completion(anEnum, nil);
}

- (void)echoAsyncNullableInt:(nullable NSNumber *)anInt
completion:(void (^)(NSNumber *_Nullable, FlutterError *_Nullable))completion {
completion(anInt, nil);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ NSObject<FlutterMessageCodec> *HostIntegrationCoreApiGetCodec(void);
- (void)echoAsyncMap:(NSDictionary<NSString *, id> *)aMap
completion:(void (^)(NSDictionary<NSString *, id> *_Nullable,
FlutterError *_Nullable))completion;
/// Returns the passed enum, to test asynchronous serialization and deserialization.
/// Responds with an error from an async function returning a value.
- (void)throwAsyncErrorWithCompletion:(void (^)(id _Nullable, FlutterError *_Nullable))completion;
/// Responds with an error from an async void function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,7 @@ void HostIntegrationCoreApiSetup(id<FlutterBinaryMessenger> binaryMessenger,
[channel setMessageHandler:nil];
}
}
/// Returns the passed enum, to test asynchronous serialization and deserialization.
/// Responds with an error from an async function returning a value.
{
FlutterBasicMessageChannel *channel = [[FlutterBasicMessageChannel alloc]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ - (void)echoAsyncMap:(NSDictionary<NSString *, id> *)aMap
completion(aMap, nil);
}

- (void)echoAsyncEnum:(AnEnum)anEnum
completion:(void (^)(AnEnum, FlutterError *_Nullable))completion {
completion(anEnum, nil);
}

- (void)echoAsyncNullableInt:(nullable NSNumber *)anInt
completion:(void (^)(NSNumber *_Nullable, FlutterError *_Nullable))completion {
completion(anInt, nil);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ NSObject<FlutterMessageCodec> *HostIntegrationCoreApiGetCodec(void);
- (void)echoAsyncMap:(NSDictionary<NSString *, id> *)aMap
completion:(void (^)(NSDictionary<NSString *, id> *_Nullable,
FlutterError *_Nullable))completion;
/// Returns the passed enum, to test asynchronous serialization and deserialization.
/// Responds with an error from an async function returning a value.
- (void)throwAsyncErrorWithCompletion:(void (^)(id _Nullable, FlutterError *_Nullable))completion;
/// Responds with an error from an async void function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,7 @@ void HostIntegrationCoreApiSetup(id<FlutterBinaryMessenger> binaryMessenger,
[channel setMessageHandler:nil];
}
}
/// Returns the passed enum, to test asynchronous serialization and deserialization.
/// Responds with an error from an async function returning a value.
{
FlutterBasicMessageChannel *channel = [[FlutterBasicMessageChannel alloc]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,35 @@ class HostIntegrationCoreApi {
}
}

/// Returns the passed enum, to test asynchronous serialization and deserialization.
Future<AnEnum> echoAsyncEnum(AnEnum arg_anEnum) async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.flutter_unit_tests.HostIntegrationCoreApi.echoAsyncEnum',
codec,
binaryMessenger: _binaryMessenger);
final List<Object?>? replyList =
await channel.send(<Object?>[arg_anEnum.index]) as List<Object?>?;
if (replyList == null) {
throw PlatformException(
code: 'channel-error',
message: 'Unable to establish connection on channel.',
);
} else if (replyList.length > 1) {
throw PlatformException(
code: replyList[0]! as String,
message: replyList[1] as String?,
details: replyList[2],
);
} else if (replyList[0] == null) {
throw PlatformException(
code: 'null-error',
message: 'Host platform returned null value for non-null return value.',
);
} else {
return AnEnum.values[replyList[0]! as int];
}
}

/// Responds with an error from an async function returning a value.
Future<Object?> throwAsyncError() async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,7 @@ class HostIntegrationCoreApi {
}
}

/// Returns the passed enum, to test asynchronous serialization and deserialization.
/// Responds with an error from an async function returning a value.
Future<Object?> throwAsyncError() async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,10 @@ interface HostIntegrationCoreApi {
fun echoAsyncList(aList: List<Any?>, callback: (Result<List<Any?>>) -> Unit)
/** Returns the passed map, to test asynchronous serialization and deserialization. */
fun echoAsyncMap(aMap: Map<String?, Any?>, callback: (Result<Map<String?, Any?>>) -> Unit)
/** Responds with an error from an async function returning a value. */
/**
* Returns the passed enum, to test asynchronous serialization and deserialization.
* Responds with an error from an async function returning a value.
*/
fun throwAsyncError(callback: (Result<Any?>) -> Unit)
/** Responds with an error from an async void function. */
fun throwAsyncErrorFromVoid(callback: (Result<Unit>) -> Unit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ class TestPlugin: FlutterPlugin, HostIntegrationCoreApi {
callback(Result.success(aMap))
}

// override fun echoAsyncEnum(anEnum: AnEnum, callback: (Result<AnEnum>) -> Unit) {
// callback(Result.success(anEnum))
// }
override fun echoAsyncEnum(anEnum: AnEnum, callback: (Result<AnEnum>) -> Unit) {
callback(Result.success(anEnum))
}

override fun echoAsyncNullableInt(anInt: Long?, callback: (Result<Long?>) -> Unit) {
callback(Result.success(anInt))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ protocol HostIntegrationCoreApi {
func echoAsync(_ aList: [Any?], completion: @escaping (Result<[Any?], Error>) -> Void)
/// Returns the passed map, to test asynchronous serialization and deserialization.
func echoAsync(_ aMap: [String?: Any?], completion: @escaping (Result<[String?: Any?], Error>) -> Void)
/// Returns the passed enum, to test asynchronous serialization and deserialization.
/// Responds with an error from an async function returning a value.
func throwAsyncError(completion: @escaping (Result<Any?, Error>) -> Void)
/// Responds with an error from an async void function.
Expand Down Expand Up @@ -1022,6 +1023,7 @@ class HostIntegrationCoreApiSetup {
} else {
echoAsyncMapChannel.setMessageHandler(nil)
}
/// Returns the passed enum, to test asynchronous serialization and deserialization.
/// Responds with an error from an async function returning a value.
let throwAsyncErrorChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.pigeon_integration_tests.HostIntegrationCoreApi.throwAsyncError", binaryMessenger: binaryMessenger, codec: codec)
if let api = api {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ public class TestPlugin: NSObject, FlutterPlugin, HostIntegrationCoreApi {
completion(.success(aMap))
}

func echoAsync(_ anEnum: AnEnum, completion: @escaping (Result<AnEnum, Error>) -> Void) {
completion(.success(anEnum))
}

func echoAsyncNullable(_ anInt: Int64?, completion: @escaping (Result<Int64?, Error>) -> Void) {
completion(.success(anInt))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ protocol HostIntegrationCoreApi {
func echoAsync(_ aList: [Any?], completion: @escaping (Result<[Any?], Error>) -> Void)
/// Returns the passed map, to test asynchronous serialization and deserialization.
func echoAsync(_ aMap: [String?: Any?], completion: @escaping (Result<[String?: Any?], Error>) -> Void)
/// Returns the passed enum, to test asynchronous serialization and deserialization.
/// Responds with an error from an async function returning a value.
func throwAsyncError(completion: @escaping (Result<Any?, Error>) -> Void)
/// Responds with an error from an async void function.
Expand Down Expand Up @@ -1022,6 +1023,7 @@ class HostIntegrationCoreApiSetup {
} else {
echoAsyncMapChannel.setMessageHandler(nil)
}
/// Returns the passed enum, to test asynchronous serialization and deserialization.
/// Responds with an error from an async function returning a value.
let throwAsyncErrorChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.pigeon_integration_tests.HostIntegrationCoreApi.throwAsyncError", binaryMessenger: binaryMessenger, codec: codec)
if let api = api {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ public class TestPlugin: NSObject, FlutterPlugin, HostIntegrationCoreApi {
completion(.success(aMap))
}

func echoAsync(_ anEnum: AnEnum, completion: @escaping (Result<AnEnum, Error>) -> Void) {
completion(.success(anEnum))
}

func echoAsyncNullable(_ anInt: Int64?, completion: @escaping (Result<Int64?, Error>) -> Void) {
completion(.success(anInt))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,9 @@ class HostIntegrationCoreApi {
virtual void EchoAsyncMap(
const flutter::EncodableMap& a_map,
std::function<void(ErrorOr<flutter::EncodableMap> reply)> result) = 0;
// Responds with an error from an async function returning a value.
// Returns the passed enum, to test asynchronous serialization and
// deserialization. Responds with an error from an async function returning a
// value.
virtual void ThrowAsyncError(
std::function<void(ErrorOr<std::optional<flutter::EncodableValue>> reply)>
result) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ void TestPlugin::EchoAsyncMap(
result(a_map);
}

void TestPlugin::EchoAsyncEnum(
const AnEnum& an_enum, std::function<void(ErrorOr<AnEnum> reply)> result) {
result(an_enum);
}

void TestPlugin::EchoAsyncNullableAllNullableTypes(
const AllNullableTypes* everything,
std::function<void(ErrorOr<std::optional<AllNullableTypes>> reply)>
Expand Down
1 change: 1 addition & 0 deletions site-shared
Submodule site-shared added at 8c92e5

0 comments on commit 2d84e30

Please sign in to comment.