Skip to content

Instantly share code, notes, and snippets.

@janv
Last active August 29, 2015 14:18
Show Gist options
  • Save janv/fd6fdd72394139df48b6 to your computer and use it in GitHub Desktop.
Save janv/fd6fdd72394139df48b6 to your computer and use it in GitHub Desktop.
Watcher
function Watcher(expression, handler, deep) {
this._expression = expression;
this._handler = handler;
this._deep = deep;
this._old = undefined;
}
Watcher.prototype = {
digest: function(){
var _new = this.expression();
var changed = !this.equals(this._old, _new);
if (changed) this._handler(_new, _old);
this._old = _new;
},
equals: function(a, b) {
return this._deep ? _.isEqual(a, b) : a === b;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment