import type { JsonObject, JsonValue } from "../json.js"; import type { ActorAlarm } from "./alarm.js"; import type { ActorContext } from "./context.js"; import { RuntimeActorContext } from "./context.js"; import type { ActorDatabase } from "./database.js"; import type { ActorSchema } from "./schema.js"; import type { ActorBroadcastOptions, ActorConnection, ActorSocket } from "./socket.js"; import type { ActorSchemas } from "./socketValidation.js"; declare const actorTypes: unique symbol; declare let actorContext: (value: object) => RuntimeActorContext | undefined; interface Actor { /** Accepts a joining connection on success. Call `socket.reject(4003, reason)` to deny it. */ onConnect?(socket: ActorSocket): Promise; /** Handles a parsed JSON message. */ onMessage?(socket: ActorSocket, message: Incoming): Promise; /** Runs after disconnection; may be skipped on server failure. */ onDisconnect?(socket: ActorSocket, code: number, reason: string, wasClean: boolean): Promise; /** Runs when the actor's scheduled alarm becomes due. The alarm is cleared afterward unless this callback sets a new one. */ onAlarm?(): Promise; } /** * Base class for durable actors. Extend directly; explicit constructors must pass their `ActorContext` to `super`. * Application methods must be async; instance fields require `@Persisted` or `@Ephemeral`. * Initialized private `ActorResource` fields are implicitly ephemeral. * @typeParam Metadata - Connection metadata supplied by the backend. * @typeParam Incoming - Application messages received by the actor. * @typeParam Outgoing - Application messages sent by the actor. * @typeParam Tag - Allowed connection tags. */ declare abstract class Actor { #private; /** @internal */ readonly [actorTypes]: { metadata: Metadata; incoming: Incoming; outgoing: Outgoing; tag: Tag; }; protected constructor(context: ActorContext); /** Actor-local SQLite. Statements autocommit unless grouped with `db.transaction`. */ get db(): ActorDatabase; /** The actor's single durable alarm slot. */ get alarm(): ActorAlarm; /** * Returns a backend reference. The first call starts the actor if needed. * @param actorId - 1–128 ASCII letters, digits, dots, underscores, or hyphens. */ static get(this: ValidActorClass, actorId: string): ActorReference; /** The bound actor ID, available from field initializers. */ protected get id(): string; /** * Lists connections during an actor call. Includes a joining socket; excludes a disconnected one. */ protected getConnections(tag?: Tag): Promise[]>; protected getConnectionCount(): Promise; /** Matches raw text frames and responds without waking the actor. Omit to clear. */ protected setWebSocketAutoResponse(pair?: { readonly request: string; readonly response: string; }): void; /** Sends JSON to matching connections, including the sender by default. Messages are not saved. */ protected broadcast(message: Outgoing, options?: ActorBroadcastOptions): void; } declare function registerActorClass(actorClass: ActorClass, state: ActorSchema): ActorDefinition; declare function findActorDefinition(actorName: string): ActorDefinition | undefined; interface ActorDefinition extends ActorClassDescription { state: ActorSchema; } interface ActorClassDescription { readonly actorName: string; readonly actorClass: ActorClass; readonly schemas: ActorSchemas; readonly methods: ReadonlySet; } type AnyActor = Actor; type ActorClass = Function & { readonly prototype: Instance; readonly schemas?: ActorSchemas; }; type AsyncMethod = (...args: never[]) => Promise; type InvalidActorMethod = { [Key in keyof Instance]-?: NonNullable extends (...args: never[]) => unknown ? NonNullable extends AsyncMethod ? never : Key : never; }[keyof Instance]; type ValidActorClass = InvalidActorMethod extends never ? TActorClass extends { readonly schemas: ActorSchemas, SocketIncoming, SocketOutgoing, SocketTag>; } ? TActorClass : TActorClass extends { readonly schemas: unknown; } ? never : TActorClass : never; type ActorReference = { [Key in keyof Instance as Instance[Key] extends AsyncMethod ? Key extends ActorLifecycleMethod ? never : Key : never]: Instance[Key]; } & { /** Opens a backend socket. Attach listeners immediately; acceptance may still be pending. */ connect(metadata: SocketMetadata): Promise, SocketOutgoing, JsonObject>>; /** Sends to all connections without running actor code. Messages are not saved. */ broadcast(message: SocketOutgoing): Promise; }; type ActorLifecycleMethod = "onConnect" | "onMessage" | "onDisconnect" | "onAlarm"; type SocketMetadata = Instance[typeof actorTypes]["metadata"]; type SocketIncoming = Instance[typeof actorTypes]["incoming"]; type SocketOutgoing = Instance[typeof actorTypes]["outgoing"]; type SocketTag = Instance[typeof actorTypes]["tag"]; /** Socket type for an actor instance type, for example `ActorSocketOf`. */ type ActorSocketOf = ActorSocket, SocketOutgoing, SocketTag>; /** Incoming message type for an actor instance type. */ type ActorMessageOf = SocketIncoming; export { Actor, actorContext, findActorDefinition, registerActorClass }; export type { ActorClass, ActorContext, ActorDefinition, ActorMessageOf, ActorReference, ActorSocketOf, AnyActor }; //# sourceMappingURL=actor.d.ts.map