diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDev.java b/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDev.java index e426acd0..e9ae8ccc 100644 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDev.java +++ b/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDev.java @@ -32,7 +32,7 @@ public class DoricDev { void onFailure(Throwable throwable); - void onReload(DoricContext context, String source); + void onReload(DoricContext context, String script); void onStartDebugging(DoricContext context); @@ -82,11 +82,11 @@ public class DoricDev { private DoricContextDebuggable debuggable; private String url; - public void addStatusListener(StatusCallback listener) { + public void addStatusCallback(StatusCallback listener) { this.callbacks.add(listener); } - public void removeStatusListener(StatusCallback listener) { + public void removeStatusCallback(StatusCallback listener) { this.callbacks.remove(listener); } diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java b/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java index 5b54bd0d..f1837b6c 100644 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java +++ b/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java @@ -62,13 +62,13 @@ public class DoricDevActivity extends AppCompatActivity implements DoricDev.Stat @Override protected void onStart() { super.onStart(); - DoricDev.getInstance().addStatusListener(this); + DoricDev.getInstance().addStatusCallback(this); } @Override protected void onStop() { super.onStop(); - DoricDev.getInstance().removeStatusListener(this); + DoricDev.getInstance().removeStatusCallback(this); } @Override @@ -197,7 +197,7 @@ public class DoricDevActivity extends AppCompatActivity implements DoricDev.Stat } @Override - public void onReload(DoricContext context, String source) { + public void onReload(DoricContext context, String script) { this.cellAdapter.notifyDataSetChanged(); } diff --git a/doric-demo/src/Counter.ts b/doric-demo/src/Counter.ts index 7d207da9..13235e56 100644 --- a/doric-demo/src/Counter.ts +++ b/doric-demo/src/Counter.ts @@ -47,7 +47,7 @@ class CounterView extends ViewHolder { this.number = root.findViewByTag("tvNumber")!; this.counter = root.findViewByTag("tvCounter")!; } -} +} class CounterVM extends ViewModel { onAttached(s: CountModel, vh: CounterView) { @@ -59,7 +59,7 @@ class CounterVM extends ViewModel { } onBind(s: CountModel, vh: CounterView) { vh.number.text = `${s.count}`; - log("onBind\nseee"); + log("onBind"); logw("onBind"); loge("onBind"); } diff --git a/doric-iOS/Devkit/Classes/DoricDev.h b/doric-iOS/Devkit/Classes/DoricDev.h index 5d4865e5..bf792219 100644 --- a/doric-iOS/Devkit/Classes/DoricDev.h +++ b/doric-iOS/Devkit/Classes/DoricDev.h @@ -23,7 +23,23 @@ NS_ASSUME_NONNULL_BEGIN +@protocol DoricDevStatusCallback +- (void)onOpen:(NSString *)url; + +- (void)onClose:(NSString *)url; + +- (void)onFailure:(NSError *)error; + +- (void)onReload:(DoricContext *)context script:(NSString *)script; + +- (void)onStartDebugging:(DoricContext *)context; + +- (void)onStopDebugging; + +@end + @interface DoricDev : NSObject +@property(nonatomic, readonly) NSString *ip; + (instancetype)instance; @@ -35,13 +51,25 @@ NS_ASSUME_NONNULL_BEGIN - (void)connectDevKit:(NSString *)url; +- (void)onOpen; + +- (void)onClose; + +- (void)onFailure:(NSError *)error; + - (void)startDebugging:(NSString *)source; - (void)stopDebugging:(BOOL)resume; +- (BOOL)isReloadingContext:(DoricContext *)context; + - (void)reload:(NSString *)source script:(NSString *)script; - (void)sendDevCommand:(NSString *)command payload:(NSDictionary *)payload; + +- (void)addStatusCallback:(id )callback; + +- (void)removeStatusCallback:(id )callback; @end NS_ASSUME_NONNULL_END diff --git a/doric-iOS/Devkit/Classes/DoricDev.m b/doric-iOS/Devkit/Classes/DoricDev.m index 6008a109..35c47557 100644 --- a/doric-iOS/Devkit/Classes/DoricDev.m +++ b/doric-iOS/Devkit/Classes/DoricDev.m @@ -65,15 +65,17 @@ - (void)stopDebug:(BOOL)resume { @interface DoricDev () @property(nonatomic, strong, nullable) DoricWSClient *wsClient; @property(nonatomic, strong) DoricContextDebuggable *debuggable; +@property(nonatomic, strong) NSHashTable > *callbacks; +@property(nonatomic, strong) NSHashTable *reloadingContexts; +@property(nonatomic, assign) BOOL devKitConnected; +@property(nonatomic, copy) NSString *url; @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]; + _callbacks = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory]; [DoricNativeDriver.instance.registry registerMonitor:[DoricDevMonitor new]]; } return self; @@ -109,28 +111,50 @@ - (void)closeDevMode { } - (BOOL)isInDevMode { - return self.wsClient != nil; + return self.devKitConnected; } - (void)connectDevKit:(NSString *)url { if (self.wsClient) { [self.wsClient close]; } + self.devKitConnected = NO; self.wsClient = [[DoricWSClient alloc] initWithUrl:url]; + self.url = url; } -- (void)onOpenEvent { - ShowToast(@"dev kit connected", DoricGravityBottom); +- (void)onOpen { + self.devKitConnected = YES; + dispatch_async(dispatch_get_main_queue(), ^{ + for (id callback in self.callbacks) { + [callback onOpen:self.url]; + } + }); } -- (void)onEOFEvent { - ShowToast(@"dev kit eof exception", DoricGravityBottom); +- (void)onClose { + self.devKitConnected = NO; + dispatch_async(dispatch_get_main_queue(), ^{ + for (id callback in self.callbacks) { + [callback onClose:self.url]; + } + }); } -- (void)onConnectExceptionEvent { - ShowToast(@"dev kit connection exception", DoricGravityBottom); +- (void)onFailure:(NSError *)error { + self.devKitConnected = NO; + dispatch_async(dispatch_get_main_queue(), ^{ + for (id callback in self.callbacks) { + [callback onFailure:error]; + } + }); } +- (BOOL)isReloadingContext:(DoricContext *)context { + return [self.reloadingContexts containsObject:context]; +} + + - (DoricContext *)matchContext:(NSString *)source { for (DoricContext *context in [DoricContextManager.instance aliveContexts]) { if ([source containsString:context.source] || [context.source isEqualToString:@"__dev__"]) { @@ -148,6 +172,12 @@ - (void)reload:(NSString *)source script:(NSString *)script { } else { DoricLog(@"Context reload :id %@,source %@", context.contextId, source); [context reload:script]; + [self.reloadingContexts addObject:context]; + dispatch_async(dispatch_get_main_queue(), ^{ + for (id callback in self.callbacks) { + [callback onReload:context script:script]; + } + }); } } else { DoricLog(@"Cannot find context source %@ for reload", source); @@ -163,6 +193,11 @@ - (void)startDebugging:(NSString *)source { }]; self.debuggable = [[DoricContextDebuggable alloc] initWithWSClient:self.wsClient context:context]; [self.debuggable startDebug]; + dispatch_async(dispatch_get_main_queue(), ^{ + for (id callback in self.callbacks) { + [callback onStartDebugging:context]; + } + }); } else { DoricLog(@"Cannot find context source %@ for debugging", source); [self.wsClient sendToDebugger:@"DEBUG_STOP" payload:@{ @@ -177,9 +212,29 @@ - (void)stopDebugging:(BOOL)resume { }]; [self.debuggable stopDebug:resume]; self.debuggable = nil; + dispatch_async(dispatch_get_main_queue(), ^{ + for (id callback in self.callbacks) { + [callback onStopDebugging]; + } + }); } - (void)sendDevCommand:(NSString *)command payload:(NSDictionary *)payload { [self.wsClient sendToServer:command payload:payload]; } + +- (void)addStatusCallback:(id)callback { + [self.callbacks addObject:callback]; +} + +- (void)removeStatusCallback:(id)callback { + [self.callbacks removeObject:callback]; +} + +- (NSString *)ip { + return [[self.url stringByReplacingOccurrencesOfString:@"ws://" + withString:@""] + stringByReplacingOccurrencesOfString:@":7777" + withString:@""]; +} @end diff --git a/doric-iOS/Devkit/Classes/DoricDevViewController.m b/doric-iOS/Devkit/Classes/DoricDevViewController.m index 6365cb83..e2179d96 100644 --- a/doric-iOS/Devkit/Classes/DoricDevViewController.m +++ b/doric-iOS/Devkit/Classes/DoricDevViewController.m @@ -21,7 +21,6 @@ // #import #import -#import "NSString+JsonString.h" #import "DoricDev.h" #import "DoricDevViewController.h" diff --git a/doric-iOS/Devkit/Classes/DoricWSClient.m b/doric-iOS/Devkit/Classes/DoricWSClient.m index 42d9a8c0..6725f47d 100644 --- a/doric-iOS/Devkit/Classes/DoricWSClient.m +++ b/doric-iOS/Devkit/Classes/DoricWSClient.m @@ -46,6 +46,7 @@ - (instancetype)initWithUrl:(NSString *)url { - (void)webSocketDidOpen:(SRWebSocket *)webSocket { DoricLog(@"webSocketDidOpen"); [[NSNotificationCenter defaultCenter] postNotificationName:@"OpenEvent" object:nil]; + [DoricDev.instance onOpen]; } - (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload { @@ -98,12 +99,12 @@ - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message { - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error { DoricLog(@"webSocketdidFailWithError"); - [[NSNotificationCenter defaultCenter] postNotificationName:@"ConnectExceptionEvent" object:nil]; + [DoricDev.instance onFailure:error]; } - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean { DoricLog(@"webSocketdidCloseWithCode"); - [[NSNotificationCenter defaultCenter] postNotificationName:@"EOFEvent" object:nil]; + [DoricDev.instance onClose]; } - (void)send:(NSDictionary *)command {