I have a list of items in array, and the number of items are many(more than 100s).
The items in array are updated by user interactions(add, remove, update).
What I want to do is to find incremental changes between the previous and the after array state, so that users can recover and forward between the previous array status and next array status.
This is achievable by saving the before and the after state of the array object. However if the number of items are too many, saving the array object could be waste of memory and it may affect the performance.
My idea is to find incremental changes between before and after, and save only the differences as the following format.
Before: [0,1,2,3]
After: [1,3,5]
Diff:
[
['delete', 0, 0], // [action, index, item]
['delete', 1, 2],
['insert', 2, 5]
]
I have searched Google and Stackoverflow for this purpose, but answers what I have found was not the exactly what I want.
I would appreciate if I can get some tips from anyone who has tried this way. If my approach was wrong, any criticism would be also helpful.
You could try implementing
applyandrevertfunctions for your actions.If you prefer to do thins in a more object-oriented way, you could do it like this:
You mentioned you wanted to get the needed mutations to get from one array to another. This is an interesting problem. There are, in theory, an infinite number of possible mutations to get from one to the other, and even when restricting yourself to the minimum number needed, there are often multiple solutions. However, you could try something like this.
This approach basically starts by deleting everything not needed, then inserting everything missing.