import type { z } from "zod"; import { EventSpec } from "./internal/service-spec.js"; import type { Subscription, SubscriptionRuntimeProps } from "./subscription.js"; /** * An EventPayload is the data sent as an event. * * It must be an object. Properties can be any type serializable as JSON. */ export interface EventPayload { [propName: string]: any; } export type EventPayloadType> = E extends Event ? Payload : never; /** * An envelope object containing the {@link event} payload associated * with its unique {@link name}. * * This envelope decouples the {@link name} fro the payload so that * there are no impositions on the structure of an event. */ export interface EventEnvelope { /** * Unique name identifying the type of the {@link event}. */ name: string; /** * The {@link EventPayload}. */ event: E; } /** * An {@link Event} is an object representing the declaration of an event * that belongs within the service. An {@link Event} has a unique {@link name}, * may be {@link emit}ed and {@link onEvent}d to. */ export interface Event extends Omit { kind: "Event"; schema?: z.Schema; /** * Subscribe to this event. The {@link handler} will be invoked every * time an event with this name is emitted within the service boundary. * * @param handler the handler function that will process the event. */ onEvent(name: Name, handler: EventHandlerFunction): Subscription; onEvent(name: Name, props: SubscriptionRuntimeProps, handlers: EventHandlerFunction): Subscription; /** * Emit events of this type within the service boundary. * * @param events a list of events to emit. */ emit(...events: E[]): Promise; } /** * A Function that processes an {@link event} of type {@link E}. */ export type EventHandlerFunction = (event: E) => Promise; /** * Declares an event that can be emitted and subscribed to. * * To declare an {@link Event}, define an interface describing the type * of the payload and then declare an event object giving it a unique name. * ```ts * interface CheckoutEvent { * customerId: string; * cartId: string; * timestamp: string; * } * * const checkoutEvent = event("Checkout"); * ``` * * To emit events, call the `emit` method: * ```ts * const checkoutWorkflow = workflow("checkoutWorkflow", async (request) => { * await checkoutEvent.emit({ * customerId: request.customerId, * cartId: request.cartId, * timestamp: new Date().toTimeString() * }); * }) * ``` * * To subscribe to events, call the `on` method. This will register a * handler that wil lbe invoked for every event of this type that is received. * * ```ts * checkoutEvent.onEvent("onCheckoutEvent", async (checkout) => { * console.log(checkout); * }); * ``` * * @param name a unique name that identifies this event type within the Service. * @param schema an optional zod schema describing the allowed data. * @returns an {@link Event} */ export declare function event(name: string, schema?: z.Schema): Event; //# sourceMappingURL=event.d.ts.map