baconjs / bacon.js

Functional reactive programming library for TypeScript and JavaScript
https://baconjs.github.io
MIT License
6.47k stars 330 forks source link

Observable.log behavior #593

Closed sderosiaux closed 9 years ago

sderosiaux commented 9 years ago

Hi,

New to baconjs, I tried a simple example: filtering an array and printing out the even numbers, and log the values in-between (log returns the original stream, it does not have any side-effect right ? (except for the gc as the doc says)).

var stream = Bacon.fromArray([1, 2])
.log("fromArray")
.filter(function(num) {
  console.log('in filter', num);
  return num % 2 === 0;
})
.log("filter")
.first()
.log("first");

Using latest release, 0.7.60 i get :

"fromArray"
1
"fromArray"
2
"fromArray"
"<end>"
"filter"
"<end>"
"first"
"<end>"

No value output, I guess it's related to the synchronous source fromArray (I saw some issues with that) -- which have been fixed in 0.8.

Using 0.8 branch, i get:

"in filter"
1
"fromArray"
1
"in filter"
2
"fromArray"
2
"filter"
2
"first"
2
"first"
"<end>"
"fromArray"
"<end>"
"filter"
"<end>"

Few things here :

Am I misunderstanding something or is there an issue somewhere (timing issue?) ?

Thanks

jackjennings commented 9 years ago

I'll let @raimohanska chime in with more details, but I also ran into this issue fairly early on as well.

One solution is to use Bacon.sequentially rather than Bacon.fromArray. fromArray dumps all of the array contents into the first subscriber immediately, which means that any subscribers that you add after the first will not receive anything except the <end> event. In your case log is eating all of the stream events generated from fromArray before filter is called.

raimohanska commented 9 years ago

Yes it's the synch source thingie. Please don't use the 0.8 branch for anything serious yet. If you want to add logging without forcing a subscription, uou can use doAction.

sderosiaux commented 9 years ago

All right, will do. Thanks!