From 510b96530e1689e69c757971820817f4539dceba Mon Sep 17 00:00:00 2001 From: soocheng Date: Mon, 24 Jul 2017 00:38:21 +0800 Subject: [PATCH] added createCacheRefreshEpic --- src/epic.js | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/src/epic.js b/src/epic.js index 58acf39..d0ac95a 100644 --- a/src/epic.js +++ b/src/epic.js @@ -260,9 +260,47 @@ export const createRequestByKeyIfNeededEpic = ({ type CacheEvictProps = { conditionType: Array | string, ducks: Ducks, - filter?: Function + filter?: Function, + mapActionToKey?: Function, + mapActionToParams?: Function }; +export const createCacheRefreshEpic = ({ + conditionType, + ducks, + filter, + mapActionToParams, + mapActionToKey, +}: CacheEvictProps) => (action$: any, store: any) => + action$ + .filter((action) => { + if (action.type === conditionType) { + return true; + } + return isArray(conditionType) && conditionType.indexOf(action.type) > -1; + }) + .filter((action) => { + if (filter) { + return filter(store.getState()); + } + if (mapActionToParams && mapActionToKey) { + const key = mapActionToKey({ + params: mapActionToParams(action), + }); + return ( + get(ducks.selector(store.getState(), key), 'payload') !== undefined + ); + } + return get(ducks.selector(store.getState()), 'payload') !== undefined; + }) + .mergeMap((action) => { + const params = mapActionToParams ? mapActionToParams(action) : {}; + return Observable.of( + ducks.requestActions.clear(params), + ducks.requestActions.request(params), + ); + }); + export const createCacheEvictEpic = ({ conditionType, ducks, @@ -281,9 +319,4 @@ export const createCacheEvictEpic = ({ } return get(ducks.selector(store.getState()), 'payload') !== undefined; }) - .mergeMap(() => - Observable.of( - ducks.requestActions.clear(), - ducks.requestActions.request(), - ), - ); + .mergeMap(() => Observable.of(ducks.requestActions.clearAll()));