baconjs / bacon.js

Functional reactive programming library for TypeScript and JavaScript

Home Page:https://baconjs.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sequentially(0,...) and fromArray() inconsistent when zipped

cefn opened this issue · comments

As described in #606

I found that constructing streams from arrays with sequentially(0, ...) causes zipped streams to terminate after the arrays are exhausted, but when the same arrays have a stream constructed through fromArray(...), this is not the case.

This is a documentation request, feature or bug request depending whether this is an intended behaviour or not.

A gist Mocha test example is available at...
https://gist.github.com/cefn/7fd3504a0b29575f3b6f
...where commenting the sequentially(0, ...) calls and uncommenting the fromArray(...) calls makes the test suite hang indefinitely as the streams never terminate.

This happens because the onValue subscriber consumes all the events from the fromArray based sources and there's nothing left for the toPromise subscriber.

So, the stream is in fact terminated before you assign your toPromise subscriber. The subscribers added after the stream is terminated will receive the End event form the stream, telling them that the stream is finished. However, the promise won't be resolved/rejected because there's nothing left in the stream.

Maybe toPromise should reject the promise if the source stream terminates without a value?

Hey thanks for looking into my case!

I think a change to the implied contract of toPromise() where it is known that the Promise will never be satisfied (with a useful error thrown) would be very productive.

The real puzzle I'm trying to solve may be underpinned by a similar issue. I've expanded upon it at http://stackoverflow.com/questions/30996636/bacon-zipped-stream-not-ending-when-matches-complete-causing-test-suite-to-hang

However, I seem to be trapped. As described in #606 (comment) I am forced to call toValues() as the zip function is skipped owing to laziness if I simply ask toPromise to tell me about the stream end. However, if I call toValues then I can't apparently also wire up toPromise to handle the stream end.

Am I using Bacon in a weird way by trying to get it to match up streams for testing like this? Is there a correct way to do it?

Happy to refile this puzzle with a different title if it's not in fact the same issue.