Ray King
2016-01-03 01:20:35 UTC
Have spent the last few day working with the new JS Interop, I have to say
I am impressed. The simplicity of the annotations and being able to pass
functions will allowInterop is good. As far as ' Making it easy to call JS
libraries' I believe we have it.
A Single thorn remains JS Promises. Dart Async is so inherent we have taken
it for granted. The life cycle of a Future is clean and with Async/Await it
all too Sweet.
What is up with these JS Promises?
Especially Interoping with them. This area has me completely lost.
Putting aside the idiosyncrasy and the fact promises can be used as
callbacks. Promise.resolve has me in knots.
Once a Js Function returns me a promise I understand it can be in one of
the three magical states, 'pending', 'fulfilled' or 'rejected'. All I was
ever seeing was 'Resolved' promises in the* [[PromiseState]]* slot. Thanks
to +Domenic Denicola I learnt that promises have states and fates,
something MDN did not make 100% clear.
So my first attempt at interacting with a promise was to assume that
JSInterop and Dart2Js had aligned with Dart world.
So off I go and:
*ServiceWorkerContainer swContain = window.navigator.serworker;*
*swContain.register(workerScriptURI, scope).then((ServiceWorkerRegistration
rego){*
*/// Then in this lamba I was confim scope and the state, handle and
unregister if required.*
*)};*
Well, that shows what happens when you assume Dart futures were
intercepting JS Promise. It did register the serviceWorker no problem. The
then was not hit. There was an object returned it was a native *JS Promise*,
with a state of resolved and a value of, you guess right
*ServiceWorkerRegisteration*. However, that does not help me I need to get
me hands on it. So after some though I started to treat it like a callback
and passed the Promise into a Completer to 'Resolve' it.
*The Mystery begins!*
Thinking others before me have certainly solved this, so after some code
searching, I found a couple of approaches. The first was the powerful
approach, the *PromoseWrapper* and accompanying *Completer* in *angular2*,
clearly written before JS 0.6..0. Also, before this version of was
https://goo.gl/8iPZsP +Filip HráÄek and his serviceWorker helper.
+Frank Pepermans approach rxDart https://goo.gl/K2mSrw, which does take
that the JS 0.6.0 approach. The core usage was in an "Observable.fromFuture
factory". Besides that, I thought Interop in *Rxdart* was another good
example along with *Chart.js*. The later does deal with JS Promise.
Finally, I looked at +alon Amir work in Lovefield, which again take a
hybrid approach https://goo.gl/EGFk0r.
So my plead is to resolve either my ignorance of how this works or an
interceptor for promise.resolve. I am happy with a workaround, yet it seem
we are so close with JS interop it would worth bring home getting https://github.com/dart-lang/dev_compiler/issues/245
done.
I am impressed. The simplicity of the annotations and being able to pass
functions will allowInterop is good. As far as ' Making it easy to call JS
libraries' I believe we have it.
A Single thorn remains JS Promises. Dart Async is so inherent we have taken
it for granted. The life cycle of a Future is clean and with Async/Await it
all too Sweet.
What is up with these JS Promises?
Especially Interoping with them. This area has me completely lost.
Putting aside the idiosyncrasy and the fact promises can be used as
callbacks. Promise.resolve has me in knots.
Once a Js Function returns me a promise I understand it can be in one of
the three magical states, 'pending', 'fulfilled' or 'rejected'. All I was
ever seeing was 'Resolved' promises in the* [[PromiseState]]* slot. Thanks
to +Domenic Denicola I learnt that promises have states and fates,
something MDN did not make 100% clear.
So my first attempt at interacting with a promise was to assume that
JSInterop and Dart2Js had aligned with Dart world.
So off I go and:
*ServiceWorkerContainer swContain = window.navigator.serworker;*
*swContain.register(workerScriptURI, scope).then((ServiceWorkerRegistration
rego){*
*/// Then in this lamba I was confim scope and the state, handle and
unregister if required.*
*)};*
Well, that shows what happens when you assume Dart futures were
intercepting JS Promise. It did register the serviceWorker no problem. The
then was not hit. There was an object returned it was a native *JS Promise*,
with a state of resolved and a value of, you guess right
*ServiceWorkerRegisteration*. However, that does not help me I need to get
me hands on it. So after some though I started to treat it like a callback
and passed the Promise into a Completer to 'Resolve' it.
*The Mystery begins!*
Thinking others before me have certainly solved this, so after some code
searching, I found a couple of approaches. The first was the powerful
approach, the *PromoseWrapper* and accompanying *Completer* in *angular2*,
clearly written before JS 0.6..0. Also, before this version of was
https://goo.gl/8iPZsP +Filip HráÄek and his serviceWorker helper.
+Frank Pepermans approach rxDart https://goo.gl/K2mSrw, which does take
that the JS 0.6.0 approach. The core usage was in an "Observable.fromFuture
factory". Besides that, I thought Interop in *Rxdart* was another good
example along with *Chart.js*. The later does deal with JS Promise.
Finally, I looked at +alon Amir work in Lovefield, which again take a
hybrid approach https://goo.gl/EGFk0r.
So my plead is to resolve either my ignorance of how this works or an
interceptor for promise.resolve. I am happy with a workaround, yet it seem
we are so close with JS interop it would worth bring home getting https://github.com/dart-lang/dev_compiler/issues/245
done.
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.