iOS:optimze devkit

This commit is contained in:
pengfeizhou 2021-02-24 15:35:44 +08:00 committed by osborn
parent f3d66b6113
commit efdabd8309
7 changed files with 104 additions and 21 deletions

View File

@ -32,7 +32,7 @@ public class DoricDev {
void onFailure(Throwable throwable); void onFailure(Throwable throwable);
void onReload(DoricContext context, String source); void onReload(DoricContext context, String script);
void onStartDebugging(DoricContext context); void onStartDebugging(DoricContext context);
@ -82,11 +82,11 @@ public class DoricDev {
private DoricContextDebuggable debuggable; private DoricContextDebuggable debuggable;
private String url; private String url;
public void addStatusListener(StatusCallback listener) { public void addStatusCallback(StatusCallback listener) {
this.callbacks.add(listener); this.callbacks.add(listener);
} }
public void removeStatusListener(StatusCallback listener) { public void removeStatusCallback(StatusCallback listener) {
this.callbacks.remove(listener); this.callbacks.remove(listener);
} }

View File

@ -62,13 +62,13 @@ public class DoricDevActivity extends AppCompatActivity implements DoricDev.Stat
@Override @Override
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();
DoricDev.getInstance().addStatusListener(this); DoricDev.getInstance().addStatusCallback(this);
} }
@Override @Override
protected void onStop() { protected void onStop() {
super.onStop(); super.onStop();
DoricDev.getInstance().removeStatusListener(this); DoricDev.getInstance().removeStatusCallback(this);
} }
@Override @Override
@ -197,7 +197,7 @@ public class DoricDevActivity extends AppCompatActivity implements DoricDev.Stat
} }
@Override @Override
public void onReload(DoricContext context, String source) { public void onReload(DoricContext context, String script) {
this.cellAdapter.notifyDataSetChanged(); this.cellAdapter.notifyDataSetChanged();
} }

View File

@ -59,7 +59,7 @@ class CounterVM extends ViewModel<CountModel, CounterView> {
} }
onBind(s: CountModel, vh: CounterView) { onBind(s: CountModel, vh: CounterView) {
vh.number.text = `${s.count}`; vh.number.text = `${s.count}`;
log("onBind\nseee"); log("onBind");
logw("onBind"); logw("onBind");
loge("onBind"); loge("onBind");
} }

View File

@ -23,7 +23,23 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@protocol DoricDevStatusCallback <NSObject>
- (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 @interface DoricDev : NSObject
@property(nonatomic, readonly) NSString *ip;
+ (instancetype)instance; + (instancetype)instance;
@ -35,13 +51,25 @@ NS_ASSUME_NONNULL_BEGIN
- (void)connectDevKit:(NSString *)url; - (void)connectDevKit:(NSString *)url;
- (void)onOpen;
- (void)onClose;
- (void)onFailure:(NSError *)error;
- (void)startDebugging:(NSString *)source; - (void)startDebugging:(NSString *)source;
- (void)stopDebugging:(BOOL)resume; - (void)stopDebugging:(BOOL)resume;
- (BOOL)isReloadingContext:(DoricContext *)context;
- (void)reload:(NSString *)source script:(NSString *)script; - (void)reload:(NSString *)source script:(NSString *)script;
- (void)sendDevCommand:(NSString *)command payload:(NSDictionary *)payload; - (void)sendDevCommand:(NSString *)command payload:(NSDictionary *)payload;
- (void)addStatusCallback:(id <DoricDevStatusCallback>)callback;
- (void)removeStatusCallback:(id <DoricDevStatusCallback>)callback;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

View File

@ -65,15 +65,17 @@ - (void)stopDebug:(BOOL)resume {
@interface DoricDev () @interface DoricDev ()
@property(nonatomic, strong, nullable) DoricWSClient *wsClient; @property(nonatomic, strong, nullable) DoricWSClient *wsClient;
@property(nonatomic, strong) DoricContextDebuggable *debuggable; @property(nonatomic, strong) DoricContextDebuggable *debuggable;
@property(nonatomic, strong) NSHashTable <id <DoricDevStatusCallback>> *callbacks;
@property(nonatomic, strong) NSHashTable <DoricContext *> *reloadingContexts;
@property(nonatomic, assign) BOOL devKitConnected;
@property(nonatomic, copy) NSString *url;
@end @end
@implementation DoricDev @implementation DoricDev
- (instancetype)init { - (instancetype)init {
if (self = [super init]) { if (self = [super init]) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onOpenEvent) name:@"OpenEvent" object:nil]; _callbacks = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onEOFEvent) name:@"EOFEvent" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onConnectExceptionEvent) name:@"ConnectExceptionEvent" object:nil];
[DoricNativeDriver.instance.registry registerMonitor:[DoricDevMonitor new]]; [DoricNativeDriver.instance.registry registerMonitor:[DoricDevMonitor new]];
} }
return self; return self;
@ -109,28 +111,50 @@ - (void)closeDevMode {
} }
- (BOOL)isInDevMode { - (BOOL)isInDevMode {
return self.wsClient != nil; return self.devKitConnected;
} }
- (void)connectDevKit:(NSString *)url { - (void)connectDevKit:(NSString *)url {
if (self.wsClient) { if (self.wsClient) {
[self.wsClient close]; [self.wsClient close];
} }
self.devKitConnected = NO;
self.wsClient = [[DoricWSClient alloc] initWithUrl:url]; self.wsClient = [[DoricWSClient alloc] initWithUrl:url];
self.url = url;
} }
- (void)onOpenEvent { - (void)onOpen {
ShowToast(@"dev kit connected", DoricGravityBottom); self.devKitConnected = YES;
dispatch_async(dispatch_get_main_queue(), ^{
for (id <DoricDevStatusCallback> callback in self.callbacks) {
[callback onOpen:self.url];
}
});
} }
- (void)onEOFEvent { - (void)onClose {
ShowToast(@"dev kit eof exception", DoricGravityBottom); self.devKitConnected = NO;
dispatch_async(dispatch_get_main_queue(), ^{
for (id <DoricDevStatusCallback> callback in self.callbacks) {
[callback onClose:self.url];
}
});
} }
- (void)onConnectExceptionEvent { - (void)onFailure:(NSError *)error {
ShowToast(@"dev kit connection exception", DoricGravityBottom); self.devKitConnected = NO;
dispatch_async(dispatch_get_main_queue(), ^{
for (id <DoricDevStatusCallback> callback in self.callbacks) {
[callback onFailure:error];
}
});
} }
- (BOOL)isReloadingContext:(DoricContext *)context {
return [self.reloadingContexts containsObject:context];
}
- (DoricContext *)matchContext:(NSString *)source { - (DoricContext *)matchContext:(NSString *)source {
for (DoricContext *context in [DoricContextManager.instance aliveContexts]) { for (DoricContext *context in [DoricContextManager.instance aliveContexts]) {
if ([source containsString:context.source] || [context.source isEqualToString:@"__dev__"]) { if ([source containsString:context.source] || [context.source isEqualToString:@"__dev__"]) {
@ -148,6 +172,12 @@ - (void)reload:(NSString *)source script:(NSString *)script {
} else { } else {
DoricLog(@"Context reload :id %@,source %@", context.contextId, source); DoricLog(@"Context reload :id %@,source %@", context.contextId, source);
[context reload:script]; [context reload:script];
[self.reloadingContexts addObject:context];
dispatch_async(dispatch_get_main_queue(), ^{
for (id <DoricDevStatusCallback> callback in self.callbacks) {
[callback onReload:context script:script];
}
});
} }
} else { } else {
DoricLog(@"Cannot find context source %@ for reload", source); 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 = [[DoricContextDebuggable alloc] initWithWSClient:self.wsClient context:context];
[self.debuggable startDebug]; [self.debuggable startDebug];
dispatch_async(dispatch_get_main_queue(), ^{
for (id <DoricDevStatusCallback> callback in self.callbacks) {
[callback onStartDebugging:context];
}
});
} else { } else {
DoricLog(@"Cannot find context source %@ for debugging", source); DoricLog(@"Cannot find context source %@ for debugging", source);
[self.wsClient sendToDebugger:@"DEBUG_STOP" payload:@{ [self.wsClient sendToDebugger:@"DEBUG_STOP" payload:@{
@ -177,9 +212,29 @@ - (void)stopDebugging:(BOOL)resume {
}]; }];
[self.debuggable stopDebug:resume]; [self.debuggable stopDebug:resume];
self.debuggable = nil; self.debuggable = nil;
dispatch_async(dispatch_get_main_queue(), ^{
for (id <DoricDevStatusCallback> callback in self.callbacks) {
[callback onStopDebugging];
}
});
} }
- (void)sendDevCommand:(NSString *)command payload:(NSDictionary *)payload { - (void)sendDevCommand:(NSString *)command payload:(NSDictionary *)payload {
[self.wsClient sendToServer:command payload: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 @end

View File

@ -21,7 +21,6 @@
// //
#import <DoricCore/Doric.h> #import <DoricCore/Doric.h>
#import <DoricCore/DoricContextManager.h> #import <DoricCore/DoricContextManager.h>
#import "NSString+JsonString.h"
#import "DoricDev.h" #import "DoricDev.h"
#import "DoricDevViewController.h" #import "DoricDevViewController.h"

View File

@ -46,6 +46,7 @@ - (instancetype)initWithUrl:(NSString *)url {
- (void)webSocketDidOpen:(SRWebSocket *)webSocket { - (void)webSocketDidOpen:(SRWebSocket *)webSocket {
DoricLog(@"webSocketDidOpen"); DoricLog(@"webSocketDidOpen");
[[NSNotificationCenter defaultCenter] postNotificationName:@"OpenEvent" object:nil]; [[NSNotificationCenter defaultCenter] postNotificationName:@"OpenEvent" object:nil];
[DoricDev.instance onOpen];
} }
- (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload { - (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 { - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error {
DoricLog(@"webSocketdidFailWithError"); 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 { - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean {
DoricLog(@"webSocketdidCloseWithCode"); DoricLog(@"webSocketdidCloseWithCode");
[[NSNotificationCenter defaultCenter] postNotificationName:@"EOFEvent" object:nil]; [DoricDev.instance onClose];
} }
- (void)send:(NSDictionary *)command { - (void)send:(NSDictionary *)command {