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

Commit

Permalink
provide better handling of preloaded initializations and still mainta…
Browse files Browse the repository at this point in the history
…in the reset behavior
  • Loading branch information
davidroeca committed Jul 7, 2017
1 parent ec6a636 commit 32fdc35
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,42 +112,52 @@ export default function undoable (reducer, rawConfig = {}) {
syncFilter: rawConfig.syncFilter || false
}

return (state = config.history, action = {}, ...slices) => {
let initialState = config.history
let defaultState = config.history
return (state = defaultState, action = {}, ...slices) => {
debug.start(action, state)

let history = state
if (!config.history) {
if (!defaultState) {
debug.log('history is uninitialized')

if (state === undefined) {
const clearHistoryAction = { type: ActionTypes.CLEAR_HISTORY }
const start = reducer(state, clearHistoryAction, ...slices)

history = config.history = createHistory(
history = initialState = createHistory(
start,
config.ignoreInitialState
)

debug.log('do not initialize on probe actions')
debug.log('do not set defaultState on probe actions')
} else if (isHistory(state)) {
history = config.history = config.ignoreInitialState
history = config.ignoreInitialState
? state : newHistory(
state.past,
state.present,
state.future
)
if (initialState === defaultState) {
// initialState hasn't been set by the state === undefined branch
initialState = defaultState = history
} else {
// initialState has been set by the state === undefined branch
// so here we finally provide a default value
defaultState = initialState
}
debug.log(
'initialHistory initialized: initialState is a history',
config.history
initialState
)
} else {
history = config.history = createHistory(
history = initialState = defaultState = createHistory(
state,
config.ignoreInitialState
)
debug.log(
'initialHistory initialized: initialState is not a history',
config.history
initialState
)
}
}
Expand Down Expand Up @@ -208,8 +218,8 @@ export default function undoable (reducer, rawConfig = {}) {

if (config.initTypes.some((actionType) => actionType === action.type)) {
debug.log('reset history due to init action')
debug.end(config.history)
return config.history
debug.end(initialState)
return initialState
}

if (history.present === res) {
Expand Down

0 comments on commit 32fdc35

Please sign in to comment.