-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Fix multi-entity multi-property undo redo #50911
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b0a3292
Fix multi-entity multi-property undo redo
youknowriad 6198c6b
Fix ignored undos
youknowriad 01ae5b2
Fix unit tests
youknowriad 7ae67d8
Add more tests
youknowriad 18733ea
Deprecate getUndoEdit and getRedoEdit
youknowriad 60db28b
Update the undo/redo format to store diffs (#51002)
youknowriad cf1b115
Better types
youknowriad 0b1bfff
Avoid mutations
youknowriad d8da54d
Code style change per review
youknowriad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { State } from './selectors'; | ||
|
||
type Optional< T > = T | undefined; | ||
|
||
/** | ||
* Returns the previous edit from the current undo offset | ||
* for the entity records edits history, if any. | ||
* | ||
* @param state State tree. | ||
* | ||
* @return The edit. | ||
*/ | ||
export function getUndoEdits( state: State ): Optional< any > { | ||
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. We know the return value is an array so |
||
return state.undo.list[ state.undo.list.length - 2 + state.undo.offset ]; | ||
} | ||
|
||
/** | ||
* Returns the next edit from the current undo offset | ||
* for the entity records edits history, if any. | ||
* | ||
* @param state State tree. | ||
* | ||
* @return The edit. | ||
*/ | ||
export function getRedoEdits( state: State ): Optional< any > { | ||
return state.undo.list[ state.undo.list.length + state.undo.offset ]; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@adamziel @jsnajdr I've learned today that:
unlock( select ).somePrivateSelector()
in "thunks".state.root
instead of juststate
(state.root
should be just an internal thing, the store author shouldn't even know it exists).Fixing that bug is going to be a breaking change. Maybe we can still "proxy" .root and deprecate it somehow though. I'm not fixing this in this unrelated PR but just wanted to let you know about this.
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.
Thunks themselves are a private implementation detail of the store. The user of the store doesn't see how a particular action is implemented.
Therefore, the thunk should be able to see the private selectors and actions without having to unlock anything. The
select
object passed into a thunk should be already "unlocked", it should expose all the private selectors and let you callselect.getUndoEdits()
directly.The same applies to private actions and the
dispatch
object.Note that the ability to pass functions to
select
anddispatch
, i.e., calling them asselect( state => state.foo )
ordispatch( { type: 'BAR' } )
, is also unique to thunks -- see how thethunkArgs
properties are constructed withObject.assign
here. This allows the thunk to access thestate
directly and dispatch actions to the reducer directly.If
select
doesn't expose private selectors, we need to fix thethunkArgs
construction: instead ofgetSelectors()
we need to get the "unlocked" selectors.It seems like this was intentional because we're passing
store.__unstableOriginalGetState()
toselect()
here. If we didn't want that, we could very easily passstore.getState()
. Or was this an oversight that I and @adamziel didn't catch 2 years ago?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.
Yes, I tried without unlocking and it doesn't work, these things are not available.
I'd argue that it's oversight because all selectors receive the "state" and not the "original state" with metadata (which is an internal thing added by the data module).