Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request omnidan#130 from arekkas/fix-129
Browse files Browse the repository at this point in the history
Add neverSkipReducer option to undoable, resolves omnidan#129
  • Loading branch information
omnidan authored Nov 11, 2016
2 parents 41b8a5d + edb51a9 commit 636c207
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ undoable(reducer, {
initTypes: ['@@redux-undo/INIT'] // history will be (re)set upon init action type

debug: false, // set to `true` to turn on debugging

neverSkipReducer: false, // prevent undoable from skipping the reducer on undo/redo
})
```

Expand Down
19 changes: 12 additions & 7 deletions src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ export default function undoable (reducer, rawConfig = {}) {
jumpToPastType: rawConfig.jumpToPastType || ActionTypes.JUMP_TO_PAST,
jumpToFutureType: rawConfig.jumpToFutureType || ActionTypes.JUMP_TO_FUTURE,
jumpType: rawConfig.jumpType || ActionTypes.JUMP,
clearHistoryType: rawConfig.clearHistoryType || ActionTypes.CLEAR_HISTORY
clearHistoryType: rawConfig.clearHistoryType || ActionTypes.CLEAR_HISTORY,
neverSkipReducer: false
}

return (state = config.history, action = {}) => {
Expand All @@ -149,6 +150,10 @@ export default function undoable (reducer, rawConfig = {}) {
}
}

const skipReducer = (res) => config.neverSkipReducer
? { ...res, present: reducer(res, action) }
: res

let res
switch (action.type) {
case undefined:
Expand All @@ -158,37 +163,37 @@ export default function undoable (reducer, rawConfig = {}) {
res = undo(history)
debug.log('perform undo')
debug.end(res)
return res
return skipReducer(res)

case config.redoType:
res = redo(history)
debug.log('perform redo')
debug.end(res)
return res
return skipReducer(res)

case config.jumpToPastType:
res = jumpToPast(history, action.index)
debug.log(`perform jumpToPast to ${action.index}`)
debug.end(res)
return res
return skipReducer(res)

case config.jumpToFutureType:
res = jumpToFuture(history, action.index)
debug.log(`perform jumpToFuture to ${action.index}`)
debug.end(res)
return res
return skipReducer(res)

case config.jumpType:
res = jump(history, action.index)
debug.log(`perform jump to ${action.index}`)
debug.end(res)
return res
return skipReducer(res)

case config.clearHistoryType:
res = createHistory(history.present)
debug.log('perform clearHistory')
debug.end(res)
return res
return skipReducer(res)

default:
res = reducer(history.present, action)
Expand Down
5 changes: 5 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import undoable, { ActionCreators, excludeAction, includeAction, isHistory } fro
const decrementActions = ['DECREMENT']

runTests('Default config')
runTests('Never skip reducer', {
undoableConfig: {
neverSkipReducer: false
}
})
runTests('No Init types', {
undoableConfig: {
initTypes: []
Expand Down

0 comments on commit 636c207

Please sign in to comment.