/** * `subscribe((when) => …)` — the listener primitive. * * A `Subscription` is a plain value: a closure that receives a `when` * function and registers listeners through it. The closure binds late — * at registration the App passes its own runtime's `when`, so each * subscription targets the right runtime. No ambient state, no * side-effect imports, multi-app safe. * * // app/listeners/welcome-mail.ts * import { subscribe } from "@nwire/app"; * import { UserRegistered } from "../events"; * * export default subscribe((when) => { * when(UserRegistered, async (user, ctx) => { * await ctx.send(sendWelcome({ email: user.email })); * }); * }); * * Subscriptions are registered through `app.subscribe(sub | subs)`. */ import type { EventDefinition, EventPayload } from "@nwire/messages"; import type { ListenerContext } from "@nwire/runtime"; /** * Late-bound listener registration handed to a subscribe closure. The * handler receives the event payload and a {@link ListenerContext} — the * subset of the workflow `when` ctx, so the same block moves into a * `defineWorkflow` unchanged. */ export type WhenFn = (event: E, fn: (payload: EventPayload, ctx: ListenerContext) => Promise | void) => void; /** * A subscription is a closure waiting for an App's `when` to be handed in. * `install(when)` is the contract: the App passes its runtime's `when` and * the closure registers everything it knows. */ export interface Subscription { readonly $kind: "subscription"; install(when: WhenFn): void; } /** * Wrap a registration closure as a Subscription value. The closure is * held by reference — when an App later `subscribe(sub)`s it, the closure * runs with the App's `when` injected. */ export declare function subscribe(fn: (when: WhenFn) => void): Subscription; /** True if `value` looks like a Subscription. */ export declare function isSubscription(value: unknown): value is Subscription;