0

Well Bacon is still one big surprise for me...

bus = new Bacon.Bus()
busProperty = bus.toProperty()

bus.push 'someValue'

busProperty.onValue (val) ->
    console.log val

This way, nothing is logged to console at all. I am probably looking from the wrong angle, but I would be expecting, that Property provides whatever was the latest value. Apparently, it's not that easy...

busProperty.log()

bus.push 'someValue'

busProperty.onValue (val) ->
    console.log val

Suddenly I am getting 'someValue' logged twice. That means the Property doesn't care about stream values until there is consumer ? Is it possible to overcome this somehow ? It seems rather silly that I would need to attach empty consumer just for the kicks of storing value into Property.

This is part of my app design, that stream receives value before any consumers are attached. However I do care about that value from the past. Essentially I am looking for synchronous version of Promise/A :)

FredyC
  • 3,339
  • 2
  • 26
  • 36

1 Answers1

0

This is a known issue, but "by design". The FAQ entry Why isn't my Property updated? explains:

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.

You may consider using a Bacon.Model instead. That'll give you a "settable property" with a bunch of additional features.

Bergi
  • 513,640
  • 108
  • 821
  • 1,164
  • Thanks, that looks somewhat interesting, but I don't like the idea, that I would have to pass around the whole Model that allows changes to value by anyone. That's why I like Property, it's doesn't provide means to change value. Sure I could wrap the `get` method, but then I cannot use it in other streams without hassle. – FredyC May 21 '14 at 05:05
  • Ok, I am going to accept your answer, because it's pretty much valid. I haven't used Bacon.Model in my current scenario, rather changed approach to the problem at whole. However this Model will find its use later for sure. – FredyC May 26 '14 at 07:08
  • Could you maybe [write an own answer](http://stackoverflow.com/help/self-answer) how you changed your approach? And maybe even accept that, just upvote me - basically all I've done is cited an FAQ entry. – Bergi May 26 '14 at 11:46
  • Well that would be out of scope for thjs question and I would need to elaborate more about background. It's basically about changing how and when are streams created and used. Not really related to FRP itself as this is obviously given "by design" as you have said. – FredyC May 26 '14 at 21:15