feat:optimize iOS Native Driver method

This commit is contained in:
pengfei.zhou 2021-07-21 15:04:55 +08:00 committed by osborn
parent 8e2ebaf833
commit 74e9aa0e38

View File

@ -55,13 +55,29 @@ + (instancetype)instance {
} }
- (DoricAsyncResult *)invokeDoricMethod:(NSString *)method argumentsArray:(NSArray *)args { - (DoricAsyncResult *)invokeDoricMethod:(NSString *)method argumentsArray:(NSArray *)args {
id contextId = args.count > 0 ? args[0] : nil;
DoricPerformanceProfile *profile = nil;
if ([contextId isKindOfClass:NSString.class]) {
profile = [DoricContextManager.instance getContext:contextId].performanceProfile;
}
NSMutableArray *printedArgs = [[NSMutableArray alloc] init];
for (id arg in args) {
if (arg == contextId) {
continue;
}
[printedArgs addObject:arg];
}
NSString *anchorName = [NSString stringWithFormat:@"Call:%@", [printedArgs componentsJoinedByString:@","]];
[profile prepare:anchorName];
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init]; DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
__weak typeof(self) _self = self; __weak typeof(self) _self = self;
[self.jsExecutor ensureRunOnJSThread:^{ [self.jsExecutor ensureRunOnJSThread:^{
__strong typeof(_self) self = _self; __strong typeof(_self) self = _self;
if (!self) return; if (!self) return;
@try { @try {
[profile start:anchorName];
JSValue *jsValue = [self.jsExecutor invokeDoricMethod:method argumentsArray:args]; JSValue *jsValue = [self.jsExecutor invokeDoricMethod:method argumentsArray:args];
[profile end:anchorName];
[ret setupResult:jsValue]; [ret setupResult:jsValue];
} @catch (NSException *exception) { } @catch (NSException *exception) {
[ret setupError:exception]; [ret setupError:exception];
@ -79,24 +95,12 @@ - (DoricAsyncResult *)invokeDoricMethod:(NSString *)method argumentsArray:(NSArr
} }
- (DoricAsyncResult<JSValue *> *)invokeDoricMethod:(NSString *)method arguments:(va_list)args { - (DoricAsyncResult<JSValue *> *)invokeDoricMethod:(NSString *)method arguments:(va_list)args {
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
NSMutableArray *array = [[NSMutableArray alloc] init]; NSMutableArray *array = [[NSMutableArray alloc] init];
id arg; id arg;
while ((arg = va_arg(args, id)) != nil) { while ((arg = va_arg(args, id)) != nil) {
[array addObject:arg]; [array addObject:arg];
} }
__weak typeof(self) _self = self; return [self invokeDoricMethod:method argumentsArray:array];
[self.jsExecutor ensureRunOnJSThread:^{
__strong typeof(_self) self = _self;
if (!self) return;
@try {
JSValue *jsValue = [self.jsExecutor invokeDoricMethod:method argumentsArray:array];
[ret setupResult:jsValue];
} @catch (NSException *exception) {
[ret setupError:exception];
}
}];
return ret;
} }
- (DoricAsyncResult<JSValue *> *)invokeContextEntity:(NSString *)contextId method:(NSString *)method, ... { - (DoricAsyncResult<JSValue *> *)invokeContextEntity:(NSString *)contextId method:(NSString *)method, ... {
@ -108,36 +112,15 @@ - (DoricAsyncResult *)invokeDoricMethod:(NSString *)method argumentsArray:(NSArr
} }
- (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method arguments:(va_list)args { - (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method arguments:(va_list)args {
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
NSMutableArray *array = [[NSMutableArray alloc] init]; NSMutableArray *array = [[NSMutableArray alloc] init];
NSMutableArray *printedArgs = [[NSMutableArray alloc] init];
[array addObject:contextId]; [array addObject:contextId];
[array addObject:method]; [array addObject:method];
[printedArgs addObject:method];
id arg = va_arg(args, id); id arg = va_arg(args, id);
while (arg != nil) { while (arg != nil) {
[array addObject:arg]; [array addObject:arg];
[printedArgs addObject:arg];
arg = va_arg(args, JSValue *); arg = va_arg(args, JSValue *);
} }
DoricPerformanceProfile *performanceProfile = [DoricContextManager.instance getContext:contextId].performanceProfile; return [self invokeContextEntity:contextId method:method argumentsArray:array];
NSString *anchorName = [NSString stringWithFormat:@"call:%@", [printedArgs componentsJoinedByString:@","]];
[performanceProfile prepare:anchorName];
__weak typeof(self) _self = self;
[self.jsExecutor ensureRunOnJSThread:^{
__strong typeof(_self) self = _self;
if (!self) return;
@try {
[performanceProfile start:anchorName];
JSValue *jsValue = [self.jsExecutor invokeDoricMethod:DORIC_CONTEXT_INVOKE argumentsArray:array];
[ret setupResult:jsValue];
[performanceProfile end:anchorName];
} @catch (NSException *exception) {
[ret setupError:exception];
[self.jsExecutor.registry onException:exception inContext:[[DoricContextManager instance] getContext:contextId]];
}
}];
return ret;
} }
- (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method argumentsArray:(NSArray *)args { - (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method argumentsArray:(NSArray *)args {
@ -145,26 +128,15 @@ - (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString
NSMutableArray *array = [[NSMutableArray alloc] init]; NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:contextId]; [array addObject:contextId];
[array addObject:method]; [array addObject:method];
for (id arg in args) { [array addObjectsFromArray:args];
[array addObject:arg]; DoricAsyncResult *result = [self invokeDoricMethod:DORIC_CONTEXT_INVOKE argumentsArray:array];
} result.resultCallback = ^(id result) {
DoricPerformanceProfile *performanceProfile = [DoricContextManager.instance getContext:contextId].performanceProfile; [ret setupResult:result];
NSString *anchorName = [NSString stringWithFormat:@"call:%@,%@", method, [args componentsJoinedByString:@","]]; };
[performanceProfile prepare:anchorName]; result.exceptionCallback = ^(NSException *e) {
__weak typeof(self) _self = self; [ret setupError:e];
[self.jsExecutor ensureRunOnJSThread:^{ [self.jsExecutor.registry onException:e inContext:[[DoricContextManager instance] getContext:contextId]];
__strong typeof(_self) self = _self; };
if (!self) return;
@try {
[performanceProfile start:anchorName];
JSValue *jsValue = [self.jsExecutor invokeDoricMethod:DORIC_CONTEXT_INVOKE argumentsArray:array];
[ret setupResult:jsValue];
[performanceProfile end:anchorName];
} @catch (NSException *exception) {
[ret setupError:exception];
[self.jsExecutor.registry onException:exception inContext:[[DoricContextManager instance] getContext:contextId]];
}
}];
return ret; return ret;
} }