From 76d7e5edaa5d281359f1f727ccc76301dbe1880f Mon Sep 17 00:00:00 2001 From: Josh Perez Date: Sun, 12 Apr 2015 00:15:42 -0700 Subject: [PATCH] Adds beforeEach and afterEach as lifecycle --- dist/alt-browser-with-addons.js | 32 +++++++++++++++------ dist/alt-browser.js | 32 +++++++++++++++------ dist/alt-with-runtime.js | 32 +++++++++++++++------ dist/alt.js | 32 +++++++++++++++------ src/alt.js | 49 +++++++++++++++++++++++++-------- test/before-and-after-test.js | 45 ++++++++++++++++++++++++------ 6 files changed, 165 insertions(+), 57 deletions(-) diff --git a/dist/alt-browser-with-addons.js b/dist/alt-browser-with-addons.js index 9144d2df..6ff39f0b 100644 --- a/dist/alt-browser-with-addons.js +++ b/dist/alt-browser-with-addons.js @@ -1020,6 +1020,17 @@ var STATE_CONTAINER = Symbol("the state container"); var GlobalActionsNameRegistry = {}; +function warn(msg) { + /* istanbul ignore else */ + if (typeof console !== "undefined") { + console.warn(new ReferenceError(msg)); + } +} + +function deprecatedBeforeAfterEachWarning() { + warn("beforeEach/afterEach functions on the store are deprecated " + "use beforeEach/afterEach as a lifecycle method instead"); +} + function formatAsConstant(name) { return name.replace(/[a-z]([A-Z])/g, function (i) { return "" + i[0] + "_" + i[1].toLowerCase(); @@ -1073,7 +1084,10 @@ var AltStore = (function () { // Register dispatcher this.dispatchToken = dispatcher.register(function (payload) { - if (typeof model.beforeEach === "function") { + if (model[LIFECYCLE].beforeEach) { + model[LIFECYCLE].beforeEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]); + } else if (typeof model.beforeEach === "function") { + deprecatedBeforeAfterEachWarning(); model.beforeEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]); } if (model[LISTENERS][payload.action]) { @@ -1083,7 +1097,10 @@ var AltStore = (function () { _this8.emitChange(); } } - if (typeof model.afterEach === "function") { + if (model[LIFECYCLE].afterEach) { + model[LIFECYCLE].afterEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]); + } else if (typeof model.afterEach === "function") { + deprecatedBeforeAfterEachWarning(); model.afterEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]); } }); @@ -1389,13 +1406,10 @@ var Alt = (function () { var key = iden || StoreModel.name || StoreModel.displayName || ""; if (saveStore && (this.stores[key] || !key)) { - /* istanbul ignore else */ - if (typeof console !== "undefined") { - if (this.stores[key]) { - console.warn(new ReferenceError("A store named " + key + " already exists, double check your store " + "names or pass in your own custom identifier for each store")); - } else { - console.warn(new ReferenceError("Store name was not specified")); - } + if (this.stores[key]) { + warn("A store named " + key + " already exists, double check your store " + "names or pass in your own custom identifier for each store"); + } else { + warn("Store name was not specified"); } key = uid(this.stores, key); diff --git a/dist/alt-browser.js b/dist/alt-browser.js index fe126aa5..abd57f8e 100644 --- a/dist/alt-browser.js +++ b/dist/alt-browser.js @@ -764,6 +764,17 @@ var STATE_CONTAINER = Symbol("the state container"); var GlobalActionsNameRegistry = {}; +function warn(msg) { + /* istanbul ignore else */ + if (typeof console !== "undefined") { + console.warn(new ReferenceError(msg)); + } +} + +function deprecatedBeforeAfterEachWarning() { + warn("beforeEach/afterEach functions on the store are deprecated " + "use beforeEach/afterEach as a lifecycle method instead"); +} + function formatAsConstant(name) { return name.replace(/[a-z]([A-Z])/g, function (i) { return "" + i[0] + "_" + i[1].toLowerCase(); @@ -817,7 +828,10 @@ var AltStore = (function () { // Register dispatcher this.dispatchToken = dispatcher.register(function (payload) { - if (typeof model.beforeEach === "function") { + if (model[LIFECYCLE].beforeEach) { + model[LIFECYCLE].beforeEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]); + } else if (typeof model.beforeEach === "function") { + deprecatedBeforeAfterEachWarning(); model.beforeEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]); } if (model[LISTENERS][payload.action]) { @@ -827,7 +841,10 @@ var AltStore = (function () { _this8.emitChange(); } } - if (typeof model.afterEach === "function") { + if (model[LIFECYCLE].afterEach) { + model[LIFECYCLE].afterEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]); + } else if (typeof model.afterEach === "function") { + deprecatedBeforeAfterEachWarning(); model.afterEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]); } }); @@ -1133,13 +1150,10 @@ var Alt = (function () { var key = iden || StoreModel.name || StoreModel.displayName || ""; if (saveStore && (this.stores[key] || !key)) { - /* istanbul ignore else */ - if (typeof console !== "undefined") { - if (this.stores[key]) { - console.warn(new ReferenceError("A store named " + key + " already exists, double check your store " + "names or pass in your own custom identifier for each store")); - } else { - console.warn(new ReferenceError("Store name was not specified")); - } + if (this.stores[key]) { + warn("A store named " + key + " already exists, double check your store " + "names or pass in your own custom identifier for each store"); + } else { + warn("Store name was not specified"); } key = uid(this.stores, key); diff --git a/dist/alt-with-runtime.js b/dist/alt-with-runtime.js index 824a3ab1..a66a1323 100644 --- a/dist/alt-with-runtime.js +++ b/dist/alt-with-runtime.js @@ -21,6 +21,17 @@ var STATE_CONTAINER = Symbol("the state container"); var GlobalActionsNameRegistry = {}; +function warn(msg) { + /* istanbul ignore else */ + if (typeof console !== "undefined") { + console.warn(new ReferenceError(msg)); + } +} + +function deprecatedBeforeAfterEachWarning() { + warn("beforeEach/afterEach functions on the store are deprecated " + "use beforeEach/afterEach as a lifecycle method instead"); +} + function formatAsConstant(name) { return name.replace(/[a-z]([A-Z])/g, function (i) { return "" + i[0] + "_" + i[1].toLowerCase(); @@ -74,7 +85,10 @@ var AltStore = (function () { // Register dispatcher this.dispatchToken = dispatcher.register(function (payload) { - if (typeof model.beforeEach === "function") { + if (model[LIFECYCLE].beforeEach) { + model[LIFECYCLE].beforeEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]); + } else if (typeof model.beforeEach === "function") { + deprecatedBeforeAfterEachWarning(); model.beforeEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]); } if (model[LISTENERS][payload.action]) { @@ -84,7 +98,10 @@ var AltStore = (function () { _this8.emitChange(); } } - if (typeof model.afterEach === "function") { + if (model[LIFECYCLE].afterEach) { + model[LIFECYCLE].afterEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]); + } else if (typeof model.afterEach === "function") { + deprecatedBeforeAfterEachWarning(); model.afterEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]); } }); @@ -387,13 +404,10 @@ var Alt = (function () { var key = iden || StoreModel.name || StoreModel.displayName || ""; if (saveStore && (this.stores[key] || !key)) { - /* istanbul ignore else */ - if (typeof console !== "undefined") { - if (this.stores[key]) { - console.warn(new ReferenceError("A store named " + key + " already exists, double check your store " + "names or pass in your own custom identifier for each store")); - } else { - console.warn(new ReferenceError("Store name was not specified")); - } + if (this.stores[key]) { + warn("A store named " + key + " already exists, double check your store " + "names or pass in your own custom identifier for each store"); + } else { + warn("Store name was not specified"); } key = uid(this.stores, key); diff --git a/dist/alt.js b/dist/alt.js index a3802d53..0bc001fc 100644 --- a/dist/alt.js +++ b/dist/alt.js @@ -33,6 +33,17 @@ var STATE_CONTAINER = Symbol("the state container"); var GlobalActionsNameRegistry = {}; +function warn(msg) { + /* istanbul ignore else */ + if (typeof console !== "undefined") { + console.warn(new ReferenceError(msg)); + } +} + +function deprecatedBeforeAfterEachWarning() { + warn("beforeEach/afterEach functions on the store are deprecated " + "use beforeEach/afterEach as a lifecycle method instead"); +} + function formatAsConstant(name) { return name.replace(/[a-z]([A-Z])/g, function (i) { return "" + i[0] + "_" + i[1].toLowerCase(); @@ -86,7 +97,10 @@ var AltStore = (function () { // Register dispatcher this.dispatchToken = dispatcher.register(function (payload) { - if (typeof model.beforeEach === "function") { + if (model[LIFECYCLE].beforeEach) { + model[LIFECYCLE].beforeEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]); + } else if (typeof model.beforeEach === "function") { + deprecatedBeforeAfterEachWarning(); model.beforeEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]); } if (model[LISTENERS][payload.action]) { @@ -96,7 +110,10 @@ var AltStore = (function () { _this8.emitChange(); } } - if (typeof model.afterEach === "function") { + if (model[LIFECYCLE].afterEach) { + model[LIFECYCLE].afterEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]); + } else if (typeof model.afterEach === "function") { + deprecatedBeforeAfterEachWarning(); model.afterEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]); } }); @@ -402,13 +419,10 @@ var Alt = (function () { var key = iden || StoreModel.name || StoreModel.displayName || ""; if (saveStore && (this.stores[key] || !key)) { - /* istanbul ignore else */ - if (typeof console !== "undefined") { - if (this.stores[key]) { - console.warn(new ReferenceError("A store named " + key + " already exists, double check your store " + "names or pass in your own custom identifier for each store")); - } else { - console.warn(new ReferenceError("Store name was not specified")); - } + if (this.stores[key]) { + warn("A store named " + key + " already exists, double check your store " + "names or pass in your own custom identifier for each store"); + } else { + warn("Store name was not specified"); } key = uid(this.stores, key); diff --git a/src/alt.js b/src/alt.js index 23c5abbc..d6bf12cc 100644 --- a/src/alt.js +++ b/src/alt.js @@ -20,6 +20,20 @@ const STATE_CONTAINER = Symbol('the state container') const GlobalActionsNameRegistry = {} +function warn(msg) { + /* istanbul ignore else */ + if (typeof console !== 'undefined') { + console.warn(new ReferenceError(msg)) + } +} + +function deprecatedBeforeAfterEachWarning() { + warn( + 'beforeEach/afterEach functions on the store are deprecated ' + + 'use beforeEach/afterEach as a lifecycle method instead' + ) +} + function formatAsConstant(name) { return name.replace(/[a-z]([A-Z])/g, (i) => { return `${i[0]}_${i[1].toLowerCase()}` @@ -69,7 +83,14 @@ class AltStore { // Register dispatcher this.dispatchToken = dispatcher.register((payload) => { - if (typeof model.beforeEach === 'function') { + if (model[LIFECYCLE].beforeEach) { + model[LIFECYCLE].beforeEach( + payload.action.toString(), + payload.data, + this[STATE_CONTAINER] + ) + } else if (typeof model.beforeEach === 'function') { + deprecatedBeforeAfterEachWarning() model.beforeEach( payload.action.toString(), payload.data, @@ -83,7 +104,14 @@ class AltStore { this.emitChange() } } - if (typeof model.afterEach === 'function') { + if (model[LIFECYCLE].afterEach) { + model[LIFECYCLE].afterEach( + payload.action.toString(), + payload.data, + this[STATE_CONTAINER] + ) + } else if (typeof model.afterEach === 'function') { + deprecatedBeforeAfterEachWarning() model.afterEach( payload.action.toString(), payload.data, @@ -367,16 +395,13 @@ class Alt { let key = iden || StoreModel.name || StoreModel.displayName || '' if (saveStore && (this.stores[key] || !key)) { - /* istanbul ignore else */ - if (typeof console !== 'undefined') { - if (this.stores[key]) { - console.warn(new ReferenceError( - `A store named ${key} already exists, double check your store ` + - `names or pass in your own custom identifier for each store` - )) - } else { - console.warn(new ReferenceError('Store name was not specified')) - } + if (this.stores[key]) { + warn( + `A store named ${key} already exists, double check your store ` + + `names or pass in your own custom identifier for each store` + ) + } else { + warn('Store name was not specified') } key = uid(this.stores, key) diff --git a/test/before-and-after-test.js b/test/before-and-after-test.js index f4e77026..576ed5e0 100644 --- a/test/before-and-after-test.js +++ b/test/before-and-after-test.js @@ -17,12 +17,14 @@ const store = alt.createStore({ change: action.fire }, + lifecycle: { + beforeEach, + afterEach + }, + change(x) { this.setState({ a: x }) }, - - beforeEach, - afterEach }) export default { @@ -65,14 +67,16 @@ export default { bindListeners: { change: action.fire }, + lifecycle: { + beforeEach(a, b, state) { + beforeValue = state.a + }, + afterEach(a, b, state) { + afterValue = state.a + } + }, change(x) { this.setState({ a: x }) - }, - beforeEach(a, b, state) { - beforeValue = state.a - }, - afterEach(a, b, state) { - afterValue = state.a } }) @@ -80,6 +84,29 @@ export default { assert.ok(beforeValue === 1, 'before has current state') assert.ok(afterValue === 2, 'after has next state') + }, + + 'deprecated warning'() { + console.warn = sinon.spy() + + class Foo { + constructor() { + this.bindListeners({ + change: action.fire + }) + } + beforeEach() { } + afterEach() { } + change() { } + } + + assert(console.warn.callCount === 0) + + alt.createStore(Foo) + + action.fire() + + assert.ok(console.warn.calledTwice) } } }