TermUI.Component.StatePersistence (TermUI v0.2.0)
View SourceETS-based state persistence for crash recovery.
This module allows components to persist their state before crashes and recover it on restart. State is stored in an ETS table that survives component process crashes.
Usage
# Persist state (typically called on state changes)
StatePersistence.persist(:my_component, state)
# Recover state on restart
case StatePersistence.recover(:my_component) do
{:ok, state} -> {:ok, state}
:not_found -> {:ok, initial_state}
end
# Clear persisted state
StatePersistence.clear(:my_component)
Summary
Functions
Returns a specification to start this module under a supervisor.
Clears persisted state for a component.
Clears all persisted state.
Clears restart history for a component.
Returns the count of persisted states.
Gets metadata about persisted state.
Gets the restart count for a component within the time window.
Lists all component IDs with persisted state.
Persists component state to ETS.
Records restart event for a component.
Recovers persisted state for a component.
Checks if restart intensity limit has been reached.
Sets restart intensity limits for a component.
Starts the state persistence server.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec clear(term()) :: :ok
Clears persisted state for a component.
Parameters
component_id- Component identifier
Returns
:ok- State cleared (or was not present)
@spec clear_all() :: :ok
Clears all persisted state.
Mainly useful for testing.
@spec clear_restart_history(term()) :: :ok
Clears restart history for a component.
@spec count() :: non_neg_integer()
Returns the count of persisted states.
Gets metadata about persisted state.
Returns
{:ok, metadata}- Metadata including persisted_at timestamp:not_found- No state persisted for this component
@spec get_restart_count(term()) :: non_neg_integer()
Gets the restart count for a component within the time window.
@spec list_persisted() :: [term()]
Lists all component IDs with persisted state.
Persists component state to ETS.
Parameters
component_id- Component identifierstate- State to persistopts- Options:props- Original props for last_props recovery mode
Returns
:ok- State persisted successfully
@spec record_restart(term()) :: :ok
Records restart event for a component.
Used for tracking restart counts and detecting restart storms.
Recovers persisted state for a component.
Parameters
component_id- Component identifiermode- Recovery mode (default::last_state):last_state- Return the full persisted state:last_props- Return only the persisted props:reset- Return :not_found (forces re-initialization)
Returns
{:ok, state}- State found and returned:not_found- No state persisted for this component
Checks if restart intensity limit has been reached.
Returns
true- Restart limit exceededfalse- Within limits
@spec set_restart_limits(term(), non_neg_integer(), non_neg_integer()) :: :ok
Sets restart intensity limits for a component.
Parameters
component_id- Component identifiermax_restarts- Maximum restarts allowedmax_seconds- Time window in seconds
@spec start_link(keyword()) :: GenServer.on_start()
Starts the state persistence server.