import type { AnyActorDefinition, BaseActorDefinition, } from "@/actor/definition"; import type { EventSchemaConfig, InferEventArgs, InferQueueCompleteMap, InferSchemaMap, QueueSchemaConfig, } from "@/actor/schema"; import type { QueueSendNoWaitOptions, QueueSendResult, QueueSendWaitOptions, } from "./queue"; /** * Action function returned by Actor connections and handles. * * @typedef {Function} ActorActionFunction * @template Args * @template Response * @param {...Args} args - Arguments for the action function. * @returns {Promise} */ export type ActorActionFunction< Args extends Array = unknown[], Response = unknown, > = ( ...args: Args extends [unknown, ...infer Rest] ? Rest : Args ) => Promise; type IsAny = 0 extends 1 & T ? true : false; type LooseEventSubscribe = ( eventName: string, callback: (...args: any[]) => void, ) => () => void; type ActorActionMap = { [K in keyof NonNullable]: NonNullable[K] extends ( ...args: infer Args ) => infer Return ? ActorActionFunction> : NonNullable[K] extends Record ? ActorActionMap[K]> : never; }; type ActionsOf = AD["config"] extends { actions?: infer R; } ? R : never; export interface ActorGatewayOptions { skipReadyWait?: boolean; } export type ResolvedActorGatewayOptions = Required; export function resolveActorGatewayOptions( defaults: ActorGatewayOptions = {}, overrides?: ActorGatewayOptions, ): ResolvedActorGatewayOptions { const skipReadyWait = overrides?.skipReadyWait ?? defaults.skipReadyWait ?? false; return { skipReadyWait, }; } export interface ActorActionOptions extends ActorGatewayOptions { signal?: AbortSignal; } export type ActorConnectOptions = ActorGatewayOptions; export type ActorFetchInit = RequestInit & ActorGatewayOptions; export type ActorWebSocketOptions = ActorGatewayOptions; /** * Maps action methods from actor definition to typed function signatures. */ export type ActorDefinitionActions = // biome-ignore lint/suspicious/noExplicitAny: safe to use any here IsAny extends true ? Record> : ActorActionMap>; type ActorQueueSend = { /** @deprecated Queues were deprecated in 3.0 and will be removed in a future version. Use actions and store pending work in SQLite (`c.db`). See https://rivet.dev/actors/docs/deprecated/queues */ ( name: K, body: InferSchemaMap[K], options: QueueSendWaitOptions, ): Promise[K]>>; /** @deprecated Queues were deprecated in 3.0 and will be removed in a future version. Use actions and store pending work in SQLite (`c.db`). See https://rivet.dev/actors/docs/deprecated/queues */ ( name: K, body: InferSchemaMap[K], options?: QueueSendNoWaitOptions, ): Promise; /** @deprecated Queues were deprecated in 3.0 and will be removed in a future version. Use actions and store pending work in SQLite (`c.db`). See https://rivet.dev/actors/docs/deprecated/queues */ ( name: keyof TQueues extends never ? string : never, body: unknown, options: QueueSendWaitOptions, ): Promise; /** @deprecated Queues were deprecated in 3.0 and will be removed in a future version. Use actions and store pending work in SQLite (`c.db`). See https://rivet.dev/actors/docs/deprecated/queues */ ( name: keyof TQueues extends never ? string : never, body: unknown, options?: QueueSendNoWaitOptions, ): Promise; }; type ActorEventSubscribe = { ( eventName: K, callback: (...args: InferEventArgs[K]>) => void, ): () => void; ( eventName: keyof TEvents extends never ? string : never, callback: (...args: any[]) => void, ): () => void; }; export type ActorDefinitionQueueSend = // biome-ignore lint/suspicious/noExplicitAny: safe to use any here IsAny extends true ? { send: ActorQueueSend> } : AD extends { config: { queues?: infer Q } } ? Q extends QueueSchemaConfig ? { send: ActorQueueSend } : {} : AD extends BaseActorDefinition< any, any, any, any, any, any, any, infer Q, any > ? Q extends QueueSchemaConfig ? { send: ActorQueueSend } : {} : {}; export type ActorDefinitionEventSubscriptions = // biome-ignore lint/suspicious/noExplicitAny: safe to use any here IsAny extends true ? { on: LooseEventSubscribe; once: LooseEventSubscribe; } : AD extends { config: { events?: infer E } } ? E extends EventSchemaConfig ? { on: ActorEventSubscribe; once: ActorEventSubscribe; } : {} : AD extends BaseActorDefinition< any, any, any, any, any, any, infer E, any, any > ? E extends EventSchemaConfig ? { on: ActorEventSubscribe; once: ActorEventSubscribe; } : {} : {};