Add API for synchronous rendering to Panel
This commit is contained in:
@@ -21,8 +21,12 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.github.pengfeizhou.jscore.ArchiveException;
|
||||
import com.github.pengfeizhou.jscore.JSArray;
|
||||
import com.github.pengfeizhou.jscore.JSDecoder;
|
||||
import com.github.pengfeizhou.jscore.JSONBuilder;
|
||||
import com.github.pengfeizhou.jscore.JSObject;
|
||||
import com.github.pengfeizhou.jscore.JSValue;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
@@ -44,6 +48,7 @@ import pub.doric.navbar.IDoricNavBar;
|
||||
import pub.doric.navigator.IDoricNavigator;
|
||||
import pub.doric.performance.DoricPerformanceProfile;
|
||||
import pub.doric.plugin.DoricJavaPlugin;
|
||||
import pub.doric.plugin.ShaderPlugin;
|
||||
import pub.doric.resource.DoricResource;
|
||||
import pub.doric.shader.RootNode;
|
||||
import pub.doric.shader.ViewNode;
|
||||
@@ -383,4 +388,50 @@ public class DoricContext {
|
||||
public void releaseJavaValue(SoftReference<RetainedJavaValue> retainedJavaValue) {
|
||||
retainedJavaValues.remove(retainedJavaValue);
|
||||
}
|
||||
|
||||
public AsyncResult<JSDecoder> pureCallEntity(String methodName, Object... args) {
|
||||
final AsyncResult<JSDecoder> asyncResult = new AsyncResult<>();
|
||||
final Object[] nArgs = new Object[args.length + 2];
|
||||
nArgs[0] = getContextId();
|
||||
nArgs[1] = methodName;
|
||||
if (args.length > 0) {
|
||||
System.arraycopy(args, 0, nArgs, 2, args.length);
|
||||
}
|
||||
getDriver().invokeDoricMethod(DoricConstant.DORIC_CONTEXT_INVOKE_PURE, nArgs).setCallback(new AsyncResult.Callback<JSDecoder>() {
|
||||
@Override
|
||||
public void onResult(JSDecoder result) {
|
||||
asyncResult.setResult(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
getDriver().getRegistry().onException(DoricContext.this, t instanceof Exception ? (Exception) t : new RuntimeException(t));
|
||||
asyncResult.setError(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFinish() {
|
||||
|
||||
}
|
||||
});
|
||||
return asyncResult;
|
||||
}
|
||||
|
||||
public void renderSynchronously() {
|
||||
AsyncResult<JSDecoder> asyncResult = pureCallEntity(DoricConstant.DORIC_ENTITY_FETCH_DIRTY_DATA);
|
||||
JSDecoder jsDecoder = asyncResult.synchronous().get();
|
||||
DoricJavaPlugin shaderPlugin = obtainPlugin(getDriver().getRegistry().acquirePluginInfo("Shader"));
|
||||
try {
|
||||
JSValue result = jsDecoder.decode();
|
||||
if (shaderPlugin instanceof ShaderPlugin && result.isArray()) {
|
||||
JSArray jsArray = result.asArray();
|
||||
for (int i = 0; i < jsArray.size(); i++) {
|
||||
JSObject model = jsArray.get(i).asObject();
|
||||
((ShaderPlugin) shaderPlugin).render(model, null);
|
||||
}
|
||||
}
|
||||
} catch (ArchiveException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -29,6 +29,7 @@ import com.github.pengfeizhou.jscore.JavaValue;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import pub.doric.DoricContext;
|
||||
import pub.doric.async.AsyncResult;
|
||||
import pub.doric.extension.bridge.DoricMethod;
|
||||
@@ -54,7 +55,7 @@ public class ShaderPlugin extends DoricJavaPlugin {
|
||||
}
|
||||
|
||||
@DoricMethod(thread = ThreadMode.UI)
|
||||
public void render(final JSObject jsObject, final DoricPromise promise) {
|
||||
public void render(final JSObject jsObject, @Nullable final DoricPromise promise) {
|
||||
final DoricPerformanceProfile profile = getDoricContext().getPerformanceProfile();
|
||||
profile.prepare(DoricPerformanceProfile.STEP_RENDER);
|
||||
getDoricContext().getDriver().asyncCall(new Callable<Object>() {
|
||||
@@ -90,7 +91,9 @@ public class ShaderPlugin extends DoricJavaPlugin {
|
||||
targetView.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
promise.resolve();
|
||||
if (promise != null) {
|
||||
promise.resolve();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -76,4 +76,5 @@ public class DoricConstant {
|
||||
public static final String DORIC_ENTITY_HIDDEN = "__onHidden__";
|
||||
public static final String DORIC_ENTITY_BUILD = "__build__";
|
||||
public static final String DORIC_ENTITY_ENV_CHANGE = "__onEnvChanged__";
|
||||
public static final String DORIC_ENTITY_FETCH_DIRTY_DATA = "__fetchEffectiveData__";
|
||||
}
|
||||
|
Reference in New Issue
Block a user