Dart JS Interop 0.6.0 and JS Promises - resolving

二次信任 提交于 2021-01-27 10:57:09

问题


It does not appear there is a clear interception of JS Promises by the Interop or dart2JS.

ServiceWorkerContainer swContain = window.navigator.serworker;

swContain.register(workerScriptURI,scope).then((ServiceWorkerRegistration rego){
/// Here confirm scope and the state, handle and unregister if required.
)};

Yet it appears to be no way of pulling this off without wrapping the promise and a completer. When I then I can not get to work in a dependable fashion.

Long post here: https://groups.google.com/a/dartlang.org/forum/#!topic/web/_DCR6vMBm7Y

WorkAround That I Am Running With

So this works well as of 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))

}

I tried blending the above with the existing 'dart:html' library, in an attempt not to wrap so much. But it got really messy with hiding imports and all sort of stuff.

All the dancing around will become pointless as soon as async/await get lined up with promises. I have omitted some of the wrapping, basicly if you need the Object or a value from it you need to wrap.

来源:https://stackoverflow.com/questions/34572517/dart-js-interop-0-6-0-and-js-promises-resolving

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!