import type { JsonValue } from "../json.js"; import type { HostSocketEffect, SocketConnection, SocketMessage, SocketSource } from "./socketProtocol.js"; import type { ActorSchemas, ActorStateMessage, ActorStateUpdate } from "./socketValidation.js"; type ActorSocketState = "connecting" | "open" | "closed"; /** A JSON value, encoded automatically when sent. */ type ActorSocketMessage = JsonValue; /** Actor-side socket. Metadata and tags last only for this connection. */ interface ActorSocket { readonly id: string; /** Assign the whole value to update it. Maximum: 16 KiB of JSON. */ metadata: Metadata; readonly tags: readonly Tag[]; readonly state: ActorSocketState; /** Sends JSON. Throws if closed. Delivery does not confirm saved state, and messages may arrive out of order. */ send(message: Outgoing): void; /** * @param code - Defaults to 1000; accepts 1000 or application codes 3000–4999. * @param reason - Defaults to an empty string; at most 123 UTF-8 bytes. */ close(code?: number, reason?: string): void; /** * Rejects a connection during `onConnect`. Pass an explicit code, such as 4003. * @param code - Use 3000–4999; the default 1008 is currently rejected. * @param reason - Defaults to "connection rejected"; at most 123 UTF-8 bytes. */ reject(code?: number, reason?: string): void; /** Replaces tags. Maximum: 10 unique tags, 1–256 Unicode characters each. */ setTags(...tags: Tag[]): void; } /** Filters connections for a broadcast. */ interface ActorBroadcastOptions { /** Excludes the selected connections. */ readonly except?: Pick | readonly Pick[]; /** Empty or omitted selects all connections. */ readonly tags?: readonly Tag[]; /** Defaults to "all". */ readonly tagMatch?: "all" | "any"; } /** * Backend socket with automatic JSON encoding. Attach listeners after `connect()`. * Reconnect in your application; messages are not replayed. */ interface ActorConnection { /** WebSocket state: 0 connecting, 1 open, 2 closing, 3 closed. */ readonly readyState: number; /** Sends JSON while open. Pass objects directly. */ send(data: Send): void; /** Reasons are limited to 123 UTF-8 bytes. */ close(code?: number, reason?: string): void; /** The `open` event normally fires before `connect()` resolves. */ addEventListener>(type: Type, listener: (event: ActorConnectionEventMap[Type]) => void): void; removeEventListener>(type: Type, listener: (event: ActorConnectionEventMap[Type]) => void): void; } interface ActorConnectionEventMap { readonly open: { readonly type: "open"; }; readonly message: { readonly type: "message"; readonly data: Receive | ActorStateMessage | ActorStateUpdate; }; readonly close: { readonly type: "close"; readonly code: number; readonly reason: string; readonly wasClean: boolean; }; readonly error: { readonly type: "error"; }; } declare function actorConnections(instance: object, tag?: string): Promise[]>; declare function broadcastActor(instance: object, message: unknown, options?: ActorBroadcastOptions): void; declare function runWithActorSockets(instance: object, connections: readonly SocketConnection[] | SocketSource, operation: (scope: ActorSocketScope) => Promise, publish?: (effects: readonly HostSocketEffect[]) => void, schemas?: ActorSchemas, eventConnections?: readonly SocketConnection[]): Promise<{ readonly value: T; readonly effects: readonly HostSocketEffect[]; }>; declare class ActorSocketScope { private readonly connections; readonly effects: Pick; private readonly schemas; private readonly byId; private readonly loading; private querying; constructor(connections: readonly SocketConnection[] | SocketSource, effects: Pick, schemas: ActorSchemas, eventConnections: readonly SocketConnection[]); getConnections(tag?: string): Promise; getConnectionCount(): Promise; setWebSocketAutoResponse(pair?: { readonly request: string; readonly response: string; }): void; settle(): Promise; private query; eventSocket(connection: SocketConnection, state: ActorSocketState): RuntimeActorSocket; connection(connectionId: string): RuntimeActorSocket; broadcast(message: unknown, options?: ActorBroadcastOptions): void; private wrap; } declare class RuntimeActorSocket implements ActorSocket { private readonly effects; private readonly schemas; private stateValue; private metadataValue; private tagsValue; constructor(connection: SocketConnection, effects: Pick, schemas: ActorSchemas, stateValue?: ActorSocketState); readonly id: string; get state(): ActorSocketState; get metadata(): Metadata; set metadata(value: Metadata); get tags(): readonly string[]; send(message: ActorSocketMessage): void; close(code?: number, reason?: string): void; reject(code?: number, reason?: string): void; setTags(...tags: string[]): void; setState(state: ActorSocketState): void; } declare function socketMessage(message: unknown, schemas?: ActorSchemas): SocketMessage; declare function decodeSocketMessage(message: SocketMessage, schemas?: ActorSchemas): ActorSocketMessage; declare function actorConnectionCount(instance: object): Promise; declare function setActorAutoResponse(instance: object, pair?: { readonly request: string; readonly response: string; }): void; export { actorConnectionCount, setActorAutoResponse, actorConnections, broadcastActor, decodeSocketMessage, runWithActorSockets, socketMessage }; export type { ActorSocketScope, ActorBroadcastOptions, ActorConnection, ActorConnectionEventMap, ActorSocket, ActorSocketMessage, ActorSocketState }; //# sourceMappingURL=socket.d.ts.map