Skip to content

Commit

Permalink
Add deliver() and discardChanges() methods to Stateful.
Browse files Browse the repository at this point in the history
Previously they were just in Invalidating.

The new methods call deliver() and discardChanges() on every listener registered via
Stateful#observe().

Invalidating has a minor behavior change: deliver() and discardChanges() now affect every listener
registered via observe(), not just the two listeners registered by Invalidating itself.

Fixes #42.
  • Loading branch information
wkeese committed Mar 23, 2015
1 parent 7256144 commit 9438672
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 23 deletions.
19 changes: 0 additions & 19 deletions Invalidating.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,6 @@ define([
return this._hComputing;
},

/**
* Synchronously deliver change records for computed properties and then UI rendering
* so that `refreshingRendering()` is called if there are pending change records.
*/
deliver: function () {
this._hComputing && this._hComputing.deliver();
this._hRendering && this._hRendering.deliver();
return this._hComputing;
},

/**
* Discard change records.
*/
discardChanges: function () {
this._hComputing && this._hComputing.discardChanges();
this._hRendering && this._hRendering.discardChanges();
return this._hComputing;
},

/**
* Callback function to calculate computed properties upon property changes.
* @param {Object} newValues The hash table of new property values, keyed by property names.
Expand Down
36 changes: 34 additions & 2 deletions Stateful.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/** @module decor/Stateful */
define([
"dcl/advise",
"dcl/dcl",
"./features",
"./Observable"
], function (dcl, has, Observable) {
], function (advise, dcl, has, Observable) {
var apn = {};

/**
Expand Down Expand Up @@ -215,6 +216,15 @@ define([
}, this);
},

/**
* Get list of properties that Stateful#observe() should observe.
* @returns {string[]} list of properties
* @protected
*/
getPropsToObserve: function () {
return this.constructor._props;
},

/**
* Observes for change in properties.
* Callback is called at the end of micro-task of changes with a hash table of
Expand Down Expand Up @@ -246,9 +256,31 @@ define([
* stateful.baz = 10;
*/
observe: function (callback) {
var h = new Stateful.PropertyListObserver(this, this.constructor._props);
// create new listener
var h = new Stateful.PropertyListObserver(this, this.getPropsToObserve());
h.open(callback, this);

// make this.deliver() and this.discardComputing() call deliver() and discardComputing() on new listener
var a1 = advise.after(this, "deliver", h.deliver.bind(h)),
a2 = advise.after(this, "discardChanges", h.discardChanges.bind(h));
advise.before(h, "close", function () {
a1.unadvise();
a2.unadvise();
});

return h;
},

/**
* Synchronously deliver change records to all listeners registered via `observe()`.
*/
deliver: function () {
},

/**
* Discard change records for all listeners registered via `observe()`.
*/
discardChanges: function () {
}
});

Expand Down
32 changes: 30 additions & 2 deletions tests/unit/Stateful.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,35 @@ define([
hObserve.deliver();
assert.deepEqual(changes, [{foo: "Foo1"}]);
},
"Stateful#deliver(), Stateful#discardChanges()": function () {
var stateful = new (dcl(Stateful, {
foo: undefined,
bar: undefined
}))({
foo: "Foo0",
bar: "Bar0"
});
var log = "";
stateful.observe(function () {
log += "first";
});
stateful.observe(function () {
log += ", second";
});
stateful.foo = "Foo1";
stateful.bar = "Bar1";
stateful.deliver();
assert.strictEqual(log, "first, second", "deliver()");

log = "";
stateful.foo = "Foo2";
stateful.bar = "Bar2";
stateful.discardChanges();
stateful.deliver();
setTimeout(this.async().callback(function () {
assert.strictEqual(log, "", "discardChanges()");
}), 10);
},
"observe filter": function () {
// Check to make sure reported changes are consistent between platforms with and without Object.observe()
// native support
Expand Down Expand Up @@ -386,7 +415,6 @@ define([
stateful.foo = 22;
stateful.anotherFunc = function () { };
stateful.instanceProp = 33;
},

}
});
});

0 comments on commit 9438672

Please sign in to comment.