Comparing version 0.5.0 to 0.6.0
18
index.js
@@ -29,19 +29,10 @@ /* Fluxury - Copyright 2015 Peter W Moresi */ | ||
/* create a named store with an initialState and a reducer to move it forward */ | ||
createStore: function(name, initialState, reducer, methods={}, waitFor=[]) { | ||
createStore: function(name, initialState, reducer, methods={}) { | ||
var currentState = Object.freeze(initialState); | ||
var emitter = new EventEmitter(); | ||
// The last argument is always waitFor. If methods is an array then it is used for waitFor. | ||
if (Array.isArray(methods)) { | ||
waitFor = methods; | ||
methods = {}; | ||
} | ||
return Object.freeze(Object.assign({ | ||
name: name, | ||
dispatchToken: dispatcher.register( function(action) { | ||
if (waitFor.length > 0) { | ||
dispatcher.waitFor(waitFor); | ||
} | ||
var newState = reducer(currentState, action, dispatcher); | ||
var newState = reducer(currentState, action, dispatcher.waitFor); | ||
if (currentState !== newState) { | ||
@@ -57,4 +48,3 @@ currentState = Object.freeze(newState); | ||
return currentState; | ||
}, | ||
waitFor: waitFor | ||
} | ||
}, Object.keys(methods).reduce(function(a, b, i) { | ||
@@ -67,5 +57,3 @@ var newFunc = {}; | ||
}, {}))); | ||
} | ||
}); |
@@ -43,3 +43,2 @@ /* Fluxury - Copyright 2015 Peter W Moresi */ | ||
var methods = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3]; | ||
var waitFor = arguments.length <= 4 || arguments[4] === undefined ? [] : arguments[4]; | ||
@@ -49,15 +48,6 @@ var currentState = Object.freeze(initialState); | ||
// The last argument is always waitFor. If methods is an array then it is used for waitFor. | ||
if (Array.isArray(methods)) { | ||
waitFor = methods; | ||
methods = {}; | ||
} | ||
return Object.freeze(Object.assign({ | ||
name: name, | ||
dispatchToken: dispatcher.register(function (action) { | ||
if (waitFor.length > 0) { | ||
dispatcher.waitFor(waitFor); | ||
} | ||
var newState = reducer(currentState, action, dispatcher); | ||
var newState = reducer(currentState, action, dispatcher.waitFor); | ||
if (currentState !== newState) { | ||
@@ -73,9 +63,6 @@ currentState = Object.freeze(newState); | ||
return currentState; | ||
}, | ||
waitFor: waitFor | ||
} | ||
}, Object.keys(methods).reduce(function (a, b, i) { | ||
var newFunc = {}; | ||
newFunc[b] = function () { | ||
var _methods; | ||
for (var _len2 = arguments.length, params = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
@@ -85,3 +72,3 @@ params[_key2] = arguments[_key2]; | ||
return (_methods = methods)[b].apply(_methods, [currentState].concat(params)); | ||
return methods[b].apply(methods, [currentState].concat(params)); | ||
}; | ||
@@ -91,4 +78,3 @@ return Object.assign({}, a, newFunc); | ||
} | ||
}); | ||
module.exports = exports['default']; |
{ | ||
"name": "fluxury", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Add luxury sugar to simplify implementing Facebook's flavor of Flux architecture.", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -97,23 +97,7 @@ # fluxury | ||
3. createStore(name, initialState, reducer[ , methods, waitFor]) | ||
3. createStore(name, initialState, reducer, methods) | ||
Create a new store with a name, initialState, reducer and optionally an object with methods and an array with dispatch tokens sent to waitFor. | ||
Create a new store with a name, initialState, reducer function and an object with methods that maybe used to operate state. | ||
```js | ||
import {INC} from './MyActions'; | ||
import {createStore} from 'fluxury'; | ||
export default createStore('CountStore', 0, function(state, action) { | ||
if (action.type === INC) { | ||
return state + 1; | ||
} | ||
return state; | ||
}, { | ||
getCount: (state) => state // state is the count itself! | ||
}); | ||
``` | ||
Perhaps you prefer the classic switch case form: | ||
```js | ||
import {INC} from './MyActions' | ||
@@ -128,22 +112,39 @@ import {createStore} from 'fluxury'; | ||
return state; | ||
}, { | ||
getCount: (state) => state // state is the count itself! | ||
}) | ||
``` | ||
In this example you can make them both disappear: | ||
In addition to the state and action the reduce function sends the waitFor as the third argument. This allows stores to express dependencies on data in other stores and ensure that their reducers are executed prior to continuing execution. | ||
```js | ||
import {INC} from './MyActions' | ||
import {loadMessage} from './MyActions' | ||
import {createStore} from 'fluxury'; | ||
export default createStore('CountStore', 0, function(state, action) { | ||
return state + (action.type === INC ? 1 : 0); | ||
const MessageStore = createStore('MessageStore', [], function(state, action) { | ||
switch(action.type) { | ||
case loadMessage: | ||
return state.concat(action.message) | ||
default: | ||
return state | ||
} | ||
}) | ||
const MessageCountStore = createStore( 'MessageCountStore', 0, | ||
function(state, action, waitFor) { | ||
// ensure that MessageStore reducer is executed before continuing | ||
waitFor([MessageStore.dispatchToken]) | ||
switch(action.type) { | ||
case loadMessage: | ||
return state+1 | ||
default: | ||
return state | ||
} | ||
} | ||
) | ||
``` | ||
In addition to the state and action the reducer function sends the dispatcher | ||
as the third argument. | ||
## Store Properties and Methods | ||
|name|comment| | ||
| name | comment | | ||
|---------|------| | ||
@@ -154,3 +155,2 @@ | name | The name supplied when creating the store | | ||
| getState | A function that returns the current state | | ||
| waitFor | The array passed into createStore | | ||
@@ -157,0 +157,0 @@ ## Put it all together |
@@ -80,3 +80,3 @@ var test = require('tape'); | ||
t.deepEqual( Object.keys(store), ['name', 'dispatchToken', 'addListener', 'getState', 'waitFor'] ); | ||
t.deepEqual( Object.keys(store), ['name', 'dispatchToken', 'addListener', 'getState'] ); | ||
}) | ||
@@ -113,3 +113,2 @@ | ||
'getState', | ||
'waitFor', | ||
'get', | ||
@@ -116,0 +115,0 @@ 'has', |
17867
228