Emit events
Declare an event, emit it from a handler with the handler's writes, and keep it for listeners that come later.
A luna can have news that other luna types need, such as a card assigned to someone. Calling each of them from the handler ties the luna to every reader, and a reader you add next month misses everything before it.
An event is a typed fact that a luna emits once. Any luna type of your app can listen to it, and the emitting luna keeps it for listeners that come later.
Declare an event and emit it
defineEvent takes a name and the payload’s type. Define each event once, in a file your lunas import.
import { defineEvent, t } from "luna";
export const CardAssigned = defineEvent(
"cards.assigned",
t.object({ card: t.int(), user: t.string() }),
);
In a handler, ctx.emit takes the event and its payload.
import { defineLuna, t, table } from "luna";
import { CardAssigned } from "../events.ts";
export const Card = defineLuna({
name: "Card",
key: { board: t.string(), card: t.int() },
schema: { assignees: table({ user: t.string() }, { primaryKey: ["user"] }) },
handlers: (h) => ({
assign: h({ args: { user: t.string() } }, (ctx, { user }) => {
ctx.db.assignees.upsert({ user });
ctx.emit(CardAssigned, { card: ctx.key.card, user });
}),
}),
});
An event name uses letters, digits, _, . and -, at most 128 characters.
Events leave only with the handler’s writes
An event is a write. It goes out when the handler’s writes are stored, and listeners hear it only then.
If the handler throws or fails, its events are dropped with its writes. No listener ever hears an event from a call that failed. To keep the writes and the events of a call that fails, fail with ctx.fail(code, data, { keep: true }).
Inside ctx.transaction, an event emitted before a throw is taken back with the transaction’s other writes. Here the handler catches the throw and succeeds, but the event never goes out.
assign: h({ args: { user: t.string() } }, (ctx, { user }) => {
try {
ctx.transaction(() => {
ctx.db.assignees.upsert({ user });
ctx.emit(CardAssigned, { card: ctx.key.card, user });
if (ctx.db.assignees.count() > 5) throw new Error("too many");
});
return true;
} catch {
return false;
}
}),
Use the place an emit returns
ctx.emit returns the event’s place in this luna’s log of that event. The first event a luna emits of a type gets 0, the next 1, and so on.
const at = ctx.emit(CardAssigned, { card: ctx.key.card, user });
The place and the emitting luna’s key together name the event. A failed call’s places are given to the next events, since its events never happened.
Check the payload
Luna checks each payload against the event’s type when you emit it. A payload of the wrong type fails the handler with an error that names the event.
A payload is at most 1 MiB as JSON. A larger one fails the handler in the same way.
Keep events for listeners that come later
Each emitting luna keeps the events it emitted, for 7 days unless you say otherwise. When a deploy adds a listener, each luna sends it the events it still keeps, so the new listener starts from the past.
Pass retention to choose how long events stay.
export const CardAssigned = defineEvent(
"cards.assigned",
t.object({ card: t.int(), user: t.string() }),
{ retention: { maxAge: "30d", maxCount: 10_000 } },
);
| Option | Does |
|---|---|
maxAge |
Drops events older than this, such as "30d", or a number of milliseconds |
maxCount |
Keeps at most this many events in each luna |
maxBytes |
Keeps at most this many bytes of events as JSON in each luna. The newest event always stays |
Your retention replaces the default. Without maxAge in it, events are not dropped by age.
Next steps
- Listen to events shows how a luna type hears an event and which of its lunas runs.
- React to table changes hears row changes without emitting anything.
- Query across lunas fills an index from events.