@@ -539,12 +539,12 @@ export class UndoRedo {
539539 }
540540
541541 public clearRedoStack ( ) {
542- this . cleanupOldDataForEntries ( this . redoStack )
542+ this . cleanupOldDataForEntries ( this . redoStack , this . undoStack )
543543 this . redoStack = [ ]
544544 }
545545
546546 public clearUndoStack ( ) {
547- this . cleanupOldDataForEntries ( this . undoStack )
547+ this . cleanupOldDataForEntries ( this . undoStack , this . redoStack )
548548 this . undoStack = [ ]
549549 }
550550
@@ -857,7 +857,7 @@ export class UndoRedo {
857857 const evictCount = Math . max ( 0 , this . undoStack . length - this . undoLimit )
858858 if ( evictCount > 0 ) {
859859 const evicted = this . undoStack . splice ( 0 , evictCount )
860- this . cleanupOldDataForEntries ( evicted )
860+ this . cleanupOldDataForEntries ( evicted , [ ... this . undoStack , ... this . redoStack ] )
861861 }
862862 }
863863
@@ -881,12 +881,20 @@ export class UndoRedo {
881881 }
882882
883883 /**
884- * Removes oldData entries referenced by permanently discarded undo/redo entries.
884+ * Removes oldData entries referenced by permanently discarded undo/redo entries,
885+ * but only if not still referenced by entries remaining on the other stack or
886+ * in-progress batch.
885887 */
886- private cleanupOldDataForEntries ( entries : UndoEntry [ ] ) {
887- for ( const entry of entries ) {
888+ private cleanupOldDataForEntries ( discardedEntries : UndoEntry [ ] , retainedEntries : UndoEntry [ ] ) {
889+ const retainedVersions = new Set < number > ( [
890+ ...retainedEntries . flatMap ( e => e . getReferencedOldDataVersions ( ) ) ,
891+ ...( this . batchUndoEntry ?. getReferencedOldDataVersions ( ) ?? [ ] ) ,
892+ ] )
893+ for ( const entry of discardedEntries ) {
888894 for ( const version of entry . getReferencedOldDataVersions ( ) ) {
889- this . oldData . delete ( version )
895+ if ( ! retainedVersions . has ( version ) ) {
896+ this . oldData . delete ( version )
897+ }
890898 }
891899 }
892900 }
0 commit comments