feat:when reload script,reload all matched context,when debug,target last matched context
This commit is contained in:
		@@ -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<DoricContext> 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<DoricContext> matchAllContext(String source) {
 | 
			
		||||
        List<DoricContext> 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<DoricContext> 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) {
 | 
			
		||||
 
 | 
			
		||||
@@ -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"
 | 
			
		||||
                                                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 <DoricDevStatusCallback> callback in self.callbacks) {
 | 
			
		||||
                    [callback onReload:context script:script];
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
    NSArray<DoricContext *> *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 <DoricDevStatusCallback> 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<DoricContext *> *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"
 | 
			
		||||
        }];
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user