Skip to content

Commit

Permalink
Fix setState so it handles values
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Oct 3, 2015
1 parent b1a3f7a commit f6be9c3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/alt/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ export function createStoreConfig(globalConfig, StoreModel) {

return state
},
setState: fn.assign,
setState(currentState, nextState) {
if (Object.prototype.toString.call(nextState) === '[object Object]') {
return fn.assign(currentState, nextState)
} else {
return nextState
}
},
}, globalConfig, StoreModel.config)
}

Expand Down
29 changes: 29 additions & 0 deletions test/value-stores-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ const store2 = alt.createStore({
}
})

const store3 = alt.createStore({
state: 21,

displayName: 'ValueStore3',

bindListeners: {
fire: actions.fire
},

fire() {
this.setState(this.state + 1)
}
})

export default {
'value stores': {
beforeEach() {
Expand All @@ -47,6 +61,21 @@ export default {
actions.fire()
},

'stores can contain state as any value (non reduce)'(done) {
assert(store3.state === 21, 'store state is value')
assert(store3.getState() === 21, 'getState returns value too')

const unlisten = store3.listen((state) => {
assert(state === 22, 'incremented store state')
unlisten()
done()
})

assert(JSON.parse(alt.takeSnapshot()).ValueStore3 === 21, 'snapshot ok')

actions.fire()
},

'store with array works too'() {
assert.deepEqual(store2.state, [1, 2, 3])
actions.fire()
Expand Down

0 comments on commit f6be9c3

Please sign in to comment.