Refact:delete and optimize code
This commit is contained in:
parent
2f7762e670
commit
04c19992ad
@ -113,7 +113,7 @@ public class DoricDebugDriver implements IDoricDriver {
|
||||
public Boolean call() {
|
||||
try {
|
||||
if (contextId.equals(theContextId)) {
|
||||
DevKit.getInstance().stopDebugging(false);
|
||||
DoricDev.getInstance().stopDebugging(false);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
|
@ -1,18 +1,38 @@
|
||||
package pub.doric.devkit;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.github.pengfeizhou.jscore.JSONBuilder;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
import org.greenrobot.eventbus.Subscribe;
|
||||
import org.greenrobot.eventbus.ThreadMode;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import pub.doric.Doric;
|
||||
import pub.doric.DoricContext;
|
||||
import pub.doric.DoricContextManager;
|
||||
import pub.doric.DoricNativeDriver;
|
||||
import pub.doric.devkit.event.ConnectExceptionEvent;
|
||||
import pub.doric.devkit.event.EOFExceptionEvent;
|
||||
import pub.doric.devkit.event.OpenEvent;
|
||||
import pub.doric.devkit.event.StopDebugEvent;
|
||||
import pub.doric.devkit.ui.DoricDevActivity;
|
||||
import pub.doric.devkit.util.SimulatorUtil;
|
||||
import pub.doric.utils.DoricLog;
|
||||
|
||||
public class DoricDev {
|
||||
public final boolean isRunningInEmulator;
|
||||
|
||||
private static class Inner {
|
||||
private static final DoricDev sInstance = new DoricDev();
|
||||
}
|
||||
|
||||
private DoricDev() {
|
||||
DevKit.isRunningInEmulator = SimulatorUtil.isSimulator(Doric.application());
|
||||
this.isRunningInEmulator = SimulatorUtil.isSimulator(Doric.application());
|
||||
DoricNativeDriver.getInstance().getRegistry().registerMonitor(new DoricDevMonitor());
|
||||
EventBus.getDefault().register(this);
|
||||
}
|
||||
|
||||
public static DoricDev getInstance() {
|
||||
@ -26,10 +46,106 @@ public class DoricDev {
|
||||
}
|
||||
|
||||
public void closeDevMode() {
|
||||
DevKit.getInstance().disconnectDevKit();
|
||||
disconnectDevKit();
|
||||
}
|
||||
|
||||
public boolean isInDevMode() {
|
||||
return DevKit.getInstance().devKitConnected;
|
||||
return devKitConnected;
|
||||
}
|
||||
|
||||
|
||||
private WSClient wsClient;
|
||||
|
||||
private boolean devKitConnected = false;
|
||||
|
||||
private DoricContextDebuggable debuggable;
|
||||
|
||||
public void connectDevKit(String url) {
|
||||
wsClient = new WSClient(url);
|
||||
}
|
||||
|
||||
public void sendDevCommand(String command, JSONObject jsonObject) {
|
||||
wsClient.sendToServer(command, jsonObject);
|
||||
}
|
||||
|
||||
public void disconnectDevKit() {
|
||||
wsClient.close();
|
||||
wsClient = null;
|
||||
}
|
||||
|
||||
public void startDebugging(String source) {
|
||||
if (debuggable != null) {
|
||||
debuggable.stopDebug(true);
|
||||
}
|
||||
DoricContext context = matchContext(source);
|
||||
if (context == null) {
|
||||
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 {
|
||||
wsClient.sendToDebugger(
|
||||
"DEBUG_RES",
|
||||
new JSONBuilder()
|
||||
.put("contextId", context.getContextId())
|
||||
.toJSONObject());
|
||||
debuggable = new DoricContextDebuggable(wsClient, context);
|
||||
debuggable.startDebug();
|
||||
}
|
||||
}
|
||||
|
||||
public void stopDebugging(boolean resume) {
|
||||
wsClient.sendToDebugger("DEBUG_STOP", new JSONBuilder()
|
||||
.put("msg", "Stop debugging")
|
||||
.toJSONObject());
|
||||
if (debuggable != null) {
|
||||
debuggable.stopDebug(resume);
|
||||
debuggable = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onOpenEvent(OpenEvent openEvent) {
|
||||
devKitConnected = true;
|
||||
Toast.makeText(Doric.application(), "dev kit connected", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onEOFEvent(EOFExceptionEvent eofExceptionEvent) {
|
||||
devKitConnected = false;
|
||||
Toast.makeText(Doric.application(), "dev kit eof exception", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onConnectExceptionEvent(ConnectExceptionEvent connectExceptionEvent) {
|
||||
devKitConnected = false;
|
||||
Toast.makeText(Doric.application(), "dev kit connection exception", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onQuitDebugEvent(StopDebugEvent quitDebugEvent) {
|
||||
stopDebugging(true);
|
||||
}
|
||||
|
||||
public DoricContext matchContext(String source) {
|
||||
for (DoricContext context : DoricContextManager.aliveContexts()) {
|
||||
if (source.contains(context.getSource()) || context.getSource().equals("__dev__")) {
|
||||
return context;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void reload(String source, String script) {
|
||||
DoricContext context = matchContext(source);
|
||||
if (context == null) {
|
||||
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);
|
||||
context.reload(script);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -39,8 +39,8 @@ public class DoricDevMonitor implements IDoricMonitor {
|
||||
}
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
e.printStackTrace(new PrintWriter(stringWriter));
|
||||
DevKit.getInstance().sendDevCommand(
|
||||
IDevKit.Command.EXCEPTION,
|
||||
DoricDev.getInstance().sendDevCommand(
|
||||
"EXCEPTION",
|
||||
new JSONBuilder()
|
||||
.put("source", "In source file: " + (context != null ? context.getSource() : "Unknown"))
|
||||
.put("exception", stringWriter.toString())
|
||||
@ -66,8 +66,8 @@ public class DoricDevMonitor implements IDoricMonitor {
|
||||
DoricLog.suffix_d("_js", message);
|
||||
break;
|
||||
}
|
||||
DevKit.getInstance().sendDevCommand(
|
||||
IDevKit.Command.LOG,
|
||||
DoricDev.getInstance().sendDevCommand(
|
||||
"LOG",
|
||||
new JSONBuilder()
|
||||
.put("type", typeString)
|
||||
.put("message", message)
|
||||
|
@ -32,7 +32,6 @@ import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.WebSocket;
|
||||
import okhttp3.WebSocketListener;
|
||||
import pub.doric.DoricContext;
|
||||
import pub.doric.devkit.event.ConnectExceptionEvent;
|
||||
import pub.doric.devkit.event.EOFExceptionEvent;
|
||||
import pub.doric.devkit.event.OpenEvent;
|
||||
@ -96,13 +95,13 @@ public class WSClient extends WebSocketListener {
|
||||
}
|
||||
if ("DEBUG_REQ".equals(cmd)) {
|
||||
String source = payload.optString("source");
|
||||
DevKit.getInstance().startDebugging(source);
|
||||
DoricDev.getInstance().startDebugging(source);
|
||||
} else if ("DEBUG_STOP".equals(cmd)) {
|
||||
DevKit.getInstance().stopDebugging(true);
|
||||
DoricDev.getInstance().stopDebugging(true);
|
||||
} else if ("RELOAD".equals(cmd)) {
|
||||
String source = payload.optString("source");
|
||||
String script = payload.optString("script");
|
||||
DevKit.getInstance().reload(source, script);
|
||||
DoricDev.getInstance().reload(source, script);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -25,9 +25,7 @@ import org.greenrobot.eventbus.ThreadMode;
|
||||
|
||||
import pub.doric.DoricContext;
|
||||
import pub.doric.DoricContextManager;
|
||||
import pub.doric.devkit.DevKit;
|
||||
import pub.doric.devkit.DoricDev;
|
||||
import pub.doric.devkit.IDevKit;
|
||||
import pub.doric.devkit.R;
|
||||
import pub.doric.devkit.event.ConnectExceptionEvent;
|
||||
import pub.doric.devkit.event.EOFExceptionEvent;
|
||||
@ -49,9 +47,8 @@ public class DoricDevActivity extends AppCompatActivity {
|
||||
if (DoricDev.getInstance().isInDevMode()) {
|
||||
initViews();
|
||||
} else {
|
||||
if (DevKit.isRunningInEmulator) {
|
||||
DevKit.ip = "10.0.2.2";
|
||||
DevKit.getInstance().connectDevKit("ws://" + DevKit.ip + ":7777");
|
||||
if (DoricDev.getInstance().isRunningInEmulator) {
|
||||
DoricDev.getInstance().connectDevKit("ws://" + "10.0.2.2" + ":7777");
|
||||
} else {
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
@ -93,9 +90,8 @@ public class DoricDevActivity extends AppCompatActivity {
|
||||
}
|
||||
if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
|
||||
String result = bundle.getString(CodeUtils.RESULT_STRING);
|
||||
DevKit.ip = result;
|
||||
Toast.makeText(this, "dev kit connecting to " + result, Toast.LENGTH_LONG).show();
|
||||
DevKit.getInstance().connectDevKit("ws://" + result + ":7777");
|
||||
DoricDev.getInstance().connectDevKit("ws://" + result + ":7777");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -153,8 +149,8 @@ public class DoricDevActivity extends AppCompatActivity {
|
||||
cell.findViewById(R.id.debug_text_view).setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
DevKit.getInstance().sendDevCommand(
|
||||
IDevKit.Command.DEBUG,
|
||||
DoricDev.getInstance().sendDevCommand(
|
||||
"DEBUG",
|
||||
new JSONBuilder()
|
||||
.put("source", doricContext.getSource())
|
||||
.toJSONObject());
|
||||
|
@ -24,7 +24,6 @@
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface DoricDev : NSObject
|
||||
@property(nonatomic, strong, nullable) DoricWSClient *wsClient;
|
||||
|
||||
+ (instancetype)instance;
|
||||
|
||||
@ -41,6 +40,8 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
- (void)stopDebugging:(BOOL)resume;
|
||||
|
||||
- (void)reload:(NSString *)source script:(NSString *)script;
|
||||
|
||||
- (void)sendDevCommand:(NSString *)command payload:(NSDictionary *)payload;
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
@ -63,6 +63,7 @@ - (void)stopDebug:(BOOL)resume {
|
||||
|
||||
|
||||
@interface DoricDev ()
|
||||
@property(nonatomic, strong, nullable) DoricWSClient *wsClient;
|
||||
@property(nonatomic, strong) DoricContextDebuggable *debuggable;
|
||||
@end
|
||||
|
||||
@ -177,4 +178,8 @@ - (void)stopDebugging:(BOOL)resume {
|
||||
[self.debuggable stopDebug:resume];
|
||||
self.debuggable = nil;
|
||||
}
|
||||
|
||||
- (void)sendDevCommand:(NSString *)command payload:(NSDictionary *)payload {
|
||||
[self.wsClient sendToServer:command payload:payload];
|
||||
}
|
||||
@end
|
||||
|
@ -30,7 +30,7 @@ - (void)onException:(NSException *)exception inContext:(DoricContext *)context {
|
||||
if (!DoricDev.instance.isInDevMode) {
|
||||
return;
|
||||
}
|
||||
[DoricDev.instance.wsClient sendToServer:@"EXCEPTION"
|
||||
[DoricDev.instance sendDevCommand:@"EXCEPTION"
|
||||
payload:@{
|
||||
@"source": [context.source stringByReplacingOccurrencesOfString:@".js" withString:@".ts"],
|
||||
@"exception": exception.reason
|
||||
@ -48,7 +48,7 @@ - (void)onLog:(DoricLogType)type message:(NSString *)message {
|
||||
typeString = @"ERROR";
|
||||
}
|
||||
|
||||
[DoricDev.instance.wsClient sendToServer:@"LOG"
|
||||
[DoricDev.instance sendDevCommand:@"LOG"
|
||||
payload:@{
|
||||
@"type": typeString,
|
||||
@"message": message
|
||||
|
Reference in New Issue
Block a user