Skip to content

Commit

Permalink
Let you specify your own Immutable structure
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Apr 19, 2015
1 parent 89f1bd2 commit abf6d49
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 48 deletions.
20 changes: 8 additions & 12 deletions src/utils/ImmutableUtil.js
@@ -1,8 +1,6 @@
import Immutable from 'immutable'

function makeImmutableObject(Collection, store, iden) {
store.state = Collection(store.state || {})

function makeImmutableObject(store, iden) {
if (iden) {
store.displayName = iden
}
Expand All @@ -20,12 +18,10 @@ function makeImmutableObject(Collection, store, iden) {
return store
}

function makeImmutableClass(Collection, alt, Store, iden) {
function makeImmutableClass(alt, Store, iden) {
class ImmutableClass extends Store {
constructor() {
super()

this.state = Collection(this.state)
constructor(...args) {
super(...args)

this.on('serialize', function () {
return this.getInstance().getState().toJS()
Expand All @@ -37,12 +33,12 @@ function makeImmutableClass(Collection, alt, Store, iden) {
}
}

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

return ImmutableClass
}

function enhance(alt, Collection = Immutable.Map) {
function enhance(alt) {
const stateKey = alt._stateKey

alt.setState = (currentState, nextState) => {
Expand All @@ -53,8 +49,8 @@ function enhance(alt, Collection = Immutable.Map) {

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

alt._stateKey = 'state'
const store = alt.createStore(StoreModel, iden, ...args)
Expand Down
44 changes: 34 additions & 10 deletions test/immutable-stores.js
Expand Up @@ -5,16 +5,38 @@ import { assert } from 'chai'

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

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

'using list'() {
const alt = new Alt()
ImmutableUtil.enhance(alt)
const store = alt.createImmutableStore({
state: Immutable.List([1, 2, 3])
}, '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) {
assert(x === 'hello world')
this.state = Immutable.Map({ x: x })
}, 'MyImmutableStore', 'hello world')

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

'immutable stores as an object'() {
const alt = new Alt()
ImmutableUtil.enhance(alt)
Expand All @@ -24,9 +46,9 @@ export default {
const store = alt.createImmutableStore({
displayName: 'ImmutableStore',

state: {
state: Immutable.Map({
bar: 'hello'
},
}),

bindListeners: {
handleFoo: actions.fire
Expand Down Expand Up @@ -70,16 +92,18 @@ export default {
handleFoo: actions.fork
})

this.state = {
this.state = Immutable.Map({
bar: 'hello'
}
})
}

ImmutableStore.prototype.handleFoo = function (x) {
this.setState(this.state.set('foo', x))
}

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

const store = alt.createImmutableStore(ImmutableStore)

assert.isUndefined(store.getState().toJS().foo, 'foo has not been defined')
assert(store.getState().toJS().bar === 'hello', 'bar is part of state')
Expand Down Expand Up @@ -112,9 +136,9 @@ export default {
remove: actions.rm
})

this.state = {
this.state = Immutable.Map({
bar: 'hello'
}
})
}

handleFoo(x) {
Expand Down
37 changes: 11 additions & 26 deletions utils/ImmutableUtil.js
@@ -1,22 +1,8 @@
"use strict";

var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };

var _get = function get(object, property, receiver) { var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc && desc.writable) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };

var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; };

var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };

var Immutable = _interopRequire(require("immutable"));

var Symbol = _interopRequire(require("es-symbol"));

var IMMUTABLE_STATE = Symbol();

function makeImmutableObject(Collection, store, iden) {
store.state = Collection(store.state || {});
var Immutable = babelHelpers.interopRequire(require("immutable"));

function makeImmutableObject(store, iden) {
if (iden) {
store.displayName = iden;
}
Expand All @@ -34,14 +20,16 @@ function makeImmutableObject(Collection, store, iden) {
return store;
}

function makeImmutableClass(Collection, alt, Store, iden) {
function makeImmutableClass(alt, Store, iden) {
var ImmutableClass = (function (_Store) {
function ImmutableClass() {
_classCallCheck(this, ImmutableClass);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}

_get(Object.getPrototypeOf(ImmutableClass.prototype), "constructor", this).call(this);
babelHelpers.classCallCheck(this, ImmutableClass);

this.state = Collection(this.state);
babelHelpers.get(Object.getPrototypeOf(ImmutableClass.prototype), "constructor", this).apply(this, args);

this.on("serialize", function () {
return this.getInstance().getState().toJS();
Expand All @@ -52,19 +40,16 @@ function makeImmutableClass(Collection, alt, Store, iden) {
});
}

_inherits(ImmutableClass, _Store);

babelHelpers.inherits(ImmutableClass, _Store);
return ImmutableClass;
})(Store);

ImmutableClass.displayName = iden || Store.name || Store.displayName || "";
ImmutableClass.displayName = iden || Store.displayName || "";

return ImmutableClass;
}

function enhance(alt) {
var Collection = arguments[1] === undefined ? Immutable.Map : arguments[1];

var stateKey = alt._stateKey;

alt.setState = function (currentState, nextState) {
Expand All @@ -80,7 +65,7 @@ function enhance(alt) {
args[_key - 2] = arguments[_key];
}

var StoreModel = typeof store === "function" ? makeImmutableClass(Collection, alt, store, iden) : makeImmutableObject(Collection, store, iden);
var StoreModel = typeof store === "function" ? makeImmutableClass(alt, store, iden) : makeImmutableObject(store, iden);

alt._stateKey = "state";
var store = alt.createStore.apply(alt, [StoreModel, iden].concat(args));
Expand Down

0 comments on commit abf6d49

Please sign in to comment.