Some ways to achieve redo and undo operations.
The key of redo and undo operations are diff - diff between every operation.
So the problem is how to get diff between operations.
We can get diff by BFS/DFS, but this way is not good for some operations of array:
- move
- add
- delete
and also this way has performance defect.
If you used get/set of lodash, you must be familiar with the string path style:
const obj = {}
set(obj, 'a.b.c', 'a')
get(obj, 'a.b.c', 'default')
We can provide some utils extend from get/set, and intercept to get diff.
The Command pattern is the classic way to do redo and undo operation.
If you want to intercept assign operation of a object, you may think of proxy which is a meta way to do that.