From 78ef8bf3f903fc89f4039a6eff457a7e9763597a Mon Sep 17 00:00:00 2001 From: Josh Perez Date: Fri, 24 Apr 2015 01:44:28 -0700 Subject: [PATCH] Make immutable a decorator --- package.json | 8 ++++---- src/alt/index.js | 4 ++-- src/utils/ImmutableUtil.js | 41 +++++++++++++++----------------------- test/babel/index.js | 3 +++ test/immutable-stores.js | 34 +++++++++++++------------------ 5 files changed, 39 insertions(+), 51 deletions(-) create mode 100644 test/babel/index.js diff --git a/package.json b/package.json index 3c2eb7f0..321675b8 100644 --- a/package.json +++ b/package.json @@ -39,12 +39,12 @@ "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", @@ -52,7 +52,7 @@ "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", diff --git a/src/alt/index.js b/src/alt/index.js index c1bd7152..45a899f9 100644 --- a/src/alt/index.js +++ b/src/alt/index.js @@ -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 diff --git a/src/utils/ImmutableUtil.js b/src/utils/ImmutableUtil.js index ee096c99..5da4ae22 100644 --- a/src/utils/ImmutableUtil.js +++ b/src/utils/ImmutableUtil.js @@ -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 () { @@ -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) @@ -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 diff --git a/test/babel/index.js b/test/babel/index.js new file mode 100644 index 00000000..f41c7a71 --- /dev/null +++ b/test/babel/index.js @@ -0,0 +1,3 @@ +require('babel/register')({ + stage: 0 +}) diff --git a/test/immutable-stores.js b/test/immutable-stores.js index c79a4101..b159d009 100644 --- a/test/immutable-stores.js +++ b/test/immutable-stores.js @@ -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', @@ -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({ @@ -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') @@ -118,7 +113,6 @@ export default { 'immutable stores as a constructor'() { const alt = new Alt() - ImmutableUtil.enhance(alt) const actions = alt.generateActions('fork') @@ -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') @@ -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({ @@ -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')