What to do with `Bus().toProperty()`?
See original GitHub issueIāve read (and answered to) numerous complaints that code involving bus.toProperty() ādoes not workā. And I totally agree that itās counterintuitive that something like this (from Bacon FAQ) does not work:
var bus = new Bacon.Bus;
var property = bus.toProperty('FIRST');
bus.push('SECOND');
property.onValue(function(val) {
console.log(val);
});
Youād like the console to log āSECONDā but it logs āFIRSTā instead.
Whatās happening here is that your Property wonāt get updated when there are no listeners. Before you add an actual listener using onValue, the Property is not active; itās not listening to the underlying Bus. It ignores input until someoneās interested. So it all boils down to the ālazinessā of all Bacon streams and properties. This is a key feature too: the streams automatically āplug inā to their sources when a subscriber is added, and āunplugā when there are no more subscribers.
A simple patch for this would be to have Bus::toProperty() always add a dummy subscriber to the resulting Property. Then, again, it would fail in setups like this:
bus.map(x => x*2).toProperty();
⦠so itās not such a great patch after all. Or is it? Because if you flipped it like
bus.toProperty().map(x => x*2);
it would be good again!
The underlying problem is that the subscriber-count-based mechanism for change propagation in Bacon.js doesnāt serve you well in cases when you create a stateful Property on top of a stateless EventStream unless you always have at least one subscriber on the Property.
One option Iām half-seriously considering is to have Properties throw an error in cases where you re-subscribe after the last subscriber has been removed. This is probably always a bug in application code, because it can lead to propagating outdated values.
Thoughts?
Issue Analytics
- State:
- Created 5 years ago
- Comments:18 (11 by maintainers)

Top Related StackOverflow Question
Hi guys! I wrote something on property reactivation on #770. What do you think?
If
Bacon.fromClipboardexisted, it would probably do the initialreadText()for the first subscriber on setup š