From 5c74729fbc31d20dada69266b8ef216b2dd0428e Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Fri, 5 Nov 2021 15:40:25 +0800 Subject: [PATCH] android: enable setTimeout and setInterval --- .../java/pub/doric/engine/DoricJSEngine.java | 3 +++ .../doric/engine/DoricWebViewJSExecutor.java | 6 ++---- doric-js/bundle/doric-web.js | 19 ++++++++++--------- doric-js/index.web.ts | 11 ++++++++--- doric-js/lib/index.web.d.ts | 1 + doric-js/lib/index.web.js | 19 ++++++++++--------- 6 files changed, 34 insertions(+), 25 deletions(-) diff --git a/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java b/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java index 218b71bd..33b24c0e 100644 --- a/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java +++ b/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java @@ -83,6 +83,9 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time initJSEngine(); injectGlobal(); initDoricRuntime(); + if (mDoricJSE instanceof DoricWebViewJSExecutor) { + mDoricJSE.loadJS("_prepared();", ""); + } initialized = true; globalProfile.end(DoricPerformanceProfile.PART_INIT); } diff --git a/doric-android/doric/src/main/java/pub/doric/engine/DoricWebViewJSExecutor.java b/doric-android/doric/src/main/java/pub/doric/engine/DoricWebViewJSExecutor.java index 4ae2d1ab..e030029c 100644 --- a/doric-android/doric/src/main/java/pub/doric/engine/DoricWebViewJSExecutor.java +++ b/doric-android/doric/src/main/java/pub/doric/engine/DoricWebViewJSExecutor.java @@ -43,7 +43,7 @@ import pub.doric.utils.DoricLog; /** - * @Description: This contains a webView which is used for executing JavaScript + * @Description: This uses a webView to execute JavaScript * @Author: pengfei.zhou * @CreateDate: 2021/11/3 */ @@ -215,12 +215,10 @@ public class DoricWebViewJSExecutor implements IDoricJSE { @Override public JSDecoder invokeMethod(String objectName, String functionName, JavaValue[] javaValues, boolean hashKey) throws JSRuntimeException { JSONArray jsonArray = new JSONArray(); - for (int i = 0; i < javaValues.length; i++) { - JavaValue javaValue = javaValues[i]; + for (JavaValue javaValue : javaValues) { JSONObject jsonObject = wrapJavaValue(javaValue); jsonArray.put(jsonObject); } - returnFuture = new SettableFuture<>(); String script = String.format( "__invokeMethod('%s','%s','%s')", diff --git a/doric-js/bundle/doric-web.js b/doric-js/bundle/doric-web.js index de735baa..3ce0dfb6 100644 --- a/doric-js/bundle/doric-web.js +++ b/doric-js/bundle/doric-web.js @@ -85,6 +85,9 @@ function _rawValue(v) { return v.value; case "object": case "array": + if (typeof v.value === 'string') { + return JSON.parse(v.value); + } return v.value; default: return undefined; @@ -104,21 +107,13 @@ 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); const args = JSON.parse(stringifiedArgs); - args.forEach(e => { - NativeClient.log(`Arg:${e},${typeof e}`); - }); const rawArgs = args.map(e => _rawValue(e)); - rawArgs.forEach(e => { - NativeClient.log(`RawArg:${e},${typeof e}`); - }); const ret = Reflect.apply(thisFunction, thisObject, rawArgs); - const returnVal = JSON.stringify(_wrappedValue(ret)); - NativeClient.log(`return:${returnVal}`); + const returnVal = ret ? JSON.stringify(_wrappedValue(ret)) : ""; NativeClient.returnNative(returnVal); } catch (e) { @@ -126,3 +121,9 @@ function __invokeMethod(objectName, functionName, stringifiedArgs) { NativeClient.returnNative(""); } } +function _prepared() { + window.setTimeout = Reflect.get(window, "doricSetTimeout"); + window.setInterval = Reflect.get(window, "doricSetInterval"); + window.clearTimeout = Reflect.get(window, "doricClearTimeout"); + window.clearInterval = Reflect.get(window, "doricClearInterval"); +} diff --git a/doric-js/index.web.ts b/doric-js/index.web.ts index 6796edcf..88286534 100644 --- a/doric-js/index.web.ts +++ b/doric-js/index.web.ts @@ -119,18 +119,23 @@ 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); const args = JSON.parse(stringifiedArgs) as WrappedValue[]; const rawArgs = args.map(e => _rawValue(e)); const ret = Reflect.apply(thisFunction, thisObject, rawArgs); - const returnVal = JSON.stringify(_wrappedValue(ret)) - NativeClient.log(`return:${returnVal}`) + const returnVal = ret ? JSON.stringify(_wrappedValue(ret)) : "" NativeClient.returnNative(returnVal) } catch (e) { NativeClient.log(`error:${e},${(e as any).stack}`) NativeClient.returnNative("") } +} + +function _prepared() { + window.setTimeout = Reflect.get(window, "doricSetTimeout"); + window.setInterval = Reflect.get(window, "doricSetInterval"); + window.clearTimeout = Reflect.get(window, "doricClearTimeout"); + window.clearInterval = Reflect.get(window, "doricClearInterval"); } \ No newline at end of file diff --git a/doric-js/lib/index.web.d.ts b/doric-js/lib/index.web.d.ts index f5042a09..bff7d4d1 100644 --- a/doric-js/lib/index.web.d.ts +++ b/doric-js/lib/index.web.d.ts @@ -26,3 +26,4 @@ declare function _rawValue(v: WrappedValue): RawValue; 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; diff --git a/doric-js/lib/index.web.js b/doric-js/lib/index.web.js index 995ddf16..904fe68a 100644 --- a/doric-js/lib/index.web.js +++ b/doric-js/lib/index.web.js @@ -83,6 +83,9 @@ function _rawValue(v) { return v.value; case "object": case "array": + if (typeof v.value === 'string') { + return JSON.parse(v.value); + } return v.value; default: return undefined; @@ -102,21 +105,13 @@ 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); const args = JSON.parse(stringifiedArgs); - args.forEach(e => { - NativeClient.log(`Arg:${e},${typeof e}`); - }); const rawArgs = args.map(e => _rawValue(e)); - rawArgs.forEach(e => { - NativeClient.log(`RawArg:${e},${typeof e}`); - }); const ret = Reflect.apply(thisFunction, thisObject, rawArgs); - const returnVal = JSON.stringify(_wrappedValue(ret)); - NativeClient.log(`return:${returnVal}`); + const returnVal = ret ? JSON.stringify(_wrappedValue(ret)) : ""; NativeClient.returnNative(returnVal); } catch (e) { @@ -124,3 +119,9 @@ function __invokeMethod(objectName, functionName, stringifiedArgs) { NativeClient.returnNative(""); } } +function _prepared() { + window.setTimeout = Reflect.get(window, "doricSetTimeout"); + window.setInterval = Reflect.get(window, "doricSetInterval"); + window.clearTimeout = Reflect.get(window, "doricClearTimeout"); + window.clearInterval = Reflect.get(window, "doricClearInterval"); +}