function complete

This commit is contained in:
王劲鹏 2020-02-27 16:13:14 +08:00 committed by osborn
parent f9f48d9c8a
commit 9764e720fc
8 changed files with 101 additions and 15 deletions

View File

@ -23,12 +23,6 @@
#import <Foundation/Foundation.h>
#import "DoricDriverProtocol.h"
typedef NS_ENUM(NSInteger, DoricQueueMode) {
JS = 0,
UI,
INDEPENDENT
};
NS_ASSUME_NONNULL_BEGIN
@interface DoricDebugDriver : NSObject <DoricDriverProtocol>

View File

@ -19,18 +19,31 @@
//
// Created by jingpeng.wang on 2020/2/25.
//
#import <DoricCore/Doric.h>
#import <DoricCore/DoricContextManager.h>
#import <DoricCore/DoricNativeDriver.h>
#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

View File

@ -21,6 +21,7 @@
//
#import <DoricCore/Doric.h>
#import <DoricCore/DoricContextManager.h>
#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

View File

@ -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

View File

@ -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 {

View File

@ -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];

View File

@ -55,6 +55,8 @@ NS_ASSUME_NONNULL_BEGIN
- (void)reload:(NSString *)script;
- (void)reInit;
- (void)onShow;
- (void)onHidden;

View File

@ -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];
}