Skip to content

Commit

Permalink
Pass full payload object to before/after
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Apr 22, 2015
1 parent 4a70f8e commit 0b4f3c6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 28 deletions.
12 changes: 6 additions & 6 deletions docs/lifecycleListeners.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand All @@ -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.

Expand Down
19 changes: 3 additions & 16 deletions src/alt/AltStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
}
Expand All @@ -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(
Expand Down
12 changes: 6 additions & 6 deletions test/before-and-after-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'() {
Expand All @@ -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
}
},
Expand Down

0 comments on commit 0b4f3c6

Please sign in to comment.