I am thinking of ways to implement undo,redo functionality in an angular app.
The first and most basic idea was to use a model that completely describes the app internals, lets call it AppModel
. On every change worth recording, you simply create a new object of AppModel
and push it on to the stack and update currentIndex.
In absolute worst case scenario, an object of AppModel would have to fill out 500 text fields, with average length of 20 characters. Thats 10000 characters or 10kB for every update.
How bad is this number? I don't think if would cause memory issues, but would it make the app freeze every time a push onto stack happens? This a basic implementation:
historyStack: AppModel[];
currentIndex:number = -1;
push(newAppModel:AppModel){
//delete all after current index
this.historyStack.splice(++this.currentIndex, 0, newAppModel);
}
forward(){
if(this.currentIndex < this.historyStack.length-1){
this.currentIndex++;
return this.historyStack[this.currentIndex];
}
return this.historyStack[this.currentIndex];
}
back(){
return this.historyStack[this.currentIndex--];
}
The other option I could think of, is to store function calls that perform the redo
and reverse
operations. This approach would require me to also store which objects need to call the functions. Those objects might me deleted by user, so there also must be a way to recreate the objects. This is getting painful as I type :)
What way do you recommend?