import { ActorRefFrom, AnyActorRef, EventObject } from "xstate"; /** @internal */ declare const DOMAIN = "sanity/comlink"; /** @internal */ declare const RESPONSE_TIMEOUT_DEFAULT = 3000; /** @internal */ declare const FETCH_TIMEOUT_DEFAULT = 10000; /** @internal */ declare const HEARTBEAT_INTERVAL = 1000; /** @internal */ declare const HANDSHAKE_INTERVAL = 500; /** * @public */ declare const MSG_RESPONSE = "comlink/response"; /** * @public */ declare const MSG_HEARTBEAT = "comlink/heartbeat"; /** @internal */ declare const MSG_DISCONNECT = "comlink/disconnect"; /** @internal */ declare const MSG_HANDSHAKE_SYN = "comlink/handshake/syn"; /** @internal */ declare const MSG_HANDSHAKE_SYN_ACK = "comlink/handshake/syn-ack"; /** @internal */ declare const MSG_HANDSHAKE_ACK = "comlink/handshake/ack"; /** @internal */ declare const HANDSHAKE_MSG_TYPES: string[]; /** @internal */ declare const INTERNAL_MSG_TYPES: string[]; /** * @public */ type Status = 'idle' | 'handshaking' | 'connected' | 'disconnected'; /** * @public */ type StatusEvent = { connection: string; status: Status; }; /** * @public */ type MessageType = string; /** * @public */ type MessageData = Record | undefined; /** * @public */ interface Message { type: MessageType; data?: MessageData; response?: MessageData; } /** * @public */ interface RequestData { data?: MessageData; expectResponse?: boolean; responseTo?: string; type: MessageType; resolvable?: PromiseWithResolvers; options?: { responseTimeout?: number; signal?: AbortSignal; suppressWarnings?: boolean; }; } /** * @public */ type WithoutResponse = Omit; /** * @public */ interface ListenInput { count?: number; domain: string; exclude: string[]; from: string; include: string[]; responseType: string; target: MessageEventSource | undefined; to: string; } /** * @public */ interface BufferAddedEmitEvent { type: 'buffer.added'; message: TMessage; } /** * @public */ interface BufferFlushedEmitEvent { type: 'buffer.flushed'; messages: TMessage[]; } /** * @public */ interface HeartbeatEmitEvent { type: 'heartbeat'; } /** * @public */ interface StatusEmitEvent { type: 'status'; status: Status; } type MessageEmitEvent = { type: 'message'; message: ProtocolMessage; }; /** * @public */ type InternalEmitEvent = BufferAddedEmitEvent | BufferFlushedEmitEvent | MessageEmitEvent | StatusEmitEvent; /** * @public */ type ProtocolMessage = { id: string; channelId: string; data?: TMessage['data']; domain: string; from: string; responseTo?: string; to: string; type: TMessage['type']; }; /** * @public */ interface ResponseMessage { type: typeof MSG_RESPONSE; data: MessageData; } /** * @public */ interface HeartbeatMessage { type: typeof MSG_HEARTBEAT; data: undefined; } /** * @internal */ interface DisconnectMessage { type: typeof MSG_DISCONNECT; data: undefined; } /** * @internal */ type HandshakeMessageType = typeof MSG_HANDSHAKE_ACK | typeof MSG_HANDSHAKE_SYN | typeof MSG_HANDSHAKE_SYN_ACK; /** * @internal */ type InternalMessageType = typeof MSG_DISCONNECT | typeof MSG_HANDSHAKE_ACK | typeof MSG_HANDSHAKE_SYN | typeof MSG_HANDSHAKE_SYN_ACK | typeof MSG_HEARTBEAT | typeof MSG_RESPONSE; /** * @public */ interface RequestMachineContext { channelId: string; data: MessageData | undefined; domain: string; expectResponse: boolean; from: string; id: string; parentRef: AnyActorRef; resolvable: PromiseWithResolvers | undefined; response: TSends['response'] | null; responseTimeout: number | undefined; responseTo: string | undefined; signal: AbortSignal | undefined; suppressWarnings: boolean | undefined; sources: Set; targetOrigin: string; to: string; type: MessageType; } /** * @public */ type RequestActorRef = ActorRefFrom>>; /** * @public */ declare const createRequestMachine: () => import("xstate").StateMachine, { type: 'message'; data: ProtocolMessage; } | { type: 'abort'; }, { "listen for response"?: import("xstate").ActorRefFromLogic>, { requestId: string; sources: Set; signal?: AbortSignal; }, import("xstate").EventObject>> | undefined; }, { src: "listen"; logic: import("xstate").ObservableActorLogic>, { requestId: string; sources: Set; signal?: AbortSignal; }, import("xstate").EventObject>; id: "listen for response"; }, { type: "on abort"; params: import("xstate").NonReducibleUnknown; } | { type: "on fail"; params: import("xstate").NonReducibleUnknown; } | { type: "on success"; params: import("xstate").NonReducibleUnknown; } | { type: "send message"; params: { message: ProtocolMessage; }; }, { type: "expectsResponse"; params: unknown; }, "initialTimeout" | "responseTimeout", "aborted" | "awaiting" | "failed" | "idle" | "sending" | "success", string, { channelId: string; data?: TSends['data']; domain: string; expectResponse?: boolean; from: string; parentRef: AnyActorRef; resolvable?: PromiseWithResolvers; responseTimeout?: number; responseTo?: string; signal?: AbortSignal; sources: Set | MessageEventSource; suppressWarnings?: boolean; targetOrigin: string; to: string; type: TSends['type']; }, { requestId: string; response: TSends['response'] | null; responseTo: string | undefined; }, { type: 'request.failed'; requestId: string; } | { type: 'request.aborted'; requestId: string; } | { type: 'request.success'; requestId: string; response: MessageData | null; responseTo: string | undefined; }, import("xstate").MetaObject, { states: { readonly idle: {}; readonly sending: {}; readonly awaiting: {}; readonly failed: {}; readonly success: {}; readonly aborted: {}; }; }>; /** * @public */ type ConnectionActorLogic = ReturnType>; /** * @public */ type ConnectionActor = ActorRefFrom>>; /** * @public */ type Connection = { actor: ConnectionActor; connect: () => void; disconnect: () => void; id: string; name: string; machine: ReturnType>; on: >(type: TType, handler: (data: TMessage['data']) => Promise | TMessage['response']) => () => void; onStatus: (handler: (status: Status) => void, filter?: Status) => () => void; post: >(...params: (TMessage['data'] extends undefined ? [TType] : never) | [TType, TMessage['data']]) => void; setTarget: (target: MessageEventSource) => void; start: () => () => void; stop: () => void; target: MessageEventSource | undefined; }; /** * @public */ interface ConnectionInput { connectTo: string; domain?: string; heartbeat?: boolean; name: string; id?: string; target?: MessageEventSource; targetOrigin: string; } /** * @public */ declare const createConnectionMachine: = WithoutResponse>() => import("xstate").StateMachine<{ buffer: Array; channelId: string; connectTo: string; domain: string; heartbeat: boolean; id: string; name: string; requests: Array>; target: MessageEventSource | undefined; targetOrigin: string; }, { type: 'connect'; } | { type: 'disconnect'; } | { type: 'message.received'; message: MessageEvent>; } | { type: 'post'; data: TSendsWithoutResponse; } | { type: 'response'; respondTo: string; data: Pick; } | { type: 'request.aborted'; requestId: string; } | { type: 'request.failed'; requestId: string; } | { type: 'request.success'; requestId: string; response: TSends['response'] | null; responseTo: string | undefined; } | { type: 'request'; data: RequestData | RequestData[]; } | { type: 'syn'; } | { type: 'target.set'; target: MessageEventSource; }, { [x: string]: import("xstate").ActorRefFromLogic, { type: 'message'; data: ProtocolMessage; } | { type: 'abort'; }, { "listen for response"?: import("xstate").ActorRefFromLogic>, { requestId: string; sources: Set; signal?: AbortSignal; }, EventObject>> | undefined; }, { src: "listen"; logic: import("xstate").ObservableActorLogic>, { requestId: string; sources: Set; signal?: AbortSignal; }, EventObject>; id: "listen for response"; }, { type: "on abort"; params: import("xstate").NonReducibleUnknown; } | { type: "on fail"; params: import("xstate").NonReducibleUnknown; } | { type: "on success"; params: import("xstate").NonReducibleUnknown; } | { type: "send message"; params: { message: ProtocolMessage; }; }, { type: "expectsResponse"; params: unknown; }, "initialTimeout" | "responseTimeout", "aborted" | "awaiting" | "failed" | "idle" | "sending" | "success", string, { channelId: string; data?: TSends["data"] | undefined; domain: string; expectResponse?: boolean; from: string; parentRef: import("xstate").AnyActorRef; resolvable?: PromiseWithResolvers | undefined; responseTimeout?: number; responseTo?: string; signal?: AbortSignal; sources: Set | MessageEventSource; suppressWarnings?: boolean; targetOrigin: string; to: string; type: TSends["type"]; }, { requestId: string; response: TSends["response"] | null; responseTo: string | undefined; }, { type: 'request.failed'; requestId: string; } | { type: 'request.aborted'; requestId: string; } | { type: 'request.success'; requestId: string; response: MessageData | null; responseTo: string | undefined; }, import("xstate").MetaObject, { states: { readonly idle: {}; readonly sending: {}; readonly awaiting: {}; readonly failed: {}; readonly success: {}; readonly aborted: {}; }; }>> | undefined; "listen for handshake"?: import("xstate").ActorRefFromLogic; }, ListenInput, EventObject>> | undefined; "listen for messages"?: import("xstate").ActorRefFromLogic; }, ListenInput, EventObject>> | undefined; "send heartbeat"?: import("xstate").ActorRefFromLogic> | undefined; "send syn"?: import("xstate").ActorRefFromLogic> | undefined; }, { src: "listen"; logic: import("xstate").ObservableActorLogic<{ type: string; message: MessageEvent; }, ListenInput, EventObject>; id: "listen for handshake" | "listen for messages"; } | { src: "requestMachine"; logic: import("xstate").StateMachine, { type: 'message'; data: ProtocolMessage; } | { type: 'abort'; }, { "listen for response"?: import("xstate").ActorRefFromLogic>, { requestId: string; sources: Set; signal?: AbortSignal; }, EventObject>> | undefined; }, { src: "listen"; logic: import("xstate").ObservableActorLogic>, { requestId: string; sources: Set; signal?: AbortSignal; }, EventObject>; id: "listen for response"; }, { type: "on abort"; params: import("xstate").NonReducibleUnknown; } | { type: "on fail"; params: import("xstate").NonReducibleUnknown; } | { type: "on success"; params: import("xstate").NonReducibleUnknown; } | { type: "send message"; params: { message: ProtocolMessage; }; }, { type: "expectsResponse"; params: unknown; }, "initialTimeout" | "responseTimeout", "aborted" | "awaiting" | "failed" | "idle" | "sending" | "success", string, { channelId: string; data?: TSends["data"] | undefined; domain: string; expectResponse?: boolean; from: string; parentRef: import("xstate").AnyActorRef; resolvable?: PromiseWithResolvers | undefined; responseTimeout?: number; responseTo?: string; signal?: AbortSignal; sources: Set | MessageEventSource; suppressWarnings?: boolean; targetOrigin: string; to: string; type: TSends["type"]; }, { requestId: string; response: TSends["response"] | null; responseTo: string | undefined; }, { type: 'request.failed'; requestId: string; } | { type: 'request.aborted'; requestId: string; } | { type: 'request.success'; requestId: string; response: MessageData | null; responseTo: string | undefined; }, import("xstate").MetaObject, { states: { readonly idle: {}; readonly sending: {}; readonly awaiting: {}; readonly failed: {}; readonly success: {}; readonly aborted: {}; }; }>; id: string | undefined; } | { src: "sendBackAtInterval"; logic: import("xstate").CallbackActorLogic; id: "send heartbeat" | "send syn"; }, { type: "buffer message"; params: import("xstate").NonReducibleUnknown; } | { type: "create request"; params: import("xstate").NonReducibleUnknown; } | { type: "emit received message"; params: import("xstate").NonReducibleUnknown; } | { type: "emit status"; params: { status: Status; }; } | { type: "post message"; params: import("xstate").NonReducibleUnknown; } | { type: "remove request"; params: import("xstate").NonReducibleUnknown; } | { type: "respond"; params: import("xstate").NonReducibleUnknown; } | { type: "send disconnect"; params: import("xstate").NonReducibleUnknown; } | { type: "send handshake ack"; params: import("xstate").NonReducibleUnknown; } | { type: "send handshake syn"; params: import("xstate").NonReducibleUnknown; } | { type: "send pending messages"; params: import("xstate").NonReducibleUnknown; } | { type: "set target"; params: import("xstate").NonReducibleUnknown; }, { type: "has target"; params: unknown; } | { type: "should send heartbeats"; params: unknown; }, never, "disconnected" | "handshaking" | "idle" | { connected: { heartbeat: "checking" | "sending"; }; }, string, ConnectionInput, import("xstate").NonReducibleUnknown, BufferAddedEmitEvent | BufferFlushedEmitEvent | MessageEmitEvent | StatusEmitEvent, import("xstate").MetaObject, { id: "connection"; states: { readonly idle: {}; readonly handshaking: { id: "handshaking"; }; readonly connected: { states: { readonly heartbeat: { states: { readonly checking: {}; readonly sending: {}; }; }; }; }; readonly disconnected: { id: "disconnected"; }; }; }>; /** * @public */ declare const createConnection: (input: ConnectionInput, machine?: ConnectionActorLogic) => Connection; /** * @public */ declare const createListenLogic: (compatMap?: (event: MessageEvent) => MessageEvent) => import("xstate").ObservableActorLogic<{ type: string; message: MessageEvent; }, ListenInput, import("xstate").EventObject>; /** * @public */ type ChannelInput = Omit; /** * @public */ interface ChannelInstance { on: >(type: TType, handler: (data: TMessage['data']) => Promise | TMessage['response']) => () => void; onInternalEvent: ['type'], TEvent extends Extract, { type: TType; }>>(type: TType, handler: (event: TEvent) => void) => () => void; onStatus: (handler: (event: StatusEvent) => void) => void; post: >(...params: (TMessage['data'] extends undefined ? [TType] : never) | [TType, TMessage['data']]) => void; start: () => () => void; stop: () => void; } /** * @public */ interface Controller { addTarget: (target: MessageEventSource) => () => void; createChannel: (input: ChannelInput, machine?: ConnectionActorLogic) => ChannelInstance; destroy: () => void; } /** * @public */ declare const createController: (input: { targetOrigin: string; }) => Controller; /** * @public */ interface NodeInput { name: string; connectTo: string; domain?: string; } /** * @public */ type NodeActorLogic = ReturnType>; /** * @public */ type NodeActor = ActorRefFrom>; /** * @public */ type Node = { actor: NodeActor; fetch: >(...params: (TMessage['data'] extends undefined ? [TType] : never) | [TType, TMessage['data']] | [TType, TMessage['data'], { signal?: AbortSignal; suppressWarnings?: boolean; }]) => TSends extends TMessage ? TSends['type'] extends TType ? Promise : never : never; machine: NodeActorLogic; on: >(type: TType, handler: (event: TMessage['data']) => TMessage['response']) => () => void; onStatus: (handler: (status: Exclude) => void, filter?: Exclude) => () => void; post: >(...params: (TMessage['data'] extends undefined ? [TType] : never) | [TType, TMessage['data']]) => void; start: () => () => void; stop: () => void; }; /** * @public */ declare const createNodeMachine: = WithoutResponse>() => import("xstate").StateMachine<{ buffer: Array<{ data: TSendsWithoutResponse; resolvable?: PromiseWithResolvers; options?: { signal?: AbortSignal; suppressWarnings?: boolean; }; }>; channelId: string | null; connectTo: string; domain: string; handshakeBuffer: Array<{ type: 'message.received'; message: MessageEvent>; }>; name: string; requests: Array>; target: MessageEventSource | undefined; targetOrigin: string | null; }, { type: 'heartbeat.received'; message: MessageEvent>; } | { type: 'message.received'; message: MessageEvent>; } | { type: 'handshake.syn'; message: MessageEvent>; } | { type: 'post'; data: TSendsWithoutResponse; resolvable?: PromiseWithResolvers; options?: { responseTimeout?: number; signal?: AbortSignal; suppressWarnings?: boolean; }; } | { type: 'request.aborted'; requestId: string; } | { type: 'request.failed'; requestId: string; } | { type: 'request.success'; requestId: string; response: TSends['response'] | null; responseTo: string | undefined; } | { type: 'request'; data: RequestData | RequestData[]; }, { [x: string]: import("xstate").ActorRefFromLogic, { type: 'message'; data: ProtocolMessage; } | { type: 'abort'; }, { "listen for response"?: import("xstate").ActorRefFromLogic>, { requestId: string; sources: Set; signal?: AbortSignal; }, import("xstate").EventObject>> | undefined; }, { src: "listen"; logic: import("xstate").ObservableActorLogic>, { requestId: string; sources: Set; signal?: AbortSignal; }, import("xstate").EventObject>; id: "listen for response"; }, { type: "on abort"; params: import("xstate").NonReducibleUnknown; } | { type: "on fail"; params: import("xstate").NonReducibleUnknown; } | { type: "on success"; params: import("xstate").NonReducibleUnknown; } | { type: "send message"; params: { message: ProtocolMessage; }; }, { type: "expectsResponse"; params: unknown; }, "initialTimeout" | "responseTimeout", "aborted" | "awaiting" | "failed" | "idle" | "sending" | "success", string, { channelId: string; data?: TSends["data"] | undefined; domain: string; expectResponse?: boolean; from: string; parentRef: import("xstate").AnyActorRef; resolvable?: PromiseWithResolvers | undefined; responseTimeout?: number; responseTo?: string; signal?: AbortSignal; sources: Set | MessageEventSource; suppressWarnings?: boolean; targetOrigin: string; to: string; type: TSends["type"]; }, { requestId: string; response: TSends["response"] | null; responseTo: string | undefined; }, { type: 'request.failed'; requestId: string; } | { type: 'request.aborted'; requestId: string; } | { type: 'request.success'; requestId: string; response: MessageData | null; responseTo: string | undefined; }, import("xstate").MetaObject, { states: { readonly idle: {}; readonly sending: {}; readonly awaiting: {}; readonly failed: {}; readonly success: {}; readonly aborted: {}; }; }>> | undefined; "listen for disconnect"?: import("xstate").ActorRefFromLogic; }, ListenInput, import("xstate").EventObject>> | undefined; "listen for handshake ack"?: import("xstate").ActorRefFromLogic; }, ListenInput, import("xstate").EventObject>> | undefined; "listen for handshake syn"?: import("xstate").ActorRefFromLogic; }, ListenInput, import("xstate").EventObject>> | undefined; "listen for heartbeat"?: import("xstate").ActorRefFromLogic; }, ListenInput, import("xstate").EventObject>> | undefined; "listen for messages"?: import("xstate").ActorRefFromLogic; }, ListenInput, import("xstate").EventObject>> | undefined; }, { src: "listen"; logic: import("xstate").ObservableActorLogic<{ type: string; message: MessageEvent; }, ListenInput, import("xstate").EventObject>; id: "listen for disconnect" | "listen for handshake ack" | "listen for handshake syn" | "listen for heartbeat" | "listen for messages"; } | { src: "requestMachine"; logic: import("xstate").StateMachine, { type: 'message'; data: ProtocolMessage; } | { type: 'abort'; }, { "listen for response"?: import("xstate").ActorRefFromLogic>, { requestId: string; sources: Set; signal?: AbortSignal; }, import("xstate").EventObject>> | undefined; }, { src: "listen"; logic: import("xstate").ObservableActorLogic>, { requestId: string; sources: Set; signal?: AbortSignal; }, import("xstate").EventObject>; id: "listen for response"; }, { type: "on abort"; params: import("xstate").NonReducibleUnknown; } | { type: "on fail"; params: import("xstate").NonReducibleUnknown; } | { type: "on success"; params: import("xstate").NonReducibleUnknown; } | { type: "send message"; params: { message: ProtocolMessage; }; }, { type: "expectsResponse"; params: unknown; }, "initialTimeout" | "responseTimeout", "aborted" | "awaiting" | "failed" | "idle" | "sending" | "success", string, { channelId: string; data?: TSends["data"] | undefined; domain: string; expectResponse?: boolean; from: string; parentRef: import("xstate").AnyActorRef; resolvable?: PromiseWithResolvers | undefined; responseTimeout?: number; responseTo?: string; signal?: AbortSignal; sources: Set | MessageEventSource; suppressWarnings?: boolean; targetOrigin: string; to: string; type: TSends["type"]; }, { requestId: string; response: TSends["response"] | null; responseTo: string | undefined; }, { type: 'request.failed'; requestId: string; } | { type: 'request.aborted'; requestId: string; } | { type: 'request.success'; requestId: string; response: MessageData | null; responseTo: string | undefined; }, import("xstate").MetaObject, { states: { readonly idle: {}; readonly sending: {}; readonly awaiting: {}; readonly failed: {}; readonly success: {}; readonly aborted: {}; }; }>; id: string | undefined; }, { type: "buffer handshake"; params: import("xstate").NonReducibleUnknown; } | { type: "buffer message"; params: import("xstate").NonReducibleUnknown; } | { type: "create request"; params: import("xstate").NonReducibleUnknown; } | { type: "emit heartbeat"; params: import("xstate").NonReducibleUnknown; } | { type: "emit received message"; params: import("xstate").NonReducibleUnknown; } | { type: "emit status"; params: { status: Exclude; }; } | { type: "post message"; params: import("xstate").NonReducibleUnknown; } | { type: "process pending handshakes"; params: import("xstate").NonReducibleUnknown; } | { type: "remove request"; params: import("xstate").NonReducibleUnknown; } | { type: "send handshake syn ack"; params: import("xstate").NonReducibleUnknown; } | { type: "send pending messages"; params: import("xstate").NonReducibleUnknown; } | { type: "send response"; params: import("xstate").NonReducibleUnknown; } | { type: "set connection config"; params: import("xstate").NonReducibleUnknown; }, { type: "hasSource"; params: unknown; }, never, "connected" | "handshaking" | "idle", string, NodeInput, import("xstate").NonReducibleUnknown, BufferAddedEmitEvent | BufferFlushedEmitEvent | HeartbeatEmitEvent | MessageEmitEvent | (StatusEmitEvent & { status: Exclude; }), import("xstate").MetaObject, { id: "node"; states: { readonly idle: {}; readonly handshaking: {}; readonly connected: {}; }; }>; /** * @public */ declare const createNode: (input: NodeInput, machine?: NodeActorLogic) => Node; export { type BufferAddedEmitEvent, type BufferFlushedEmitEvent, type ChannelInput, type ChannelInstance, type Connection, type ConnectionActor, type ConnectionActorLogic, type ConnectionInput, type Controller, DOMAIN, type DisconnectMessage, FETCH_TIMEOUT_DEFAULT, HANDSHAKE_INTERVAL, HANDSHAKE_MSG_TYPES, HEARTBEAT_INTERVAL, type HandshakeMessageType, type HeartbeatEmitEvent, type HeartbeatMessage, INTERNAL_MSG_TYPES, type InternalEmitEvent, type InternalMessageType, type ListenInput, MSG_DISCONNECT, MSG_HANDSHAKE_ACK, MSG_HANDSHAKE_SYN, MSG_HANDSHAKE_SYN_ACK, MSG_HEARTBEAT, MSG_RESPONSE, type Message, type MessageData, type MessageEmitEvent, type MessageType, type Node, type NodeActor, type NodeActorLogic, type NodeInput, type ProtocolMessage, RESPONSE_TIMEOUT_DEFAULT, type RequestActorRef, type RequestData, type RequestMachineContext, type ResponseMessage, type Status, type StatusEmitEvent, type StatusEvent, type WithoutResponse, createConnection, createConnectionMachine, createController, createListenLogic, createNode, createNodeMachine, createRequestMachine }; //# sourceMappingURL=index.d.ts.map