From edb51a961ce15d0cfeefe78f071f506321f2280b Mon Sep 17 00:00:00 2001 From: "Aeneas Rekkas (arekkas)" Date: Fri, 11 Nov 2016 12:27:01 +0100 Subject: [PATCH] add neverSkipReducer option to undoable, resolves #129 --- README.md | 2 ++ src/reducer.js | 19 ++++++++++++------- test/index.spec.js | 5 +++++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d4505b3..dc85f19 100644 --- a/README.md +++ b/README.md @@ -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 }) ``` diff --git a/src/reducer.js b/src/reducer.js index 5487c80..390f810 100644 --- a/src/reducer.js +++ b/src/reducer.js @@ -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 = {}) => { @@ -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: @@ -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) diff --git a/test/index.spec.js b/test/index.spec.js index daa2895..6cacaf0 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -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: []