The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as Schema from "effect/Schema"; |
| 2 | import type * as Telemetry from "./Telemetry.ts"; |
| 3 | |
| 4 | /** |
| 5 | * Field map shared by every Domain message builder. |
| 6 | */ |
| 7 | export type Fields = Schema.Struct.Fields; |
| 8 | |
| 9 | /** |
| 10 | * The decoded data carried by a message with fields `F`. |
| 11 | */ |
| 12 | export type Data<F extends Fields> = Schema.Struct<F>["Type"]; |
| 13 | |
| 14 | /** |
| 15 | * Instance type of an Event class: its `_tag` plus its data. |
| 16 | */ |
| 17 | export type Instance<Tag extends string, F extends Fields> = { readonly _tag: Tag } & Data<F>; |
| 18 | |
| 19 | /** |
| 20 | * Structural constraint satisfied by every class produced by {@link make}. |
| 21 | */ |
| 22 | export interface Any extends Schema.Top { |
| 23 | readonly kind: "Event"; |
| 24 | readonly tag: string; |
| 25 | new (...args: any[]): { readonly _tag: string }; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * A fact your domain recorded, named in the past tense. Aggregates record |
| 30 | * events; their state, every view and every policy follow from them. |
| 31 | * |
| 32 | * ### Declaring events |
| 33 | * **Example:** An event with data |
| 34 | * ```typescript |
| 35 | * import * as Domain from "@alchemy.run/domain"; |
| 36 | * import * as Schema from "effect/Schema"; |
| 37 | * |
| 38 | * export class MoneyDeposited extends Domain.Event("MoneyDeposited", { |
| 39 | * amount: Schema.Int, |
| 40 | * balanceAfter: Schema.Int, |
| 41 | * }) {} |
| 42 | * ``` |
| 43 | * |
| 44 | * **Example:** An event with no data |
| 45 | * ```typescript |
| 46 | * import * as Domain from "@alchemy.run/domain"; |
| 47 | * |
| 48 | * export class AccountClosed extends Domain.Event("AccountClosed") {} |
| 49 | * ``` |
| 50 | * |
| 51 | * @primitive |
| 52 | */ |
| 53 | export const make = <const Tag extends string, const F extends Fields = {}>( |
| 54 | tag: Tag, |
| 55 | fields: F = {} as F, |
| 56 | options?: { |
| 57 | /** |
| 58 | * Span attributes to record for this event, on every span it passes |
| 59 | * through. Nothing from it is recorded unless you return it here, and a |
| 60 | * `Redacted` value is recorded as `<redacted>`. |
| 61 | */ |
| 62 | readonly attributes?: Telemetry.Attributes<Instance<Tag, F>>; |
| 63 | }, |
| 64 | ): EventClass<Tag, F> => { |
| 65 | // The base type is conditional on `Self`, so it is not a constructor until `make` returns. |
| 66 | const Base: new (...args: any[]) => object = Schema.TaggedClass<Instance<Tag, F>>(tag)( |
| 67 | tag, |
| 68 | fields, |
| 69 | ) as any; |
| 70 | return class extends Base { |
| 71 | static readonly kind = "Event" as const; |
| 72 | static readonly tag: Tag = tag; |
| 73 | static readonly attributes = options?.attributes; |
| 74 | } as unknown as EventClass<Tag, F>; |
| 75 | }; |
| 76 | |
| 77 | /** |
| 78 | * The class type returned by {@link make}. |
| 79 | */ |
| 80 | export interface EventClass<Tag extends string, F extends Fields> extends Schema.Class< |
| 81 | Instance<Tag, F>, |
| 82 | Schema.TaggedStruct<Tag, F>, |
| 83 | {} |
| 84 | > { |
| 85 | readonly kind: "Event"; |
| 86 | readonly tag: Tag; |
| 87 | /** Span attributes the event adds, if it declares any. */ |
| 88 | readonly attributes: Telemetry.Attributes<Instance<Tag, F>> | undefined; |
| 89 | } |