|
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +const ramda_1 = require("ramda"); |
| 4 | +exports.INITIAL_STATE = { |
| 5 | + cache: [] |
| 6 | +}; |
| 7 | +exports.REDUX_ACTION_RETRY = 'REDUX_ACTION_RETRY'; |
| 8 | +exports.RESET = `RESET`; |
| 9 | +exports.RETRY_ALL = `RETRY_ALL`; |
| 10 | +exports.REMOVE = `REMOVE`; |
| 11 | +function resetActionCreator() { |
| 12 | + return { |
| 13 | + type: exports.REDUX_ACTION_RETRY, |
| 14 | + [exports.RESET]: true |
| 15 | + }; |
| 16 | +} |
| 17 | +exports.resetActionCreator = resetActionCreator; |
| 18 | +function removeActionCreator(action) { |
| 19 | + return { |
| 20 | + type: exports.REDUX_ACTION_RETRY, |
| 21 | + [exports.REMOVE]: action |
| 22 | + }; |
| 23 | +} |
| 24 | +exports.removeActionCreator = removeActionCreator; |
| 25 | +function retryAllActionCreator() { |
| 26 | + return { |
| 27 | + type: exports.REDUX_ACTION_RETRY, |
| 28 | + [exports.RETRY_ALL]: true |
| 29 | + }; |
| 30 | +} |
| 31 | +exports.retryAllActionCreator = retryAllActionCreator; |
| 32 | +exports.cacheLens = ramda_1.lensProp('cache'); |
| 33 | +function createRetryMechanishm(initConfig) { |
| 34 | + const defaultConfig = { |
| 35 | + stateKeyName: exports.REDUX_ACTION_RETRY, |
| 36 | + cache: {}, |
| 37 | + extensions: [ |
| 38 | + upsert, |
| 39 | + reset, |
| 40 | + remove, |
| 41 | + ] |
| 42 | + }; |
| 43 | + const config = ramda_1.mergeWithKey((k, l, r) => k === 'extensions' ? [...l, ...r] : r, defaultConfig, initConfig); |
| 44 | + const { shouldRetryFunctions, reducers, } = config.extensions.reduce((acc, extension) => { |
| 45 | + const c = extension(config); |
| 46 | + if (c.shouldRetryAction) { |
| 47 | + acc.shouldRetryFunctions.push(c.shouldRetryAction); |
| 48 | + } |
| 49 | + if (c.reducer) { |
| 50 | + acc.reducers.push(c.reducer); |
| 51 | + } |
| 52 | + return acc; |
| 53 | + }, { |
| 54 | + shouldRetryFunctions: [], |
| 55 | + reducers: [], |
| 56 | + }); |
| 57 | + const shouldRetry = ramda_1.allPass(shouldRetryFunctions); |
| 58 | + return { |
| 59 | + stateKeyName: config.stateKeyName, |
| 60 | + reducer: (state = exports.INITIAL_STATE, action) => { |
| 61 | + return reducers.reduce((s, fn) => fn(s, action), state); |
| 62 | + }, |
| 63 | + reduxActionRetryMiddleware: ({ getState, dispatch }) => next => (action) => { |
| 64 | + if (action.type === exports.REDUX_ACTION_RETRY && action[exports.RETRY_ALL]) { |
| 65 | + const result = next(action); |
| 66 | + const cache = ramda_1.path([config.stateKeyName, 'cache'], getState()); |
| 67 | + cache.forEach(wrap => { |
| 68 | + if (shouldRetry(action, wrap)) { |
| 69 | + dispatch(wrap.action); |
| 70 | + } |
| 71 | + }); |
| 72 | + return result; |
| 73 | + } |
| 74 | + else { |
| 75 | + return next(action); |
| 76 | + } |
| 77 | + } |
| 78 | + }; |
| 79 | +} |
| 80 | +exports.createRetryMechanishm = createRetryMechanishm; |
| 81 | +function reset(_) { |
| 82 | + return { |
| 83 | + reducer: (state = exports.INITIAL_STATE, action) => { |
| 84 | + return (action.type === exports.REDUX_ACTION_RETRY && action[exports.RESET]) |
| 85 | + ? exports.INITIAL_STATE |
| 86 | + : state; |
| 87 | + } |
| 88 | + }; |
| 89 | +} |
| 90 | +function remove(_) { |
| 91 | + return { |
| 92 | + reducer: (state = exports.INITIAL_STATE, action) => { |
| 93 | + if (action.type === exports.REDUX_ACTION_RETRY && action[exports.REMOVE]) { |
| 94 | + return ramda_1.over(exports.cacheLens, ramda_1.reject(cachedAction => cachedAction.action.meta[exports.REDUX_ACTION_RETRY].id === action[exports.REMOVE].meta[exports.REDUX_ACTION_RETRY].id), state); |
| 95 | + } |
| 96 | + return state; |
| 97 | + } |
| 98 | + }; |
| 99 | +} |
| 100 | +function upsert(config) { |
| 101 | + const { actionWrapperFuns, } = config.extensions.reduce((acc, extension) => { |
| 102 | + if (extension != upsert) { |
| 103 | + const c = extension(config); |
| 104 | + if (c.actionWrapper) { |
| 105 | + acc.actionWrapperFuns.push(c.actionWrapper); |
| 106 | + } |
| 107 | + } |
| 108 | + return acc; |
| 109 | + }, { |
| 110 | + actionWrapperFuns: [], |
| 111 | + }); |
| 112 | + return { |
| 113 | + reducer: (state, action) => { |
| 114 | + if (config.cache[action.type]) { |
| 115 | + const coincidenceIndex = ramda_1.findIndex((cachedAction => cachedAction.action.meta[exports.REDUX_ACTION_RETRY].id === action.meta[exports.REDUX_ACTION_RETRY].id), state.cache); |
| 116 | + const baseActionWrap = (coincidenceIndex < 0) |
| 117 | + ? { action } |
| 118 | + : state.cache[coincidenceIndex]; |
| 119 | + const actionWrap = ramda_1.converge((...a) => ramda_1.mergeAll(a), [ramda_1.identity, ...actionWrapperFuns])(baseActionWrap); |
| 120 | + return ramda_1.over(exports.cacheLens, (cache) => (coincidenceIndex < 0) |
| 121 | + ? ramda_1.append(actionWrap, cache) |
| 122 | + : ramda_1.set(ramda_1.lensIndex(coincidenceIndex), actionWrap)(cache), state); |
| 123 | + } |
| 124 | + return state; |
| 125 | + } |
| 126 | + }; |
| 127 | +} |
0 commit comments