import * as _ from "lodash" ; import { BaseEvent , BaseListener , BaseObject } from "@projectstorm/react-core" ; import { HistoryState } from "./HistoryState" ; export interface HistoryBankListener extends BaseListener < HistoryBank > { forward ? ( event ) ; backward ? ( event ) ; } export class HistoryBank extends BaseObject < HistoryBankListener > { history : HistoryState [ ] ; pointer : number ; constructor ( ) { super ( ) ; this . history = [ ] ; this . pointer = 0 ; } pushState ( state ) { if ( _ . isEqual ( this . history [ this . pointer ] , state ) ) { return ; } this . pointer ++ ; this . history . splice ( this . pointer ) ; this . history . push ( state ) ; } goForward ( ) { if ( this . pointer === this . history . length - 1 ) { return ; } this . pointer ++ ; this . iterateListeners ( "history moved forward" , ( listener , event ) => { if ( listener . forward ) { listener . forward ( { ... event , state : this . history [ this . pointer ] } ) ; } } ) ; } goBackward ( ) { if ( this . pointer <= 0 ) { return ; } this . pointer -- ; this . iterateListeners ( "history moved backward" , ( listener , event ) => { if ( listener . backward ) { listener . backward ( { ... event , state : this . history [ this . pointer ] } ) ; } } ) ; } }