code refactor

This commit is contained in:
王劲鹏 2019-12-11 14:52:35 +08:00
parent 63550af3ad
commit c55da9c640
8 changed files with 43 additions and 28 deletions

View File

@ -7,10 +7,11 @@ import pub.doric.DoricNativeDriver;
public class DoricContextDebuggable { public class DoricContextDebuggable {
private DoricContext doricContext; private DoricContext doricContext;
private DoricDebugDriver doricDebugDriver; private DoricDebugDriver doricDebugDriver;
public static boolean isDebugging = false; public boolean isDebugging = false;
public DoricContextDebuggable(String contextId) { public DoricContextDebuggable(String contextId) {
this.doricContext = DoricContextManager.getContext(contextId); this.doricContext = DoricContextManager.getContext(contextId);
isDebugging = true;
} }
public void startDebug() { public void startDebug() {

View File

@ -80,7 +80,7 @@ public class WSClient extends WebSocketListener {
break; break;
case "SWITCH_TO_DEBUG": { case "SWITCH_TO_DEBUG": {
String contextId = jsonObject.optString("contextId"); String contextId = jsonObject.optString("contextId");
EventBus.getDefault().post(new EnterDebugEvent(contextId)); EventBus.getDefault().post(new EnterDebugEvent());
} }
break; break;
} }

View File

@ -2,13 +2,4 @@ package pub.doric.devkit.event;
public class EnterDebugEvent { public class EnterDebugEvent {
private String contextId;
public EnterDebugEvent(String contextId) {
this.contextId = contextId;
}
public String getContextId() {
return contextId;
}
} }

View File

@ -0,0 +1,13 @@
package pub.doric.devkit.event;
public class StartDebugEvent {
private String contextId;
public StartDebugEvent(String contextId) {
this.contextId = contextId;
}
public String getContextId() {
return contextId;
}
}

View File

@ -1,4 +1,5 @@
package pub.doric.devkit.event; package pub.doric.devkit.event;
public class QuitDebugEvent { public class StopDebugEvent {
} }

View File

@ -25,7 +25,7 @@ import okhttp3.WebSocket;
import okhttp3.WebSocketListener; import okhttp3.WebSocketListener;
import pub.doric.devkit.DevKit; import pub.doric.devkit.DevKit;
import pub.doric.devkit.IStatusCallback; import pub.doric.devkit.IStatusCallback;
import pub.doric.devkit.event.QuitDebugEvent; import pub.doric.devkit.event.StopDebugEvent;
public class RemoteJSExecutor { public class RemoteJSExecutor {
private final WebSocket webSocket; private final WebSocket webSocket;
@ -59,7 +59,7 @@ public class RemoteJSExecutor {
System.out.println("remote js executor eof"); System.out.println("remote js executor eof");
LockSupport.unpark(current); LockSupport.unpark(current);
EventBus.getDefault().post(new QuitDebugEvent()); EventBus.getDefault().post(new StopDebugEvent());
} }
} }

View File

@ -15,12 +15,14 @@ import androidx.fragment.app.DialogFragment;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import org.greenrobot.eventbus.EventBus;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.DoricContextManager; import pub.doric.DoricContextManager;
import pub.doric.devkit.DoricContextDebuggable;
import pub.doric.devkit.DoricDev; import pub.doric.devkit.DoricDev;
import pub.doric.devkit.IDevKit; import pub.doric.devkit.IDevKit;
import pub.doric.devkit.R; import pub.doric.devkit.R;
import pub.doric.devkit.event.StartDebugEvent;
public class DebugContextPanel extends DialogFragment { public class DebugContextPanel extends DialogFragment {
@ -69,7 +71,7 @@ public class DebugContextPanel extends DialogFragment {
cell.findViewById(R.id.debug_text_view).setOnClickListener(new View.OnClickListener() { cell.findViewById(R.id.debug_text_view).setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
DoricContextDebuggable.isDebugging = true; EventBus.getDefault().post(new StartDebugEvent(doricContext.getContextId()));
JsonObject jsonObject = new JsonObject(); JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("contextId", doricContext.getContextId()); jsonObject.addProperty("contextId", doricContext.getContextId());
jsonObject.addProperty("source", doricContext.getSource().replace(".js", ".ts")); jsonObject.addProperty("source", doricContext.getSource().replace(".js", ".ts"));

View File

@ -30,8 +30,9 @@ import pub.doric.DoricContext;
import pub.doric.DoricContextManager; import pub.doric.DoricContextManager;
import pub.doric.devkit.DoricContextDebuggable; import pub.doric.devkit.DoricContextDebuggable;
import pub.doric.devkit.event.EnterDebugEvent; import pub.doric.devkit.event.EnterDebugEvent;
import pub.doric.devkit.event.QuitDebugEvent;
import pub.doric.devkit.event.ReloadEvent; import pub.doric.devkit.event.ReloadEvent;
import pub.doric.devkit.event.StartDebugEvent;
import pub.doric.devkit.event.StopDebugEvent;
import pub.doric.devkit.util.SensorManagerHelper; import pub.doric.devkit.util.SensorManagerHelper;
/** /**
@ -77,30 +78,36 @@ public class DemoDebugActivity extends DoricActivity {
sensorHelper.stop(); sensorHelper.stop();
} }
@Subscribe(threadMode = ThreadMode.MAIN)
public void onStartDebugEvent(StartDebugEvent startDebugEvent) {
doricContextDebuggable = new DoricContextDebuggable(startDebugEvent.getContextId());
}
@Subscribe(threadMode = ThreadMode.MAIN) @Subscribe(threadMode = ThreadMode.MAIN)
public void onEnterDebugEvent(EnterDebugEvent enterDebugEvent) { public void onEnterDebugEvent(EnterDebugEvent enterDebugEvent) {
doricContextDebuggable = new DoricContextDebuggable(enterDebugEvent.getContextId());
doricContextDebuggable.startDebug(); doricContextDebuggable.startDebug();
} }
@Subscribe(threadMode = ThreadMode.MAIN)
public void onQuitDebugEvent(StopDebugEvent quitDebugEvent) {
doricContextDebuggable.stopDebug();
}
@Subscribe(threadMode = ThreadMode.MAIN) @Subscribe(threadMode = ThreadMode.MAIN)
public void onReloadEvent(ReloadEvent reloadEvent) { public void onReloadEvent(ReloadEvent reloadEvent) {
if (DoricContextDebuggable.isDebugging) {
System.out.println("is debugging");
} else {
for (DoricContext context : DoricContextManager.aliveContexts()) { for (DoricContext context : DoricContextManager.aliveContexts()) {
if (reloadEvent.source.contains(context.getSource())) { if (reloadEvent.source.contains(context.getSource())) {
if (doricContextDebuggable != null &&
doricContextDebuggable.isDebugging &&
doricContextDebuggable.getContext().getContextId().equals(context.getContextId())) {
System.out.println("is debugging context id: " + context.getContextId());
} else {
context.reload(reloadEvent.script); context.reload(reloadEvent.script);
} }
} }
} }
} }
@Subscribe(threadMode = ThreadMode.MAIN)
public void onQuitDebugEvent(QuitDebugEvent quitDebugEvent) {
doricContextDebuggable.stopDebug();
}
@Override @Override
public boolean onKeyDown(int keyCode, KeyEvent event) { public boolean onKeyDown(int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_MENU == event.getKeyCode()) { if (KeyEvent.KEYCODE_MENU == event.getKeyCode()) {