Skip to content

Commit

Permalink
Adds beforeEach and afterEach as lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Apr 12, 2015
1 parent d9b69f9 commit 76d7e5e
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 57 deletions.
32 changes: 23 additions & 9 deletions dist/alt-browser-with-addons.js
Expand Up @@ -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();
Expand Down Expand Up @@ -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]) {
Expand All @@ -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]);
}
});
Expand Down Expand Up @@ -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);
Expand Down
32 changes: 23 additions & 9 deletions dist/alt-browser.js
Expand Up @@ -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();
Expand Down Expand Up @@ -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]) {
Expand All @@ -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]);
}
});
Expand Down Expand Up @@ -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);
Expand Down
32 changes: 23 additions & 9 deletions dist/alt-with-runtime.js
Expand Up @@ -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();
Expand Down Expand Up @@ -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]) {
Expand All @@ -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]);
}
});
Expand Down Expand Up @@ -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);
Expand Down
32 changes: 23 additions & 9 deletions dist/alt.js
Expand Up @@ -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();
Expand Down Expand Up @@ -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]) {
Expand All @@ -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]);
}
});
Expand Down Expand Up @@ -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);
Expand Down
49 changes: 37 additions & 12 deletions src/alt.js
Expand Up @@ -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()}`
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
45 changes: 36 additions & 9 deletions test/before-and-after-test.js
Expand Up @@ -17,12 +17,14 @@ const store = alt.createStore({
change: action.fire
},

lifecycle: {
beforeEach,
afterEach
},

change(x) {
this.setState({ a: x })
},

beforeEach,
afterEach
})

export default {
Expand Down Expand Up @@ -65,21 +67,46 @@ 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
}
})

action.fire(2)

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)
}
}
}

1 comment on commit 76d7e5e

@jdlehman
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I literally just did something similar in a branch and only found out that you had already did it because the git push failed since we named our branches the same thing. Great minds... 😄

Please sign in to comment.