Interop或dart2JS似乎没有明确拦截JS Promises。
ServiceWorkerContainer swContain = window.navigator.serworker;
swContain.register(workerScriptURI,scope).then((ServiceWorkerRegistration rego){
/// Here confirm scope and the state, handle and unregister if required.
)};
然而,如果没有包含承诺和完成者,似乎没有办法解决这个问题。当我那时,我无法以可靠的方式工作。
此处有长篇文章:https://groups.google.com/a/dartlang.org/forum/#!topic/web/_DCR6vMBm7Y
我正在运行的WorkAround
所以这适用于0.6.0:
@JS("Promise")
class Promise {
external void then(Function onFulfilled, Function onRejected);
external Static Promise resolve (dynamic value);
}
@JS("ServiceWorkerContainer")
class ServiceWorkerContainer {
external Promise register(String scriptURL, Map options)
}
main(){
ServiceWorkContainer swCTX = navigator.serviceWorker;
successHandler(dynamic value) {
// Call Promise resolve(value) if the not settled. Ultimately
ServiceWorkerRegistration swRego = value;
// Now call anything you want on the Rego, just wrap what you need.
failureHandler(dynamic value) {
// Deal with it here
/// This the main show here.
swCTX.register('script-name.dart.js', scope).then(allowInterop(successHandler), allowInterop(failureHandler))
}
我尝试将上面的内容与现有的'dart:html'库混合在一起,试图不要包装太多。但是隐藏进口和各种东西真的很乱。
一旦异步/等待与承诺对齐,所有的舞蹈将变得毫无意义。我已经省略了一些包装,基本上如果你需要Object或你需要包装它的值。