Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions src/lib/sovd-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1738,26 +1738,44 @@ export class SovdApiClient {
* @param onError Callback for errors
* @returns Cleanup function to close the connection
*/
subscribeFaultStream(onFault: (fault: Fault) => void, onError?: (error: Error) => void): () => void {
subscribeFaultStream(
onFaultConfirmed: (fault: Fault) => void,
onFaultCleared: (fault: Fault) => void,
onError?: (error: Error) => void
): () => void {
const eventSource = new EventSource(this.getUrl('faults/stream'));

eventSource.onmessage = (event) => {
const parseFault = (event: MessageEvent): Fault | null => {
try {
// API may return raw fault format that needs transformation
// API returns: { event_type, fault, timestamp }
const rawData = JSON.parse(event.data);
// Check if this is the raw API format (has fault_code) or already transformed
if ('fault_code' in rawData) {
const fault = this.transformFault(rawData);
onFault(fault);
} else {
// Already in Fault format
onFault(rawData as Fault);
const faultData = rawData.fault || rawData;
if ('fault_code' in faultData) {
return this.transformFault(faultData);
}
return faultData as Fault;
} catch {
onError?.(new Error('Failed to parse fault event'));
return null;
}
};

eventSource.addEventListener('fault_confirmed', (event: MessageEvent) => {
const fault = parseFault(event);
if (fault) onFaultConfirmed(fault);
});

eventSource.addEventListener('fault_cleared', (event: MessageEvent) => {
const fault = parseFault(event);
if (fault) onFaultCleared(fault);
});

// Fallback for unnamed events - treat as confirmed
eventSource.onmessage = (event: MessageEvent) => {
const fault = parseFault(event);
if (fault) onFaultConfirmed(fault);
};

eventSource.onerror = () => {
onError?.(new Error('Fault stream connection error'));
};
Expand Down
17 changes: 17 additions & 0 deletions src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,10 @@ export const useAppStore = create<AppState>()(

// Load root entities after successful connection
await get().loadRootEntities();

// Subscribe to fault stream for real-time toast notifications
get().subscribeFaultStream();

return true;
} catch (error) {
const message = error instanceof Error ? error.message : 'Connection failed';
Expand All @@ -573,6 +577,9 @@ export const useAppStore = create<AppState>()(
// Stop execution polling
get().stopExecutionPolling();

// Unsubscribe from fault stream
get().unsubscribeFaultStream();

set({
serverUrl: null,
baseEndpoint: '',
Expand Down Expand Up @@ -1392,6 +1399,7 @@ export const useAppStore = create<AppState>()(
}

const cleanup = client.subscribeFaultStream(
// onFaultConfirmed
(fault) => {
const { faults } = get();
// Add or update fault in the list
Expand All @@ -1407,6 +1415,15 @@ export const useAppStore = create<AppState>()(
}
toast.warning(`Fault: ${fault.message}`, { autoClose: 5000 });
},
// onFaultCleared - no toast here, clearFault() already shows one for UI-triggered clears
(fault) => {
const { faults } = get();
const newFaults = faults.filter(
(f) => !(f.code === fault.code && f.entity_id === fault.entity_id)
);
set({ faults: newFaults });
},
// onError
(error) => {
toast.error(`Fault stream error: ${error.message}`);
}
Expand Down