Appolo State
simple state manager for nodejs built with typescript
Installation
npm install appolo-state --save
Usage
Store(initialState: T = {},options = {maxStates: 1})
initialState
- any object
options
:
- maxStates
- max number of history states to hold default 1
let store = < counter: number >counter: 0 maxStates: 10;
setState(value: Partial<T>,options={arrayMerge:"extend"})
change the current to new state
event stateChanged
if fired on state changed
let store = < counter: number >counter: 0; store;
state():T
return copy of the current state
let store = < counter: number >counter: 0; store; let state = store
stateAt(index: number): T
return state copy at given index
let store = < counter: number >counter: 0maxStates:10; store;store; storecounter; // 1
get prevState():T
return the previous state
get nextState():T
return the next state
goToState(index: number)
index - the state index to change to
let store = < counter: number >counter: 0maxStates:10; store;store;store; storecounter // 1
goToPrevState()
go to previous state
goToNextState()
go to next state
Events
store extends appolo-event-dispatcher
every to time the state is changed the stateChanged
if fired
let store = < counter: number >counter: 0; store store;
Actions
you can use the action decorator to define custom action events the store will fire event with the action name when the action is finished
extend Store< counter: number > supercounter: 0 @ { this } @ async { await this } { let store = <>; store storemyCounter = 1; store; let state = await storeconsole // 2 }
Iterator
you can loop over all the states from the beginning iterator
let store = < counter: number >counter: 0; store;store; for let state of store console // 0 ,1 ,2
reset
let store = < counter: number >counter: 0; store;store; store storecounter // 0
Tests
npm run test
License
MIT