From ef5bb1738057d5219daac832129d505f688925a9 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Sat, 20 Jul 2019 13:40:06 +0800 Subject: [PATCH] add context.bridge --- .../penfeizhou/doricdemo/DemoPlugin.java | 8 +++++++- .../penfeizhou/doricdemo/MyApplication.java | 2 ++ .../doric/engine/DoricJSEngine.java | 20 +++++++------------ js-framework/demo.ts | 5 ++++- js-framework/src/runtime/sandbox.ts | 14 +++++++++++++ 5 files changed, 34 insertions(+), 15 deletions(-) diff --git a/Android/app/src/main/java/com/github/penfeizhou/doricdemo/DemoPlugin.java b/Android/app/src/main/java/com/github/penfeizhou/doricdemo/DemoPlugin.java index 38be8d6c..390b715d 100644 --- a/Android/app/src/main/java/com/github/penfeizhou/doricdemo/DemoPlugin.java +++ b/Android/app/src/main/java/com/github/penfeizhou/doricdemo/DemoPlugin.java @@ -6,6 +6,7 @@ import com.github.penfeizhou.doric.DoricContext; import com.github.penfeizhou.doric.extension.bridge.DoricMethod; import com.github.penfeizhou.doric.extension.bridge.DoricPlugin; import com.github.penfeizhou.doric.plugin.DoricJavaPlugin; +import com.github.penfeizhou.doric.utils.ThreadMode; /** * @Description: com.github.penfeizhou.doricdemo @@ -18,8 +19,13 @@ public class DemoPlugin extends DoricJavaPlugin { super(doricContext); } - @DoricMethod + @DoricMethod(thread = ThreadMode.UI) public void test() { Toast.makeText(getDoricContext().getContext(), "test", Toast.LENGTH_SHORT).show(); } + + @DoricMethod(thread = ThreadMode.UI) + public void testPromise() { + Toast.makeText(getDoricContext().getContext(), "test", Toast.LENGTH_SHORT).show(); + } } diff --git a/Android/app/src/main/java/com/github/penfeizhou/doricdemo/MyApplication.java b/Android/app/src/main/java/com/github/penfeizhou/doricdemo/MyApplication.java index e0f80bbf..4aaff9dc 100644 --- a/Android/app/src/main/java/com/github/penfeizhou/doricdemo/MyApplication.java +++ b/Android/app/src/main/java/com/github/penfeizhou/doricdemo/MyApplication.java @@ -3,6 +3,7 @@ package com.github.penfeizhou.doricdemo; import android.app.Application; import com.github.penfeizhou.doric.Doric; +import com.github.penfeizhou.doric.DoricRegistry; /** * @Description: Doric @@ -14,5 +15,6 @@ public class MyApplication extends Application { public void onCreate() { super.onCreate(); Doric.init(this); + DoricRegistry.register(new DemoLibrary()); } } diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/engine/DoricJSEngine.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/engine/DoricJSEngine.java index d44669af..6e4c9706 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/engine/DoricJSEngine.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/engine/DoricJSEngine.java @@ -43,7 +43,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time } }); mTimerExtension = new DoricTimerExtension(looper, this); - mDoricBridgeExtension = new DoricBridgeExtension(); + mDoricBridgeExtension = new DoricBridgeExtension(mDoricRegistry); } public Handler getJSHandler() { @@ -174,17 +174,6 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time return String.format(DoricConstant.TEMPLATE_MODULE, moduleName, content); } - public JSDecoder invokeContextEntityMethod(final String contextId, final String method, final Object... args) { - final Object[] nArgs = new Object[args.length + 2]; - nArgs[0] = contextId; - nArgs[1] = method; - if (args.length > 0) { - System.arraycopy(args, 0, nArgs, 2, args.length); - } - return invokeDoricMethod(DoricConstant.DORIC_CONTEXT_INVOKE, nArgs); - } - - public JSDecoder invokeDoricMethod(final String method, final Object... args) { ArrayList values = new ArrayList<>(); for (Object arg : args) { @@ -196,6 +185,11 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time @Override public void callback(long timerId) { - invokeDoricMethod(DoricConstant.DORIC_TIMER_CALLBACK, timerId); + try { + invokeDoricMethod(DoricConstant.DORIC_TIMER_CALLBACK, timerId); + } catch (Exception e) { + e.printStackTrace(); + DoricLog.e("Timer Callback error:%s", e.getLocalizedMessage()); + } } } diff --git a/js-framework/demo.ts b/js-framework/demo.ts index 2b169d7b..933c0e54 100644 --- a/js-framework/demo.ts +++ b/js-framework/demo.ts @@ -19,7 +19,7 @@ log('console', Object.getOwnPropertyNames(console)) setTimeout(() => { log('exec setTimeout') - context.callNative("modal", "toast", "Hello,Doric!") + // context.callNative("modal", "toast", "Hello,Doric!") }, 1000) const timerId = setInterval(() => { log('exec setInterval') @@ -39,5 +39,8 @@ export class MyPage extends Panel { log("Hello.HEGO") logw("Hello.HEGO") loge("Hello.HEGO") + setTimeout(() => { + context.bridge.demo_test() + }, 1000) } } \ No newline at end of file diff --git a/js-framework/src/runtime/sandbox.ts b/js-framework/src/runtime/sandbox.ts index 96b8f185..7e160033 100644 --- a/js-framework/src/runtime/sandbox.ts +++ b/js-framework/src/runtime/sandbox.ts @@ -71,8 +71,22 @@ export class Context { entity: any id: string callbacks: Map = new Map + bridge: { [index: string]: (args?: any) => Promise } + constructor(id: string) { this.id = id + this.bridge = new Proxy({}, { + get: (target, p: any) => { + if (typeof p === 'string') { + return (args?: any) => { + const array = p.split('_') + return this.callNative(array[0], array[1], args) + } + } else { + return Reflect.get(target, p) + } + } + }) } callNative(namespace: string, method: string, args?: any): Promise { const callbackId = uniqueId('callback')