question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What to do with `Bus().toProperty()`?

See original GitHub issue

I’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:open
  • Created 5 years ago
  • Comments:18 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
raimohanskacommented, Aug 21, 2020

Hi guys! I wrote something on property reactivation on #770. What do you think?

0reactions
steve-taylorcommented, Aug 19, 2020

If Bacon.fromClipboard existed, it would probably do the initial readText() for the first subscriber on setup šŸ˜›

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bus | baconjs
Function will receive event objects for all new value, end and error events in the stream. The subscribe() call returns a unsubscribe function...
Read more >
Observable As Property Helper - ReactiveUI
The [Reactive] property with .BindTo() should be used for business-critical code. It will invoke the INotifyPropertyChanged and INotifyPropertyChangingĀ ...
Read more >
JMSHeaderNode (IBM Integration Bus API)
Get the JMSHeaderNode "JMS Message Priority" property. java.lang.String, getJmsReplyTo(). Get the JMSHeaderNode "JMS Reply To" property. java.lang.
Read more >
Azure Service Bus messages, payloads, and serialization
This article provides an overview of Azure Service Bus messages, ... should do so based on user properties and not lean on the...
Read more >
Magnet Transportation Information - eChoices - Lausd.net
... that you would like your child to get on the bus or Magnet School to find a list of schools with nearby...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found