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

Commit

Permalink
more functionality updates; redo/undo logic still need tweaking
Browse files Browse the repository at this point in the history
  • Loading branch information
davidroeca committed Dec 9, 2016
1 parent 3a20e06 commit 1e3a679
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function insert (history, state, limit) {
const historyOverflow = limit && lengthWithoutFuture(history) >= limit

const pastSliced = past.slice(historyOverflow ? 1 : 0)
const newPast = _latestUnfiltered
const newPast = _latestUnfiltered != null
? [
...pastSliced,
_latestUnfiltered
Expand All @@ -38,9 +38,8 @@ function undo (history) {

if (past.length <= 0) return history

const newFuture = _latestUnfiltered
? _latestUnfiltered === future[0]
? future : [
const newFuture = _latestUnfiltered != null
? [
_latestUnfiltered,
...future
] : [
Expand All @@ -62,9 +61,8 @@ function redo (history) {

if (future.length <= 0) return history

const newPast = _latestUnfiltered
? _latestUnfiltered === past[past.length - 1]
? past : [
const newPast = _latestUnfiltered != null
? [
...past,
_latestUnfiltered
] : [
Expand Down Expand Up @@ -119,11 +117,16 @@ function jump (history, n) {
}

// createHistory
function createHistory (state) {
return {
function createHistory (state, ignoreInitialState) {
return ignoreInitialState ? {
past: [],
present: state,
future: []
} : {
past: [],
present: state,
_latestUnfiltered: state,
future: []
}
}

Expand All @@ -141,7 +144,8 @@ export default function undoable (reducer, rawConfig = {}) {
jumpToFutureType: rawConfig.jumpToFutureType || ActionTypes.JUMP_TO_FUTURE,
jumpType: rawConfig.jumpType || ActionTypes.JUMP,
clearHistoryType: rawConfig.clearHistoryType || ActionTypes.CLEAR_HISTORY,
neverSkipReducer: rawConfig.neverSkipReducer || false
neverSkipReducer: rawConfig.neverSkipReducer || false,
ignoreInitialState: rawConfig.ignoreInitialState || false
}

return (state = config.history, action = {}) => {
Expand All @@ -152,7 +156,10 @@ export default function undoable (reducer, rawConfig = {}) {
debug.log('history is uninitialized')

if (state === undefined) {
history = createHistory(reducer(state, { type: '@@redux-undo/CREATE_HISTORY' }))
history = createHistory(reducer(
state, { type: '@@redux-undo/CREATE_HISTORY' }),
config.ignoreInitialState
)
debug.log('do not initialize on probe actions')
} else if (isHistory(state)) {
history = config.history = state
Expand Down Expand Up @@ -227,7 +234,7 @@ export default function undoable (reducer, rawConfig = {}) {
// if filtering an action, first check latestUnfiltered, and update past;
// then clear _latestUnfiltered and update present
const { past, future, _latestUnfiltered } = history
const newPast = _latestUnfiltered
const newPast = _latestUnfiltered != null
? [
...past,
_latestUnfiltered
Expand Down

0 comments on commit 1e3a679

Please sign in to comment.