UNPKG

easy-peasy

Version:

Vegetarian friendly state for React

68 lines (62 loc) 1.93 kB
import { actionOnSymbol, thunkOnSymbol } from './constants'; import { get } from './lib'; export function createListenerMiddleware(references) { return () => next => action => { const result = next(action); if ( action && references.internals.listenerActionMap[action.type] && references.internals.listenerActionMap[action.type].length > 0 ) { const sourceAction = references.internals.actionCreatorDict[action.type]; references.internals.listenerActionMap[action.type].forEach( actionCreator => { actionCreator({ type: sourceAction ? sourceAction.type : action.type, payload: action.payload, error: action.error, result: action.result, }); }, ); } return result; }; } export function bindListenerDefinitions( listenerDefinitions, actionCreators, actionCreatorDict, listenerActionMap, ) { listenerDefinitions.forEach(listenerActionOrThunk => { const listenerMeta = listenerActionOrThunk[actionOnSymbol] || listenerActionOrThunk[thunkOnSymbol]; const targets = listenerMeta.targetResolver( get(listenerMeta.parent, actionCreators), actionCreators, ); const targetTypes = (Array.isArray(targets) ? targets : [targets]).reduce( (acc, target) => { if ( typeof target === 'function' && target.type && actionCreatorDict[target.type] ) { acc.push(target.type); } else if (typeof target === 'string') { acc.push(target); } return acc; }, [], ); listenerMeta.resolvedTargets = targetTypes; targetTypes.forEach(targetType => { const listenerReg = listenerActionMap[targetType] || []; listenerReg.push(actionCreatorDict[listenerMeta.type]); listenerActionMap[targetType] = listenerReg; }); }); }