-
-
Notifications
You must be signed in to change notification settings - Fork 187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Distinct state fixes #59
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -207,9 +207,11 @@ export default function undoable (reducer, rawConfig = {}) { | |
return (state, action = {}) => { | ||
debugStart(action, state) | ||
|
||
let justCreatedHistory = false | ||
let history | ||
if (!config.history) { | ||
debug('create history on init') | ||
justCreatedHistory = true | ||
|
||
if (state === undefined) { | ||
config.history = createHistory(reducer(state, {})) | ||
|
@@ -289,11 +291,12 @@ export default function undoable (reducer, rawConfig = {}) { | |
} | ||
} | ||
|
||
if (history.present !== res) { | ||
// Don't add entry to history if it was just created | ||
if ((justCreatedHistory && history.present === res) || action.type === '@@redux-undo/INIT') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. gaearon has stated in reduxjs/redux#186 that one day they may change the name of If we use a flag that is set to I'm still struggling with this one 👻 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Think you mean Crap.. good point.
The flag is in the scope of the function, so that won't cause an extra insert right? And HMR will always replace the whole reducers and reapply our decorator around the recuder right?
But if we want to reset to the history upon
Like this: c5bc86b And on HMR, why don't we create some functional tests with HMR and webpack, haha that would be a nightmare. No, just kidding. Gaearon just wrote a nice longread on the current state of HMR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pff, I've spend some more hours on this but cannot get it right.. I think we do have some big problems with HMR. See the Maybe you could check the comments out and let me know what you think. I've added some comments in the code... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a note, we are using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well that is the question.. you think you could make HMR with both initial state from stores and reducers work without touching |
||
debug('not inserted, history was just created or action === redux-undo/INIT') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So yeah.. I reintroduced the INIT action here. Cause we never want to insert when that is fired right? All other actions will be recorded again, so also the actions resulting in the same state as before. As long as the distinctState filter is not active. I think that is the preferred behavior. |
||
} else { | ||
history = insert(history, res, config.limit) | ||
debug('inserted new state into history') | ||
} else { | ||
debug('not inserted, history is unchanged') | ||
} | ||
|
||
debug('history: ', history, ' free: ', config.limit - length(history)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
const { expect } = require('chai') | ||
const { default: undoable, ActionCreators, excludeAction, isHistory } = require('../src/index') | ||
const { default: undoable, ActionCreators, excludeAction, distinctState, isHistory } = require('../src/index') | ||
const Redux = require('redux') | ||
|
||
const excludedActionsOne = ['DECREMENT'] | ||
|
@@ -27,6 +27,8 @@ const initialStateTwo = { | |
|
||
const testConfigThree = { | ||
limit: -1, | ||
filter: distinctState(), | ||
FOR_TEST_ONLY_distinctState: true, | ||
initTypes: [] | ||
} | ||
const initialStateThree = { | ||
|
@@ -50,7 +52,6 @@ function runTestWithConfig (testConfig, initialStoreState, label) { | |
let mockUndoableReducer | ||
let mockInitialState | ||
let incrementedState | ||
let doubleIncrementedState | ||
let countReducer | ||
let store | ||
|
||
|
@@ -62,6 +63,8 @@ function runTestWithConfig (testConfig, initialStoreState, label) { | |
return state + 1 | ||
case 'DECREMENT': | ||
return state - 1 | ||
case 'DUMMY': | ||
return state | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added action without state changes, oh maybe that was not necessary... Well it nice and verbose now.. right :D ? |
||
default: | ||
return state | ||
} | ||
|
@@ -73,13 +76,11 @@ function runTestWithConfig (testConfig, initialStoreState, label) { | |
|
||
mockInitialState = mockUndoableReducer(undefined, {}) | ||
incrementedState = mockUndoableReducer(mockInitialState, { type: 'INCREMENT' }) | ||
doubleIncrementedState = mockUndoableReducer(incrementedState, { type: 'INCREMENT' }) | ||
console.info(' Beginning Test! Good luck!') | ||
console.info(' initialStoreState: ', initialStoreState) | ||
console.info(' store.getState(): ', store.getState()) | ||
console.info(' mockInitialState: ', mockInitialState) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved those down, only needed once |
||
console.info(' incrementedState: ', incrementedState) | ||
console.info(' doubleIncrementedState:', doubleIncrementedState) | ||
console.info('') | ||
|
||
expect(store.getState()).to.deep.equal(mockInitialState, 'mockInitialState should be the same as our store\'s state') | ||
|
@@ -118,6 +119,17 @@ function runTestWithConfig (testConfig, initialStoreState, label) { | |
} | ||
}) | ||
|
||
it('should record non state changing actions when filter is not set', () => { | ||
let dummyState = mockUndoableReducer(incrementedState, { type: 'DUMMY' }) | ||
if (testConfig.FOR_TEST_ONLY_distinctState) { | ||
expect(dummyState).to.deep.equal(incrementedState) | ||
} else { | ||
expect(dummyState.present).to.deep.equal(incrementedState.present) | ||
expect(dummyState.past).to.deep.equal([...incrementedState.past, incrementedState.present]) | ||
expect(dummyState.future).to.deep.equal(incrementedState.future) | ||
} | ||
}) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And a test for distinct state filter, will increase coverage to 100% when the other is merged :) |
||
it('should reset upon init actions', () => { | ||
let reInitializedState | ||
if (testConfig.initTypes) { | ||
|
@@ -302,6 +314,7 @@ function runTestWithConfig (testConfig, initialStoreState, label) { | |
let doubleUndoState | ||
let doubleRedoState | ||
before('perform a jump action', () => { | ||
let doubleIncrementedState = mockUndoableReducer(incrementedState, { type: 'INCREMENT' }) | ||
jumpToPastState = mockUndoableReducer(doubleIncrementedState, ActionCreators.jump(jumpStepsToPast)) | ||
jumpToFutureState = mockUndoableReducer(mockInitialState, ActionCreators.jump(jumpStepsToFuture)) | ||
doubleUndoState = mockUndoableReducer(doubleIncrementedState, ActionCreators.undo()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Set flag for later