import Tms from '@fmfe/tms.js' ; interface Snapshot { state : string ; time : number ; position : string ; payloads : string ; } export default class TmsSnapshot < S = { } > { store : S ; Tms : any ; snapshots : Array < Snapshot > = [ ] ; playIndex : number = 0 ; playing : boolean = false ; playNextTime : number = 0 ; constructor ( store , Tms ) { this . store = store ; this . Tms = Tms ; } /**
     * 添加一条快照
     */ push ( snapshot ) { this . snapshots . push ( snapshot ) ; return this ; } /**
     * 恢复第几个快照
     */ recoveryOfIndex ( index ) { const stateText = this . snapshots [ index ] . state ; const restoreSnapshot = ( store , stateText ) : void => { const state = typeof stateText === 'string' ? JSON . parse ( stateText ) : stateText ; const _restoreSnapshot = ( store , state ) => { Object . keys ( state ) . forEach ( k => { const currentState = state [ k ] ; if ( ( store [ k ] as any ) instanceof this . Tms ) { _restoreSnapshot ( store [ k ] , currentState ) ; } else { store [ k ] = currentState ; } } ) ; } ; _restoreSnapshot ( store , state ) ; } ; restoreSnapshot ( this . store , stateText ) ; return this ; } /**
     * 播放快照
     */ play ( ) { const playIndex = this . playIndex ; const recoveryIndex = this . snapshots . length - playIndex - 1 ; if ( this . snapshots . length - 1 > playIndex ) { this . playNextTime = this . snapshots [ recoveryIndex - 1 ] . time - this . snapshots [ recoveryIndex ] . time ; setTimeout ( ( ) => { this . play ( ) ; } , this . playNextTime , playIndex ) ; } if ( this . snapshots . length > playIndex ) { this . recoveryOfIndex ( recoveryIndex ) ; } if ( this . snapshots . length - 1 > playIndex ) { this . playIndex ++ ; this . playing = true ; } else { this . playIndex = 0 ; this . playNextTime = 0 ; this . playing = false ; } return this ; } clear ( ) { this . snapshots . splice ( 0 , this . snapshots . length ) ; } }