import * as _vueuse_core0 from "@vueuse/core"; import { Ref, ShallowRef } from "vue"; //#region src/use-event-source.d.ts /** * EventSource 连接状态 * - CONNECTING: 连接中 * - OPEN: 已连接 * - CLOSED: 已关闭 */ type State = 'CONNECTING' | 'OPEN' | 'CLOSED'; /** * 自动重连配置 * @property retries - 重试次数,默认为 3 * @property delay - 重试延迟(毫秒),默认为 1000 * @property onFailed - 重试失败回调 */ type AutoRetry = boolean | { retries?: number; delay?: number; onFailed?: () => void; }; /** * 原始消息类型 */ type MessageRaw = string | ArrayBuffer | Blob; /** * 处理器类型映射 */ type HandlerType = { [key: string]: unknown; }; /** * useEventSource 选项配置 * @template T 处理器类型 * @example * const options: UseEventSourceOptions = { * manual: false, * autoRetry: true, * parseMessage: true, * handlerKey: 'type', * maxRecordSize: 100 * } */ type UseEventSourceOptions = EventSourceInit & { /** 是否手动连接,默认为 false */manual?: boolean; /** 自动重连配置,默认为 false */ autoRetry?: AutoRetry; /** * 消息解析配置 * - boolean: 是否解析为 JSON * - function: 自定义解析函数 * 默认为 false */ parseMessage?: boolean | ((raw: MessageRaw) => Record | Promise>); /** 处理器键名,默认为 'type' */ handlerKey?: string; /** 最大记录大小,默认为 100 */ maxRecordSize?: number; }; /** * EventSource 组合式函数 * 用于创建和管理 EventSource 连接的 Vue 组合式函数 * * @template T 处理器类型 * @template D 消息数据类型 * @param url EventSource 连接地址,支持响应式引用 * @param options 配置选项 * @returns EventSource 实例和相关状态、方法 * * @example * // 基本用法 * const { status, data, onMessage } = useEventSource('https://api.example.com/events') * * onMessage((event) => { * console.log('Received message:', event.data) * }) * * @example * // 带自动重连的用法 * const { status, connect } = useEventSource('https://api.example.com/events', { * autoRetry: { * retries: 5, * delay: 2000, * onFailed: () => console.error('Failed to connect after retries') * } * }) * * @example * // 带消息解析的用法 * const { registerHandler } = useEventSource<{ * update: { id: number; value: string } * error: { code: number; message: string } * }>('https://api.example.com/events', { * parseMessage: true * }) * * registerHandler('update', (data) => { * console.log('Update received:', data) * }) */ declare function useEventSource(url?: string | URL | Ref, options?: UseEventSourceOptions): { source: ShallowRef; url: Ref; status: Ref; data: Ref; dataRecord: Ref; messageEvent: Ref | null, MessageEvent | null>; messageEventRecord: Ref[], MessageEvent[]>; error: Ref; controller: ShallowRef; connect: (url?: string | URL, options?: EventSourceInit) => void; close: () => void; destroy: () => void; registerHandler: (type: K, handler: (data: T[K]) => void) => () => void; cancelHandler: (type: K, handler: (data: T[K]) => void) => void; registerEvent: (type: string, handler: (ev: MessageEvent) => void) => void; onOpen: _vueuse_core0.EventHookOn<[Event]>; onMessage: _vueuse_core0.EventHookOn<[MessageEvent]>; onError: _vueuse_core0.EventHookOn<[Event]>; onFailed: _vueuse_core0.EventHookOn<[Event]>; }; /** * useEventSource 返回类型 * 包含 EventSource 实例、状态、方法和事件钩子 * @example * const es: UseEventSourceReturns = useEventSource('https://api.example.com/events') */ type UseEventSourceReturns = ReturnType; //#endregion export { UseEventSourceOptions, UseEventSourceReturns, useEventSource };