export class UndoStack < T > { constructor ( maxSize ? ) { if ( maxSize !== undefined && maxSize < 1 ) { throw new Error ( 'Cannot have negative or zero maximum size' ) ; } this . _maxSize = maxSize ; } /**
     * Last active element on stack. The one that will be undone by a call to `undo`.
     */ get previous ( ) : T | undefined { return this . _stack [ this . _current ] ; } /**
     * First inactive element on stack. The one that will be redone by a call to `redo`.
     */ get next ( ) : T | undefined { return this . _stack [ this . _current + 1 ] ; } push ( elem ) { this . _current += 1 ; this . _stack . splice ( this . _current , this . _stack . length - this . _current , elem ) ; if ( this . _maxSize !== undefined ) { const overrun = this . _stack . length - this . _maxSize ; if ( overrun > 0 ) { this . _stack . splice ( 0 , overrun ) ; this . _current - overrun ; } } } /**
     * Undo and return the element that was undone.
     */ undo ( ) { if ( this . _current < 0 ) { throw new Error ( 'Nothing to undo' ) ; } return this . _stack [ this . _current -- ] ; } /**
     * Redo and return the element that was redone.
     */ redo ( ) { if ( this . _current >= this . _stack . length - 1 ) { throw new Error ( 'Nothing to redo' ) ; } return this . _stack [ ++ this . _current ] ; } private _current = - 1 ; private _stack : T [ ] = [ ] ; private _maxSize : number | undefined ; }