baconjs / bacon.js

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

Properties plugged into buses can be lost #568

Open Macil opened 9 years ago

Macil commented 9 years ago

This just threw me for a loop in a project:

var bus = new Bacon.Bus();
bus.toProperty(1).onValue(function(x){ console.log('x', x); });
// possibly time passes
bus.plug(Bacon.constant(9));

The output given is just "x 1". I'm not sure if this is intended or not.

Macil commented 9 years ago

Kefir works with the above code fine.

Adding .toEventStream() works around the issue, which initially seems fair enough if intended:

bus.plug(Bacon.constant(9).toEventStream());

I had tried that earlier, but with a log statement still in there, which caused things to not work because log would eat the event before the stream got plugged into the bus. Kefir's lack of toEventStream and insistence on properties instead of synchronously-emitting streams is making a lot of sense to me lately.

// doesn't work
bus.plug(Bacon.constant(9).toEventStream().log('foo'));
phadej commented 9 years ago

The plug is documented as:

bus.plug(stream) plugs the given stream to the Bus. All events from the given stream will be delivered to the subscribers of the Bus. Returns a function that can be used to unplug the same stream.

the bus.plug(property) behaviour is undefined, and firing only x: 1 event makes sense to me.

Macil commented 9 years ago

Could plugging a property into a bus be made to throw an error if it doesn't fully work? (I guess that would be a potentially breaking change for 0.8.)

semmel commented 6 years ago

Cannot reproduce behaviour in the OP with Bacon 2.0:

var bus = new Bacon.Bus();
var unsub = bus.toProperty(1).onValue(console.log);
// -> 1
bus.plug(Bacon.constant(2));
// - > 2

Certainly according to the documentation one should just plug in EventStreams, so it's better to

...
bus.plug(Bacon.constant(3).toEventStream());
// -> 3

works as expected I guess.