Skip to content

Commit

Permalink
Make immutable a decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Apr 25, 2015
1 parent 1de5e95 commit 78ef8bf
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 51 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,20 @@
"license": "MIT",
"scripts": {
"build": "npm run clean && npm run build-alt && npm run build-utils && npm run build-alt-browser && npm run build-alt-browser-with-addons",
"build-alt": "babel src/alt --out-dir lib",
"build-utils": "babel src/utils --out-dir utils",
"build-alt": "babel src/alt --out-dir lib --stage 0",
"build-utils": "babel src/utils --out-dir utils --stage 0",
"build-alt-browser": "browserify src/alt -t [envify --NODE_ENV production ] -t babelify --outfile dist/alt.js --standalone Alt",
"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",
"build-test": "babel src/alt --out-dir lib -r && babel src/utils --out-dir utils -r",
"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",
"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",
"clean": "rimraf lib && rimraf utils",
"lint": "eslint src mixins components",
"posttest": "npm run build-alt",
"prepublish": "npm run lint && npm run test && npm run build",
"pretest": "npm run clean && npm run build-test",
"test": "npm run tests-node",
"test-browser": "browserify test/browser/index.js -t babelify --outfile test/browser/tests.js",
"tests-node": "mocha -u exports -R spec --compilers js:babel/register --require babel-core/external-helpers test"
"tests-node": "mocha -u exports -R spec --require ./test/babel --require babel-core/external-helpers test"
},
"browserify-shim": {
"react": "global:React",
Expand Down
4 changes: 2 additions & 2 deletions src/alt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import getInternalMethods from './utils/getInternalMethods'
import uid from './utils/uid'
import { Dispatcher } from 'flux'
import { warn } from './utils/warnings'
import * as StoreUtils from './utils/createStore'
import * as StoreUtils from './utils/StoreUtils'
import * as Sym from './symbols/symbols'
import * as StateFunctions from './utils/stateFunctions'
import * as StateFunctions from './utils/StateFunctions'
import createStoreConfig from './utils/createStoreConfig'

const { createStoreFromObject, createStoreFromClass } = StoreUtils
Expand Down
41 changes: 16 additions & 25 deletions src/utils/ImmutableUtil.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import Immutable from 'immutable'

function makeImmutableObject(store, iden) {
if (iden) {
store.displayName = iden
}

function makeImmutableObject(store) {
store.lifecycle = store.lifecycle || {}

store.lifecycle.serialize = function () {
Expand All @@ -18,7 +14,7 @@ function makeImmutableObject(store, iden) {
return store
}

function makeImmutableClass(Store, iden) {
function makeImmutableClass(Store) {
class ImmutableClass extends Store {
constructor(...args) {
super(...args)
Expand All @@ -33,34 +29,29 @@ function makeImmutableClass(Store, iden) {
}
}

ImmutableClass.displayName = iden || Store.displayName || ''
ImmutableClass.displayName = Store.displayName || Store.name || ''

return ImmutableClass
}

function enhance(alt) {
alt.createImmutableStore = (store, iden, ...args) => {
const StoreModel = typeof store === 'function'
? makeImmutableClass(store, iden)
: makeImmutableObject(store, iden)
function immutable(store) {
const StoreModel = typeof store === 'function'
? makeImmutableClass(store)
: makeImmutableObject(store)

StoreModel.config = {
stateKey: 'state',
StoreModel.config = {
stateKey: 'state',

setState(currentState, nextState) {
return nextState
},
setState(currentState, nextState) {
return nextState
},

getState(currentState) {
return currentState
}
getState(currentState) {
return currentState
}

const immutableStore = alt.createStore(StoreModel, iden, ...args)
return immutableStore
}

return alt
return StoreModel
}

export default { enhance }
export default immutable
3 changes: 3 additions & 0 deletions test/babel/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require('babel/register')({
stage: 0
})
34 changes: 14 additions & 20 deletions test/immutable-stores.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
import Alt from '../dist/alt-with-runtime'
import Immutable from 'immutable'
import ImmutableUtil from '../utils/ImmutableUtil'
import immutable from '../utils/ImmutableUtil'
import { assert } from 'chai'

export default {
'Immutable Stores': {
'no name immutable'() {
const alt = new Alt()
ImmutableUtil.enhance(alt)
const store = alt.createImmutableStore(function ImmutableStore() {
const store = alt.createStore(immutable(function () {
this.state = Immutable.Map({})
})
}))

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

'normal stores'() {
const alt = new Alt()
ImmutableUtil.enhance(alt)

const action = alt.generateActions('addY')

const store1 = alt.createImmutableStore({
const store1 = alt.createStore(immutable({
displayName: 'ImmutableStore',
bindListeners: { addY: action.addY },
state: Immutable.Map({ x: 1 }),
addY() {
this.setState(this.state.set('y', 2))
}
})
}))

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

'using list'() {
const alt = new Alt()
ImmutableUtil.enhance(alt)
const store = alt.createImmutableStore({
const store = alt.createStore(immutable({
state: Immutable.List([1, 2, 3])
}, 'ListImmutableStore')
}), 'ListImmutableStore')

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

'passing args to constructor'() {
const alt = new Alt()
ImmutableUtil.enhance(alt)

const store = alt.createImmutableStore(function ImmutableStore(x) {
const store = alt.createStore(immutable(function ImmutableStore(x) {
assert(x === 'hello world')
this.state = Immutable.Map({ x: x })
}, 'MyImmutableStore', 'hello world')
}), 'MyImmutableStore', 'hello world')

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

'immutable stores as an object'() {
const alt = new Alt()
ImmutableUtil.enhance(alt)

const actions = alt.generateActions('fire')

const store = alt.createImmutableStore({
const store = alt.createStore(immutable({
displayName: 'ImmutableStore',

state: Immutable.Map({
Expand All @@ -92,7 +87,7 @@ export default {
handleFoo: function (x) {
this.setState(this.state.set('foo', x))
}
})
}))

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

'immutable stores as a constructor'() {
const alt = new Alt()
ImmutableUtil.enhance(alt)

const actions = alt.generateActions('fork')

Expand All @@ -138,7 +132,7 @@ export default {

ImmutableStore.displayName = 'ImmutableStore'

const store = alt.createImmutableStore(ImmutableStore)
const store = alt.createStore(immutable(ImmutableStore))

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

'immutable stores as a class'() {
const alt = new Alt()
ImmutableUtil.enhance(alt)

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

@immutable
class ImmutableStore {
constructor() {
this.bindListeners({
Expand All @@ -185,7 +179,7 @@ export default {
}
}

const store = alt.createImmutableStore(ImmutableStore, 'ImmutableStore')
const store = alt.createStore(ImmutableStore, 'ImmutableStore')

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

Expand Down

0 comments on commit 78ef8bf

Please sign in to comment.