From 0b4f3c68326cd18294bf0f3b51be46547e739720 Mon Sep 17 00:00:00 2001 From: Josh Perez Date: Wed, 22 Apr 2015 00:14:00 -0700 Subject: [PATCH] Pass full payload object to before/after --- docs/lifecycleListeners.md | 12 ++++++------ src/alt/AltStore.js | 19 +++---------------- test/before-and-after-test.js | 12 ++++++------ 3 files changed, 15 insertions(+), 28 deletions(-) diff --git a/docs/lifecycleListeners.md b/docs/lifecycleListeners.md index 4e988d7c..bf4b62c6 100644 --- a/docs/lifecycleListeners.md +++ b/docs/lifecycleListeners.md @@ -103,9 +103,9 @@ class Store { ```js class Store { constructor() { - this.on('error', (err, actionName, payloadData, currentState) => { - if (actionName === MyActions.fire) { - logError(err, payloadData); + this.on('error', (err, payload, currentState) => { + if (payload.action === MyActions.fire) { + logError(err, payload.data); } }); @@ -122,15 +122,15 @@ class Store { ## beforeEach -> (action: string, data: mixed, state: object): undefined +> (payload: object, 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. +`payload` is an object which contains the keys `action` for the action name and `data` for the data that you're dispatching; `state` is the current store's state. ## afterEach -> (action: string, data: mixed, state: object): undefined +> (payload: object, 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. diff --git a/src/alt/AltStore.js b/src/alt/AltStore.js index 71feca73..98c738e0 100644 --- a/src/alt/AltStore.js +++ b/src/alt/AltStore.js @@ -36,11 +36,7 @@ export default class AltStore { // Register dispatcher this.dispatchToken = alt.dispatcher.register((payload) => { if (model[LIFECYCLE].beforeEach) { - model[LIFECYCLE].beforeEach( - payload.action.toString(), - payload.data, - this[STATE_CONTAINER] - ) + model[LIFECYCLE].beforeEach(payload, this[STATE_CONTAINER]) } else if (typeof model.beforeEach === 'function') { deprecatedBeforeAfterEachWarning() model.beforeEach( @@ -57,12 +53,7 @@ export default class AltStore { result = model[LISTENERS][payload.action](payload.data) } catch (e) { if (this[LIFECYCLE].error) { - this[LIFECYCLE].error( - e, - payload.action.toString(), - payload.data, - this[STATE_CONTAINER] - ) + this[LIFECYCLE].error(e, payload, this[STATE_CONTAINER]) } else { throw e } @@ -76,11 +67,7 @@ export default class AltStore { } if (model[LIFECYCLE].afterEach) { - model[LIFECYCLE].afterEach( - payload.action.toString(), - payload.data, - this[STATE_CONTAINER] - ) + model[LIFECYCLE].afterEach(payload, this[STATE_CONTAINER]) } else if (typeof model.afterEach === 'function') { deprecatedBeforeAfterEachWarning() model.afterEach( diff --git a/test/before-and-after-test.js b/test/before-and-after-test.js index 576ed5e0..46be86f2 100644 --- a/test/before-and-after-test.js +++ b/test/before-and-after-test.js @@ -50,11 +50,11 @@ export default { 'args passed in'() { action.fire(2) - assert.ok(beforeEach.args[0].length === 3, '3 args are passed') - assert.ok(afterEach.args[0].length === 3, '3 args are passed') + assert.ok(beforeEach.args[0].length === 2, '2 args are passed') + assert.ok(afterEach.args[0].length === 2, '2 args are passed') - assert.ok(beforeEach.args[0][1] === 2, 'before has payload') - assert.ok(afterEach.args[0][1] === 2, 'after has payload') + assert.ok(beforeEach.args[0][0].data === 2, 'before has payload') + assert.ok(afterEach.args[0][0].data === 2, 'after has payload') }, 'before and after get state'() { @@ -68,10 +68,10 @@ export default { change: action.fire }, lifecycle: { - beforeEach(a, b, state) { + beforeEach(x, state) { beforeValue = state.a }, - afterEach(a, b, state) { + afterEach(x, state) { afterValue = state.a } },