Skip to content

Commit 78ef8bf

Browse files
committed
Make immutable a decorator
1 parent 1de5e95 commit 78ef8bf

File tree

5 files changed

+39
-51
lines changed

5 files changed

+39
-51
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@
3939
"license": "MIT",
4040
"scripts": {
4141
"build": "npm run clean && npm run build-alt && npm run build-utils && npm run build-alt-browser && npm run build-alt-browser-with-addons",
42-
"build-alt": "babel src/alt --out-dir lib",
43-
"build-utils": "babel src/utils --out-dir utils",
42+
"build-alt": "babel src/alt --out-dir lib --stage 0",
43+
"build-utils": "babel src/utils --out-dir utils --stage 0",
4444
"build-alt-browser": "browserify src/alt -t [envify --NODE_ENV production ] -t babelify --outfile dist/alt.js --standalone Alt",
4545
"build-alt-browser-with-addons": "browserify src/alt/addons.js -t [envify --NODE_ENV production ] -t babelify -t browserify-shim --outfile dist/alt-with-addons.js --standalone Alt",
4646
"build-test": "babel src/alt --out-dir lib -r && babel src/utils --out-dir utils -r",
47-
"coverage": "npm run build-test && istanbul cover node_modules/mocha/bin/_mocha -- -u exports -R spec --compilers js:babel/register --require babel-core/external-helpers test",
47+
"coverage": "npm run build-test && istanbul cover node_modules/mocha/bin/_mocha -- -u exports -R spec --require ./test/babel --require babel-core/external-helpers test",
4848
"clean": "rimraf lib && rimraf utils",
4949
"lint": "eslint src mixins components",
5050
"posttest": "npm run build-alt",
5151
"prepublish": "npm run lint && npm run test && npm run build",
5252
"pretest": "npm run clean && npm run build-test",
5353
"test": "npm run tests-node",
5454
"test-browser": "browserify test/browser/index.js -t babelify --outfile test/browser/tests.js",
55-
"tests-node": "mocha -u exports -R spec --compilers js:babel/register --require babel-core/external-helpers test"
55+
"tests-node": "mocha -u exports -R spec --require ./test/babel --require babel-core/external-helpers test"
5656
},
5757
"browserify-shim": {
5858
"react": "global:React",

src/alt/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import getInternalMethods from './utils/getInternalMethods'
66
import uid from './utils/uid'
77
import { Dispatcher } from 'flux'
88
import { warn } from './utils/warnings'
9-
import * as StoreUtils from './utils/createStore'
9+
import * as StoreUtils from './utils/StoreUtils'
1010
import * as Sym from './symbols/symbols'
11-
import * as StateFunctions from './utils/stateFunctions'
11+
import * as StateFunctions from './utils/StateFunctions'
1212
import createStoreConfig from './utils/createStoreConfig'
1313

1414
const { createStoreFromObject, createStoreFromClass } = StoreUtils

src/utils/ImmutableUtil.js

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import Immutable from 'immutable'
22

3-
function makeImmutableObject(store, iden) {
4-
if (iden) {
5-
store.displayName = iden
6-
}
7-
3+
function makeImmutableObject(store) {
84
store.lifecycle = store.lifecycle || {}
95

106
store.lifecycle.serialize = function () {
@@ -18,7 +14,7 @@ function makeImmutableObject(store, iden) {
1814
return store
1915
}
2016

21-
function makeImmutableClass(Store, iden) {
17+
function makeImmutableClass(Store) {
2218
class ImmutableClass extends Store {
2319
constructor(...args) {
2420
super(...args)
@@ -33,34 +29,29 @@ function makeImmutableClass(Store, iden) {
3329
}
3430
}
3531

36-
ImmutableClass.displayName = iden || Store.displayName || ''
32+
ImmutableClass.displayName = Store.displayName || Store.name || ''
3733

3834
return ImmutableClass
3935
}
4036

41-
function enhance(alt) {
42-
alt.createImmutableStore = (store, iden, ...args) => {
43-
const StoreModel = typeof store === 'function'
44-
? makeImmutableClass(store, iden)
45-
: makeImmutableObject(store, iden)
37+
function immutable(store) {
38+
const StoreModel = typeof store === 'function'
39+
? makeImmutableClass(store)
40+
: makeImmutableObject(store)
4641

47-
StoreModel.config = {
48-
stateKey: 'state',
42+
StoreModel.config = {
43+
stateKey: 'state',
4944

50-
setState(currentState, nextState) {
51-
return nextState
52-
},
45+
setState(currentState, nextState) {
46+
return nextState
47+
},
5348

54-
getState(currentState) {
55-
return currentState
56-
}
49+
getState(currentState) {
50+
return currentState
5751
}
58-
59-
const immutableStore = alt.createStore(StoreModel, iden, ...args)
60-
return immutableStore
6152
}
6253

63-
return alt
54+
return StoreModel
6455
}
6556

66-
export default { enhance }
57+
export default immutable

test/babel/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require('babel/register')({
2+
stage: 0
3+
})

test/immutable-stores.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
import Alt from '../dist/alt-with-runtime'
22
import Immutable from 'immutable'
3-
import ImmutableUtil from '../utils/ImmutableUtil'
3+
import immutable from '../utils/ImmutableUtil'
44
import { assert } from 'chai'
55

66
export default {
77
'Immutable Stores': {
88
'no name immutable'() {
99
const alt = new Alt()
10-
ImmutableUtil.enhance(alt)
11-
const store = alt.createImmutableStore(function ImmutableStore() {
10+
const store = alt.createStore(immutable(function () {
1211
this.state = Immutable.Map({})
13-
})
12+
}))
1413

1514
assert(Object.keys(store.getState().toJS()).length === 0)
1615
},
1716

1817
'normal stores'() {
1918
const alt = new Alt()
20-
ImmutableUtil.enhance(alt)
2119

2220
const action = alt.generateActions('addY')
2321

24-
const store1 = alt.createImmutableStore({
22+
const store1 = alt.createStore(immutable({
2523
displayName: 'ImmutableStore',
2624
bindListeners: { addY: action.addY },
2725
state: Immutable.Map({ x: 1 }),
2826
addY() {
2927
this.setState(this.state.set('y', 2))
3028
}
31-
})
29+
}))
3230

3331
const store2 = alt.createStore({
3432
displayName: 'MutableStore',
@@ -52,33 +50,30 @@ export default {
5250

5351
'using list'() {
5452
const alt = new Alt()
55-
ImmutableUtil.enhance(alt)
56-
const store = alt.createImmutableStore({
53+
const store = alt.createStore(immutable({
5754
state: Immutable.List([1, 2, 3])
58-
}, 'ListImmutableStore')
55+
}), 'ListImmutableStore')
5956

6057
assert(store.getState().get(0) === 1)
6158
},
6259

6360
'passing args to constructor'() {
6461
const alt = new Alt()
65-
ImmutableUtil.enhance(alt)
6662

67-
const store = alt.createImmutableStore(function ImmutableStore(x) {
63+
const store = alt.createStore(immutable(function ImmutableStore(x) {
6864
assert(x === 'hello world')
6965
this.state = Immutable.Map({ x: x })
70-
}, 'MyImmutableStore', 'hello world')
66+
}), 'MyImmutableStore', 'hello world')
7167

7268
assert(store.getState().toJS().x === 'hello world')
7369
},
7470

7571
'immutable stores as an object'() {
7672
const alt = new Alt()
77-
ImmutableUtil.enhance(alt)
7873

7974
const actions = alt.generateActions('fire')
8075

81-
const store = alt.createImmutableStore({
76+
const store = alt.createStore(immutable({
8277
displayName: 'ImmutableStore',
8378

8479
state: Immutable.Map({
@@ -92,7 +87,7 @@ export default {
9287
handleFoo: function (x) {
9388
this.setState(this.state.set('foo', x))
9489
}
95-
})
90+
}))
9691

9792
assert.isUndefined(store.getState().toJS().foo, 'foo has not been defined')
9893
assert(store.getState().toJS().bar === 'hello', 'bar is part of state')
@@ -118,7 +113,6 @@ export default {
118113

119114
'immutable stores as a constructor'() {
120115
const alt = new Alt()
121-
ImmutableUtil.enhance(alt)
122116

123117
const actions = alt.generateActions('fork')
124118

@@ -138,7 +132,7 @@ export default {
138132

139133
ImmutableStore.displayName = 'ImmutableStore'
140134

141-
const store = alt.createImmutableStore(ImmutableStore)
135+
const store = alt.createStore(immutable(ImmutableStore))
142136

143137
assert.isUndefined(store.getState().toJS().foo, 'foo has not been defined')
144138
assert(store.getState().toJS().bar === 'hello', 'bar is part of state')
@@ -160,10 +154,10 @@ export default {
160154

161155
'immutable stores as a class'() {
162156
const alt = new Alt()
163-
ImmutableUtil.enhance(alt)
164157

165158
const actions = alt.generateActions('fork', 'rm')
166159

160+
@immutable
167161
class ImmutableStore {
168162
constructor() {
169163
this.bindListeners({
@@ -185,7 +179,7 @@ export default {
185179
}
186180
}
187181

188-
const store = alt.createImmutableStore(ImmutableStore, 'ImmutableStore')
182+
const store = alt.createStore(ImmutableStore, 'ImmutableStore')
189183

190184
assert.isUndefined(store.getState().toJS().foo, 'foo has not been defined')
191185

0 commit comments

Comments
 (0)