feat:fix when android excute js on same thread,cause stucked
This commit is contained in:
parent
5c74729fbc
commit
7282146e14
@ -74,7 +74,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
DoricDev.getInstance();
|
||||
// DoricDev.getInstance();
|
||||
}
|
||||
|
||||
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
||||
|
@ -46,7 +46,7 @@ public class MyApplication extends Application {
|
||||
DoricSingleton.getInstance().setEnvironmentValue(map);
|
||||
}
|
||||
}, intentFilter);
|
||||
DoricNativeDriver.getInstance();
|
||||
// DoricNativeDriver.getInstance();
|
||||
Doric.enablePerformance(true);
|
||||
Doric.enableRenderSnapshot(true);
|
||||
}
|
||||
|
@ -100,8 +100,11 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
|
||||
|
||||
protected void initJSEngine() {
|
||||
mDoricJSE = new DoricWebViewJSExecutor(Doric.application());
|
||||
//mDoricJSE = new DoricNativeJSExecutor();
|
||||
if (mDoricJSE instanceof DoricWebViewJSExecutor) {
|
||||
loadBuiltinJS("doric-web.js");
|
||||
}
|
||||
}
|
||||
|
||||
public void setEnvironmentValue(Map<String, Object> values) {
|
||||
mEnvironmentMap.putAll(values);
|
||||
|
@ -17,6 +17,9 @@ package pub.doric.engine;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
import android.os.Looper;
|
||||
import android.text.TextUtils;
|
||||
import android.webkit.ConsoleMessage;
|
||||
import android.webkit.JavascriptInterface;
|
||||
@ -48,8 +51,9 @@ import pub.doric.utils.DoricLog;
|
||||
* @CreateDate: 2021/11/3
|
||||
*/
|
||||
public class DoricWebViewJSExecutor implements IDoricJSE {
|
||||
private final WebView webView;
|
||||
private WebView webView;
|
||||
private final Map<String, JavaFunction> globalFunctions = new HashMap<>();
|
||||
private final Handler handler;
|
||||
|
||||
private static Object unwrapJSObject(JSONObject jsonObject) {
|
||||
String type = jsonObject.optString("type");
|
||||
@ -127,6 +131,7 @@ public class DoricWebViewJSExecutor implements IDoricJSE {
|
||||
|
||||
@JavascriptInterface
|
||||
public void returnNative(String result) {
|
||||
DoricLog.d("return Native" + result);
|
||||
if (returnFuture != null) {
|
||||
returnFuture.set(result);
|
||||
}
|
||||
@ -178,26 +183,39 @@ public class DoricWebViewJSExecutor implements IDoricJSE {
|
||||
}
|
||||
|
||||
@SuppressLint({"JavascriptInterface", "SetJavaScriptEnabled"})
|
||||
public DoricWebViewJSExecutor(Context context) {
|
||||
this.webView = new WebView(context.getApplicationContext());
|
||||
WebSettings webSettings = this.webView.getSettings();
|
||||
public DoricWebViewJSExecutor(final Context context) {
|
||||
HandlerThread webViewHandlerThread = new HandlerThread("DoricWebViewJSExecutor");
|
||||
webViewHandlerThread.start();
|
||||
this.handler = new Handler(webViewHandlerThread.getLooper());
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
webView = new WebView(context.getApplicationContext());
|
||||
WebSettings webSettings = webView.getSettings();
|
||||
webSettings.setJavaScriptEnabled(true);
|
||||
this.webView.setWebChromeClient(new DoricWebChromeClient());
|
||||
this.webView.loadUrl("about:blank");
|
||||
webView.setWebChromeClient(new DoricWebChromeClient());
|
||||
webView.loadUrl("about:blank");
|
||||
WebViewCallback webViewCallback = new WebViewCallback();
|
||||
this.webView.addJavascriptInterface(webViewCallback, "NativeClient");
|
||||
webView.addJavascriptInterface(webViewCallback, "NativeClient");
|
||||
WebView.setWebContentsDebuggingEnabled(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public String loadJS(String script, String source) {
|
||||
this.webView.evaluateJavascript(script, null);
|
||||
public String loadJS(final String script, String source) {
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
webView.evaluateJavascript(script, null);
|
||||
}
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSDecoder evaluateJS(String script, String source, boolean hashKey) throws JSRuntimeException {
|
||||
this.webView.evaluateJavascript(script, null);
|
||||
loadJS(script, source);
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -209,7 +227,7 @@ public class DoricWebViewJSExecutor implements IDoricJSE {
|
||||
|
||||
@Override
|
||||
public void injectGlobalJSObject(String name, JavaValue javaValue) {
|
||||
loadJS(String.format("_injectGlobalObject('%s','%s')", name, javaValue.getValue()), "");
|
||||
loadJS(String.format("__injectGlobalObject('%s','%s')", name, javaValue.getValue()), "");
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -220,13 +238,14 @@ public class DoricWebViewJSExecutor implements IDoricJSE {
|
||||
jsonArray.put(jsonObject);
|
||||
}
|
||||
returnFuture = new SettableFuture<>();
|
||||
String script = String.format(
|
||||
final String script = String.format(
|
||||
"__invokeMethod('%s','%s','%s')",
|
||||
objectName,
|
||||
functionName,
|
||||
jsonArray.toString());
|
||||
loadJS(script, "");
|
||||
String result = returnFuture.get();
|
||||
returnFuture = null;
|
||||
if (!TextUtils.isEmpty(result)) {
|
||||
try {
|
||||
JSONObject jsonObject = new JSONObject(result);
|
||||
|
@ -93,7 +93,7 @@ function _rawValue(v) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
function _injectGlobalObject(name, args) {
|
||||
function __injectGlobalObject(name, args) {
|
||||
Reflect.set(window, name, JSON.parse(args));
|
||||
}
|
||||
function __injectGlobalFunction(name) {
|
||||
@ -107,6 +107,7 @@ function __injectGlobalFunction(name) {
|
||||
});
|
||||
}
|
||||
function __invokeMethod(objectName, functionName, stringifiedArgs) {
|
||||
NativeClient.log(`invoke:${objectName}.${functionName}(${stringifiedArgs})`);
|
||||
try {
|
||||
const thisObject = Reflect.get(window, objectName);
|
||||
const thisFunction = Reflect.get(thisObject, functionName);
|
||||
@ -114,6 +115,7 @@ function __invokeMethod(objectName, functionName, stringifiedArgs) {
|
||||
const rawArgs = args.map(e => _rawValue(e));
|
||||
const ret = Reflect.apply(thisFunction, thisObject, rawArgs);
|
||||
const returnVal = ret ? JSON.stringify(_wrappedValue(ret)) : "";
|
||||
NativeClient.log(`return:${returnVal}`);
|
||||
NativeClient.returnNative(returnVal);
|
||||
}
|
||||
catch (e) {
|
||||
|
@ -103,7 +103,7 @@ function _rawValue(v: WrappedValue): RawValue {
|
||||
}
|
||||
}
|
||||
|
||||
function _injectGlobalObject(name: string, args: string) {
|
||||
function __injectGlobalObject(name: string, args: string) {
|
||||
Reflect.set(window, name, JSON.parse(args));
|
||||
}
|
||||
|
||||
@ -119,6 +119,7 @@ function __injectGlobalFunction(name: string) {
|
||||
}
|
||||
|
||||
function __invokeMethod(objectName: string, functionName: string, stringifiedArgs: string) {
|
||||
// NativeClient.log(`invoke:${objectName}.${functionName}(${stringifiedArgs})`)
|
||||
try {
|
||||
const thisObject = Reflect.get(window, objectName);
|
||||
const thisFunction = Reflect.get(thisObject, functionName);
|
||||
@ -126,6 +127,7 @@ function __invokeMethod(objectName: string, functionName: string, stringifiedArg
|
||||
const rawArgs = args.map(e => _rawValue(e));
|
||||
const ret = Reflect.apply(thisFunction, thisObject, rawArgs);
|
||||
const returnVal = ret ? JSON.stringify(_wrappedValue(ret)) : ""
|
||||
// NativeClient.log(`return:${returnVal}`)
|
||||
NativeClient.returnNative(returnVal)
|
||||
} catch (e) {
|
||||
NativeClient.log(`error:${e},${(e as any).stack}`)
|
||||
|
2
doric-js/lib/index.web.d.ts
vendored
2
doric-js/lib/index.web.d.ts
vendored
@ -23,7 +23,7 @@ declare function _binaryValue(v: RawValue): {
|
||||
};
|
||||
declare function _wrappedValue(v: RawValue): WrappedValue;
|
||||
declare function _rawValue(v: WrappedValue): RawValue;
|
||||
declare function _injectGlobalObject(name: string, args: string): void;
|
||||
declare function __injectGlobalObject(name: string, args: string): void;
|
||||
declare function __injectGlobalFunction(name: string): void;
|
||||
declare function __invokeMethod(objectName: string, functionName: string, stringifiedArgs: string): void;
|
||||
declare function _prepared(): void;
|
||||
|
@ -91,7 +91,7 @@ function _rawValue(v) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
function _injectGlobalObject(name, args) {
|
||||
function __injectGlobalObject(name, args) {
|
||||
Reflect.set(window, name, JSON.parse(args));
|
||||
}
|
||||
function __injectGlobalFunction(name) {
|
||||
@ -105,6 +105,7 @@ function __injectGlobalFunction(name) {
|
||||
});
|
||||
}
|
||||
function __invokeMethod(objectName, functionName, stringifiedArgs) {
|
||||
NativeClient.log(`invoke:${objectName}.${functionName}(${stringifiedArgs})`);
|
||||
try {
|
||||
const thisObject = Reflect.get(window, objectName);
|
||||
const thisFunction = Reflect.get(thisObject, functionName);
|
||||
@ -112,6 +113,7 @@ function __invokeMethod(objectName, functionName, stringifiedArgs) {
|
||||
const rawArgs = args.map(e => _rawValue(e));
|
||||
const ret = Reflect.apply(thisFunction, thisObject, rawArgs);
|
||||
const returnVal = ret ? JSON.stringify(_wrappedValue(ret)) : "";
|
||||
NativeClient.log(`return:${returnVal}`);
|
||||
NativeClient.returnNative(returnVal);
|
||||
}
|
||||
catch (e) {
|
||||
|
Reference in New Issue
Block a user