feat:fix command view cannot find view,let find on UI Thread

This commit is contained in:
pengfei.zhou 2020-03-23 13:23:04 +08:00 committed by osborn
parent 7408d9cfed
commit 2046baa23d
2 changed files with 92 additions and 83 deletions

View File

@ -90,77 +90,84 @@ public class ShaderPlugin extends DoricJavaPlugin {
} }
@DoricMethod @DoricMethod
public void command(JSObject jsObject, final DoricPromise doricPromise) { public void command(final JSObject jsObject, final DoricPromise doricPromise) {
final JSValue[] viewIds = jsObject.getProperty("viewIds").asArray().toArray(); getDoricContext().getDriver().asyncCall(new Callable<Object>() {
final String name = jsObject.getProperty("name").asString().value(); @Override
final JSValue args = jsObject.getProperty("args"); public Object call() throws Exception {
ViewNode viewNode = null; final JSValue[] viewIds = jsObject.getProperty("viewIds").asArray().toArray();
for (JSValue value : viewIds) { final String name = jsObject.getProperty("name").asString().value();
if (viewNode == null) { final JSValue args = jsObject.getProperty("args");
viewNode = getDoricContext().targetViewNode(value.asString().value()); ViewNode viewNode = null;
} else { for (JSValue value : viewIds) {
if (value.isString() && viewNode instanceof SuperNode) { if (viewNode == null) {
String viewId = value.asString().value(); viewNode = getDoricContext().targetViewNode(value.asString().value());
viewNode = ((SuperNode) viewNode).getSubNodeById(viewId); } 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<ViewNode> 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<JavaValue> callable = new Callable<JavaValue>() {
@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<JavaValue> asyncResult = getDoricContext().getDriver()
.asyncCall(callable, ThreadMode.UI);
if (!method.getReturnType().equals(Void.TYPE)) {
asyncResult.setCallback(new AsyncResult.Callback<JavaValue>() {
@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<ViewNode> 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<JavaValue> callable = new Callable<JavaValue>() {
@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<JavaValue> asyncResult = getDoricContext().getDriver()
.asyncCall(callable, ThreadMode.UI);
if (!method.getReturnType().equals(Void.TYPE)) {
asyncResult.setCallback(new AsyncResult.Callback<JavaValue>() {
@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);
} }

View File

@ -50,24 +50,26 @@ - (void)render:(NSDictionary *)argument withPromise:(DoricPromise *)promise {
} }
- (void)command:(NSDictionary *)argument withPromise:(DoricPromise *)promise { - (void)command:(NSDictionary *)argument withPromise:(DoricPromise *)promise {
NSArray *viewIds = argument[@"viewIds"]; dispatch_async(dispatch_get_main_queue(), ^{
id args = argument[@"args"]; NSArray *viewIds = argument[@"viewIds"];
NSString *name = argument[@"name"]; id args = argument[@"args"];
DoricViewNode *viewNode = nil; NSString *name = argument[@"name"];
for (NSString *viewId in viewIds) { DoricViewNode *viewNode = nil;
if (!viewNode) { for (NSString *viewId in viewIds) {
viewNode = [self.doricContext targetViewNode:viewId]; if (!viewNode) {
} else { viewNode = [self.doricContext targetViewNode:viewId];
if ([viewNode isKindOfClass:[DoricSuperNode class]]) { } else {
viewNode = [((DoricSuperNode *) viewNode) subNodeWithViewId:viewId]; if ([viewNode isKindOfClass:[DoricSuperNode class]]) {
viewNode = [((DoricSuperNode *) viewNode) subNodeWithViewId:viewId];
}
} }
} }
} if (!viewNode) {
if (!viewNode) { [promise reject:@"Cannot find opposite view"];
[promise reject:@"Cannot find opposite view"]; } else {
} else { [self findClass:[viewNode class] target:viewNode method:name promise:promise argument:args];
[self findClass:[viewNode class] target:viewNode method:name promise:promise argument:args]; }
} });
} }
- (id)createParamWithMethodName:(NSString *)method promise:(DoricPromise *)promise argument:(id)argument { - (id)createParamWithMethodName:(NSString *)method promise:(DoricPromise *)promise argument:(id)argument {