How to monitor for a specific state change using ReactiveDict?

var user = new ReactiveDict;
user.set(“name”, John);
user.set(“time”, “some time”);

Tracker.autorun({
console.log(user.get(“name”));
consolg.log(user.get(“time” + 1 hr);
});

How to monitor for a specific state transition change, say only when “name” goes from John to David in the autorun section? And of course meaning that from David to John, or any other changes to be ignored.

EDIT: Also how to modify the time attribute along with name such that if there is such a defined change as per above, then whatever is the time attribute will simply be incremented (or decremented by 1 hour)

Thanks

You can create a ReactiveDict subclass (get the code for set method from the package source code):

CustomReactiveDict = class CustomReactiveDict extends ReactiveDict {
  set: function (keyOrObject, value, yourCustomParam) {
    ... copy code from ReactiveDict source ...
  }
}

Depending on what you exactly want to do you can add a param to not fire changed method (which will execute all autoruns that depend on this value) in some cases, or do something else that matches your use case.

Thanks @M4v3R, I modified slightly the question for another time attribute, could you also show that as well thanks. But not sure if this is correct, as the setting is done somewhere else, and I just want to detect a specific change out of many possible scenarios, not setting the name attribute. But I do want to extract the time in this given change and either increment (or decrement) the time.

When you create your CustomReactiveDict subclass, you can then use it like this to change its other properties:

var user = new ReactiveDict;
var HOUR = 3600 * 1000;  // assuming time is a UNIX timestamp (in milliseconds)
// ...
Tracker.autorun({
  var name = user.get("name");
  var time = user.get("time");
  if (name == "Something") {
    Tracker.nonreactive(function() {
      user.set("time", time + HOUR);
    });
  }
});

Hi @M4v3R

Could you be make example for the specific name detection change? As I cannot see the link between the CustomReactiveDict to the normal ReactiveDict.

I.e console.log(‘name changed from John to David’);

Thanks