| 1 | /** |
| 2 | * What the docs service tells the rest of g1t about folios: `folio.*` |
| 3 | * events on the bus (packages/contracts events.ts, `FolioEventData`). |
| 4 | * Published with no `repoId`, so a folio never reaches a repository's |
| 5 | * timeline or webhooks, and with a title only when the whole workspace |
| 6 | * can read the folio. Never throws: an event that can't be told is |
| 7 | * logged, and the change it is about still happened. |
| 8 | */ |
| 9 | import { eventsClient, type EventPayloads, type FolioEventData, type ServiceBinding } from "@g1t/contracts"; |
| 10 | |
| 11 | import { actorOf } from "../events.ts"; |
| 12 | |
| 13 | export type FolioEventType = "folio.created" | "folio.updated" | "folio.trashed" | "folio.restored" | "folio.shared" | "folio.stale"; |
| 14 | |
| 15 | export async function publishFolioEvent<T extends FolioEventType>(events: ServiceBinding | undefined, type: T, data: EventPayloads[T] & FolioEventData, actor: string | null): Promise<void> { |
| 16 | if (!events) return; |
| 17 | try { |
| 18 | await eventsClient(events).publish([{ type, source: "docs", repoId: null, actor: actorOf(actor), data } as never]); |
| 19 | } catch (error) { |
| 20 | console.error("folios could not publish", type, String(error)); |
| 21 | } |
| 22 | } |