The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { Delivery } from "./Platform.ts"; |
| 2 | |
| 3 | /** |
| 4 | * The cursors that route a source instance's events (an aggregate's events, |
| 5 | * or a view key's entries) to each view and projection. A consumer whose |
| 6 | * cursor is behind the instance's version is lagging: live routing skips it, |
| 7 | * and `Backfill` hands it the history instead. Policies are never caught up: |
| 8 | * they act on new events only. |
| 9 | * |
| 10 | * @internal |
| 11 | */ |
| 12 | |
| 13 | /** The last `seq` a source instance routed to each consumer, by {@link targetId}. */ |
| 14 | export type Routed = Readonly<Record<string, number>>; |
| 15 | |
| 16 | /** |
| 17 | * A consumer's id in {@link Routed}: `view:Name` or `projection:Name`, with |
| 18 | * `#<generation>` once it has been rebuilt, or `@<generation>` once it has |
| 19 | * been rebuilt in place (in the storage it had). A rebuilt consumer is a new |
| 20 | * one to its sources, so they replay their history to it. |
| 21 | */ |
| 22 | export const targetId = (target: { |
| 23 | readonly kind: string; |
| 24 | readonly name: string; |
| 25 | readonly generation?: number; |
| 26 | readonly inPlace?: boolean; |
| 27 | }): string => |
| 28 | `${target.kind}:${target.name}${target.generation ? `${target.inPlace ? "@" : "#"}${target.generation}` : ""}`; |
| 29 | |
| 30 | /** |
| 31 | * Route a commit's deliveries: drop those to lagging consumers (they receive |
| 32 | * the events by replay), and advance the cursors of the live ones from |
| 33 | * `before` to `after`. An instance with no cursors yet predates catch-up; its |
| 34 | * consumers are taken as caught up. |
| 35 | */ |
| 36 | export const routeLive = ( |
| 37 | targets: ReadonlyArray<string>, |
| 38 | routed: Routed | undefined, |
| 39 | before: number, |
| 40 | after: number, |
| 41 | deliveries: ReadonlyArray<Delivery>, |
| 42 | ): { readonly deliveries: ReadonlyArray<Delivery>; readonly routed: Routed } => { |
| 43 | const base: Routed = routed ?? Object.fromEntries(targets.map((target) => [target, before])); |
| 44 | const live = (target: string) => (base[target] ?? 0) === before; |
| 45 | const next: Record<string, number> = { ...base }; |
| 46 | for (const target of targets) if (live(target)) next[target] = after; |
| 47 | return { |
| 48 | deliveries: deliveries.filter( |
| 49 | (delivery) => delivery.target.kind === "policy" || live(targetId(delivery.target)), |
| 50 | ), |
| 51 | routed: next, |
| 52 | }; |
| 53 | }; |
| 54 | |
| 55 | /** What a replay needs: stored rows, in order. */ |
| 56 | export interface ReplayInput<Row> { |
| 57 | /** The instance: an aggregate id, or a view key. */ |
| 58 | readonly id: string; |
| 59 | readonly routed: Routed; |
| 60 | readonly rows: ReadonlyArray<Row>; |
| 61 | } |
| 62 | |
| 63 | /** What a replay produced. */ |
| 64 | export interface ReplayResult { |
| 65 | /** Deliveries to lagging consumers, marked `history`. */ |
| 66 | readonly deliveries: ReadonlyArray<Delivery>; |
| 67 | readonly routed: Routed; |
| 68 | } |