-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
23 additions
and
193 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,4 @@ | ||
import { invariant, isFunction } from './utils'; | ||
import enhanceReducer from './enhanceReducer'; | ||
import AppStateWithEffects from './AppStateWithEffects'; | ||
|
||
/** | ||
* Wraps Store `getState` method. Instead of returning just plain old state, | ||
* it's assumed that the returned state is instance of `AppStateWithEffects`, | ||
* therefore it proxies the call to `getAppState`. | ||
* | ||
* @param {Object} Original Store | ||
* @returns {Function} Wrapped `getState` | ||
*/ | ||
export const wrapGetState = store => () => { | ||
const state = store.getState(); | ||
|
||
if (state instanceof AppStateWithEffects) { | ||
return state.getAppState(); | ||
} else { | ||
return state; | ||
} | ||
}; | ||
|
||
/** | ||
* Wraps Store `dispatch` method. The original `dispatch` is called first, then | ||
* it iterates over all the effects returned from the `getState` function. | ||
* Every effect is then executed and dispatch is passed as the first argument. | ||
* | ||
* @param {Object} Original Store | ||
* @returns {Function} Wrapped `dispatch` | ||
*/ | ||
export const wrapDispatch = store => (...args) => { | ||
// Let's just dispatch original action, | ||
// the original dispatch might actually be | ||
// enhanced by some middlewares. | ||
const result = store.dispatch(...args); | ||
|
||
const effects = store.getState().getEffects(); | ||
|
||
invariant(effects.every(isFunction), | ||
`It's allowed to yield only functions (side effect)`); | ||
|
||
// Effects are executed after action is dispatched | ||
effects.forEach(effect => effect(wrapDispatch(store))); | ||
|
||
return result; | ||
}; | ||
|
||
/** | ||
* Wraps Store `replaceReducer` method. The implementation just calls | ||
|
@@ -53,28 +8,23 @@ export const wrapDispatch = store => (...args) => { | |
* @param {Object} Original Store | ||
* @returns {Function} Wrapped `replaceReducer` | ||
*/ | ||
export const wrapReplaceReducer = store => nextReducer => | ||
store.replaceReducer(enhanceReducer(nextReducer)); | ||
export const wrapReplaceReducer = (store, deferEffects) => nextReducer => | ||
store.replaceReducer(enhanceReducer(nextReducer, deferEffects)); | ||
|
||
/** | ||
* Creates enhanced store factory, which takes original `createStore` as argument. | ||
* The store's `dispatch` and `getState` methods are wrapped with custom implementation. | ||
* | ||
* wrappedDispatch calls the original dispatch and executes all the side effects. | ||
* | ||
* wrappedGetState unwraps original applicaiton state from `AppStateWithEffects` | ||
* Returns modified store which is capable of handling Reducers in form of Generators. | ||
* | ||
* @param {Function} Original createStore implementation to be enhanced | ||
* @returns {Function} Store factory | ||
*/ | ||
export default createStore => (rootReducer, initialState) => { | ||
const store = createStore(enhanceReducer(rootReducer), new AppStateWithEffects(initialState, [])); | ||
let store = null; | ||
const deferEffects = effects => setTimeout(() => effects.forEach(effect => effect(store.dispatch)), 0); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
tomkis
Author
Contributor
|
||
store = createStore(enhanceReducer(rootReducer, deferEffects), initialState); | ||
|
||
return { | ||
...store, | ||
dispatch: wrapDispatch(store), | ||
getState: wrapGetState(store), | ||
liftGetState: () => store.getState(), | ||
replaceReducer: wrapReplaceReducer(store) | ||
replaceReducer: wrapReplaceReducer(store, deferEffects) | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,53 @@ | ||
import AppStateWithEffects from './AppStateWithEffects'; | ||
import { | ||
isFunction, | ||
isUndefined, | ||
isIterable, | ||
invariant, | ||
first, | ||
mapIterable | ||
} from './utils'; | ||
|
||
/** | ||
* AppStateOrEffect mapper - maps Iterable to AppStateOrEffect data structure. | ||
* The last element in Iterable chain is Application State the rest are some side effect. | ||
* Iterator mapper, maps iterator to value | ||
* | ||
* @param {any} Side effect Function or Application State any | ||
* | ||
* @param {Boolean} Last element in iterable | ||
* @returns {Object} AppStateOrEffect - Object containing isEffect(Boolean) and value(Function|Any) | ||
*/ | ||
const mapIterableToEffectsAndAppState = (value, last) => ({ | ||
isEffect: !last, | ||
value | ||
}); | ||
|
||
/** | ||
* AppStateOrEffect mapper - extracts just the value. | ||
* | ||
* @param {Object} Either Application State or Effect | ||
* @returns {any} Might be either Function (for Effect) or any for Application State | ||
*/ | ||
const mapValue = appStateOrEffect => appStateOrEffect.value; | ||
|
||
/** | ||
* Predicate function - decides whether appStateOrEffect is an Effect. | ||
* | ||
* @param {Object} Either Application State or Effect | ||
* @returns {Boolean} | ||
*/ | ||
const effectsPredicate = appStateOrEffect => appStateOrEffect.isEffect; | ||
|
||
/** | ||
* Predicate function - decides whether appStateOrEffect is an Application State. | ||
* | ||
* @param {Object} Either Application State or Effect | ||
* @returns {Boolean} | ||
* @param {Object} Iterator | ||
* @returns {Any} Iterator Value | ||
*/ | ||
const appStatePredicate = appStateOrEffect => !appStateOrEffect.isEffect; | ||
const mapValue = iterator => iterator.value; | ||
|
||
/** | ||
* Reducer enhancer. Iterates over generator like reducer reduction and accumulates | ||
* all the side effects and reduced application state. | ||
* | ||
* @param {Function} Root reducer in form of generator function | ||
* @returns {Function} Reducer which returns AppStateWithEffects instead of simple Application state | ||
* @param {Function} Function used for deferring effects accumulated from Reduction | ||
* @returns {Function} Reducer | ||
*/ | ||
export default rootReducer => (stateWithEffects, action) => { | ||
export default (rootReducer, deferEffects) => (appState, action) => { | ||
invariant(isFunction(rootReducer), | ||
`Provided root reducer is not a function.`); | ||
|
||
// Calling the Root reducer should return an Iterable | ||
const reduction = rootReducer(stateWithEffects.getAppState(), action); | ||
const reduction = rootReducer(appState, action); | ||
|
||
if (isIterable(reduction)) { | ||
// Iterable returned by Root reducer is mapped into array of values. | ||
// Last value in the array is reduced application state all the other values | ||
// are just side effects. | ||
const appStateAndEffects = mapIterable(reduction, mapIterableToEffectsAndAppState); | ||
const effects = mapIterable(reduction, mapValue); | ||
const newState = effects.pop(); | ||
|
||
// It's necessary to separate array of effects and Application state from flat array | ||
const effects = appStateAndEffects.filter(effectsPredicate).map(mapValue); | ||
const appState = appStateAndEffects.filter(appStatePredicate).map(mapValue); | ||
deferEffects(effects); | ||
|
||
invariant(!isUndefined(first(appState)), | ||
invariant(!isUndefined(newState), | ||
`Root reducer does not return new application state. Undefined is returned`); | ||
|
||
return new AppStateWithEffects(first(appState), effects); | ||
return newState; | ||
} else { | ||
console.warn( | ||
'createEffectCapableStore enhancer from redux-side-effects package is used, ' + | ||
'yet the provided root reducer is not a generator function' | ||
); | ||
|
||
return new AppStateWithEffects(reduction, []); | ||
return reduction; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
To expose this via a
dispatchEvents
function, here's some pseudo code of what I'd try:Looking at my old code this is in essence what I had to implement
dispatchEffects
. The rest involved warnings. Here is another approach which (I think) might be lest performant, but it doesn't keep resetting theeffects
array reference and it supports effects of effects (anti-pattern or not).I didn't test this so you'll have to check the syntax of my while loop. I've been writing a lot of rust recently, so I might have gotten my syntaxes messed up.
A couple other notes, in my original code the reason I added a
Task
class is to cancel the timeout ifdispatchEffects
was called. I no longer think that is necessary, but I feel it's important to note here. I had also added some warnings (per your request if I remember correctly), these warnings can still be helpful in this code if you feel it is worth adding the extra logic.