state-history
A basic state history manager.
Install
npm i state-history
Basic usage
;// orconst StateHistory = ; // create historyconst history = ; // push some stateshistory;history;history; // current history is { a: 3 } history; // current history is now { a: 2 }history; // current history is now { a: 3 }history; // current history is now { a: 1 } // listen to history changeshistory;history; // triggers an history changehistory; // also triggers an history change
Properties
// number of past states in the history// it should be checked to be greater than 0 before calling `goPrev`numPrev: number; // number of future states in the history// it should be checked to be greater than 0 before calling `goNext`numNext: number;
Methods
// push a new entry onto the history stack// and returns itpushstate: T: T; // moves the pointer in the history stack to the previous entry// and returns the new current state// it is equivalent to `go(-1)`goPrev: T; // moves the pointer in the history stack to the next entry// and returns the new current state// it is equivalent to `go(1)`goNext: T; // moves the pointer in the history stack to the last entry// and returns the new current state// it is equivalent to `go(numNext)`goLast: T; // moves the pointer in the history stack by `i` entries// a negative number will go to the `-i`th previous entry// and a positive number will go to the `i`th next entrygoi: number: T; // get a specific entry from the history stack// ie: `-1` for the previous entry, `1` for the next entrygeti: number: T; // register a callback function that will be called every time// a new entry is added or when the pointer in the history stack is movedsubscribecallback:void: void; // unregister a callback functionunsubscribecallback:void: void; // unregister all callback functionsunsubscribeAll: void; // remove all entries from the state historyclear: void