(
initialCommittedState: any,
initialLiftedState: LiftedState,
errorHandler: ErrorHandler,
monitorReducer?: any,
options: Partial<StoreDevtoolsConfig> = {}
)
| 160 | * Creates a history state reducer from an app's reducer. |
| 161 | */ |
| 162 | export function liftReducerWith( |
| 163 | initialCommittedState: any, |
| 164 | initialLiftedState: LiftedState, |
| 165 | errorHandler: ErrorHandler, |
| 166 | monitorReducer?: any, |
| 167 | options: Partial<StoreDevtoolsConfig> = {} |
| 168 | ) { |
| 169 | /** |
| 170 | * Manages how the history actions modify the history state. |
| 171 | */ |
| 172 | return ( |
| 173 | reducer: ActionReducer<any, any> |
| 174 | ): ActionReducer<LiftedState, Actions> => |
| 175 | (liftedState, liftedAction) => { |
| 176 | let { |
| 177 | monitorState, |
| 178 | actionsById, |
| 179 | nextActionId, |
| 180 | stagedActionIds, |
| 181 | skippedActionIds, |
| 182 | committedState, |
| 183 | currentStateIndex, |
| 184 | computedStates, |
| 185 | isLocked, |
| 186 | isPaused, |
| 187 | } = liftedState || initialLiftedState; |
| 188 | |
| 189 | if (!liftedState) { |
| 190 | // Prevent mutating initialLiftedState |
| 191 | actionsById = Object.create(actionsById); |
| 192 | } |
| 193 | |
| 194 | function commitExcessActions(n: number) { |
| 195 | // Auto-commits n-number of excess actions. |
| 196 | let excess = n; |
| 197 | let idsToDelete = stagedActionIds.slice(1, excess + 1); |
| 198 | |
| 199 | for (let i = 0; i < idsToDelete.length; i++) { |
| 200 | if (computedStates[i + 1].error) { |
| 201 | // Stop if error is found. Commit actions up to error. |
| 202 | excess = i; |
| 203 | idsToDelete = stagedActionIds.slice(1, excess + 1); |
| 204 | break; |
| 205 | } else { |
| 206 | delete actionsById[idsToDelete[i]]; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | skippedActionIds = skippedActionIds.filter( |
| 211 | (id) => idsToDelete.indexOf(id) === -1 |
| 212 | ); |
| 213 | stagedActionIds = [0, ...stagedActionIds.slice(excess + 1)]; |
| 214 | committedState = computedStates[excess].state; |
| 215 | computedStates = computedStates.slice(excess); |
| 216 | currentStateIndex = |
| 217 | currentStateIndex > excess ? currentStateIndex - excess : 0; |
| 218 | } |
| 219 |
no test coverage detected