Skip to content

Commit

Permalink
Allow stores as a value
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Aug 6, 2015
1 parent e55a84b commit 5e18e9c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/alt/store/AltStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class AltStore {
this.lifecycle = (event, x) => {
if (lifecycleEvents[event]) lifecycleEvents[event].push(x)
}
this.state = state || model
this.state = state

this.preventDefault = false
this.displayName = model.displayName
Expand Down Expand Up @@ -61,7 +61,7 @@ class AltStore {

if (model.reduce) {
handleDispatch(() => {
model.setState(model.reduce(this.state, payload))
this.state = model.reduce(this.state, payload)
}, payload)

if (!this.preventDefault) this.emitChange()
Expand Down
17 changes: 14 additions & 3 deletions src/alt/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ function createPrototype(proto, alt, key, extras) {
export function createStoreConfig(globalConfig, StoreModel) {
StoreModel.config = fn.assign({
getState(state) {
return fn.assign({}, state)
if (Array.isArray(state)) {
return state.slice()
} else if (Object.prototype.toString.call(state) === '[object Object]') {
return fn.assign({}, state)
} else {
return state
}
},
setState: fn.assign
}, globalConfig, StoreModel.config)
Expand Down Expand Up @@ -93,7 +99,12 @@ export function createStoreFromObject(alt, StoreModel, key) {

// create the instance and fn.assign the public methods to the instance
storeInstance = fn.assign(
new AltStore(alt, StoreProto, StoreProto.state || {}, StoreModel),
new AltStore(
alt,
StoreProto,
StoreProto.state !== undefined ? StoreProto.state : {},
StoreModel
),
StoreProto.publicMethods,
{ displayName: key }
)
Expand Down Expand Up @@ -132,7 +143,7 @@ export function createStoreFromClass(alt, StoreModel, key, ...argsForClass) {
new AltStore(
alt,
store,
typeof store.state === 'object' ? store.state : null,
store.state !== undefined ? store.state : store,
StoreModel
),
utils.getInternalMethods(StoreModel),
Expand Down
39 changes: 39 additions & 0 deletions test/value-stores-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { assert } from 'chai'
import Alt from '../'
import sinon from 'sinon'

const alt = new Alt()

const actions = alt.generateActions('fire')

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

displayName: 'ValueStore',

reduce(state, payload) {
return state + 1
}
})

export default {
'value stores': {
beforeEach() {
alt.recycle()
},

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

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

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

actions.fire()
},
}
}

0 comments on commit 5e18e9c

Please sign in to comment.