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() {
|
public Boolean call() {
|
||||||
try {
|
try {
|
||||||
if (contextId.equals(theContextId)) {
|
if (contextId.equals(theContextId)) {
|
||||||
DevKit.getInstance().stopDebugging(false);
|
DoricDev.getInstance().stopDebugging(false);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -1,18 +1,38 @@
|
|||||||
package pub.doric.devkit;
|
package pub.doric.devkit;
|
||||||
|
|
||||||
import android.content.Intent;
|
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.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.ui.DoricDevActivity;
|
||||||
import pub.doric.devkit.util.SimulatorUtil;
|
import pub.doric.devkit.util.SimulatorUtil;
|
||||||
|
import pub.doric.utils.DoricLog;
|
||||||
|
|
||||||
public class DoricDev {
|
public class DoricDev {
|
||||||
|
public final boolean isRunningInEmulator;
|
||||||
|
|
||||||
private static class Inner {
|
private static class Inner {
|
||||||
private static final DoricDev sInstance = new DoricDev();
|
private static final DoricDev sInstance = new DoricDev();
|
||||||
}
|
}
|
||||||
|
|
||||||
private 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() {
|
public static DoricDev getInstance() {
|
||||||
@ -26,10 +46,106 @@ public class DoricDev {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void closeDevMode() {
|
public void closeDevMode() {
|
||||||
DevKit.getInstance().disconnectDevKit();
|
disconnectDevKit();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isInDevMode() {
|
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();
|
StringWriter stringWriter = new StringWriter();
|
||||||
e.printStackTrace(new PrintWriter(stringWriter));
|
e.printStackTrace(new PrintWriter(stringWriter));
|
||||||
DevKit.getInstance().sendDevCommand(
|
DoricDev.getInstance().sendDevCommand(
|
||||||
IDevKit.Command.EXCEPTION,
|
"EXCEPTION",
|
||||||
new JSONBuilder()
|
new JSONBuilder()
|
||||||
.put("source", "In source file: " + (context != null ? context.getSource() : "Unknown"))
|
.put("source", "In source file: " + (context != null ? context.getSource() : "Unknown"))
|
||||||
.put("exception", stringWriter.toString())
|
.put("exception", stringWriter.toString())
|
||||||
@ -66,8 +66,8 @@ public class DoricDevMonitor implements IDoricMonitor {
|
|||||||
DoricLog.suffix_d("_js", message);
|
DoricLog.suffix_d("_js", message);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
DevKit.getInstance().sendDevCommand(
|
DoricDev.getInstance().sendDevCommand(
|
||||||
IDevKit.Command.LOG,
|
"LOG",
|
||||||
new JSONBuilder()
|
new JSONBuilder()
|
||||||
.put("type", typeString)
|
.put("type", typeString)
|
||||||
.put("message", message)
|
.put("message", message)
|
||||||
|
@ -32,7 +32,6 @@ import okhttp3.Request;
|
|||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
import okhttp3.WebSocket;
|
import okhttp3.WebSocket;
|
||||||
import okhttp3.WebSocketListener;
|
import okhttp3.WebSocketListener;
|
||||||
import pub.doric.DoricContext;
|
|
||||||
import pub.doric.devkit.event.ConnectExceptionEvent;
|
import pub.doric.devkit.event.ConnectExceptionEvent;
|
||||||
import pub.doric.devkit.event.EOFExceptionEvent;
|
import pub.doric.devkit.event.EOFExceptionEvent;
|
||||||
import pub.doric.devkit.event.OpenEvent;
|
import pub.doric.devkit.event.OpenEvent;
|
||||||
@ -96,13 +95,13 @@ public class WSClient extends WebSocketListener {
|
|||||||
}
|
}
|
||||||
if ("DEBUG_REQ".equals(cmd)) {
|
if ("DEBUG_REQ".equals(cmd)) {
|
||||||
String source = payload.optString("source");
|
String source = payload.optString("source");
|
||||||
DevKit.getInstance().startDebugging(source);
|
DoricDev.getInstance().startDebugging(source);
|
||||||
} else if ("DEBUG_STOP".equals(cmd)) {
|
} else if ("DEBUG_STOP".equals(cmd)) {
|
||||||
DevKit.getInstance().stopDebugging(true);
|
DoricDev.getInstance().stopDebugging(true);
|
||||||
} else if ("RELOAD".equals(cmd)) {
|
} else if ("RELOAD".equals(cmd)) {
|
||||||
String source = payload.optString("source");
|
String source = payload.optString("source");
|
||||||
String script = payload.optString("script");
|
String script = payload.optString("script");
|
||||||
DevKit.getInstance().reload(source, script);
|
DoricDev.getInstance().reload(source, script);
|
||||||
}
|
}
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -25,9 +25,7 @@ import org.greenrobot.eventbus.ThreadMode;
|
|||||||
|
|
||||||
import pub.doric.DoricContext;
|
import pub.doric.DoricContext;
|
||||||
import pub.doric.DoricContextManager;
|
import pub.doric.DoricContextManager;
|
||||||
import pub.doric.devkit.DevKit;
|
|
||||||
import pub.doric.devkit.DoricDev;
|
import pub.doric.devkit.DoricDev;
|
||||||
import pub.doric.devkit.IDevKit;
|
|
||||||
import pub.doric.devkit.R;
|
import pub.doric.devkit.R;
|
||||||
import pub.doric.devkit.event.ConnectExceptionEvent;
|
import pub.doric.devkit.event.ConnectExceptionEvent;
|
||||||
import pub.doric.devkit.event.EOFExceptionEvent;
|
import pub.doric.devkit.event.EOFExceptionEvent;
|
||||||
@ -49,9 +47,8 @@ public class DoricDevActivity extends AppCompatActivity {
|
|||||||
if (DoricDev.getInstance().isInDevMode()) {
|
if (DoricDev.getInstance().isInDevMode()) {
|
||||||
initViews();
|
initViews();
|
||||||
} else {
|
} else {
|
||||||
if (DevKit.isRunningInEmulator) {
|
if (DoricDev.getInstance().isRunningInEmulator) {
|
||||||
DevKit.ip = "10.0.2.2";
|
DoricDev.getInstance().connectDevKit("ws://" + "10.0.2.2" + ":7777");
|
||||||
DevKit.getInstance().connectDevKit("ws://" + DevKit.ip + ":7777");
|
|
||||||
} else {
|
} else {
|
||||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
|
||||||
!= PackageManager.PERMISSION_GRANTED) {
|
!= PackageManager.PERMISSION_GRANTED) {
|
||||||
@ -93,9 +90,8 @@ public class DoricDevActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
|
if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
|
||||||
String result = bundle.getString(CodeUtils.RESULT_STRING);
|
String result = bundle.getString(CodeUtils.RESULT_STRING);
|
||||||
DevKit.ip = result;
|
|
||||||
Toast.makeText(this, "dev kit connecting to " + result, Toast.LENGTH_LONG).show();
|
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() {
|
cell.findViewById(R.id.debug_text_view).setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
DevKit.getInstance().sendDevCommand(
|
DoricDev.getInstance().sendDevCommand(
|
||||||
IDevKit.Command.DEBUG,
|
"DEBUG",
|
||||||
new JSONBuilder()
|
new JSONBuilder()
|
||||||
.put("source", doricContext.getSource())
|
.put("source", doricContext.getSource())
|
||||||
.toJSONObject());
|
.toJSONObject());
|
||||||
|
@ -24,7 +24,6 @@
|
|||||||
NS_ASSUME_NONNULL_BEGIN
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
@interface DoricDev : NSObject
|
@interface DoricDev : NSObject
|
||||||
@property(nonatomic, strong, nullable) DoricWSClient *wsClient;
|
|
||||||
|
|
||||||
+ (instancetype)instance;
|
+ (instancetype)instance;
|
||||||
|
|
||||||
@ -41,6 +40,8 @@ NS_ASSUME_NONNULL_BEGIN
|
|||||||
- (void)stopDebugging:(BOOL)resume;
|
- (void)stopDebugging:(BOOL)resume;
|
||||||
|
|
||||||
- (void)reload:(NSString *)source script:(NSString *)script;
|
- (void)reload:(NSString *)source script:(NSString *)script;
|
||||||
|
|
||||||
|
- (void)sendDevCommand:(NSString *)command payload:(NSDictionary *)payload;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_END
|
NS_ASSUME_NONNULL_END
|
||||||
|
@ -63,6 +63,7 @@ - (void)stopDebug:(BOOL)resume {
|
|||||||
|
|
||||||
|
|
||||||
@interface DoricDev ()
|
@interface DoricDev ()
|
||||||
|
@property(nonatomic, strong, nullable) DoricWSClient *wsClient;
|
||||||
@property(nonatomic, strong) DoricContextDebuggable *debuggable;
|
@property(nonatomic, strong) DoricContextDebuggable *debuggable;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@ -177,4 +178,8 @@ - (void)stopDebugging:(BOOL)resume {
|
|||||||
[self.debuggable stopDebug:resume];
|
[self.debuggable stopDebug:resume];
|
||||||
self.debuggable = nil;
|
self.debuggable = nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)sendDevCommand:(NSString *)command payload:(NSDictionary *)payload {
|
||||||
|
[self.wsClient sendToServer:command payload:payload];
|
||||||
|
}
|
||||||
@end
|
@end
|
||||||
|
@ -30,11 +30,11 @@ - (void)onException:(NSException *)exception inContext:(DoricContext *)context {
|
|||||||
if (!DoricDev.instance.isInDevMode) {
|
if (!DoricDev.instance.isInDevMode) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
[DoricDev.instance.wsClient sendToServer:@"EXCEPTION"
|
[DoricDev.instance sendDevCommand:@"EXCEPTION"
|
||||||
payload:@{
|
payload:@{
|
||||||
@"source": [context.source stringByReplacingOccurrencesOfString:@".js" withString:@".ts"],
|
@"source": [context.source stringByReplacingOccurrencesOfString:@".js" withString:@".ts"],
|
||||||
@"exception": exception.reason
|
@"exception": exception.reason
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)onLog:(DoricLogType)type message:(NSString *)message {
|
- (void)onLog:(DoricLogType)type message:(NSString *)message {
|
||||||
@ -48,10 +48,10 @@ - (void)onLog:(DoricLogType)type message:(NSString *)message {
|
|||||||
typeString = @"ERROR";
|
typeString = @"ERROR";
|
||||||
}
|
}
|
||||||
|
|
||||||
[DoricDev.instance.wsClient sendToServer:@"LOG"
|
[DoricDev.instance sendDevCommand:@"LOG"
|
||||||
payload:@{
|
payload:@{
|
||||||
@"type": typeString,
|
@"type": typeString,
|
||||||
@"message": message
|
@"message": message
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
@end
|
@end
|
||||||
|
Reference in New Issue
Block a user