Use an append-only log to store "commands" #35
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.
Motivation
I never got
redo
working because the previous method was hard to test and reason about. This seems like a reasonable size refactor. See if it can help me to getredo
out 🙂#34
Context
Currently, I am trying to implement
undo
andredo
using two mutable stacks. One stack keeps track of all the applied "commands", the other is used as a temp buffer.When an user action is committed, we would "push" it to the first stack. When the user undoes, we would "pop" the first stack (history stack), re-apply what is now on the first stack, and push the popped "command" onto the temp stack (redo stack).
For redo, we simply pop the redo stack, then apply the command and push it back to the history stack.
In my opinion, Mutating two logs is hard to reason about than just keeping track of the offsets of an append-only log.
What this PR does
This PR attempts to get rid of the two mutable stacks, and just use a single append-only log to store "commands" instead. We will use "offsets" of the log to implement the undo/redo logic.