diff --git a/doric-iOS/Devkit/Classes/DoricDebugDriver.h b/doric-iOS/Devkit/Classes/DoricDebugDriver.h index bca04b65..b333952a 100644 --- a/doric-iOS/Devkit/Classes/DoricDebugDriver.h +++ b/doric-iOS/Devkit/Classes/DoricDebugDriver.h @@ -23,12 +23,6 @@ #import #import "DoricDriverProtocol.h" -typedef NS_ENUM(NSInteger, DoricQueueMode) { - JS = 0, - UI, - INDEPENDENT -}; - NS_ASSUME_NONNULL_BEGIN @interface DoricDebugDriver : NSObject diff --git a/doric-iOS/Devkit/Classes/DoricDev.m b/doric-iOS/Devkit/Classes/DoricDev.m index 9f0d5888..6cc2e074 100644 --- a/doric-iOS/Devkit/Classes/DoricDev.m +++ b/doric-iOS/Devkit/Classes/DoricDev.m @@ -19,18 +19,31 @@ // // Created by jingpeng.wang on 2020/2/25. // +#import +#import +#import #import "DoricDev.h" #import "DoricWSClient.h" +#import "DoricDebugDriver.h" @interface DoricDev () @property(nonatomic, strong) DoricWSClient *wsclient; +@property(nonatomic, strong) DoricContext *context; +@property(nonatomic, strong) DoricDebugDriver *driver; @end @implementation DoricDev - (instancetype)init { if (self = [super init]) { + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onOpenEvent) name:@"OpenEvent" object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onEOFEvent) name:@"EOFEvent" object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onConnectExceptionEvent) name:@"ConnectExceptionEvent" object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onStartDebugEvent:) name:@"StartDebugEvent" object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onEnterDebugEvent) name:@"EnterDebugEvent" object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onStopDebugEvent) name:@"StopDebugEvent" object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onDebuggerReadyEvent) name:@"DebuggerReadyEvent" object:nil]; } return self; } @@ -62,4 +75,41 @@ - (void)disconnectDevKit { } } +- (void)onOpenEvent { + ShowToast(@"dev kit connected", BOTTOM); +} + +- (void)onEOFEvent { + ShowToast(@"dev kit eof exception", BOTTOM); +} + +- (void)onConnectExceptionEvent { + ShowToast(@"dev kit connection exception", BOTTOM); +} + +- (void)onStartDebugEvent:(NSNotification *)notification { + NSString *contextId = notification.object; + ShowToast(contextId, BOTTOM); + for (NSValue *value in [[DoricContextManager instance] aliveContexts]) { + DoricContext *context = value.nonretainedObjectValue; + BOOL result = [context.contextId compare:contextId] == NSOrderedSame; + if (result) { + _context = context; + } + } +} + +- (void)onEnterDebugEvent { + _driver = [DoricDebugDriver new]; +} + +- (void)onStopDebugEvent { + _context.driver = [DoricNativeDriver instance]; + [_context reInit]; +} + +- (void)onDebuggerReadyEvent { + _context.driver = _driver; + [_context reInit]; +} @end diff --git a/doric-iOS/Devkit/Classes/DoricDevViewController.m b/doric-iOS/Devkit/Classes/DoricDevViewController.m index 74960206..bad1b0a5 100644 --- a/doric-iOS/Devkit/Classes/DoricDevViewController.m +++ b/doric-iOS/Devkit/Classes/DoricDevViewController.m @@ -21,6 +21,7 @@ // #import #import +#import "NSString+JsonString.h" #import "DoricDev.h" #import "DoricDevViewController.h" @@ -67,7 +68,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellID"]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } - cell.textLabel.text = path; + cell.textLabel.text = [path stringByAppendingString:@" Debug"]; return cell; } @@ -76,6 +77,19 @@ - (BOOL)isSimulator { } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { + NSValue *value = [DoricContextManager.instance aliveContexts][(NSUInteger) indexPath.row]; + DoricContext *context = value.nonretainedObjectValue; + [[NSNotificationCenter defaultCenter] postNotificationName:@"StartDebugEvent" object:context.contextId]; + NSDictionary *jsonDic = @{ + @"cmd": @"DEBUG", + @"data": @{ + @"contextId": context.contextId, + @"source": [context.source stringByReplacingOccurrencesOfString:@".js" withString:@".ts"] + } + }; + + NSString *jsonStr = [NSString dc_convertToJsonWithDic:jsonDic]; + [[DoricDev instance] sendDevCommand:jsonStr]; } @end diff --git a/doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.m b/doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.m index 83379e3c..7871f817 100644 --- a/doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.m +++ b/doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.m @@ -54,6 +54,7 @@ - (instancetype)init { - (void)webSocketDidOpen:(SRWebSocket *)webSocket { DoricLog(@"debugger webSocketDidOpen"); DC_UNLOCK(self.semaphore); + [[NSNotificationCenter defaultCenter] postNotificationName:@"DebuggerReadyEvent" object:nil]; } - (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload { @@ -116,6 +117,7 @@ - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error { - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean { DoricLog(@"debugger webSocketdidCloseWithCode"); + [[NSNotificationCenter defaultCenter] postNotificationName:@"StopDebugEvent" object:nil]; } #pragma mark - DoricJSExecutorProtocol diff --git a/doric-iOS/Devkit/Classes/DoricWSClient.m b/doric-iOS/Devkit/Classes/DoricWSClient.m index 28bf8206..d632d246 100644 --- a/doric-iOS/Devkit/Classes/DoricWSClient.m +++ b/doric-iOS/Devkit/Classes/DoricWSClient.m @@ -41,6 +41,7 @@ - (instancetype)initWithUrl:(NSString *)url { - (void)webSocketDidOpen:(SRWebSocket *)webSocket { DoricLog(@"webSocketDidOpen"); + [[NSNotificationCenter defaultCenter] postNotificationName:@"OpenEvent" object:nil]; } - (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload { @@ -57,22 +58,29 @@ - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message { DoricLog(@"webSocketdidReceiveMessage parse error:%@", err); return; } - NSString *source = [[dic valueForKey:@"source"] mutableCopy]; - NSString *script = [dic valueForKey:@"script"]; - for (NSValue *value in [[DoricContextManager instance] aliveContexts]) { - DoricContext *context = value.nonretainedObjectValue; - if ([source containsString:context.source]) { - [context reload:script]; - } + NSString *cmd = [[dic valueForKey:@"cmd"] mutableCopy]; + if ([cmd compare:@"SWITCH_TO_DEBUG"] == NSOrderedSame) { + [[NSNotificationCenter defaultCenter] postNotificationName:@"EnterDebugEvent" object:nil]; + } else if ([cmd compare:@"RELOAD"] == NSOrderedSame) { +// NSString *source = [[dic valueForKey:@"source"] mutableCopy]; +// NSString *script = [dic valueForKey:@"script"]; +// for (NSValue *value in [[DoricContextManager instance] aliveContexts]) { +// DoricContext *context = value.nonretainedObjectValue; +// if ([source containsString:context.source]) { +// [context reload:script]; +// } +// } } } - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error { DoricLog(@"webSocketdidFailWithError"); + [[NSNotificationCenter defaultCenter] postNotificationName:@"ConnectExceptionEvent" object:nil]; } - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean { DoricLog(@"webSocketdidCloseWithCode"); + [[NSNotificationCenter defaultCenter] postNotificationName:@"EOFEvent" object:nil]; } - (void)send:(NSString *)command { diff --git a/doric-iOS/Example/Example/ViewController.m b/doric-iOS/Example/Example/ViewController.m index 4050da8b..795729d0 100644 --- a/doric-iOS/Example/Example/ViewController.m +++ b/doric-iOS/Example/Example/ViewController.m @@ -68,7 +68,17 @@ - (BOOL)isSimulator { - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 0) { - [self.navigationController pushViewController:[DoricDevViewController new] animated:NO]; + NSString *file = self.demoFilePaths[44]; + DoricViewController *doricViewController = [[DoricViewController alloc] + initWithSource:[NSString stringWithFormat:@"assets://src/%@", file] + alias:self.demoFilePaths[44] + extra:nil + ]; + [self.navigationController pushViewController:doricViewController animated:NO]; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [self.navigationController pushViewController:[DoricDevViewController new] animated:NO]; + } + ); return; } NSString *file = self.demoFilePaths[(NSUInteger) indexPath.row]; diff --git a/doric-iOS/Pod/Classes/DoricContext.h b/doric-iOS/Pod/Classes/DoricContext.h index d94efaf6..d980a76f 100644 --- a/doric-iOS/Pod/Classes/DoricContext.h +++ b/doric-iOS/Pod/Classes/DoricContext.h @@ -55,6 +55,8 @@ NS_ASSUME_NONNULL_BEGIN - (void)reload:(NSString *)script; +- (void)reInit; + - (void)onShow; - (void)onHidden; diff --git a/doric-iOS/Pod/Classes/DoricContext.m b/doric-iOS/Pod/Classes/DoricContext.m index 9fa4f426..37847dd1 100644 --- a/doric-iOS/Pod/Classes/DoricContext.m +++ b/doric-iOS/Pod/Classes/DoricContext.m @@ -96,6 +96,12 @@ - (void)reload:(NSString *)script { [self onShow]; } +- (void)reInit { + self.rootNode.viewId = nil; + [self callEntity:DORIC_ENTITY_INIT, self.initialParams, nil]; + [self callEntity:DORIC_ENTITY_CREATE, nil]; +} + - (void)onShow { [self callEntity:DORIC_ENTITY_SHOW, nil]; }