This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/iOS/Pod/Classes/DoricDriver.m

172 lines
5.1 KiB
Mathematica
Raw Normal View History

2019-07-26 14:19:42 +08:00
//
// DoricDriver.m
// Doric
//
// Created by pengfei.zhou on 2019/7/26.
//
#import "DoricDriver.h"
2019-07-27 14:36:41 +08:00
#import "DoricJSEngine.h"
#import "DoricConstant.h"
2019-08-14 10:08:33 +08:00
#import "DoricWSClient.h"
2019-07-26 14:19:42 +08:00
2019-10-12 14:48:19 +08:00
@interface DoricDriver ()
@property(nonatomic, strong) DoricJSEngine *jsExecutor;
@property(nonatomic, strong) DoricWSClient *wsclient;
2019-07-26 14:19:42 +08:00
@end
@implementation DoricDriver
2019-07-27 14:36:41 +08:00
@dynamic registry;
2019-07-29 13:48:27 +08:00
- (instancetype)init {
2019-10-12 14:48:19 +08:00
if (self = [super init]) {
2019-07-29 13:48:27 +08:00
_jsExecutor = [[DoricJSEngine alloc] init];
}
return self;
}
- (DoricRegistry *)registry {
2019-07-27 14:36:41 +08:00
return self.jsExecutor.registry;
}
2019-10-12 14:48:19 +08:00
+ (instancetype)instance {
2019-07-26 14:19:42 +08:00
static DoricDriver *_instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[DoricDriver alloc] init];
});
return _instance;
}
2019-10-12 14:48:19 +08:00
- (DoricAsyncResult<JSValue *> *)invokeDoricMethod:(NSString *)method, ... {
2019-07-27 14:36:41 +08:00
va_list args;
va_start(args, method);
DoricAsyncResult *ret = [self invokeDoricMethod:method arguments:args];
va_end(args);
return ret;
}
2019-10-12 14:48:19 +08:00
- (DoricAsyncResult<JSValue *> *)invokeDoricMethod:(NSString *)method arguments:(va_list)args {
2019-07-27 14:36:41 +08:00
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
2019-07-30 11:25:05 +08:00
NSMutableArray *array = [[NSMutableArray alloc] init];
id arg;
2019-10-12 14:48:19 +08:00
while ((arg = va_arg(args, id)) != nil) {
2019-07-30 11:25:05 +08:00
[array addObject:arg];
}
2019-07-27 14:36:41 +08:00
__weak typeof(self) _self = self;
2019-10-12 14:48:19 +08:00
dispatch_async(self.jsExecutor.jsQueue, ^() {
2019-07-27 14:36:41 +08:00
__strong typeof(_self) self = _self;
if (!self) return;
@try {
2019-07-30 11:25:05 +08:00
JSValue *jsValue = [self.jsExecutor invokeDoricMethod:method argumentsArray:array];
2019-07-27 14:36:41 +08:00
[ret setupResult:jsValue];
} @catch (NSException *exception) {
[ret setupError:exception];
}
});
return ret;
}
2019-10-12 14:48:19 +08:00
- (DoricAsyncResult<JSValue *> *)invokeContextEntity:(NSString *)contextId method:(NSString *)method, ... {
2019-07-29 13:48:27 +08:00
va_list args;
va_start(args, method);
DoricAsyncResult *ret = [self invokeContextEntity:contextId method:method arguments:args];
va_end(args);
return ret;
}
- (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method arguments:(va_list)args {
2019-07-27 14:36:41 +08:00
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:contextId];
[array addObject:method];
id arg = va_arg(args, id);
2019-10-12 14:48:19 +08:00
while (arg != nil) {
2019-07-27 14:36:41 +08:00
[array addObject:arg];
arg = va_arg(args, JSValue *);
}
__weak typeof(self) _self = self;
2019-10-12 14:48:19 +08:00
dispatch_async(self.jsExecutor.jsQueue, ^() {
2019-07-27 14:36:41 +08:00
__strong typeof(_self) self = _self;
if (!self) return;
@try {
JSValue *jsValue = [self.jsExecutor invokeDoricMethod:DORIC_CONTEXT_INVOKE argumentsArray:array];
[ret setupResult:jsValue];
} @catch (NSException *exception) {
2019-07-29 13:48:27 +08:00
[ret setupError:exception];
2019-07-27 14:36:41 +08:00
}
});
return ret;
}
2019-10-12 14:48:19 +08:00
2019-07-30 21:05:27 +08:00
- (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method argumentsArray:(NSArray *)args {
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:contextId];
[array addObject:method];
2019-10-12 14:48:19 +08:00
for (id arg in args) {
[array addObject:arg];
2019-07-30 21:05:27 +08:00
}
__weak typeof(self) _self = self;
2019-10-12 14:48:19 +08:00
dispatch_async(self.jsExecutor.jsQueue, ^() {
2019-07-30 21:05:27 +08:00
__strong typeof(_self) self = _self;
if (!self) return;
@try {
JSValue *jsValue = [self.jsExecutor invokeDoricMethod:DORIC_CONTEXT_INVOKE argumentsArray:array];
[ret setupResult:jsValue];
} @catch (NSException *exception) {
[ret setupError:exception];
}
});
return ret;
}
2019-07-27 14:36:41 +08:00
2019-07-29 13:48:27 +08:00
- (DoricAsyncResult *)createContext:(NSString *)contextId script:(NSString *)script source:(NSString *)source {
2019-07-27 14:36:41 +08:00
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
__weak typeof(self) _self = self;
2019-10-12 14:48:19 +08:00
dispatch_async(self.jsExecutor.jsQueue, ^() {
2019-07-27 14:36:41 +08:00
__strong typeof(_self) self = _self;
2019-10-12 14:48:19 +08:00
if (!self) return;
@try {
2019-07-27 14:36:41 +08:00
[self.jsExecutor prepareContext:contextId script:script source:source];
[ret setupResult:[NSNumber numberWithBool:YES]];
} @catch (NSException *exception) {
[ret setupError:exception];
}
});
return ret;
}
2019-07-29 13:48:27 +08:00
- (DoricAsyncResult *)destroyContext:(NSString *)contextId {
2019-07-27 14:36:41 +08:00
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
__weak typeof(self) _self = self;
2019-10-12 14:48:19 +08:00
dispatch_async(self.jsExecutor.jsQueue, ^() {
2019-07-27 14:36:41 +08:00
__strong typeof(_self) self = _self;
2019-10-12 14:48:19 +08:00
if (!self) return;
@try {
2019-07-27 14:36:41 +08:00
[self.jsExecutor destroyContext:contextId];
[ret setupResult:[NSNumber numberWithBool:YES]];
} @catch (NSException *exception) {
[ret setupError:exception];
}
});
return ret;
}
2019-08-14 10:08:33 +08:00
- (void)connectDevKit:(NSString *)url {
2019-10-12 14:48:19 +08:00
if (self.wsclient) {
2019-08-14 10:08:33 +08:00
[self.wsclient close];
}
self.wsclient = [[DoricWSClient alloc] initWithUrl:url];
}
- (void)disconnectDevKit {
2019-10-12 14:48:19 +08:00
if (self.wsclient) {
2019-08-14 10:08:33 +08:00
[self.wsclient close];
self.wsclient = nil;
}
}
2019-07-27 14:36:41 +08:00
2019-07-26 14:19:42 +08:00
@end