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
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<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));
public void command(final JSObject jsObject, final DoricPromise doricPromise) {
getDoricContext().getDriver().asyncCall(new Callable<Object>() {
@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<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 {
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 {