From 94386725e58cad52f461a01c0fbca52c30bbc54f Mon Sep 17 00:00:00 2001 From: Bill Keese Date: Mon, 23 Mar 2015 11:30:06 +0900 Subject: [PATCH] Add deliver() and discardChanges() methods to Stateful. 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. --- Invalidating.js | 19 ------------------- Stateful.js | 36 ++++++++++++++++++++++++++++++++++-- tests/unit/Stateful.js | 32 ++++++++++++++++++++++++++++++-- 3 files changed, 64 insertions(+), 23 deletions(-) diff --git a/Invalidating.js b/Invalidating.js index 0a55b38..c96b3d8 100644 --- a/Invalidating.js +++ b/Invalidating.js @@ -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. diff --git a/Stateful.js b/Stateful.js index 2978041..f002252 100644 --- a/Stateful.js +++ b/Stateful.js @@ -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 = {}; /** @@ -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 @@ -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 () { } }); diff --git a/tests/unit/Stateful.js b/tests/unit/Stateful.js index 2c0f7bb..6d35618 100644 --- a/tests/unit/Stateful.js +++ b/tests/unit/Stateful.js @@ -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 @@ -386,7 +415,6 @@ define([ stateful.foo = 22; stateful.anotherFunc = function () { }; stateful.instanceProp = 33; - }, - + } }); });