Skip to content

Instantly share code, notes, and snippets.

@janv
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save janv/fd6fdd72394139df48b6 to your computer and use it in GitHub Desktop.

Select an option

Save janv/fd6fdd72394139df48b6 to your computer and use it in GitHub Desktop.

Revisions

  1. janv revised this gist Apr 4, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions watcher.js
    Original file line number Diff line number Diff line change
    @@ -8,12 +8,12 @@ function Watcher(expression, handler, deep) {
    Watcher.prototype = {
    digest: function(){
    var _new = this.expression();
    var changed = this.compare(this._old, _new);
    var changed = !this.equals(this._old, _new);
    if (changed) this._handler(_new, _old);
    this._old = _new;
    },

    compare: function(a, b) {
    return this._deep ? _.isEqual(a, b) : a ===b;
    equals: function(a, b) {
    return this._deep ? _.isEqual(a, b) : a === b;
    }
    }
  2. janv created this gist Apr 4, 2015.
    19 changes: 19 additions & 0 deletions watcher.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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.compare(this._old, _new);
    if (changed) this._handler(_new, _old);
    this._old = _new;
    },

    compare: function(a, b) {
    return this._deep ? _.isEqual(a, b) : a ===b;
    }
    }