Skip to content

Commit

Permalink
Allow snapshots using the store reference
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Apr 26, 2015
1 parent 0563352 commit a25f2c8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/alt/utils/StateFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ export function setAppState(instance, data, onStore) {

export function snapshot(instance, ...storeNames) {
const stores = storeNames.length ? storeNames : Object.keys(instance.stores)
return stores.reduce((obj, key) => {
const store = instance.stores[key]
return stores.reduce((obj, storeHandle) => {
const storeName = storeHandle.displayName || storeHandle
const store = instance.stores[storeName]
if (store[LIFECYCLE].snapshot) {
store[LIFECYCLE].snapshot()
}
const customSnapshot = store[LIFECYCLE].serialize &&
store[LIFECYCLE].serialize()
obj[key] = customSnapshot ? customSnapshot : store.getState()
obj[storeName] = customSnapshot ? customSnapshot : store.getState()
return obj
}, {})
}
Expand Down
8 changes: 8 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,10 @@ const tests = {
const snapshot = alt.takeSnapshot('MyStore', 'AltSecondStore')
assert.deepEqual(Object.keys(JSON.parse(snapshot)), ['MyStore', 'AltSecondStore'], 'the snapshot includes specified stores')
assert.isFalse(Object.keys(JSON.parse(snapshot)).includes('LifeCycleStore'), 'the snapshot does not include unspecified stores')

const snapshot2 = alt.takeSnapshot(myStore, secondStore)
assert.deepEqual(Object.keys(JSON.parse(snapshot2)), ['MyStore', 'AltSecondStore'], 'the snapshot includes specified stores')
assert.isFalse(Object.keys(JSON.parse(snapshot2)).includes('LifeCycleStore'), 'the snapshot does not include unspecified stores')
},

'serializing/deserializing snapshot/bootstrap data'(){
Expand Down Expand Up @@ -793,6 +797,10 @@ const tests = {
alt.recycle('MyStore')
assert(myStore.getState().name === 'first', 'I can recycle specific stores')
assert(secondStore.getState().name === 'butterfly', 'and other stores will not be recycled')

myActions.updateName('butterfly')
alt.recycle(myStore)
assert(myStore.getState().name === 'first', 'I can recycle specific stores')
},

'recycling invalid stores'() {
Expand Down

0 comments on commit a25f2c8

Please sign in to comment.