success to call promise

This commit is contained in:
pengfei.zhou
2019-07-22 11:22:19 +08:00
parent eb1c076dfb
commit 410ee99414
6 changed files with 57 additions and 31 deletions

View File

@@ -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();
}
}

View File

@@ -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'
}

View File

@@ -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);
}
}