Skip to main content
Version: Next

RecoverableState <TStateModel>

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.

Index

Constructors

constructor

Accessors

currentValue

  • get currentValue(): TStateModel
  • Get the current state.


    Returns TStateModel

Methods

initialize

  • initialize(): Promise<TStateModel>
  • 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

  • persistState(eventData): Promise<void>
  • 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(): Promise<void>
  • 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

  • teardown(): Promise<void>
  • 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>