RecoverableState <TStateModel>
Index
Constructors
constructor
Initialize a new recoverable state object.
Parameters
options: RecoverableStateOptions<TStateModel>
Configuration options for the recoverable state
Returns RecoverableState<TStateModel>
Accessors
currentValue
Get the current state.
Returns TStateModel
Methods
initialize
Initialize the recoverable state.
This method must be called before using the recoverable state. It loads the saved state if persistence is enabled and registers the object to listen for PERSIST_STATE events.
Returns Promise<TStateModel>
The loaded state object
persistState
Persist the current state to the KeyValueStore.
This method is typically called in response to a PERSIST_STATE event, but can also be called directly when needed.
Parameters
optionaleventData: { isMigrating: boolean }
Optional data associated with a PERSIST_STATE event
isMigrating: boolean
Returns Promise<void>
reset
Reset the state to the default values and clear any persisted state.
Resets the current state to the default state and, if persistence is enabled, clears the persisted state from the KeyValueStore.
Returns Promise<void>
teardown
Clean up resources used by the recoverable state.
If persistence is enabled, this method deregisters the object from PERSIST_STATE events and persists the current state one last time.
Returns Promise<void>
A class for managing persistent recoverable state using a plain JavaScript object.
This class facilitates state persistence to a
KeyValueStore
, allowing data to be saved and retrieved across migrations or restarts. It manages the loading, saving, and resetting of state data, with optional persistence capabilities.The state is represented by a plain JavaScript object that can be serialized to and deserialized from JSON. The class automatically hooks into the event system to persist state when needed.