From 2046baa23de1a109a8198d81e083fc831c963e4c Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Mon, 23 Mar 2020 13:23:04 +0800 Subject: [PATCH] feat:fix command view cannot find view,let find on UI Thread --- .../java/pub/doric/plugin/ShaderPlugin.java | 141 +++++++++--------- .../Pod/Classes/Plugin/DoricShaderPlugin.m | 34 +++-- 2 files changed, 92 insertions(+), 83 deletions(-) diff --git a/doric-android/doric/src/main/java/pub/doric/plugin/ShaderPlugin.java b/doric-android/doric/src/main/java/pub/doric/plugin/ShaderPlugin.java index 3ad9bb23..19b68063 100644 --- a/doric-android/doric/src/main/java/pub/doric/plugin/ShaderPlugin.java +++ b/doric-android/doric/src/main/java/pub/doric/plugin/ShaderPlugin.java @@ -90,77 +90,84 @@ public class ShaderPlugin extends DoricJavaPlugin { } @DoricMethod - public void command(JSObject jsObject, final DoricPromise doricPromise) { - final JSValue[] viewIds = jsObject.getProperty("viewIds").asArray().toArray(); - final String name = jsObject.getProperty("name").asString().value(); - final JSValue args = jsObject.getProperty("args"); - ViewNode viewNode = null; - for (JSValue value : viewIds) { - if (viewNode == null) { - viewNode = getDoricContext().targetViewNode(value.asString().value()); - } else { - if (value.isString() && viewNode instanceof SuperNode) { - String viewId = value.asString().value(); - viewNode = ((SuperNode) viewNode).getSubNodeById(viewId); - } - } - } - if (viewNode == null) { - doricPromise.reject(new JavaValue("Cannot find opposite view")); - } else { - final ViewNode targetViewNode = viewNode; - DoricMetaInfo pluginInfo = getDoricContext().getDriver().getRegistry() - .acquireViewNodeInfo(viewNode.getType()); - final Method method = pluginInfo.getMethod(name); - if (method == null) { - String errMsg = String.format( - "Cannot find plugin method in class:%s,method:%s", - viewNode.getClass(), - name); - doricPromise.reject(new JavaValue(errMsg)); - } else { - Callable callable = new Callable() { - @Override - public JavaValue call() throws Exception { - Class[] classes = method.getParameterTypes(); - Object ret; - if (classes.length == 0) { - ret = method.invoke(targetViewNode); - } else if (classes.length == 1) { - ret = method.invoke(targetViewNode, - createParam(classes[0], doricPromise, args)); - } else { - ret = method.invoke(targetViewNode, - createParam(classes[0], doricPromise, args), - createParam(classes[1], doricPromise, args)); + public void command(final JSObject jsObject, final DoricPromise doricPromise) { + getDoricContext().getDriver().asyncCall(new Callable() { + @Override + public Object call() throws Exception { + final JSValue[] viewIds = jsObject.getProperty("viewIds").asArray().toArray(); + final String name = jsObject.getProperty("name").asString().value(); + final JSValue args = jsObject.getProperty("args"); + ViewNode viewNode = null; + for (JSValue value : viewIds) { + if (viewNode == null) { + viewNode = getDoricContext().targetViewNode(value.asString().value()); + } else { + if (value.isString() && viewNode instanceof SuperNode) { + String viewId = value.asString().value(); + viewNode = ((SuperNode) viewNode).getSubNodeById(viewId); } - return DoricUtils.toJavaValue(ret); } - }; - AsyncResult asyncResult = getDoricContext().getDriver() - .asyncCall(callable, ThreadMode.UI); - if (!method.getReturnType().equals(Void.TYPE)) { - asyncResult.setCallback(new AsyncResult.Callback() { - @Override - public void onResult(JavaValue result) { - doricPromise.resolve(result); - } - - @Override - public void onError(Throwable t) { - doricPromise.resolve(new JavaValue(t.getLocalizedMessage())); - getDoricContext().getDriver().getRegistry().onException(getDoricContext(), - t instanceof Exception ? (Exception) t : new RuntimeException(t)); - } - - @Override - public void onFinish() { - - } - }); } + if (viewNode == null) { + doricPromise.reject(new JavaValue("Cannot find opposite view")); + } else { + final ViewNode targetViewNode = viewNode; + DoricMetaInfo pluginInfo = getDoricContext().getDriver().getRegistry() + .acquireViewNodeInfo(viewNode.getType()); + final Method method = pluginInfo.getMethod(name); + if (method == null) { + String errMsg = String.format( + "Cannot find plugin method in class:%s,method:%s", + viewNode.getClass(), + name); + doricPromise.reject(new JavaValue(errMsg)); + } else { + Callable callable = new Callable() { + @Override + public JavaValue call() throws Exception { + Class[] classes = method.getParameterTypes(); + Object ret; + if (classes.length == 0) { + ret = method.invoke(targetViewNode); + } else if (classes.length == 1) { + ret = method.invoke(targetViewNode, + createParam(classes[0], doricPromise, args)); + } else { + ret = method.invoke(targetViewNode, + createParam(classes[0], doricPromise, args), + createParam(classes[1], doricPromise, args)); + } + return DoricUtils.toJavaValue(ret); + } + }; + AsyncResult asyncResult = getDoricContext().getDriver() + .asyncCall(callable, ThreadMode.UI); + if (!method.getReturnType().equals(Void.TYPE)) { + asyncResult.setCallback(new AsyncResult.Callback() { + @Override + public void onResult(JavaValue result) { + doricPromise.resolve(result); + } + + @Override + public void onError(Throwable t) { + doricPromise.resolve(new JavaValue(t.getLocalizedMessage())); + getDoricContext().getDriver().getRegistry().onException(getDoricContext(), + t instanceof Exception ? (Exception) t : new RuntimeException(t)); + } + + @Override + public void onFinish() { + + } + }); + } + } + } + return null; } - } + }, ThreadMode.UI); + } diff --git a/doric-iOS/Pod/Classes/Plugin/DoricShaderPlugin.m b/doric-iOS/Pod/Classes/Plugin/DoricShaderPlugin.m index 24b240bd..702c6869 100644 --- a/doric-iOS/Pod/Classes/Plugin/DoricShaderPlugin.m +++ b/doric-iOS/Pod/Classes/Plugin/DoricShaderPlugin.m @@ -50,24 +50,26 @@ - (void)render:(NSDictionary *)argument withPromise:(DoricPromise *)promise { } - (void)command:(NSDictionary *)argument withPromise:(DoricPromise *)promise { - NSArray *viewIds = argument[@"viewIds"]; - id args = argument[@"args"]; - NSString *name = argument[@"name"]; - DoricViewNode *viewNode = nil; - for (NSString *viewId in viewIds) { - if (!viewNode) { - viewNode = [self.doricContext targetViewNode:viewId]; - } else { - if ([viewNode isKindOfClass:[DoricSuperNode class]]) { - viewNode = [((DoricSuperNode *) viewNode) subNodeWithViewId:viewId]; + dispatch_async(dispatch_get_main_queue(), ^{ + NSArray *viewIds = argument[@"viewIds"]; + id args = argument[@"args"]; + NSString *name = argument[@"name"]; + DoricViewNode *viewNode = nil; + for (NSString *viewId in viewIds) { + if (!viewNode) { + viewNode = [self.doricContext targetViewNode:viewId]; + } else { + if ([viewNode isKindOfClass:[DoricSuperNode class]]) { + viewNode = [((DoricSuperNode *) viewNode) subNodeWithViewId:viewId]; + } } } - } - if (!viewNode) { - [promise reject:@"Cannot find opposite view"]; - } else { - [self findClass:[viewNode class] target:viewNode method:name promise:promise argument:args]; - } + if (!viewNode) { + [promise reject:@"Cannot find opposite view"]; + } else { + [self findClass:[viewNode class] target:viewNode method:name promise:promise argument:args]; + } + }); } - (id)createParamWithMethodName:(NSString *)method promise:(DoricPromise *)promise argument:(id)argument {