mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-16 13:23:41 +00:00
feat: unify native API invocation with async callbacks (#1476)
This commit is contained in:
21 files changed
+1017
-184
No files matched your search
@@ -8,6 +8,45 @@
|
||||
|
||||
@end
|
||||
|
||||
@interface GopeedInvokeResultForwarder : NSObject <LibgopeedInvokeResultListener>
|
||||
|
||||
@property(nonatomic, copy, nullable) FlutterResult result;
|
||||
|
||||
- (instancetype)initWithResult:(FlutterResult)result;
|
||||
|
||||
@end
|
||||
|
||||
@implementation GopeedInvokeResultForwarder
|
||||
|
||||
- (instancetype)initWithResult:(FlutterResult)result {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_result = [result copy];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)onResult:(int64_t)requestID
|
||||
success:(BOOL)success
|
||||
payload:(NSString * _Nullable)payload {
|
||||
FlutterResult result = self.result;
|
||||
self.result = nil;
|
||||
if (result == nil) {
|
||||
return;
|
||||
}
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
if (success) {
|
||||
result(payload ?: @"");
|
||||
} else {
|
||||
result([FlutterError errorWithCode:@"ERROR"
|
||||
message:payload ?: @"InvokeAsync failed"
|
||||
details:nil]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation GopeedTaskEventForwarder
|
||||
|
||||
- (instancetype)initWithChannel:(FlutterMethodChannel *)channel {
|
||||
@@ -33,3 +72,15 @@ void GopeedSubscribeTaskEventsWithForwarder(
|
||||
GopeedTaskEventForwarder * _Nullable listener) {
|
||||
LibgopeedSubscribeTaskEvents(mask, listener);
|
||||
}
|
||||
|
||||
void GopeedInvokeAsyncWithResult(
|
||||
NSString *method,
|
||||
NSString *path,
|
||||
NSString *query,
|
||||
NSString *body,
|
||||
int64_t requestID,
|
||||
FlutterResult result) {
|
||||
GopeedInvokeResultForwarder *forwarder =
|
||||
[[GopeedInvokeResultForwarder alloc] initWithResult:result];
|
||||
LibgopeedInvokeAsync(method, path, query, body, requestID, forwarder);
|
||||
}
|
||||
Reference in new issue
Block a user