From 410ee994145ddf77f56024c0d279aba0a05645bd Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Mon, 22 Jul 2019 11:22:19 +0800 Subject: [PATCH] success to call promise --- .../penfeizhou/doricdemo/DemoPlugin.java | 9 +++- Android/doric/build.gradle | 11 ++++- .../doric/extension/bridge/DoricPromise.java | 16 ++++--- js-framework/demo.ts | 45 +++++++++++-------- js-framework/src/runtime/sandbox.ts | 4 +- js-framework/src/ui/panel.ts | 3 +- 6 files changed, 57 insertions(+), 31 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 390b715d..96bb527c 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 @@ -5,8 +5,10 @@ import android.widget.Toast; 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.extension.bridge.DoricPromise; import com.github.penfeizhou.doric.plugin.DoricJavaPlugin; import com.github.penfeizhou.doric.utils.ThreadMode; +import com.github.pengfeizhou.jscore.JavaValue; /** * @Description: com.github.penfeizhou.doricdemo @@ -25,7 +27,12 @@ public class DemoPlugin extends DoricJavaPlugin { } @DoricMethod(thread = ThreadMode.UI) - public void testPromise() { + public void testPromise(boolean b, DoricPromise doricPromise) { + if (b) { + doricPromise.resolve(new JavaValue("resolved by me")); + } else { + doricPromise.reject(new JavaValue("rejected by me")); + } Toast.makeText(getDoricContext().getContext(), "test", Toast.LENGTH_SHORT).show(); } } diff --git a/Android/doric/build.gradle b/Android/doric/build.gradle index 20b82584..02d5bd7d 100644 --- a/Android/doric/build.gradle +++ b/Android/doric/build.gradle @@ -28,6 +28,15 @@ android { } } +afterEvaluate { + buildJSBundle.exec() +} + +task buildJSBundle(type: Exec) { + workingDir project.rootDir.getParent() + "/js-framework" + commandLine 'npm', 'run', 'build' +} + dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) @@ -35,5 +44,5 @@ dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' - implementation 'com.github.pengfeizhou:jsc4a:0.1.0' + api 'com.github.pengfeizhou:jsc4a:0.1.0' } diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricPromise.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricPromise.java index 1a94c72a..a461f821 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricPromise.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricPromise.java @@ -19,18 +19,22 @@ public class DoricPromise { } public void resolve(JavaValue... javaValue) { + Object[] params = new Object[javaValue.length + 2]; + params[0] = context.getContextId(); + params[1] = callbackId; + System.arraycopy(javaValue, 0, params, 2, javaValue.length); context.getDriver().invokeDoricMethod( DoricConstant.DORIC_BRIDGE_RESOLVE, - context.getContextId(), - callbackId, - javaValue); + params); } public void reject(JavaValue... javaValue) { + Object[] params = new Object[javaValue.length + 2]; + params[0] = context.getContextId(); + params[1] = callbackId; + System.arraycopy(javaValue, 0, params, 2, javaValue.length); context.getDriver().invokeDoricMethod( DoricConstant.DORIC_BRIDGE_REJECT, - context.getContextId(), - callbackId, - javaValue); + params); } } diff --git a/js-framework/demo.ts b/js-framework/demo.ts index cdea813b..de5e956d 100644 --- a/js-framework/demo.ts +++ b/js-framework/demo.ts @@ -9,26 +9,26 @@ v.bgColor = Color.parse('#00ff00') v.config = { alignment: Alignment.start } -console.log(v.toModel()) +// console.log(v.toModel()) const layout = new VLayout layout.space = 10 console.log(layout.viewId) console.log(layout.toModel()) -log('console', Object.getOwnPropertyNames(console)) +// log('console', Object.getOwnPropertyNames(console)) -setTimeout(() => { - log('exec setTimeout') - // context.callNative("modal", "toast", "Hello,Doric!") -}, 1000) -const timerId = setInterval(() => { - log('exec setInterval') -}, 1000) +// setTimeout(() => { +// log('exec setTimeout') +// // context.callNative("modal", "toast", "Hello,Doric!") +// }, 1000) +// const timerId = setInterval(() => { +// log('exec setInterval') +// }, 1000) -setTimeout(() => { - log('exec cancelTimer') - clearInterval(timerId) -}, 5000) +// setTimeout(() => { +// log('exec cancelTimer') +// clearInterval(timerId) +// }, 5000) @Link(context) export class MyPage extends Panel { @@ -36,11 +36,18 @@ export class MyPage extends Panel { return layout } log() { - log("Hello.HEGO") - logw("Hello.HEGO") - loge("Hello.HEGO") - setTimeout(() => { - context.bridge.demo_test() - }, 1000) + // log("Hello.HEGO") + // logw("Hello.HEGO") + // loge("Hello.HEGO") + context.bridge.demo_testPromise(true).then((r) => { + log('resolve', r) + }, (e) => { + log('reject', e) + }) + context.bridge.demo_testPromise(false).then((r) => { + log('resolve', r) + }, (e) => { + log('reject', e) + }) } } \ No newline at end of file diff --git a/js-framework/src/runtime/sandbox.ts b/js-framework/src/runtime/sandbox.ts index 7e160033..fa2ffabd 100644 --- a/js-framework/src/runtime/sandbox.ts +++ b/js-framework/src/runtime/sandbox.ts @@ -46,7 +46,7 @@ export function jsCallResolve(contextId: string, callbackId: string, args?: any) for (let i = 2; i < arguments.length; i++) { argumentsList.push(arguments[i]) } - Reflect.apply(callback.resolve, context, args) + Reflect.apply(callback.resolve, context, argumentsList) } export function jsCallReject(contextId: string, callbackId: string, args?: any) { @@ -64,7 +64,7 @@ export function jsCallReject(contextId: string, callbackId: string, args?: any) for (let i = 2; i < arguments.length; i++) { argumentsList.push(arguments[i]) } - Reflect.apply(callback.reject, context, args) + Reflect.apply(callback.reject, context, argumentsList) } export class Context { diff --git a/js-framework/src/ui/panel.ts b/js-framework/src/ui/panel.ts index 3bb279ed..8209cf1a 100644 --- a/js-framework/src/ui/panel.ts +++ b/js-framework/src/ui/panel.ts @@ -1,7 +1,6 @@ import { } from './../runtime/global'; import { View, Stack, Group } from "./view"; -import { log } from 'util'; -import { loge } from '../util/log'; +import { loge, log } from '../util/log'; export function Link(context: any) { return (constructor: T) => {