Javascript GWT:如何执行作为param传递的本机js函数(因此无法全局访问)
下面是重现我正在努力解决的问题的最小代码片段:Javascript GWT:如何执行作为param传递的本机js函数(因此无法全局访问),javascript,gwt,jsni,Javascript,Gwt,Jsni,下面是重现我正在努力解决的问题的最小代码片段: import java.util.function.Consumer; public class NaivePromise<T> { public NaivePromise(Consumer<Consumer<T>> resolve) { super(); create(resolve); } public native NaivePromise<T> create(
import java.util.function.Consumer;
public class NaivePromise<T> {
public NaivePromise(Consumer<Consumer<T>> resolve) {
super();
create(resolve);
}
public native NaivePromise<T> create(Consumer<Consumer<T>> handler) /*-{
return new Promise(function (resolve) {
console.log("DBG NATIVE RESOLVE");
handler.@java.util.function.Consumer::accept(*)(resolve)
});
}-*/;
public static void pong() {
new NaivePromise<String>(resolve -> {
resolve.accept("HERE WE'LL GET AN ERROR, SINCE RESOLVE IS ACTUALLY A NATIVE FUNCTION");
});
}
}
import java.util.function.Consumer;
公共阶级承诺{
公共承诺(消费者解决){
超级();
创建(解决);
}
创建公共本机应用程序(使用者处理程序)/*-{
返回新承诺(函数(解析){
log(“DBG本机解析”);
处理程序。@java.util.function.Consumer::accept(*)(解析)
});
}-*/;
公共静态void pong(){
新承诺(解决->{
accept(“这里我们将得到一个错误,因为resolve实际上是一个本机函数”);
});
}
}
我的问题是-如何执行作为lambda传递给GWT使用者(或任何其他功能接口)的本机函数 您必须使
解析
成为JavaScriptObject
而不是消费者
,并使用JSNI调用它:
private native void call(JavaScriptObject resolve, String arg) /*-{
resolve(arg);
}-*/;
尽管您实际上应该在这里使用JsInterop,使用
@JsFunction
接口;实际上,可能只需要使用Elemental 2对Promise
的映射,您就必须将解析为JavaScriptObject
而不是消费者
,并使用JSNI将其称为:
private native void call(JavaScriptObject resolve, String arg) /*-{
resolve(arg);
}-*/;
尽管您实际上应该在这里使用JsInterop,使用@JsFunction
接口;实际上可能只是使用元素2的Promise
映射