Skip to content

Commit

Permalink
Add before and after hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Mar 29, 2015
1 parent 2af3c13 commit 46daa25
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 0 deletions.
6 changes: 6 additions & 0 deletions dist/alt-browser-with-addons.js
Original file line number Diff line number Diff line change
Expand Up @@ -1072,12 +1072,18 @@ var AltStore = (function () {

// Register dispatcher
this.dispatchToken = dispatcher.register(function (payload) {
if (typeof model.beforeEach === "function") {
model.beforeEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]);
}
if (model[LISTENERS][payload.action]) {
var result = model[LISTENERS][payload.action](payload.data);
if (result !== false) {
_this8.emitChange();
}
}
if (typeof model.afterEach === "function") {
model.afterEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]);
}
});

if (this[LIFECYCLE].init) {
Expand Down
6 changes: 6 additions & 0 deletions dist/alt-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -816,12 +816,18 @@ var AltStore = (function () {

// Register dispatcher
this.dispatchToken = dispatcher.register(function (payload) {
if (typeof model.beforeEach === "function") {
model.beforeEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]);
}
if (model[LISTENERS][payload.action]) {
var result = model[LISTENERS][payload.action](payload.data);
if (result !== false) {
_this8.emitChange();
}
}
if (typeof model.afterEach === "function") {
model.afterEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]);
}
});

if (this[LIFECYCLE].init) {
Expand Down
6 changes: 6 additions & 0 deletions dist/alt-with-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,18 @@ var AltStore = (function () {

// Register dispatcher
this.dispatchToken = dispatcher.register(function (payload) {
if (typeof model.beforeEach === "function") {
model.beforeEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]);
}
if (model[LISTENERS][payload.action]) {
var result = model[LISTENERS][payload.action](payload.data);
if (result !== false) {
_this8.emitChange();
}
}
if (typeof model.afterEach === "function") {
model.afterEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]);
}
});

if (this[LIFECYCLE].init) {
Expand Down
6 changes: 6 additions & 0 deletions dist/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,18 @@ var AltStore = (function () {

// Register dispatcher
this.dispatchToken = dispatcher.register(function (payload) {
if (typeof model.beforeEach === "function") {
model.beforeEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]);
}
if (model[LISTENERS][payload.action]) {
var result = model[LISTENERS][payload.action](payload.data);
if (result !== false) {
_this8.emitChange();
}
}
if (typeof model.afterEach === "function") {
model.afterEach(payload.action.toString(), payload.data, _this8[STATE_CONTAINER]);
}
});

if (this[LIFECYCLE].init) {
Expand Down
12 changes: 12 additions & 0 deletions docs/createStore.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,15 @@ 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 src/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,26 @@ class AltStore {

// Register dispatcher
this.dispatchToken = dispatcher.register((payload) => {
if (typeof model.beforeEach === 'function') {
model.beforeEach(
payload.action.toString(),
payload.data,
this[STATE_CONTAINER]
)
}
if (model[LISTENERS][payload.action]) {
const result = model[LISTENERS][payload.action](payload.data)
if (result !== false) {
this.emitChange()
}
}
if (typeof model.afterEach === 'function') {
model.afterEach(
payload.action.toString(),
payload.data,
this[STATE_CONTAINER]
)
}
})

if (this[LIFECYCLE].init) {
Expand Down
85 changes: 85 additions & 0 deletions test/before-and-after-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import Alt from '../dist/alt-with-runtime'
import { assert } from 'chai'
import sinon from 'sinon'

const alt = new Alt()
const action = alt.generateActions('fire')

const beforeEach = sinon.spy()
const afterEach = sinon.spy()

const store = alt.createStore({
displayName: 'Store',

state: { a: 1 },

bindListeners: {
change: action.fire
},

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

beforeEach,
afterEach
})

export default {
'Before and After hooks': {
beforeEach() {
alt.recycle()
},

'before and after hook fire once'() {
action.fire(2)

assert.ok(beforeEach.calledOnce)
assert.ok(afterEach.calledOnce)
},

'before is called before after'() {
action.fire(2)

assert.ok(beforeEach.calledBefore(afterEach))
assert.ok(afterEach.calledAfter(beforeEach))
},

'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][1] === 2, 'before has payload')
assert.ok(afterEach.args[0][1] === 2, 'after has payload')
},

'before and after get state'() {
let beforeValue = null
let afterValue = null

const store = alt.createStore({
displayName: 'SpecialStore',
state: { a: 1 },
bindListeners: {
change: action.fire
},
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')
}
}
}

0 comments on commit 46daa25

Please sign in to comment.