diff --git a/dist/alt-with-runtime.js b/dist/alt-with-runtime.js index 33a2e74f..af3594c2 100644 --- a/dist/alt-with-runtime.js +++ b/dist/alt-with-runtime.js @@ -266,8 +266,14 @@ var Alt = (function () { }, createStore: { value: function createStore(StoreModel, iden) { - var _this6 = this; + var saveStore = arguments[2] === undefined ? true : arguments[2]; + var storeInstance = undefined; var key = iden || StoreModel.displayName || StoreModel.name; + + if (this.stores[key]) { + throw new ReferenceError("A store named " + key + " already exists, double check your store names or pass in\nyour own custom identifier for each store"); + } + // Creating a class here so we don't overload the provided store's // prototype with the mixin behaviour and I'm extending from StoreModel // so we can inherit any extensions from the provided store. @@ -290,21 +296,20 @@ var Alt = (function () { alt: this, dispatcher: this.dispatcher, getInstance: function () { - return _this6.stores[key]; + return storeInstance; } }); var store = new Store(); - if (this.stores[key]) { - throw new ReferenceError("A store named " + key + " already exists, double check your store names or pass in\nyour own custom identifier for each store"); - } + storeInstance = assign(new AltStore(this.dispatcher, store), getInternalMethods(StoreModel, builtIns)); - this.stores[key] = assign(new AltStore(this.dispatcher, store), getInternalMethods(StoreModel, builtIns)); - - saveInitialSnapshot(this, key); + if (saveStore) { + this.stores[key] = storeInstance; + saveInitialSnapshot(this, key); + } - return this.stores[key]; + return storeInstance; }, writable: true, configurable: true diff --git a/dist/alt.js b/dist/alt.js index 80af2786..8d81d17a 100644 --- a/dist/alt.js +++ b/dist/alt.js @@ -276,8 +276,14 @@ var Alt = (function () { }, createStore: { value: function createStore(StoreModel, iden) { - var _this6 = this; + var saveStore = arguments[2] === undefined ? true : arguments[2]; + var storeInstance = undefined; var key = iden || StoreModel.displayName || StoreModel.name; + + if (this.stores[key]) { + throw new ReferenceError("A store named " + key + " already exists, double check your store names or pass in\nyour own custom identifier for each store"); + } + // Creating a class here so we don't overload the provided store's // prototype with the mixin behaviour and I'm extending from StoreModel // so we can inherit any extensions from the provided store. @@ -300,21 +306,20 @@ var Alt = (function () { alt: this, dispatcher: this.dispatcher, getInstance: function () { - return _this6.stores[key]; + return storeInstance; } }); var store = new Store(); - if (this.stores[key]) { - throw new ReferenceError("A store named " + key + " already exists, double check your store names or pass in\nyour own custom identifier for each store"); - } + storeInstance = assign(new AltStore(this.dispatcher, store), getInternalMethods(StoreModel, builtIns)); - this.stores[key] = assign(new AltStore(this.dispatcher, store), getInternalMethods(StoreModel, builtIns)); - - saveInitialSnapshot(this, key); + if (saveStore) { + this.stores[key] = storeInstance; + saveInitialSnapshot(this, key); + } - return this.stores[key]; + return storeInstance; }, writable: true, configurable: true diff --git a/src/alt.js b/src/alt.js index bf2186eb..ef78f51f 100644 --- a/src/alt.js +++ b/src/alt.js @@ -233,8 +233,17 @@ class Alt { this.dispatcher.dispatch({ action, data }) } - createStore(StoreModel, iden) { + createStore(StoreModel, iden, saveStore = true) { + let storeInstance let key = iden || StoreModel.displayName || StoreModel.name + + if (this.stores[key]) { + throw new ReferenceError( +`A store named ${key} already exists, double check your store names or pass in +your own custom identifier for each store` + ) + } + // Creating a class here so we don't overload the provided store's // prototype with the mixin behaviour and I'm extending from StoreModel // so we can inherit any extensions from the provided store. @@ -250,26 +259,22 @@ class Alt { _storeName: key, alt: this, dispatcher: this.dispatcher, - getInstance: () => this.stores[key] + getInstance: () => storeInstance }) let store = new Store() - if (this.stores[key]) { - throw new ReferenceError( -`A store named ${key} already exists, double check your store names or pass in -your own custom identifier for each store` - ) - } - - this.stores[key] = assign( + storeInstance = assign( new AltStore(this.dispatcher, store), getInternalMethods(StoreModel, builtIns) ) - saveInitialSnapshot(this, key) + if (saveStore) { + this.stores[key] = storeInstance + saveInitialSnapshot(this, key) + } - return this.stores[key] + return storeInstance } generateActions(...actionNames) { diff --git a/test/index.js b/test/index.js index 650d3578..67d7f359 100644 --- a/test/index.js +++ b/test/index.js @@ -10,6 +10,8 @@ import IsomorphicMixin from '../mixins/IsomorphicMixin' import ReactComponent from './helpers/ReactComponent' +import makeFinalStore from '../utils/makeFinalStore' + let alt = new Alt() class MyActions { @@ -1139,6 +1141,75 @@ let tests = { assert.equal(e instanceof ReferenceError, true, 'invalid action reference passed in') } }, + + 'do not include store in snapshots'() { + function NoBootstrap() { } + + alt.createStore(NoBootstrap, 'NoBootstrap', false) + + let snapshot = JSON.parse(alt.takeSnapshot()) + + assert.equal(typeof snapshot.NoBootstrap, 'undefined', 'Store does not exist in snapshots') + assert.equal(typeof snapshot.AltSecondStore, 'object', 'AltSecondStore exists') + }, + + 'make final store'() { + function Action() { this.generateActions('set') } + + let action = alt.createActions(Action) + + function Store1() { + this.bindActions(action) + this.value = null + } + Store1.prototype.set = function () { + this.value = 1 + } + + function Store2() { + this.bindActions(action) + this.value = null + } + Store2.prototype.set = function () { + this.value = 2 + } + + function Store3() { + this.bindActions(action) + this.value = null + } + Store3.prototype.set = function () { + this.value = 3 + } + + // I put these out of order intentionally because order shouldn't + // determine when these get called, the finalstore should always be last. + let store1 = alt.createStore(Store1) + let FinalStore = makeFinalStore(alt) + let store2 = alt.createStore(Store2) + let store3 = alt.createStore(Store3) + + + let finalStoreCalls = 0 + + FinalStore.listen(() => { + assert.equal(++finalStoreCalls, 1, 'final store was called only once') + + assert.equal(store1.getState().value, 1, 'store1 value correct') + assert.equal(store2.getState().value, 2, 'store2 value correct') + assert.equal(store3.getState().value, 3, 'store3 value correct') + }) + + let store2Calls = 0 + + store2.listen(() => { + assert.equal(++store2Calls, 1, 'store 2 listen was only called once') + assert.equal(store2.getState().value, 2, 'store2 state was updated successfully') + }) + + // Fire off the action + action.set() + }, } export default tests diff --git a/utils/makeFinalStore.js b/utils/makeFinalStore.js new file mode 100644 index 00000000..eb8b651c --- /dev/null +++ b/utils/makeFinalStore.js @@ -0,0 +1,16 @@ +module.exports = makeFinalStore + +function FinalStore() { + this.dispatcher.register(function (payload) { + var stores = Object.keys(this.alt.stores).reduce(function (arr, store) { + return arr.push(this.alt.stores[store].dispatchToken), arr + }.bind(this), []) + + this.waitFor(stores) + this.getInstance().emitChange() + }.bind(this)) +} + +function makeFinalStore(alt) { + return alt.createStore(FinalStore, 'AltFinalStore', false) +}