This commit is contained in:
lyswhut 2023-11-04 13:29:57 +08:00
parent a879207aa9
commit 44a3a37e0d
4 changed files with 29 additions and 14 deletions

View File

@ -5,5 +5,6 @@ public class HandlerWhat {
public static final int INIT = 99;
public static final int INIT_FAILED = 500;
public static final int INIT_SUCCESS = 200;
public static final int DESTROY = 98;
public static final int LOG = 1001;
}

View File

@ -41,14 +41,22 @@ public class JavaScriptThread extends HandlerThread {
handler.sendMessage(handler.obtainMessage(HandlerWhat.INIT_FAILED, result));
}
}
if (message.what == HandlerWhat.INIT) return;
if (message.what == HandlerWhat.ACTION) {
Object[] data = (Object[]) message.obj;
Log.d("UserApi [handler]", "handler action: " + data[0]);
javaScriptExecutor.callJS((String) data[0], data[1]);
return;
switch (message.what) {
case HandlerWhat.INIT: break;
case HandlerWhat.ACTION: {
Object[] data = (Object[]) message.obj;
Log.d("UserApi [handler]", "handler action: " + data[0]);
javaScriptExecutor.callJS((String) data[0], data[1]);
return;
}
case HandlerWhat.DESTROY:
javaScriptExecutor.destroy();
javaScriptExecutor = null;
break;
default:
Log.w("UserApi [handler]", "Unknown message what: " + message.what);
break;
}
Log.w("UserApi [handler]", "Unknown message what: " + message.what);
}
};
}

View File

@ -185,4 +185,9 @@ public class QuickJS {
return null;
}
}
public void destroy () {
this.jsContext.destroy();
this.jsContext = null;
}
}

View File

@ -1,6 +1,7 @@
package cn.toside.music.mobile.userApi;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.facebook.react.bridge.Arguments;
@ -33,10 +34,10 @@ public class UserApiModule extends ReactContextBaseJavaModule {
if (this.javaScriptThread != null) destroy();
Bundle info = Arguments.toBundle(data);
this.javaScriptThread = new JavaScriptThread(this.reactContext, info);
final JsHandler jsHandler = new JsHandler(this.reactContext.getMainLooper(), this.utilsEvent);
this.javaScriptThread.prepareHandler(jsHandler);
this.javaScriptThread.prepareHandler(new JsHandler(this.reactContext.getMainLooper(), this.utilsEvent));
this.javaScriptThread.getHandler().sendEmptyMessage(HandlerWhat.INIT);
this.javaScriptThread.setUncaughtExceptionHandler((thread, ex) -> {
Handler jsHandler = javaScriptThread.getHandler();
Message message = jsHandler.obtainMessage();
message.what = HandlerWhat.LOG;
message.obj = new Object[]{"error", "Uncaught exception in JavaScriptThread: " + ex.getMessage()};
@ -50,19 +51,19 @@ public class UserApiModule extends ReactContextBaseJavaModule {
public boolean sendAction(String action, String info) {
JavaScriptThread javaScriptThread = this.javaScriptThread;
if (javaScriptThread == null) return false;
Message message = javaScriptThread.getHandler().obtainMessage();
Handler jsHandler = javaScriptThread.getHandler();
Message message = jsHandler.obtainMessage();
message.what = HandlerWhat.ACTION;
message.obj = new Object[]{action, info};
this.javaScriptThread.getHandler().sendMessage(message);
jsHandler.sendMessage(message);
return true;
}
@ReactMethod
public void destroy() {
JavaScriptThread javaScriptThread = this.javaScriptThread;
if (javaScriptThread == null) {
return;
}
if (javaScriptThread == null) return;
javaScriptThread.getHandler().sendEmptyMessage(HandlerWhat.DESTROY);
javaScriptThread.stopThread();
this.javaScriptThread = null;
}