Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toDeferred suggestion #31

Open
wolfflow opened this issue Mar 8, 2014 · 17 comments
Open

toDeferred suggestion #31

wolfflow opened this issue Mar 8, 2014 · 17 comments

Comments

@wolfflow
Copy link
Member

wolfflow commented Mar 8, 2014

Sometimes, you need to turn your Bacon observable back to $.Deferred (/Promise/etc.).
(e.g. in order to provide a solution for jQuery users which are not familiar with Bacon =) ) and, AFAIK, RxJS has smth similar.

A method suggestion gist is here:
https://gist.github.com/wolfflow/b0a7a9a37938f80a9c6d

@raimohanska
Copy link
Contributor

The idea sounds good. Dependency on jQuery is not acceptable in the core though. Should we consider a more generic asPromise method instead, without ext deps?

The jq deferred based impl can of course be provided as a plugin too.

-juha-

On 8.3.2014, at 22.18, Daniel K [email protected] wrote:

Sometimes, you need to turn your Bacon observable back to $.Deferred (/Promise/etc.).
(e.g. in order to provide a solution for jQuery users which are not familiar with Bacon =) ) and, AFAIK, RxJS has smth similar.

A method suggestion gist is here:
https://gist.github.com/wolfflow/b0a7a9a37938f80a9c6d


Reply to this email directly or view it on GitHub.

@wolfflow
Copy link
Member Author

What do you think about Bacon.$.toDeferred(obs) in 'bacon.jquery' ?

Also, there're several platform-dependent implementations of Promises/A (WinJS, 'native' Promises @ webkit, etc..)
Pls, take a look at gist I've made here about promises:
https://gist.github.com/wolfflow/71af0ad215c856291e5c

@raimohanska
Copy link
Contributor

Adding toDeferred in bacon.jquery sounds like a good idea. In fact I'd rather extend the Observable prototype so that stream.toDeferred() would be available.

Don't know yet what to do with all the different promise specs and implementations :)

@wolfflow
Copy link
Member Author

What about putting Bacon.Observable::toDeferred method inside bacon.jquery ?
( And, there is Bacon.Observable::ajax already )

@raimohanska
Copy link
Contributor

Yes, that's exactly what I meant :)

@raimohanska
Copy link
Contributor

So #32 was merged!

Still, we should 1) update README 2) add tests 3) refactor implementation: what is withHandler doing there? Why not plain subscribe?

@wolfflow
Copy link
Member Author

Ok will do. Surely, it must be subscribe)
Once upon a time, maybe about a year ago, when I used to move from RxJs to Bacon, there was a problem with subscribe method so that I still avoid a use of it =) =)

@raimohanska
Copy link
Contributor

Just checking: would you expect toDeferred to get the first event from the stream and ignore the rest? That's what it does as of now.

@wolfflow
Copy link
Member Author

Not at all =). Ajax Stream consists only of one event value though. I think there's need for more tests e.g. using dfd.progress

@wolfflow
Copy link
Member Author

And... it works as I expected=)

var D = Bacon.sequentially(100, [1,2,3]).toDeferred();
D.progress(function(x){console.log("P",x)})

P.S. For Bacon.fromArray([1,2,3]).toDeferred(), I get unsub is not defined error -- cause unsub is not defined for immediate values somehow. Will report an issue for that.

@raimohanska
Copy link
Contributor

I already removed the unsub call in master. Pls have a look at https://github.com/baconjs/bacon.jquery/blob/master/src/bacon.jquery.coffee#L95

The take(1).endOnError() thing does practically the same without the problems related to manual unsub.

@wolfflow
Copy link
Member Author

Yup! But there's should be progress fn call for each value in the stream to satisfy Deferred specs). What do we need to do then?

@wolfflow
Copy link
Member Author

For instance, if we ditch take(1) then we'd get following behaviour:

var log = console.log.bind(console)
// async sequence values
seq = Bacon.sequentially(100, [1,2,3]).toDeferred()
seq.progress(log) // 1 - 2 - 3
seq.done(log) // 3
// 'immediate' values
immediate = Bacon.fromArray([1,2,3]).toDeferred()
immediate.progress(log) // 3 
immediate.done(log) // 3
//ajax
ajax = Bacon.$.ajax({url:"http://myurl"}).toDeferred()
ajax.progress(log) //result
ajax.done(log) //result

All except immediate.progress are OK. So, it's needed to do smth with this one and return the unsub.

@raimohanska
Copy link
Contributor

Hmm.. Deferred specs? What are they and where?

@wolfflow
Copy link
Member Author

nevermind, just plain jquery docs for dfd. http://api.jquery.com/category/deferred-object/
)

@wolfflow
Copy link
Member Author

Look at this example -- http://codepen.io/wolfflow/pen/fcmzl/. Despite some progress event problems with fromArray, it seems to be more suitable.

@wolfflow
Copy link
Member Author

wolfflow commented May 5, 2014

baconjs/bacon.js#367

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants