Skip to content

Commit

Permalink
added createCacheRefreshEpic
Browse files Browse the repository at this point in the history
  • Loading branch information
soocheng committed Jul 23, 2017
1 parent abde3e4 commit 510b965
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions src/epic.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,47 @@ export const createRequestByKeyIfNeededEpic = ({
type CacheEvictProps = {
conditionType: Array<string> | 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,
Expand All @@ -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()));

0 comments on commit 510b965

Please sign in to comment.