Skip to content

Commit

Permalink
Revert "Revert "Before after lifecycle""
Browse files Browse the repository at this point in the history
This reverts commit dd569a0.

Conflicts:
	dist/alt-browser-with-addons.js
	dist/alt-browser.js
	dist/alt-with-runtime.js
	dist/alt.js
	src/alt.js
  • Loading branch information
goatslacker committed Apr 15, 2015
1 parent 08d56bb commit 81a4e24
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 69 deletions.
32 changes: 23 additions & 9 deletions dist/alt-browser-with-addons.js
Original file line number Diff line number Diff line change
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 @@ -1092,7 +1103,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]);
}

Expand All @@ -1116,7 +1130,10 @@ var AltStore = (function () {
_this8[STATE_CHANGED] = false;
}

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 @@ -1418,13 +1435,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
Original file line number Diff line number Diff line change
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 @@ -836,7 +847,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]);
}

Expand All @@ -860,7 +874,10 @@ var AltStore = (function () {
_this8[STATE_CHANGED] = false;
}

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 @@ -1162,13 +1179,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
Original file line number Diff line number Diff line change
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 @@ -93,7 +104,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]);
}

Expand All @@ -117,7 +131,10 @@ var AltStore = (function () {
_this8[STATE_CHANGED] = false;
}

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 @@ -416,13 +433,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
Original file line number Diff line number Diff line change
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 @@ -105,7 +116,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]);
}

Expand All @@ -129,7 +143,10 @@ var AltStore = (function () {
_this8[STATE_CHANGED] = false;
}

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 @@ -431,13 +448,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
12 changes: 0 additions & 12 deletions docs/createStore.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,3 @@ class MyStore {
## StoreModel#_storeName

This is a reference to the store's internal name. This is either the identifier you provided to `createStore` or StoreModel's class name.

## StoreModel#beforeEach

> (action: string, data: mixed, state: object): undefined
This method gets called, if defined, before the payload hits the action. You can use this method to `waitFor` other stores, save previous state, or perform any bookeeping. The state passed in to `beforeEach` is the current state pre-action.

## StoreModel#afterEach

> (action: string, data: mixed, state: object): undefined
This method gets called, if defined, after the payload hits the action and the store emits a change. You can use this method for bookeeping and as a companion to `beforeEach`. The state passed in to `afterEach` is the current state post-action.
14 changes: 14 additions & 0 deletions docs/lifecycleListeners.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,17 @@ class Store {
}
}
```

## beforeEach

> (action: string, data: mixed, state: object): undefined
This method gets called, if defined, before the payload hits the action. You can use this method to `waitFor` other stores, save previous state, or perform any bookeeping. The state passed in to `beforeEach` is the current state pre-action.

`action` corresponds to the action name that is being called, `data` is the payload, `state` is the current store's state.

## afterEach

> (action: string, data: mixed, state: object): undefined
This method gets called, if defined, after the payload hits the action and the store emits a change. You can use this method for bookeeping and as a companion to `beforeEach`. The state passed in to `afterEach` is the current state post-action.
49 changes: 37 additions & 12 deletions src/alt.js
Original file line number Diff line number Diff line change
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 @@ -91,7 +105,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 Down Expand Up @@ -124,7 +145,14 @@ class AltStore {
this[STATE_CHANGED] = false
}

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 @@ -406,16 +434,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
Loading

0 comments on commit 81a4e24

Please sign in to comment.