Search code examples
javascriptpropertiesobservablebacon.js

Bacon.js Property new value and old value


Working with a POC using Bacon.js and run into a little bit of an issue with Property values.

I am able to retrieve all new property values in the onValue callback however I would like to know what the old property value was before this new value has been set. So far I have not found any easy or elegant solution to achieve this in Bacon out of the box...am I missing something.

Even Object.observe() has a way to get to the old value of the property so surprised I cannot find equivalent behaviour in Bacon.

Would anyone have any suggestions how to handle this? Obviously I do not want to persist the latest property value anywhere in the client code strcily for the sake of being able to do the comparisons between old and new...


Solution

  • You could use slidingwindow to create a new observable with the 2 latest values:

    var myProperty = Bacon.sequentially(10, [1,2,3,4,5]) // replace with real property
    var slidingWindow = myProperty.startWith(null).slidingWindow(2,2)
    slidingWindow.onValues(function(oldValue, newValue) {
      // do something with the values
    })