@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 07:15 UTC

main
ts 110 lines 3.88 KB
Raw
1 import * as Domain from "@alchemy.run/domain";
2 import * as Schema from "effect/Schema";
3
4 export class Deposited extends Domain.Event("Deposited", { amount: Schema.Number }) {}
5 export class Deposit extends Domain.Command("Deposit", {
6 target: Schema.String,
7 input: { amount: Schema.Number },
8 events: [Deposited],
9 }) {}
10 export class Account extends Domain.Aggregate("Account", {
11 id: Schema.String,
12 state: Schema.Struct({ balance: Schema.Number }),
13 initial: { balance: 0 },
14 commands: [Deposit],
15 decide: { Deposit: (_, cmd) => [new Deposited({ amount: cmd.amount })] },
16 evolve: { Deposited: (s, e) => ({ balance: s.balance + e.amount }) },
17 }) {}
18
19 /** Every deposit to an account, in the order the view applied them: added in the second deploy. */
20 export class Deposits extends Domain.View("Deposits", {
21 from: [Account],
22 key: Account,
23 state: Schema.Struct({ amounts: Schema.Array(Schema.Number) }),
24 initial: { amounts: [] },
25 evolve: { Deposited: (s, e) => ({ amounts: [...s.amounts, e.amount] }) },
26 }) {}
27
28 /** Every deposit across every account, in the order the view applied them: one hot key. */
29 export class Timeline extends Domain.View("Timeline", {
30 from: [Account],
31 key: Schema.String,
32 keyOf: { Deposited: () => "all" },
33 state: Schema.Struct({ entries: Schema.Array(Schema.String) }),
34 initial: { entries: [] },
35 evolve: {
36 Deposited: (s, e, { source }) => ({ entries: [...s.entries, `${source.id}:${e.amount}`] }),
37 },
38 }) {}
39
40 /** The first deploy: accounts only. */
41 export class Before extends Domain.Model("Ledger", { aggregates: [Account] }) {}
42
43 /** The second deploy adds the view; its history must reach it. */
44 export class After extends Domain.Model("Ledger", {
45 aggregates: [Account],
46 views: [Deposits, Timeline],
47 }) {}
48
49 /** The view redefined: it now records each deposit doubled, at version 1. */
50 export class DoubledDeposits extends Domain.View("Deposits", {
51 from: [Account],
52 key: Account,
53 state: Schema.Struct({ amounts: Schema.Array(Schema.Number) }),
54 initial: { amounts: [] },
55 evolve: { Deposited: (s, e) => ({ amounts: [...s.amounts, e.amount * 2] }) },
56 version: 1,
57 }) {}
58
59 /** The third deploy: the redefined view, at version 1. */
60 export class Rebuilt extends Domain.Model("Ledger", {
61 aggregates: [Account],
62 views: [DoubledDeposits, Timeline],
63 }) {}
64
65 export class Noted extends Domain.Event("Noted", { note: Schema.String }) {}
66 export class Note extends Domain.Command("Note", {
67 target: Schema.String,
68 input: { note: Schema.String },
69 events: [Noted],
70 }) {}
71 /** A second aggregate, for a view that comes to read it too. */
72 export class Journal extends Domain.Aggregate("Journal", {
73 id: Schema.String,
74 state: Schema.Struct({}),
75 initial: {},
76 commands: [Note],
77 decide: { Note: (_, cmd) => [new Noted({ note: cmd.note })] },
78 evolve: { Noted: (s) => s },
79 }) {}
80
81 /** The timeline, now reading the journal too: the same view, at the same version unless bumped. */
82 export class WideTimeline extends Domain.View("Timeline", {
83 from: [Account, Journal],
84 key: Schema.String,
85 keyOf: { Deposited: () => "all", Noted: () => "all" },
86 state: Schema.Struct({ entries: Schema.Array(Schema.String) }),
87 initial: { entries: [] },
88 evolve: {
89 Deposited: (s, e, { source }) => ({ entries: [...s.entries, `${source.id}:${e.amount}`] }),
90 Noted: (s, e) => ({ entries: [...s.entries, `note:${e.note}`] }),
91 },
92 }) {}
93
94 /** A deploy whose timeline reads the journal too. */
95 export class Widened extends Domain.Model("Ledger", {
96 aggregates: [Account, Journal],
97 views: [Deposits, WideTimeline],
98 }) {}
99
100 /** The wider timeline at version 1: rebuilt from every source's history. */
101 export class WideTimelineV1 extends Domain.View("Timeline", {
102 ...WideTimeline.definition,
103 version: 1,
104 }) {}
105
106 /** A deploy whose timeline reads the journal too, at version 1. */
107 export class WidenedV1 extends Domain.Model("Ledger", {
108 aggregates: [Account, Journal],
109 views: [Deposits, WideTimelineV1],
110 }) {}