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

@@ -23,7 +23,23 @@
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
@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 <DoricDevStatusCallback>)callback;
- (void)removeStatusCallback:(id <DoricDevStatusCallback>)callback;
@end
NS_ASSUME_NONNULL_END

View File

@@ -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 <id <DoricDevStatusCallback>> *callbacks;
@property(nonatomic, strong) NSHashTable <DoricContext *> *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 <DoricDevStatusCallback> 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 <DoricDevStatusCallback> 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 <DoricDevStatusCallback> 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 <DoricDevStatusCallback> 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 <DoricDevStatusCallback> 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 <DoricDevStatusCallback> 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

View File

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

View File

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