Skip to content

Commit

Permalink
Add stateTransforms step
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Apr 25, 2015
1 parent 28a38d7 commit 1a6f528
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 8 deletions.
4 changes: 4 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ class MyStore {
}
```

#### stateTransforms

This is an array of functions you can provide which will be executed every time `createStore` or `createUnsavedStore` is ran. It will iterate through the array applying each function to your store. This can be useful if you wish to perform any pre-processing or transformations to your store before it's created.

#### setState

> setState(currentState: object, nextState: object): object
Expand Down
21 changes: 14 additions & 7 deletions src/alt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import * as Sym from './symbols/symbols'
import * as StateFunctions from './utils/StateFunctions'
import createStoreConfig from './utils/createStoreConfig'

const { createStoreFromObject, createStoreFromClass } = StoreUtils
const {
createStoreFromObject,
createStoreFromClass,
transformStore
} = StoreUtils
const {
ACTION_HANDLER,
ACTION_KEY,
Expand All @@ -36,6 +40,7 @@ class Alt {
this.dispatcher = config.dispatcher || new Dispatcher()
this.actions = {}
this.stores = {}
this.storeTransforms = config.storeTransforms || []
this[LAST_SNAPSHOT] = this[INIT_SNAPSHOT] = '{}'
}

Expand All @@ -46,15 +51,17 @@ class Alt {
createUnsavedStore(StoreModel, ...args) {
const key = StoreModel.displayName || ''
createStoreConfig(this.config, StoreModel)
const Store = transformStore(this.storeTransforms, StoreModel)

return typeof StoreModel === 'object'
? createStoreFromObject(this, StoreModel, key)
: createStoreFromClass(this, StoreModel, key, ...args)
return typeof Store === 'object'
? createStoreFromObject(this, Store, key)
: createStoreFromClass(this, Store, key, ...args)
}

createStore(StoreModel, iden, ...args) {
let key = iden || StoreModel.displayName || StoreModel.name || ''
createStoreConfig(this.config, StoreModel)
const Store = transformStore(this.storeTransforms, StoreModel)

if (this.stores[key] || !key) {
if (this.stores[key]) {
Expand All @@ -69,9 +76,9 @@ class Alt {
key = uid(this.stores, key)
}

const storeInstance = typeof StoreModel === 'object'
? createStoreFromObject(this, StoreModel, key)
: createStoreFromClass(this, StoreModel, key, ...args)
const storeInstance = typeof Store === 'object'
? createStoreFromObject(this, Store, key)
: createStoreFromClass(this, Store, key, ...args)

this.stores[key] = storeInstance
saveInitialSnapshot(this, key)
Expand Down
4 changes: 4 additions & 0 deletions src/alt/utils/StoreUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ function doSetState(store, storeInstance, state) {
}
}

export function transformStore(transforms, StoreModel) {
return transforms.reduce((Store, transform) => transform(Store), StoreModel)
}

export function createStoreFromObject(alt, StoreModel, key) {
let storeInstance

Expand Down
7 changes: 6 additions & 1 deletion test/alt-config-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,10 @@ export default {

assert.deepEqual(snapshot, {MyStore: {wrapper: {number: 2, letter: 'a'}}})
assert.deepEqual(alt.getStore('MyStore').getState(), {number: 2, letter: 'a'})
}
},

'custom transforms'() {
const alt = new Alt({ stateTransforms: [] })
assert.isArray(alt.stateTransforms)
},
}
37 changes: 37 additions & 0 deletions test/store-transforms-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { assert } from 'chai'
import Alt from '../dist/alt-with-runtime'

const alt = new Alt()

alt.storeTransforms.push(function (Store) {
Store.test = 'hello'
return Store
})

class Store {
constructor() {
this.x = 0
}
}

class Store2 {
constructor() {
this.y = 0
}
}

export default {
'store transforms': {
'when creating stores alt goes through its series of transforms'() {
const store = alt.createStore(Store)
assert(alt.storeTransforms.length === 1)
assert.isDefined(store.test)
assert(store.test === 'hello', 'store that adds hello to instance transform')
},

'unsaved stores get the same treatment'() {
const store2 = alt.createUnsavedStore(Store)
assert.isDefined(store2.test)
},
},
}

0 comments on commit 1a6f528

Please sign in to comment.