2019-10-21 09:59:22 +08:00
|
|
|
/*
|
|
|
|
* Copyright [2019] [Doric.Pub]
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
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"
|
2020-02-22 15:37:35 +08:00
|
|
|
#import "DoricContextManager.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;
|
|
|
|
}
|
|
|
|
|
2020-02-22 15:37:35 +08:00
|
|
|
- (NSString *)aliasWithContextId:(NSString *)contextId {
|
|
|
|
return [[DoricContextManager instance] getContext:contextId].source;
|
|
|
|
}
|
|
|
|
|
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];
|
2020-02-22 15:37:35 +08:00
|
|
|
[self.jsExecutor.registry onException:exception source:[self aliasWithContextId:contextId]];
|
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];
|
2020-02-22 15:37:35 +08:00
|
|
|
[self.jsExecutor.registry onException:exception source:[self aliasWithContextId:contextId]];
|
2019-07-30 21:05:27 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
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];
|
2020-01-11 11:01:38 +08:00
|
|
|
[ret setupResult:@YES];
|
2019-07-27 14:36:41 +08:00
|
|
|
} @catch (NSException *exception) {
|
|
|
|
[ret setupError:exception];
|
2020-02-22 15:37:35 +08:00
|
|
|
[self.jsExecutor.registry onException:exception source:source];
|
2019-07-27 14:36:41 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
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];
|
2020-01-11 11:01:38 +08:00
|
|
|
[ret setupResult:@YES];
|
2019-07-27 14:36:41 +08:00
|
|
|
} @catch (NSException *exception) {
|
|
|
|
[ret setupError:exception];
|
2020-02-22 15:37:35 +08:00
|
|
|
[self.jsExecutor.registry onException:exception source:[self aliasWithContextId:contextId]];
|
2019-07-27 14:36:41 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
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-11-19 16:15:54 +08:00
|
|
|
- (void)ensureSyncInMainQueue:(dispatch_block_t)block {
|
|
|
|
if (strcmp(dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), dispatch_queue_get_label(dispatch_get_main_queue())) == 0) {
|
|
|
|
block();
|
|
|
|
} else {
|
|
|
|
dispatch_async(dispatch_get_main_queue(), block);
|
|
|
|
}
|
|
|
|
}
|
2019-07-26 14:19:42 +08:00
|
|
|
@end
|