Skip to content

Commit 1a6f528

Browse files
committed
Add stateTransforms step
1 parent 28a38d7 commit 1a6f528

File tree

5 files changed

+65
-8
lines changed

5 files changed

+65
-8
lines changed

docs/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ class MyStore {
9090
}
9191
```
9292

93+
#### stateTransforms
94+
95+
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.
96+
9397
#### setState
9498

9599
> setState(currentState: object, nextState: object): object

src/alt/index.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import * as Sym from './symbols/symbols'
1111
import * as StateFunctions from './utils/StateFunctions'
1212
import createStoreConfig from './utils/createStoreConfig'
1313

14-
const { createStoreFromObject, createStoreFromClass } = StoreUtils
14+
const {
15+
createStoreFromObject,
16+
createStoreFromClass,
17+
transformStore
18+
} = StoreUtils
1519
const {
1620
ACTION_HANDLER,
1721
ACTION_KEY,
@@ -36,6 +40,7 @@ class Alt {
3640
this.dispatcher = config.dispatcher || new Dispatcher()
3741
this.actions = {}
3842
this.stores = {}
43+
this.storeTransforms = config.storeTransforms || []
3944
this[LAST_SNAPSHOT] = this[INIT_SNAPSHOT] = '{}'
4045
}
4146

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

50-
return typeof StoreModel === 'object'
51-
? createStoreFromObject(this, StoreModel, key)
52-
: createStoreFromClass(this, StoreModel, key, ...args)
56+
return typeof Store === 'object'
57+
? createStoreFromObject(this, Store, key)
58+
: createStoreFromClass(this, Store, key, ...args)
5359
}
5460

5561
createStore(StoreModel, iden, ...args) {
5662
let key = iden || StoreModel.displayName || StoreModel.name || ''
5763
createStoreConfig(this.config, StoreModel)
64+
const Store = transformStore(this.storeTransforms, StoreModel)
5865

5966
if (this.stores[key] || !key) {
6067
if (this.stores[key]) {
@@ -69,9 +76,9 @@ class Alt {
6976
key = uid(this.stores, key)
7077
}
7178

72-
const storeInstance = typeof StoreModel === 'object'
73-
? createStoreFromObject(this, StoreModel, key)
74-
: createStoreFromClass(this, StoreModel, key, ...args)
79+
const storeInstance = typeof Store === 'object'
80+
? createStoreFromObject(this, Store, key)
81+
: createStoreFromClass(this, Store, key, ...args)
7582

7683
this.stores[key] = storeInstance
7784
saveInitialSnapshot(this, key)

src/alt/utils/StoreUtils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ function doSetState(store, storeInstance, state) {
3232
}
3333
}
3434

35+
export function transformStore(transforms, StoreModel) {
36+
return transforms.reduce((Store, transform) => transform(Store), StoreModel)
37+
}
38+
3539
export function createStoreFromObject(alt, StoreModel, key) {
3640
let storeInstance
3741

test/alt-config-object.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,10 @@ export default {
6363

6464
assert.deepEqual(snapshot, {MyStore: {wrapper: {number: 2, letter: 'a'}}})
6565
assert.deepEqual(alt.getStore('MyStore').getState(), {number: 2, letter: 'a'})
66-
}
66+
},
67+
68+
'custom transforms'() {
69+
const alt = new Alt({ stateTransforms: [] })
70+
assert.isArray(alt.stateTransforms)
71+
},
6772
}

test/store-transforms-test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { assert } from 'chai'
2+
import Alt from '../dist/alt-with-runtime'
3+
4+
const alt = new Alt()
5+
6+
alt.storeTransforms.push(function (Store) {
7+
Store.test = 'hello'
8+
return Store
9+
})
10+
11+
class Store {
12+
constructor() {
13+
this.x = 0
14+
}
15+
}
16+
17+
class Store2 {
18+
constructor() {
19+
this.y = 0
20+
}
21+
}
22+
23+
export default {
24+
'store transforms': {
25+
'when creating stores alt goes through its series of transforms'() {
26+
const store = alt.createStore(Store)
27+
assert(alt.storeTransforms.length === 1)
28+
assert.isDefined(store.test)
29+
assert(store.test === 'hello', 'store that adds hello to instance transform')
30+
},
31+
32+
'unsaved stores get the same treatment'() {
33+
const store2 = alt.createUnsavedStore(Store)
34+
assert.isDefined(store2.test)
35+
},
36+
},
37+
}

0 commit comments

Comments
 (0)