UNPKG

crann

Version:

Effortless State Synchronization for Web Extensions

66 lines (65 loc) 2.21 kB
/** * StateManager - Handles in-memory state operations * * Responsibilities: * - Maintain shared state and per-agent state * - Handle state updates with change detection * - Initialize default values from config */ import { ConfigSchema, ValidatedConfig, DerivedState, DerivedSharedState, DerivedAgentState } from "./types"; import { Persistence } from "./Persistence"; export declare class StateManager<TConfig extends ConfigSchema> { private readonly config; private readonly persistence; private sharedState; private readonly agentStates; private readonly defaultSharedState; private readonly defaultAgentState; constructor(config: ValidatedConfig<TConfig>, persistence: Persistence<TConfig>); /** * Get the full derived state (shared only, from store perspective). */ getState(): DerivedState<TConfig>; /** * Get shared state only. */ getSharedState(): DerivedSharedState<TConfig>; /** * Get agent-scoped state for a specific agent. */ getAgentState(agentId: string): DerivedAgentState<TConfig>; /** * Get full state for an agent (shared + agent-scoped). */ getFullStateForAgent(agentId: string): DerivedState<TConfig>; /** * Update state, separating shared and agent-scoped changes. * Returns the changes that were actually applied. */ setState(state: Partial<DerivedState<TConfig>>, agentId?: string): { sharedChanges: Partial<DerivedSharedState<TConfig>>; agentChanges: Partial<DerivedAgentState<TConfig>>; }; /** * Update agent-scoped state only. */ setAgentState(agentId: string, state: Partial<DerivedAgentState<TConfig>>): void; /** * Hydrate shared state from storage. */ hydrateSharedState(state: Partial<DerivedSharedState<TConfig>>): void; /** * Initialize state for a new agent. */ initializeAgentState(agentId: string): void; /** * Remove state for a disconnected agent. */ removeAgentState(agentId: string): void; /** * Clear all state back to defaults. */ clear(): void; private buildDefaultSharedState; private buildDefaultAgentState; }