@stacksjs/stx
Version:
A performant UI Framework. Powered by Bun.
77 lines • 2.1 kB
TypeScript
/**
* Check if EventSource is supported
*/
export declare function isEventSourceSupported(): boolean;
/**
* Create a reactive Server-Sent Events connection
*
* @example
* ```ts
* const sse = useEventSource('/api/events', {
* events: ['notification', 'update'],
* onMessage: (event) => console.log('Message:', event.data),
* })
*
* // Subscribe to state changes
* sse.subscribe((state) => {
* console.log('Status:', state.status)
* console.log('Last data:', state.data)
* })
*
* // Listen to specific events
* sse.on('notification', (event) => {
* console.log('Notification:', event.data)
* })
*
* // Close connection
* sse.close()
* ```
*/
export declare function useEventSource<T = unknown>(url: string | URL, options?: EventSourceOptions): EventSourceRef<T>;
/**
* Simple SSE connection for receiving JSON data
*
* @example
* ```ts
* const { data, status, close } = useSSE<{ count: number }>('/api/counter')
*
* data.subscribe((value) => {
* console.log('Count:', value?.count)
* })
* ```
*/
export declare function useSSE<T = unknown>(url: string): void;
export declare interface EventSourceState<T = unknown> {
status: EventSourceStatus
data: T | null
event: string | null
lastEventId: string | null
error: Event | null
}
export declare interface EventSourceOptions {
withCredentials?: boolean
autoReconnect?: boolean | {
retries?: number
delay?: number
onFailed?: () => void
}
immediate?: boolean
events?: string[]
onOpen?: (event: Event) => void
onMessage?: (event: MessageEvent) => void
onError?: (event: Event) => void
}
export declare interface EventSourceRef<T = unknown> {
get: () => EventSourceState<T>
subscribe: (fn: (state: EventSourceState<T>) => void) => () => void
open: () => void
close: () => void
eventSource: () => EventSource | null
on: (eventName: string, callback: (event: MessageEvent) => void) => () => void
}
/**
* Event Source (SSE) Composables
*
* Reactive utilities for Server-Sent Events.
*/
export type EventSourceStatus = 'connecting' | 'open' | 'closed' | 'error'