Skip to content

Commit

Permalink
Add the FinalStore
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Feb 25, 2015
1 parent 44af732 commit c104fb7
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 30 deletions.
23 changes: 14 additions & 9 deletions dist/alt-with-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
23 changes: 14 additions & 9 deletions dist/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
29 changes: 17 additions & 12 deletions src/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand Down
71 changes: 71 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
16 changes: 16 additions & 0 deletions utils/makeFinalStore.js
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit c104fb7

Please sign in to comment.