Skip to content

@smitejs/messaging ​

Messaging endpoints for Smite: channels with consumers, producers, and an in-memory broker executor.

bash
yarn add @smitejs/messaging

13 documented symbols · version 2.0.0-SNAPSHOT.0 · source

Builders ​

functionchannel ​

ts
function channel<Payload = unknown>(app: AppDescriptor, name: string): MessagingChannelBuilder<Payload>;

Creates a channel builder for an app. The shared (app, name) piece yields a builder that refines kind/schema and attaches any number of messaging .consumer children, related under the app's messaging.channel node.

ts
const orders = channel(app, "orders")
  .kind("topic")
  .schema(z.object({ orderId: z.string() }))
  .on(() => undefined);
ts
"messaging.channel"
"orders"
"topic"
1
1

packages/messaging/src/channel.ts:84

interfaceMessagingChannelBuilder ​

ts
interface MessagingChannelBuilder<Payload> {
    readonly kind: <NextKind extends ChannelKind>(kind: NextKind) => MessagingChannelBuilder<Payload>;
    readonly schema: <Next extends Payload>(schema: z.ZodType<Next>) => MessagingChannelBuilder<Next>;
    readonly on: (fn: MessageConsumer<Payload>) => MessagingChannelBuilder<Payload>;
    readonly node: MessagingChannelDescriptor<Payload>;
}

A channel builder: pick a kind and optional zod schema from one shared channel(app, name) piece, then attach consumers with on(fn). Always a builder; each on() relates a messaging.consumer child under the channel.

packages/messaging/src/channel.ts:60

Collector ​

functionchannelsOf ​

ts
function channelsOf(app: AppDescriptor): readonly CollectedChannel[];

Walks an app's messaging.channel children and returns the collected channels with their consumers. Shared by artifact generators and the broker executor.

ts
channel(app, "a").on(() => undefined);
channel(app, "b")
  .kind("queue")
  .on(() => undefined);
const collected = channelsOf(app);
ts
["a", "b"]

packages/messaging/src/collector.ts:24

interfaceCollectedChannel ​

ts
interface CollectedChannel<Payload = unknown> {
    readonly name: string;
    readonly kind: ChannelKind;
    readonly schema?: z.ZodType<Payload>;
    readonly consumers: readonly MessageConsumer<Payload>[];
}

A channel as seen by artifact generators: its name, kind, and the payload schema it validates against.

packages/messaging/src/collector.ts:6

Executor ​

functionbroker ​

ts
function broker(app: AppDescriptor): MessageBroker;

A runtime in-memory broker that walks an app's messaging.channel IR tree (via child refs, never the registry) and delivers published payloads to the registered consumers. queue channels route to the most recent consumer; topic channels fan out to every consumer. Works standalone and behind a transport — same stateless per-trigger contract as every executor.

ts
const bus = broker(app);
const delivered = await bus.publish("events", { kind: "order.placed" });
ts
2
["first:order.placed", "second:order.placed"]

packages/messaging/src/broker.ts:20

constdispatchTo ​

ts
const dispatchTo: <Payload = unknown>(descriptor: MessagingChannelDescriptor<Payload>, consumers: readonly MessagingConsumerDescriptor<Payload>[], payload: unknown, at?: number | Date) => Promise<number>;

Fires a channel's consumers with a fresh envelope at the given (or current) instant. queue routes to the most recent consumer; topic fans out to every consumer. Returns the number of consumers invoked.

packages/messaging/src/channel.ts:128

interfaceMessageBroker ​

ts
interface MessageBroker {
    readonly channels: readonly CollectedChannel[];
    readonly publish: (channel: string, payload: unknown) => Promise<number>;
    readonly lookup: (channel: string) => CollectedChannel | undefined;
}

A runtime handle returned by broker: the collected channels, plus publish to deliver a payload and lookup to find a channel by name.

packages/messaging/src/broker.ts:6

Internals ​

interfaceMessagingChannelDescriptor ​

ts
interface MessagingChannelDescriptor<Payload> extends Descriptor<"messaging.channel", {
    readonly name: string;
    readonly kind: ChannelKind;
    readonly schema?: z.ZodType<Payload>;
}> {
}

A messaging.channel IR node.

packages/messaging/src/channel.ts:34

interfaceMessagingConsumerDescriptor ​

ts
interface MessagingConsumerDescriptor<Payload> extends Descriptor<"messaging.consumer", {
    readonly fn: MessageConsumer<Payload>;
}> {
}

A node wrapping a channel's consumer function.

packages/messaging/src/channel.ts:49

Surface ​

constmessaging ​

ts
const messaging: {
    broker: typeof broker;
    channel: typeof channel;
    channelsOf: typeof channelsOf;
};

The messaging namespace: one import for the whole messaging app extensor — channel builders, the broker executor, and the collector.

ts
const bundle = messaging;
ts
"function"
"function"
"function"

packages/messaging/src/index.ts:21

Types ​

typeChannelKind ​

ts
type ChannelKind = "queue" | "topic";

A named inbound/outbound messaging endpoint. queue delivers each message to one consumer; topic broadcasts to every consumer on the channel.

packages/messaging/src/channel.ts:5

typeMessageConsumer ​

ts
type MessageConsumer<Payload> = (envelope: MessageEnvelope<Payload>) => void | Promise<void>;

A consumer for a channel: receives each delivered MessageEnvelope.

packages/messaging/src/channel.ts:25

interfaceMessageEnvelope ​

ts
interface MessageEnvelope<Payload> {
    readonly id: string;
    readonly channel: string;
    readonly publishedAt: number;
    readonly payload: Payload;
}

The envelope a consumer receives.

packages/messaging/src/channel.ts:13

Released under the MIT License.

/ search•[ ] prev/next•j/k scroll•h home•t theme•api/smitejs-messaging