Class: StatusUpdateManager
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:125
Manages status update subscriptions and event handling with efficient incremental updates.
Remarksโ
Provides a centralized manager for subscribing to and handling real-time status updates from the backend. Handles IPC event management and cleanup automatically. Prioritizes incremental updates over full syncs for better performance.
Exampleโ
const manager = new StatusUpdateManager({
fullResyncSites: () => syncSites(),
getSites: () => store.getSites(),
setSites: (sites) => store.setSites(sites),
});
manager.subscribe();
Constructorsโ
Constructorโ
new StatusUpdateManager(options: StatusUpdateHandlerOptions): StatusUpdateManager;
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:505
Constructs a new StatusUpdateManager instance.
Parametersโ
optionsโ
Configuration options for the status update manager
Returnsโ
StatusUpdateManager
Remarksโ
Initializes the manager with the required dependencies for status update handling. Does not start listening for events until subscribe() is called.
Propertiesโ
EXPECTED_LISTENER_COUNTโ
readonly static EXPECTED_LISTENER_COUNT: number = STATUS_UPDATE_LISTENER_COUNT;
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:127
Number of listeners required for a healthy subscription.
cleanupFunctionsโ
private cleanupFunctions: () => void[] = [];
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:140
Internal
Array of cleanup functions for active event listeners.
Returnsโ
void
Remarksโ
Stores cleanup functions returned by IPC event listeners. These are called during unsubscribe() to properly remove event listeners and prevent memory leaks. Each function removes a specific event listener.
fullResyncSitesโ
private readonly fullResyncSites: () => Promise<void>;
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:152
Internal
Function to trigger full site data sync from backend.
Returnsโ
Promise<void>
Remarksโ
Callback function provided during construction that performs a complete refresh of site data from the backend. Used as fallback when incremental updates fail or when a full sync is required.
getSitesโ
private readonly getSites: () => Site[];
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:164
Internal
Function to get current sites from store.
Returnsโ
Site[]
Remarksโ
Callback function that returns the current array of sites from the store state. Used to access current data when applying incremental status updates.
isListenerAttachedโ
private isListenerAttached: boolean = false;
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:176
Internal
Flag tracking whether event listener is currently attached.
Remarksโ
Boolean flag that tracks the subscription state. True when IPC event listeners are active and receiving status updates. Used to prevent duplicate subscriptions and to provide subscription status.
subscriptionGenerationโ
private subscriptionGeneration: number = 0;
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:179
Monotonic token used to invalidate asynchronous subscription setup.
onUpdateโ
private readonly onUpdate:
| ((update: StatusUpdate) => void)
| undefined;
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:191
Internal
Optional callback for status update notifications.
Remarksโ
Optional callback function that is invoked when a status update is successfully applied. Receives the StatusUpdate object with details about the change. Can be used for logging or triggering side effects.
setSitesโ
private readonly setSites: (sites: Site[]) => void;
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:203
Internal
Function to update sites in the store.
Parametersโ
sitesโ
Site[]
Returnsโ
void
Remarksโ
Callback function that updates the store with a new array of sites. Used to apply incremental status updates and full sync results to the store state.
Methodsโ
handleIncrementalStatusUpdate()โ
private handleIncrementalStatusUpdate(event: MonitorStatusChangedEvent): Promise<void>;
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:217
Internal
Handle incremental status updates efficiently using complete event data.
Parametersโ
eventโ
Monitor status changed event with complete data
Returnsโ
Promise<void>
Remarksโ
Applies status changes using the complete monitor and site objects from the backend event. This preserves the updated history and ensures check counts increment correctly while providing real-time updates.
attemptFullResync()โ
private attemptFullResync(reason: string): Promise<void>;
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:274
Attempts a full resync and guarantees the caller never throws.
Parametersโ
reasonโ
string
Returnsโ
Promise<void>
subscribe()โ
subscribe(): Promise<StatusUpdateSubscriptionResult>;
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:302
Subscribe to status updates from the backend with efficient incremental processing.
Returnsโ
Promise<StatusUpdateSubscriptionResult>
Remarksโ
Sets up IPC event listeners for monitor status changes and monitoring lifecycle events. Automatically performs an initial full sync when subscribing. Prioritizes incremental updates over full syncs for better performance.
Exampleโ
manager.subscribe();
processStatusUpdateCandidate()โ
private processStatusUpdateCandidate(candidate: unknown, source: string): Promise<void>;
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:425
Process an unknown candidate payload that may represent an enriched monitor status change.
Parametersโ
candidateโ
unknown
sourceโ
string
Returnsโ
Promise<void>
Remarksโ
This method centralizes:
- Type guard checks
- Development diagnostics
- Fallback to full resync when payloads are incomplete
- Error containment so listener callbacks never throw
startStatusUpdateProcessing()โ
private startStatusUpdateProcessing(candidate: unknown, source: string): void;
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:460
Parametersโ
candidateโ
unknown
sourceโ
string
Returnsโ
void
recordSubscriptionError()โ
private recordSubscriptionError(
errors: string[],
scope: string,
error: unknown): Error;
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:485
Records a subscription-related error in a consistent format.
Parametersโ
errorsโ
string[]
scopeโ
string
errorโ
unknown
Returnsโ
Remarksโ
The returned error instance is the normalized Error so callers can reuse it for structured logging.
getExpectedListenerCount()โ
getExpectedListenerCount(): number;
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:517
Retrieves the expected listener count for diagnostics.
Returnsโ
number
Number of listeners the manager attempts to attach.
isSubscribed()โ
isSubscribed(): boolean;
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:529
Check if currently subscribed to status updates.
Returnsโ
boolean
True if subscribed and listening for events, false otherwise
Remarksโ
Returns true when event listeners are active.
unsubscribe()โ
unsubscribe(): void;
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:551
Unsubscribe from all status update events.
Returnsโ
void
Remarksโ
Cleans up all IPC event listeners and resets internal state. Safe to call multiple times - will not throw if already unsubscribed.
Exampleโ
import { logger } from "@app/services/logger";
manager.unsubscribe();
logger.info("Subscription active?", {
isSubscribed: manager.isSubscribed(),
}); // false
cleanupListeners()โ
private cleanupListeners(cleanupFunctions: readonly () => void[]): void;
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:564
Parametersโ
cleanupFunctionsโ
readonly () => void[]
Returnsโ
void
getCurrentSubscriptionResult()โ
private getCurrentSubscriptionResult(listenerDescriptors: readonly StatusUpdateListenerDescriptor[]): StatusUpdateSubscriptionResult;
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:577
Parametersโ
listenerDescriptorsโ
readonly StatusUpdateListenerDescriptor[]
Returnsโ
StatusUpdateSubscriptionResult
handleMonitoringLifecycleEvent()โ
Call Signatureโ
private handleMonitoringLifecycleEvent(phase: "started", event: MonitoringStartedEventData): void;
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:599
Parametersโ
phaseโ
"started"
eventโ
Returnsโ
void
Call Signatureโ
private handleMonitoringLifecycleEvent(phase: "stopped", event: MonitoringStoppedEventData): void;
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:604
Parametersโ
phaseโ
"stopped"
eventโ
Returnsโ
void
applyMonitorStatusUpdate()โ
private applyMonitorStatusUpdate(sites: Site[], event: MonitorStatusChangedEvent): Site[];
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:639
Internal
Apply monitor status update using smart merging of fresh monitor data.
Parametersโ
sitesโ
Site[]
Current sites array
eventโ
Status change event with fresh monitor data
Returnsโ
Site[]
Updated sites array
Remarksโ
Uses the fresh monitor object from the backend event to update the specific monitor while preserving the existing site structure and other monitors. This ensures updated history and response times are applied without losing site-level context.
isMonitorStatusChangedEvent()โ
private isMonitorStatusChangedEvent(data: unknown): data is MonitorStatusChangedEvent;
Defined in: src/stores/sites/utils/statusUpdateHandler.ts:661
Internal
Type guard to validate incoming data as complete MonitorStatusChangedEvent.
Parametersโ
dataโ
unknown
Unknown data from IPC events
Returnsโ
data is MonitorStatusChangedEvent
True if data conforms to MonitorStatusChangedEvent interface
Remarksโ
Performs structural validation to ensure the data has the expected shape for incremental processing with complete monitor and site objects. This helps prevent runtime errors from malformed data.