regular-redux-undo
Version:
the plugin of regular-redux to archieve undo and redo
29 lines (27 loc) • 728 B
JavaScript
import {get, set} from '../util/immuable';
/**
* 判断函数获取
* @param {*} newState
* @param {*} oldState
*/
function getChanged(newState, oldState, undoable) {
let changed = {};
return function(path) {
if (undoable) {
return get(newState.current, path) !== get(oldState.current, path);
} else {
return get(newState, path) !== get(oldState, path);
}
}
}
/**
* 根据路径判断某个state内容是否发生变化的函数
* @param {Function} reducer
*/
export default function(reducer, undoable = true) {
return (state, action) => {
let newState = reducer(state, action);
newState && (newState.changed = getChanged(newState, state, undoable));
return newState;
}
}