From fcd4449a9d9376cc002bca2a89288e5ba50efc43 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Fri, 23 Jul 2021 10:36:49 +0800 Subject: [PATCH] feat:when reload script,reload all matched context,when debug,target last matched context --- .../main/java/pub/doric/devkit/DoricDev.java | 53 ++++++++++-------- doric-iOS/Devkit/Classes/DoricDev.m | 55 ++++++++++--------- 2 files changed, 60 insertions(+), 48 deletions(-) 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 8dfe2e84..54b8eb37 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 @@ -11,7 +11,10 @@ import org.json.JSONObject; import java.io.EOFException; import java.net.ConnectException; +import java.util.ArrayList; +import java.util.Collection; import java.util.HashSet; +import java.util.List; import java.util.Set; import java.util.WeakHashMap; @@ -120,13 +123,14 @@ public class DoricDev { if (debuggable != null) { debuggable.stopDebug(true); } - final DoricContext context = matchContext(source); - if (context == null) { + List contexts = matchAllContext(source); + if (contexts.size() <= 0) { DoricLog.d("Cannot find context source %s for debugging", source); wsClient.sendToDebugger("DEBUG_STOP", new JSONBuilder() .put("msg", "Cannot find suitable alive context for debugging") .toJSONObject()); } else { + final DoricContext context = contexts.get(contexts.size() - 1); wsClient.sendToDebugger( "DEBUG_RES", new JSONBuilder() @@ -217,40 +221,45 @@ public class DoricDev { }); } - public DoricContext matchContext(String source) { + private List matchAllContext(String source) { + List list = new ArrayList<>(); source = source.replace(".js", "").replace(".ts", ""); for (DoricContext context : DoricContextManager.aliveContexts()) { String doricSource = context.getSource().replace(".js", "").replace(".ts", ""); if (source.equals(doricSource) || doricSource.equals("__dev__")) { - return context; + list.add(context); } } - return null; + return list; } public void reload(String source, final String script) { - final DoricContext context = matchContext(source); - if (context == null) { + List contexts = matchAllContext(source); + if (contexts.size() <= 0) { DoricLog.d("Cannot find context source %s for reload", source); - } else if (context.getDriver() instanceof DoricDebugDriver) { - DoricLog.d("Context source %s in debugging,skip reload", source); } else { - DoricLog.d("Context reload :id %s,source %s ", context.getContextId(), source); - uiHandler.post(new Runnable() { - @Override - public void run() { - context.reload(script); - if (reloadingContexts.get(context.getContextId()) == null) { - reloadingContexts.put(context.getContextId(), context); - } + for (final DoricContext context : contexts) { + if (context.getDriver() instanceof DoricDebugDriver) { + DoricLog.d("Context source %s in debugging,skip reload", source); + } else { + DoricLog.d("Context reload :id %s,source %s ", context.getContextId(), source); + uiHandler.post(new Runnable() { + @Override + public void run() { + context.reload(script); + if (reloadingContexts.get(context.getContextId()) == null) { + reloadingContexts.put(context.getContextId(), context); + } - for (StatusCallback callback : callbacks) { - callback.onReload(context, script); - } + for (StatusCallback callback : callbacks) { + callback.onReload(context, script); + } + } + }); } - }); - + } } + } public boolean isReloadingContext(DoricContext context) { diff --git a/doric-iOS/Devkit/Classes/DoricDev.m b/doric-iOS/Devkit/Classes/DoricDev.m index 24999955..bee8f77d 100644 --- a/doric-iOS/Devkit/Classes/DoricDev.m +++ b/doric-iOS/Devkit/Classes/DoricDev.m @@ -175,7 +175,8 @@ - (BOOL)isReloadingContext:(DoricContext *)context { } -- (DoricContext *)matchContext:(NSString *)source { +- (NSArray *)matchAllContexts:(NSString *)source { + NSMutableArray *array = [NSMutableArray new]; source = [[source stringByReplacingOccurrencesOfString:@".js" withString:@""] stringByReplacingOccurrencesOfString:@".ts" @@ -188,37 +189,44 @@ - (DoricContext *)matchContext:(NSString *)source { withString:@"" ]; if ([source isEqualToString:contextSource] || [contextSource isEqualToString:@"__dev__"]) { - return context; + [array addObject:context]; } } - return nil; + return array; } - (void)reload:(NSString *)source script:(NSString *)script { - DoricContext *context = [self matchContext:source]; - if (context) { - if ([context.driver isKindOfClass:DoricDebugDriver.class]) { - DoricLog(@"Context source %@ in debugging,skip reload", source); - } else { - DoricLog(@"Context reload :id %@,source %@", context.contextId, source); - - dispatch_async(dispatch_get_main_queue(), ^{ - [context reload:script]; - [self.reloadingContexts addObject:context]; - for (id callback in self.callbacks) { - [callback onReload:context script:script]; - } - }); - } - } else { + NSArray *contexts = [self matchAllContexts:source]; + if (contexts.count <= 0) { DoricLog(@"Cannot find context source %@ for reload", source); + } else { + [contexts forEach:^(DoricContext *context) { + if ([context.driver isKindOfClass:DoricDebugDriver.class]) { + DoricLog(@"Context source %@ in debugging,skip reload", source); + } else { + DoricLog(@"Context reload :id %@,source %@", context.contextId, source); + dispatch_async(dispatch_get_main_queue(), ^{ + [context reload:script]; + [self.reloadingContexts addObject:context]; + for (id callback in self.callbacks) { + [callback onReload:context script:script]; + } + }); + } + }]; } } - (void)startDebugging:(NSString *)source { [self.debuggable stopDebug:YES]; - DoricContext *context = [self matchContext:source]; - if (context) { + NSArray *contexts = [self matchAllContexts:source]; + if (contexts.count <= 0) { + DoricLog(@"Cannot find context source %@ for debugging", source); + [self.wsClient sendToDebugger:@"DEBUG_STOP" payload:@{ + @"msg": @"Cannot find suitable alive context for debugging" + }]; + } else { + DoricContext *context = contexts.lastObject; [self.wsClient sendToDebugger:@"DEBUG_RES" payload:@{ @"contextId": context.contextId }]; @@ -229,11 +237,6 @@ - (void)startDebugging:(NSString *)source { [callback onStartDebugging:context]; } }); - } else { - DoricLog(@"Cannot find context source %@ for debugging", source); - [self.wsClient sendToDebugger:@"DEBUG_STOP" payload:@{ - @"msg": @"Cannot find suitable alive context for debugging" - }]; } };