Discussion:
[dart-misc] JSInterop 0.6.0 Is Great ~ Promises Mysterious
Ray King
2016-01-03 01:20:35 UTC
Permalink
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.
--
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.
'Jacob Richman' via Dart Misc
2016-01-04 21:30:24 UTC
Permalink
The goal long term solution is the one described by
https://github.com/dart-lang/sdk/issues/24679
where Dart Future and JS Promise can generally be used interchangeably just
as Dart List and JS Array can be today with JS interop.

I'm starting an investigation now on how difficult that solution will be to
implement in Dart2JS.
Until there is a solution that unifies Promise and Future, I recommend
using a minimal JS interop definition for Promise. For example:
https://github.com/frankpepermans/rxdart/blob/master/lib/src/proxy/promise_proxy.dart#L3-L14
looks reasonable. As with any other JS interop code, be sure to call
allowInterop on Dart functions you are passing to JS and everything should
work fine.
Post by Ray King
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.
*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
--
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.
Ray King
2016-01-05 00:45:42 UTC
Permalink
Thanks for the reply Jacob and the stuff you have already done on 0.6.0. It
does make life easier...

generally I have not have any problems, except the one above... What is
different here is that I am not doing a clean interaction with stuff I have
written on the JS side. I have been able to call my stuff and pass closures
and even capture the this on way.

What I am doing using 'dart:html' to save myself from wrapping the Web
Apis, just trying to making life easier.

So I only interop to attempt to get that value out of the a promise... by
thening it...

In the perfect world I could doing any of the following. (I know this is
what you are working)...

import 'dart:html';

Future<ServiceWorkerRegistration> initialiseServiceWorker(Function config)
async {

var ctx = window.navigator.serviceWoker;

try {
return await ctx.register(config(swScriptUri), config(swScope));
catch(e){
throw allToHellError
}

OK back to reality,

my problem is that the call ctx.register(config(swScriptUri),
config(swScope)); Which is is actually call
ServiceWorkerContainer.register (
https://api.dartlang.org/1.13.1/dart-html/ServiceWorkerContainer-class.html)

Which returning a JSObject as far as Dart is concern (in reality it's a
Promise which is 'Resolved' and holds a promiseValue of
ServiceWorkerRegistration.

I then pass it to a wrapped Promise.resolve(promise_I_got_back) to cast it
and nothing....

I wrapped .then() from Promise and could not get it that way either.

I am sure I missing something from how I am trying to resolve from the
first promise.

Anyhelp would be appreciated.








On Tue, Jan 5, 2016 at 8:30 AM 'Jacob Richman' via Dart Misc <
Post by 'Jacob Richman' via Dart Misc
The goal long term solution is the one described by
https://github.com/dart-lang/sdk/issues/24679
where Dart Future and JS Promise can generally be used interchangeably
just as Dart List and JS Array can be today with JS interop.
I'm starting an investigation now on how difficult that solution will be
to implement in Dart2JS.
Until there is a solution that unifies Promise and Future, I recommend
https://github.com/frankpepermans/rxdart/blob/master/lib/src/proxy/promise_proxy.dart#L3-L14
looks reasonable. As with any other JS interop code, be sure to call
allowInterop on Dart functions you are passing to JS and everything should
work fine.
Post by Ray King
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.
*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
--
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
--
Ray King - +61 438 975 398
--
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.
'Jacob Richman' via Dart Misc
2016-01-06 01:57:34 UTC
Permalink
That is exactly the perfect world I'm working towards.
If you send me a link to an example repoducing what you expect should work
currently but isn't, I'll take a look
You won't be able to call await on a Promise until the work to make Future
and Promise interchangeable is done so that example definitely won't work
now. However, if you write promise code the way you would in JS combined
with using
https://github.com/frankpepermans/rxdart/blob/master/lib/src/proxy/promise_proxy.dart#L3-L14
that
will work today.
Post by Ray King
Thanks for the reply Jacob and the stuff you have already done on 0.6.0.
It does make life easier...
generally I have not have any problems, except the one above... What is
different here is that I am not doing a clean interaction with stuff I have
written on the JS side. I have been able to call my stuff and pass closures
and even capture the this on way.
What I am doing using 'dart:html' to save myself from wrapping the Web
Apis, just trying to making life easier.
So I only interop to attempt to get that value out of the a promise... by
thening it...
In the perfect world I could doing any of the following. (I know this is
what you are working)...
import 'dart:html';
Future<ServiceWorkerRegistration> initialiseServiceWorker(Function config)
async {
var ctx = window.navigator.serviceWoker;
try {
return await ctx.register(config(swScriptUri), config(swScope));
catch(e){
throw allToHellError
}
OK back to reality,
my problem is that the call ctx.register(config(swScriptUri),
config(swScope)); Which is is actually call
ServiceWorkerContainer.register (
https://api.dartlang.org/1.13.1/dart-html/ServiceWorkerContainer-class.html
)
Which returning a JSObject as far as Dart is concern (in reality it's a
Promise which is 'Resolved' and holds a promiseValue of
ServiceWorkerRegistration.
I then pass it to a wrapped Promise.resolve(promise_I_got_back) to cast it
and nothing....
I wrapped .then() from Promise and could not get it that way either.
I am sure I missing something from how I am trying to resolve from the
first promise.
Anyhelp would be appreciated.
On Tue, Jan 5, 2016 at 8:30 AM 'Jacob Richman' via Dart Misc <
Post by 'Jacob Richman' via Dart Misc
The goal long term solution is the one described by
https://github.com/dart-lang/sdk/issues/24679
where Dart Future and JS Promise can generally be used interchangeably
just as Dart List and JS Array can be today with JS interop.
I'm starting an investigation now on how difficult that solution will be
to implement in Dart2JS.
Until there is a solution that unifies Promise and Future, I recommend
https://github.com/frankpepermans/rxdart/blob/master/lib/src/proxy/promise_proxy.dart#L3-L14
looks reasonable. As with any other JS interop code, be sure to call
allowInterop on Dart functions you are passing to JS and everything should
work fine.
Post by Ray King
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.
*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
--
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
--
Ray King - +61 438 975 398
--
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
--
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.
Ray King
2016-01-14 06:01:20 UTC
Permalink
Hi jacob

Sorry I did not get back to you earlier... Today was the first opportunity
I had to look at this again.

firstly here is a Gist https://gist.github.com/rayk/0f240ac707f988819917 of
what hack out this afternoon to demo the problem.


As we have said @JS('Promise') will work, where I start running issues... I
have not been able to do this and blend in the use of the 'dart:html'
library, hence I start wrapping and wrapping even more.

the root cause is that getting at ServiceWorkerContainer then register on
it, returns _Future that appears to be able to resist resolve and a
completer... (Also accept that it could be I not seeing see a way to blend
them nicely).

Anyhow the problems continue, the executor with a promise can have either
two functions like a then(resolver, error) or just one like SkipWaiting,
that needs to given a promise to keep the ExtendableEvent alive...

Anyhow if there is a better way let me know.. I still happy with JS 0.6.0
think it is improvements and certainly meeting the goal of bring your own
JS library.. Just dealing with the events and promise of those external
libraries can be painful.

Thanks again for your time... Happy to here 1) There is a easier/better
way, 2) No this is the situation, or 3) a guesstimate about when #
/dev_compiler/issues/245
<https://github.com/dart-lang/dev_compiler/issues/245> might happen.. Just
trying to working out should change tracking in the short term,..

Thanks Agains.
Post by 'Jacob Richman' via Dart Misc
That is exactly the perfect world I'm working towards.
If you send me a link to an example repoducing what you expect should
work currently but isn't, I'll take a look
You won't be able to call await on a Promise until the work to make Future
and Promise interchangeable is done so that example definitely won't work
now. However, if you write promise code the way you would in JS combined
with using
https://github.com/frankpepermans/rxdart/blob/master/lib/src/proxy/promise_proxy.dart#L3-L14 that
will work today.
Post by Ray King
Thanks for the reply Jacob and the stuff you have already done on 0.6.0.
It does make life easier...
generally I have not have any problems, except the one above... What is
different here is that I am not doing a clean interaction with stuff I have
written on the JS side. I have been able to call my stuff and pass closures
and even capture the this on way.
What I am doing using 'dart:html' to save myself from wrapping the Web
Apis, just trying to making life easier.
So I only interop to attempt to get that value out of the a promise... by
thening it...
In the perfect world I could doing any of the following. (I know this is
what you are working)...
import 'dart:html';
Future<ServiceWorkerRegistration> initialiseServiceWorker(Function
config) async {
var ctx = window.navigator.serviceWoker;
try {
return await ctx.register(config(swScriptUri), config(swScope));
catch(e){
throw allToHellError
}
OK back to reality,
my problem is that the call ctx.register(config(swScriptUri),
config(swScope)); Which is is actually call
ServiceWorkerContainer.register (
https://api.dartlang.org/1.13.1/dart-html/ServiceWorkerContainer-class.html
)
Which returning a JSObject as far as Dart is concern (in reality it's a
Promise which is 'Resolved' and holds a promiseValue of
ServiceWorkerRegistration.
I then pass it to a wrapped Promise.resolve(promise_I_got_back) to cast
it and nothing....
I wrapped .then() from Promise and could not get it that way either.
I am sure I missing something from how I am trying to resolve from the
first promise.
Anyhelp would be appreciated.
On Tue, Jan 5, 2016 at 8:30 AM 'Jacob Richman' via Dart Misc <
Post by 'Jacob Richman' via Dart Misc
The goal long term solution is the one described by
https://github.com/dart-lang/sdk/issues/24679
where Dart Future and JS Promise can generally be used interchangeably
just as Dart List and JS Array can be today with JS interop.
I'm starting an investigation now on how difficult that solution will be
to implement in Dart2JS.
Until there is a solution that unifies Promise and Future, I recommend
https://github.com/frankpepermans/rxdart/blob/master/lib/src/proxy/promise_proxy.dart#L3-L14
looks reasonable. As with any other JS interop code, be sure to call
allowInterop on Dart functions you are passing to JS and everything should
work fine.
Post by Ray King
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.
*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
--
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
--
Ray King - +61 438 975 398
--
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
--
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.
'Jacob Richman' via Dart Misc
2016-01-14 06:20:34 UTC
Permalink
Thanks for the repo! I'll take a look at it tomorrow to see if there is
anything obvious.
Rewriting Future to base it on promise will be a large work item. I expect
to start experimenting it in the next few weeks but I won't have a clear
idea of how long it will take to complete until I have done a more
extensive investigation of what it will take.

-Jacob
Post by Ray King
Hi jacob
Sorry I did not get back to you earlier... Today was the first
opportunity I had to look at this again.
firstly here is a Gist https://gist.github.com/rayk/0f240ac707f988819917
of what hack out this afternoon to demo the problem.
I have not been able to do this and blend in the use of the 'dart:html'
library, hence I start wrapping and wrapping even more.
the root cause is that getting at ServiceWorkerContainer then register on
it, returns _Future that appears to be able to resist resolve and a
completer... (Also accept that it could be I not seeing see a way to blend
them nicely).
Anyhow the problems continue, the executor with a promise can have either
two functions like a then(resolver, error) or just one like SkipWaiting,
that needs to given a promise to keep the ExtendableEvent alive...
Anyhow if there is a better way let me know.. I still happy with JS 0.6.0
think it is improvements and certainly meeting the goal of bring your own
JS library.. Just dealing with the events and promise of those external
libraries can be painful.
Thanks again for your time... Happy to here 1) There is a easier/better
way, 2) No this is the situation, or 3) a guesstimate about when #
/dev_compiler/issues/245
<https://github.com/dart-lang/dev_compiler/issues/245> might happen..
Just trying to working out should change tracking in the short term,..
Thanks Agains.
Post by 'Jacob Richman' via Dart Misc
That is exactly the perfect world I'm working towards.
If you send me a link to an example repoducing what you expect should
work currently but isn't, I'll take a look
You won't be able to call await on a Promise until the work to make
Future and Promise interchangeable is done so that example definitely won't
work now. However, if you write promise code the way you would in JS
combined with using
https://github.com/frankpepermans/rxdart/blob/master/lib/src/proxy/promise_proxy.dart#L3-L14 that
will work today.
Post by Ray King
Thanks for the reply Jacob and the stuff you have already done on 0.6.0.
It does make life easier...
generally I have not have any problems, except the one above... What is
different here is that I am not doing a clean interaction with stuff I have
written on the JS side. I have been able to call my stuff and pass closures
and even capture the this on way.
What I am doing using 'dart:html' to save myself from wrapping the Web
Apis, just trying to making life easier.
So I only interop to attempt to get that value out of the a promise...
by thening it...
In the perfect world I could doing any of the following. (I know this is
what you are working)...
import 'dart:html';
Future<ServiceWorkerRegistration> initialiseServiceWorker(Function
config) async {
var ctx = window.navigator.serviceWoker;
try {
return await ctx.register(config(swScriptUri), config(swScope));
catch(e){
throw allToHellError
}
OK back to reality,
my problem is that the call ctx.register(config(swScriptUri),
config(swScope)); Which is is actually call
ServiceWorkerContainer.register (
https://api.dartlang.org/1.13.1/dart-html/ServiceWorkerContainer-class.html
)
Which returning a JSObject as far as Dart is concern (in reality it's a
Promise which is 'Resolved' and holds a promiseValue of
ServiceWorkerRegistration.
I then pass it to a wrapped Promise.resolve(promise_I_got_back) to cast
it and nothing....
I wrapped .then() from Promise and could not get it that way either.
I am sure I missing something from how I am trying to resolve from the
first promise.
Anyhelp would be appreciated.
On Tue, Jan 5, 2016 at 8:30 AM 'Jacob Richman' via Dart Misc <
Post by 'Jacob Richman' via Dart Misc
The goal long term solution is the one described by
https://github.com/dart-lang/sdk/issues/24679
where Dart Future and JS Promise can generally be used interchangeably
just as Dart List and JS Array can be today with JS interop.
I'm starting an investigation now on how difficult that solution will
be to implement in Dart2JS.
Until there is a solution that unifies Promise and Future, I recommend
https://github.com/frankpepermans/rxdart/blob/master/lib/src/proxy/promise_proxy.dart#L3-L14
looks reasonable. As with any other JS interop code, be sure to call
allowInterop on Dart functions you are passing to JS and everything should
work fine.
Post by Ray King
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.
*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
--
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
--
Ray King - +61 438 975 398
--
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
--
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
--
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.
Ray King
2016-01-22 00:08:50 UTC
Permalink
Hi Jacob, any update on this front or feedback on my approach... Any
insights?

Appreciate getting futures and promises align is large block of work... I
it will take what it will take.... Think it would be worth it for interop.
Until take days comes, is there a better way of doing then I have done in
the Gist I sent over.. If so can you please share..

Thanks in advance.....again.
Post by 'Jacob Richman' via Dart Misc
Thanks for the repo! I'll take a look at it tomorrow to see if there is
anything obvious.
Rewriting Future to base it on promise will be a large work item. I expect
to start experimenting it in the next few weeks but I won't have a clear
idea of how long it will take to complete until I have done a more
extensive investigation of what it will take.
-Jacob
Post by Ray King
Hi jacob
Sorry I did not get back to you earlier... Today was the first
opportunity I had to look at this again.
firstly here is a Gist https://gist.github.com/rayk/0f240ac707f988819917
of what hack out this afternoon to demo the problem.
I have not been able to do this and blend in the use of the 'dart:html'
library, hence I start wrapping and wrapping even more.
the root cause is that getting at ServiceWorkerContainer then register on
it, returns _Future that appears to be able to resist resolve and a
completer... (Also accept that it could be I not seeing see a way to blend
them nicely).
Anyhow the problems continue, the executor with a promise can have either
two functions like a then(resolver, error) or just one like SkipWaiting,
that needs to given a promise to keep the ExtendableEvent alive...
Anyhow if there is a better way let me know.. I still happy with JS 0.6.0
think it is improvements and certainly meeting the goal of bring your own
JS library.. Just dealing with the events and promise of those external
libraries can be painful.
Thanks again for your time... Happy to here 1) There is a easier/better
way, 2) No this is the situation, or 3) a guesstimate about when #
/dev_compiler/issues/245
<https://github.com/dart-lang/dev_compiler/issues/245> might happen..
Just trying to working out should change tracking in the short term,..
Thanks Agains.
Post by 'Jacob Richman' via Dart Misc
That is exactly the perfect world I'm working towards.
If you send me a link to an example repoducing what you expect should
work currently but isn't, I'll take a look
You won't be able to call await on a Promise until the work to make
Future and Promise interchangeable is done so that example definitely won't
work now. However, if you write promise code the way you would in JS
combined with using
https://github.com/frankpepermans/rxdart/blob/master/lib/src/proxy/promise_proxy.dart#L3-L14 that
will work today.
Post by Ray King
Thanks for the reply Jacob and the stuff you have already done on
0.6.0. It does make life easier...
generally I have not have any problems, except the one above... What is
different here is that I am not doing a clean interaction with stuff I have
written on the JS side. I have been able to call my stuff and pass closures
and even capture the this on way.
What I am doing using 'dart:html' to save myself from wrapping the Web
Apis, just trying to making life easier.
So I only interop to attempt to get that value out of the a promise...
by thening it...
In the perfect world I could doing any of the following. (I know this
is what you are working)...
import 'dart:html';
Future<ServiceWorkerRegistration> initialiseServiceWorker(Function
config) async {
var ctx = window.navigator.serviceWoker;
try {
return await ctx.register(config(swScriptUri), config(swScope));
catch(e){
throw allToHellError
}
OK back to reality,
my problem is that the call ctx.register(config(swScriptUri),
config(swScope)); Which is is actually call
ServiceWorkerContainer.register (
https://api.dartlang.org/1.13.1/dart-html/ServiceWorkerContainer-class.html
)
Which returning a JSObject as far as Dart is concern (in reality it's
a Promise which is 'Resolved' and holds a promiseValue of
ServiceWorkerRegistration.
I then pass it to a wrapped Promise.resolve(promise_I_got_back) to cast
it and nothing....
I wrapped .then() from Promise and could not get it that way either.
I am sure I missing something from how I am trying to resolve from the
first promise.
Anyhelp would be appreciated.
On Tue, Jan 5, 2016 at 8:30 AM 'Jacob Richman' via Dart Misc <
Post by 'Jacob Richman' via Dart Misc
The goal long term solution is the one described by
https://github.com/dart-lang/sdk/issues/24679
where Dart Future and JS Promise can generally be used interchangeably
just as Dart List and JS Array can be today with JS interop.
I'm starting an investigation now on how difficult that solution will
be to implement in Dart2JS.
Until there is a solution that unifies Promise and Future, I recommend
https://github.com/frankpepermans/rxdart/blob/master/lib/src/proxy/promise_proxy.dart#L3-L14
looks reasonable. As with any other JS interop code, be sure to call
allowInterop on Dart functions you are passing to JS and everything should
work fine.
Post by Ray King
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.
*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,
--
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
--
Ray King - +61 438 975 398
--
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
--
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
--
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.
Loading...