Skip to content

Commit

Permalink
Bake immutable-js support into alt
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Mar 28, 2015
1 parent 3563f0a commit 34be7bb
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 72 deletions.
27 changes: 25 additions & 2 deletions dist/alt-browser-with-addons.js
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,7 @@ var ACTION_KEY = Symbol("holds the actions uid symbol for listening");
var ACTION_UID = Symbol("the actions uid name");
var ALL_LISTENERS = Symbol("name of listeners");
var EE = Symbol("event emitter instance");
var IMMUTABLE = Symbol["for"]("AltImmutableStore");
var INIT_SNAPSHOT = Symbol("init snapshot storage");
var LAST_SNAPSHOT = Symbol("last snapshot storage");
var LIFECYCLE = Symbol("store lifecycle listeners");
Expand Down Expand Up @@ -1108,8 +1109,12 @@ var AltStore = (function () {
},
getState: {
value: function getState() {
// Copy over state so it's RO.
var state = this[STATE_CONTAINER];

if (this.StoreModel[IMMUTABLE] === true) {
return state;
}

return Object.keys(state).reduce(function (obj, key) {
obj[key] = state[key];
return obj;
Expand Down Expand Up @@ -1327,6 +1332,14 @@ var createStoreFromObject = function (alt, StoreModel, key, saveStore) {
}
}, StoreMixinListeners, StoreMixinEssentials, StoreModel);

if (StoreModel[IMMUTABLE] === true) {
StoreProto.setState = function (key, value) {
storeInstance[STATE_CONTAINER] = storeInstance[STATE_CONTAINER].set(key, value);
this.emitChange();
return false;
};
}

// bind the store listeners
/* istanbul ignore else */
if (StoreProto.bindListeners) {
Expand Down Expand Up @@ -1425,14 +1438,24 @@ var Alt = (function () {
}
});

if (StoreModel[IMMUTABLE] === true) {
Store.prototype.setState = function (key, value) {
storeInstance[STATE_CONTAINER] = storeInstance[STATE_CONTAINER].set(key, value);
this.emitChange();
return false;
};
}

Store.prototype[ALL_LISTENERS] = [];
Store.prototype[LIFECYCLE] = {};
Store.prototype[LISTENERS] = {};
Store.prototype[PUBLIC_METHODS] = {};

var store = new Store(this);

storeInstance = assign(new AltStore(this.dispatcher, store, null, StoreModel), getInternalMethods(StoreModel, builtIns));
var thestate = StoreModel[IMMUTABLE] === true ? store.state : null;

storeInstance = assign(new AltStore(this.dispatcher, store, thestate, StoreModel), getInternalMethods(StoreModel, builtIns));

if (saveStore) {
this.stores[key] = storeInstance;
Expand Down
27 changes: 25 additions & 2 deletions dist/alt-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ var ACTION_KEY = Symbol("holds the actions uid symbol for listening");
var ACTION_UID = Symbol("the actions uid name");
var ALL_LISTENERS = Symbol("name of listeners");
var EE = Symbol("event emitter instance");
var IMMUTABLE = Symbol["for"]("AltImmutableStore");
var INIT_SNAPSHOT = Symbol("init snapshot storage");
var LAST_SNAPSHOT = Symbol("last snapshot storage");
var LIFECYCLE = Symbol("store lifecycle listeners");
Expand Down Expand Up @@ -852,8 +853,12 @@ var AltStore = (function () {
},
getState: {
value: function getState() {
// Copy over state so it's RO.
var state = this[STATE_CONTAINER];

if (this.StoreModel[IMMUTABLE] === true) {
return state;
}

return Object.keys(state).reduce(function (obj, key) {
obj[key] = state[key];
return obj;
Expand Down Expand Up @@ -1071,6 +1076,14 @@ var createStoreFromObject = function (alt, StoreModel, key, saveStore) {
}
}, StoreMixinListeners, StoreMixinEssentials, StoreModel);

if (StoreModel[IMMUTABLE] === true) {
StoreProto.setState = function (key, value) {
storeInstance[STATE_CONTAINER] = storeInstance[STATE_CONTAINER].set(key, value);
this.emitChange();
return false;
};
}

// bind the store listeners
/* istanbul ignore else */
if (StoreProto.bindListeners) {
Expand Down Expand Up @@ -1169,14 +1182,24 @@ var Alt = (function () {
}
});

if (StoreModel[IMMUTABLE] === true) {
Store.prototype.setState = function (key, value) {
storeInstance[STATE_CONTAINER] = storeInstance[STATE_CONTAINER].set(key, value);
this.emitChange();
return false;
};
}

Store.prototype[ALL_LISTENERS] = [];
Store.prototype[LIFECYCLE] = {};
Store.prototype[LISTENERS] = {};
Store.prototype[PUBLIC_METHODS] = {};

var store = new Store(this);

storeInstance = assign(new AltStore(this.dispatcher, store, null, StoreModel), getInternalMethods(StoreModel, builtIns));
var thestate = StoreModel[IMMUTABLE] === true ? store.state : null;

storeInstance = assign(new AltStore(this.dispatcher, store, thestate, StoreModel), getInternalMethods(StoreModel, builtIns));

if (saveStore) {
this.stores[key] = storeInstance;
Expand Down
27 changes: 25 additions & 2 deletions dist/alt-with-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var ACTION_KEY = Symbol("holds the actions uid symbol for listening");
var ACTION_UID = Symbol("the actions uid name");
var ALL_LISTENERS = Symbol("name of listeners");
var EE = Symbol("event emitter instance");
var IMMUTABLE = Symbol["for"]("AltImmutableStore");
var INIT_SNAPSHOT = Symbol("init snapshot storage");
var LAST_SNAPSHOT = Symbol("last snapshot storage");
var LIFECYCLE = Symbol("store lifecycle listeners");
Expand Down Expand Up @@ -109,8 +110,12 @@ var AltStore = (function () {
},
getState: {
value: function getState() {
// Copy over state so it's RO.
var state = this[STATE_CONTAINER];

if (this.StoreModel[IMMUTABLE] === true) {
return state;
}

return Object.keys(state).reduce(function (obj, key) {
obj[key] = state[key];
return obj;
Expand Down Expand Up @@ -326,6 +331,14 @@ var createStoreFromObject = function (alt, StoreModel, key, saveStore) {
}
}, StoreMixinListeners, StoreMixinEssentials, StoreModel);

if (StoreModel[IMMUTABLE] === true) {
StoreProto.setState = function (key, value) {
storeInstance[STATE_CONTAINER] = storeInstance[STATE_CONTAINER].set(key, value);
this.emitChange();
return false;
};
}

// bind the store listeners
/* istanbul ignore else */
if (StoreProto.bindListeners) {
Expand Down Expand Up @@ -423,14 +436,24 @@ var Alt = (function () {
}
});

if (StoreModel[IMMUTABLE] === true) {
Store.prototype.setState = function (key, value) {
storeInstance[STATE_CONTAINER] = storeInstance[STATE_CONTAINER].set(key, value);
this.emitChange();
return false;
};
}

Store.prototype[ALL_LISTENERS] = [];
Store.prototype[LIFECYCLE] = {};
Store.prototype[LISTENERS] = {};
Store.prototype[PUBLIC_METHODS] = {};

var store = new Store(this);

storeInstance = assign(new AltStore(this.dispatcher, store, null, StoreModel), getInternalMethods(StoreModel, builtIns));
var thestate = StoreModel[IMMUTABLE] === true ? store.state : null;

storeInstance = assign(new AltStore(this.dispatcher, store, thestate, StoreModel), getInternalMethods(StoreModel, builtIns));

if (saveStore) {
this.stores[key] = storeInstance;
Expand Down
27 changes: 25 additions & 2 deletions dist/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var ACTION_KEY = Symbol("holds the actions uid symbol for listening");
var ACTION_UID = Symbol("the actions uid name");
var ALL_LISTENERS = Symbol("name of listeners");
var EE = Symbol("event emitter instance");
var IMMUTABLE = Symbol["for"]("AltImmutableStore");
var INIT_SNAPSHOT = Symbol("init snapshot storage");
var LAST_SNAPSHOT = Symbol("last snapshot storage");
var LIFECYCLE = Symbol("store lifecycle listeners");
Expand Down Expand Up @@ -121,8 +122,12 @@ var AltStore = (function () {
},
getState: {
value: function getState() {
// Copy over state so it's RO.
var state = this[STATE_CONTAINER];

if (this.StoreModel[IMMUTABLE] === true) {
return state;
}

return Object.keys(state).reduce(function (obj, key) {
obj[key] = state[key];
return obj;
Expand Down Expand Up @@ -340,6 +345,14 @@ var createStoreFromObject = function (alt, StoreModel, key, saveStore) {
}
}, StoreMixinListeners, StoreMixinEssentials, StoreModel);

if (StoreModel[IMMUTABLE] === true) {
StoreProto.setState = function (key, value) {
storeInstance[STATE_CONTAINER] = storeInstance[STATE_CONTAINER].set(key, value);
this.emitChange();
return false;
};
}

// bind the store listeners
/* istanbul ignore else */
if (StoreProto.bindListeners) {
Expand Down Expand Up @@ -438,14 +451,24 @@ var Alt = (function () {
}
});

if (StoreModel[IMMUTABLE] === true) {
Store.prototype.setState = function (key, value) {
storeInstance[STATE_CONTAINER] = storeInstance[STATE_CONTAINER].set(key, value);
this.emitChange();
return false;
};
}

Store.prototype[ALL_LISTENERS] = [];
Store.prototype[LIFECYCLE] = {};
Store.prototype[LISTENERS] = {};
Store.prototype[PUBLIC_METHODS] = {};

var store = new Store(this);

storeInstance = assign(new AltStore(this.dispatcher, store, null, StoreModel), getInternalMethods(StoreModel, builtIns));
var thestate = StoreModel[IMMUTABLE] === true ? store.state : null;

storeInstance = assign(new AltStore(this.dispatcher, store, thestate, StoreModel), getInternalMethods(StoreModel, builtIns));

if (saveStore) {
this.stores[key] = storeInstance;
Expand Down
51 changes: 27 additions & 24 deletions src/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const ACTION_KEY = Symbol('holds the actions uid symbol for listening')
const ACTION_UID = Symbol('the actions uid name')
const ALL_LISTENERS = Symbol('name of listeners')
const EE = Symbol('event emitter instance')
const IMMUTABLE = Symbol.for('AltImmutableStore')
const INIT_SNAPSHOT = Symbol('init snapshot storage')
const LAST_SNAPSHOT = Symbol('last snapshot storage')
const LIFECYCLE = Symbol('store lifecycle listeners')
Expand All @@ -34,13 +35,6 @@ function uid(container, name) {
return key
}

/* for now just detect whether it's instance of Immutable.js */
function isImmutable(state) {
// let's check with that for now
// https://github.com/facebook/immutable-js/issues/421
return typeof state.toJS === 'function'
}

/* istanbul ignore next */
function NoopClass() { }

Expand Down Expand Up @@ -105,18 +99,16 @@ class AltStore {
}

getState() {
// Copy over state so it's RO.
const state = this[STATE_CONTAINER]
let result = state

if (!isImmutable(state)) {
result = Object.keys(state).reduce((obj, key) => {
obj[key] = state[key]
return obj
}, {})
if (this.StoreModel[IMMUTABLE] === true) {
return state
}

return result
return Object.keys(state).reduce((obj, key) => {
obj[key] = state[key]
return obj
}, {})
}
}

Expand Down Expand Up @@ -310,19 +302,20 @@ const createStoreFromObject = (alt, StoreModel, key, saveStore) => {
return storeInstance
},
setState(values = {}) {

if (!isImmutable(values)) {
assign(this.state, values)
} else {
// one of possibles workarounds
storeInstance[STATE_CONTAINER] = values
}

assign(this.state, values)
this.emitChange()
return false
}
}, StoreMixinListeners, StoreMixinEssentials, StoreModel)

if (StoreModel[IMMUTABLE] === true) {
StoreProto.setState = function (key, value) {
storeInstance[STATE_CONTAINER] = storeInstance[STATE_CONTAINER].set(key, value)
this.emitChange()
return false
}
}

// bind the store listeners
/* istanbul ignore else */
if (StoreProto.bindListeners) {
Expand Down Expand Up @@ -411,15 +404,25 @@ class Alt {
}
})

if (StoreModel[IMMUTABLE] === true) {
Store.prototype.setState = function (key, value) {
storeInstance[STATE_CONTAINER] = storeInstance[STATE_CONTAINER].set(key, value)
this.emitChange()
return false
}
}

Store.prototype[ALL_LISTENERS] = []
Store.prototype[LIFECYCLE] = {}
Store.prototype[LISTENERS] = {}
Store.prototype[PUBLIC_METHODS] = {}

const store = new Store(this)

const thestate = StoreModel[IMMUTABLE] === true ? store.state : null

storeInstance = assign(
new AltStore(this.dispatcher, store, null, StoreModel),
new AltStore(this.dispatcher, store, thestate, StoreModel),
getInternalMethods(StoreModel, builtIns)
)

Expand Down
40 changes: 0 additions & 40 deletions test/immutable-store-state.js

This file was deleted.

0 comments on commit 34be7bb

Please sign in to comment.