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 https://groups.google.com/a/dartlang.org/forum/#!topic/web/_DCR6vMBm7Y
我正在运行的解决方法
所以从 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”库混合,试图不包装太多。但由于隐藏进口和各种东西,它变得非常混乱。
一旦 async/await 与 Promise 对齐,所有的跳舞都将变得毫无意义。我省略了一些包装,基本上,如果您需要对象或其中的值,则需要包装。