feat:when reload script,reload all matched context,when debug,target last matched context

This commit is contained in:
pengfei.zhou 2021-07-23 10:36:49 +08:00 committed by osborn
parent f75a6f8071
commit fcd4449a9d
2 changed files with 60 additions and 48 deletions

View File

@ -11,7 +11,10 @@ import org.json.JSONObject;
import java.io.EOFException; import java.io.EOFException;
import java.net.ConnectException; import java.net.ConnectException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.WeakHashMap; import java.util.WeakHashMap;
@ -120,13 +123,14 @@ public class DoricDev {
if (debuggable != null) { if (debuggable != null) {
debuggable.stopDebug(true); debuggable.stopDebug(true);
} }
final DoricContext context = matchContext(source); List<DoricContext> contexts = matchAllContext(source);
if (context == null) { if (contexts.size() <= 0) {
DoricLog.d("Cannot find context source %s for debugging", source); DoricLog.d("Cannot find context source %s for debugging", source);
wsClient.sendToDebugger("DEBUG_STOP", new JSONBuilder() wsClient.sendToDebugger("DEBUG_STOP", new JSONBuilder()
.put("msg", "Cannot find suitable alive context for debugging") .put("msg", "Cannot find suitable alive context for debugging")
.toJSONObject()); .toJSONObject());
} else { } else {
final DoricContext context = contexts.get(contexts.size() - 1);
wsClient.sendToDebugger( wsClient.sendToDebugger(
"DEBUG_RES", "DEBUG_RES",
new JSONBuilder() new JSONBuilder()
@ -217,40 +221,45 @@ public class DoricDev {
}); });
} }
public DoricContext matchContext(String source) { private List<DoricContext> matchAllContext(String source) {
List<DoricContext> list = new ArrayList<>();
source = source.replace(".js", "").replace(".ts", ""); source = source.replace(".js", "").replace(".ts", "");
for (DoricContext context : DoricContextManager.aliveContexts()) { for (DoricContext context : DoricContextManager.aliveContexts()) {
String doricSource = context.getSource().replace(".js", "").replace(".ts", ""); String doricSource = context.getSource().replace(".js", "").replace(".ts", "");
if (source.equals(doricSource) || doricSource.equals("__dev__")) { if (source.equals(doricSource) || doricSource.equals("__dev__")) {
return context; list.add(context);
} }
} }
return null; return list;
} }
public void reload(String source, final String script) { public void reload(String source, final String script) {
final DoricContext context = matchContext(source); List<DoricContext> contexts = matchAllContext(source);
if (context == null) { if (contexts.size() <= 0) {
DoricLog.d("Cannot find context source %s for reload", source); 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 { } else {
DoricLog.d("Context reload :id %s,source %s ", context.getContextId(), source); for (final DoricContext context : contexts) {
uiHandler.post(new Runnable() { if (context.getDriver() instanceof DoricDebugDriver) {
@Override DoricLog.d("Context source %s in debugging,skip reload", source);
public void run() { } else {
context.reload(script); DoricLog.d("Context reload :id %s,source %s ", context.getContextId(), source);
if (reloadingContexts.get(context.getContextId()) == null) { uiHandler.post(new Runnable() {
reloadingContexts.put(context.getContextId(), context); @Override
} public void run() {
context.reload(script);
if (reloadingContexts.get(context.getContextId()) == null) {
reloadingContexts.put(context.getContextId(), context);
}
for (StatusCallback callback : callbacks) { for (StatusCallback callback : callbacks) {
callback.onReload(context, script); callback.onReload(context, script);
} }
}
});
} }
}); }
} }
} }
public boolean isReloadingContext(DoricContext context) { public boolean isReloadingContext(DoricContext context) {

View File

@ -175,7 +175,8 @@ - (BOOL)isReloadingContext:(DoricContext *)context {
} }
- (DoricContext *)matchContext:(NSString *)source { - (NSArray<DoricContext *> *)matchAllContexts:(NSString *)source {
NSMutableArray <DoricContext *> *array = [NSMutableArray new];
source = [[source stringByReplacingOccurrencesOfString:@".js" source = [[source stringByReplacingOccurrencesOfString:@".js"
withString:@""] withString:@""]
stringByReplacingOccurrencesOfString:@".ts" stringByReplacingOccurrencesOfString:@".ts"
@ -188,37 +189,44 @@ - (DoricContext *)matchContext:(NSString *)source {
withString:@"" withString:@""
]; ];
if ([source isEqualToString:contextSource] || [contextSource isEqualToString:@"__dev__"]) { if ([source isEqualToString:contextSource] || [contextSource isEqualToString:@"__dev__"]) {
return context; [array addObject:context];
} }
} }
return nil; return array;
} }
- (void)reload:(NSString *)source script:(NSString *)script { - (void)reload:(NSString *)source script:(NSString *)script {
DoricContext *context = [self matchContext:source]; NSArray<DoricContext *> *contexts = [self matchAllContexts:source];
if (context) { if (contexts.count <= 0) {
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 <DoricDevStatusCallback> callback in self.callbacks) {
[callback onReload:context script:script];
}
});
}
} else {
DoricLog(@"Cannot find context source %@ for reload", source); 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 <DoricDevStatusCallback> callback in self.callbacks) {
[callback onReload:context script:script];
}
});
}
}];
} }
} }
- (void)startDebugging:(NSString *)source { - (void)startDebugging:(NSString *)source {
[self.debuggable stopDebug:YES]; [self.debuggable stopDebug:YES];
DoricContext *context = [self matchContext:source]; NSArray<DoricContext *> *contexts = [self matchAllContexts:source];
if (context) { 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:@{ [self.wsClient sendToDebugger:@"DEBUG_RES" payload:@{
@"contextId": context.contextId @"contextId": context.contextId
}]; }];
@ -229,11 +237,6 @@ - (void)startDebugging:(NSString *)source {
[callback onStartDebugging:context]; [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"
}];
} }
}; };