baconjs / bacon.js

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

Strange behavior when initializing properties #339

Closed dylemma closed 10 years ago

dylemma commented 10 years ago

I've got a setup along the lines of

var _x = 0, _xChanges = new Bacon.Bus()
function getX(){ return _x }
function setX(x2){ _x = x2 }
var x = _xChanges.toProperty(_x)

And eventually the x property will be wired up to some UI thing, i.e. x.onValue(...). But before that, I call setX(somethingElse). The problem is that if I call setX before actually wiring it up, the x property still has an initial value of 0, instead of the desired somethingElse. When I added a .log('value of x:') to the definition of x, the behavior changed to what I expected.

A slightly more distilled version:

var changes = new Bacon.Bus(),
    vals = changes.toProperty(0)
changes.push(3)
vals.log('value:')
// prints "value: 0", but I expected "value: 3"

Is this behavior intentional? Do I need to make sure I'm adding some listener to the property before attempting to change its value?

raimohanska commented 10 years ago

FAQ :)

https://github.com/baconjs/bacon.js/wiki/FAQ#wiki-why-isnt-my-property-updated

dylemma commented 10 years ago

Oh dang I spent all that time looking around on the main readme and didn't even think to look for/in the FAQ. Thanks.