@instantdb/core
Version:
Instant's core local abstraction
93 lines • 2.83 kB
TypeScript
export interface EventSourceType {
readonly url: string;
readonly readyState: number;
onopen: ((this: EventSourceType, ev: Event) => any) | null;
onmessage: ((this: EventSourceType, ev: MessageEvent) => any) | null;
onerror: ((this: EventSourceType, ev: Event) => any) | null;
close(): void;
}
export interface EventSourceConstructor {
OPEN: number;
CONNECTING: number;
CLOSED: number;
new (url: string): EventSourceType;
}
type Conn = EventSourceType | WebSocket;
type OpenEvent<T extends Conn> = {
target: Connection<T>;
};
type MessageData = {
op: string;
[key: string]: any;
};
type SendMessageData = {
'client-event-id': string;
[key: string]: any;
};
type MsgEvent<T extends Conn> = {
target: Connection<T>;
message: MessageData | MessageData[];
};
type CloseEvent<T extends Conn> = {
target: Connection<T>;
};
interface ErrorEvent<T extends Conn> {
target: Connection<T>;
}
export type TransportType = 'ws' | 'sse';
export interface Connection<T extends Conn> {
conn: T;
type: 'ws' | 'sse';
id: string;
close(): void;
isOpen(): boolean;
isConnecting(): boolean;
send(msg: SendMessageData): void;
onopen: ((event: OpenEvent<T>) => void) | null;
onmessage: ((event: MsgEvent<T>) => void) | null;
onclose: ((event: CloseEvent<T>) => void) | null;
onerror: ((event: ErrorEvent<T>) => void) | null;
}
export declare class WSConnection implements Connection<WebSocket> {
type: TransportType;
conn: WebSocket;
id: string;
onopen: (event: OpenEvent<WebSocket>) => void;
onmessage: (event: MsgEvent<WebSocket>) => void;
onclose: (event: CloseEvent<WebSocket>) => void;
onerror: (event: ErrorEvent<WebSocket>) => void;
constructor(url: string);
close(): void;
isOpen(): boolean;
isConnecting(): boolean;
send(msg: SendMessageData): void;
}
export declare class SSEConnection implements Connection<EventSourceType> {
type: TransportType;
private initParams;
private sendQueue;
private sendPromise;
private closeFired;
private sseInitTimeout;
private ES;
private messageUrl;
conn: EventSourceType;
url: string;
id: string;
onopen: (event: OpenEvent<EventSourceType>) => void;
onmessage: (event: MsgEvent<EventSourceType>) => void;
onclose: (event: CloseEvent<EventSourceType>) => void;
onerror: (event: ErrorEvent<EventSourceType>) => void;
constructor(ES: EventSourceConstructor, url: string, messageUrl?: string);
private handleMessage;
private handleError;
private handleClose;
private postMessages;
private flushQueue;
send(msg: SendMessageData): void;
isOpen(): boolean;
isConnecting(): boolean;
close(): void;
}
export {};
//# sourceMappingURL=Connection.d.ts.map