Skip to content

Commit e362e7e

Browse files
committed
docs(examples): add examples/classes/atom_in_class_test.xi
A class driving a global atom through its methods (wrapped behind an interface). Complements machine_in_class_test — an atom is shared/managed, so it's used by name rather than held as per-instance state.
1 parent 1309967 commit e362e7e

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Feature: a class can drive a global `atom` from its methods. An atom is a
2+
// managed, shared singleton (event-sourced state), so — unlike a machine value —
3+
// it isn't held as per-instance `state`; the class references it by name and
4+
// wraps it behind an interface (testable, decoupled).
5+
state Counter = { n: Integer }
6+
7+
atom tally {
8+
initial Counter { n: 0 }
9+
transition inc(s: Counter) -> Counter { return Counter { n: s.n + 1 } }
10+
transition add(s: Counter, k: Integer) -> Counter { return Counter { n: s.n + k } }
11+
}
12+
13+
interface Tally {
14+
consumer bump()
15+
consumer addN(n: Integer)
16+
producer total() -> Integer
17+
}
18+
class TallyStore implements Tally {
19+
deps {}
20+
consumer bump() { tally.inc() }
21+
consumer addN(n: Integer) { tally.add(n) }
22+
producer total() -> Integer { return tally.current.n }
23+
}
24+
module App { bind Tally -> TallyStore as singleton }
25+
26+
test "a class drives a global atom through its methods" {
27+
let t = App.resolve(Tally)
28+
t.bump()
29+
t.bump()
30+
t.addN(5)
31+
assertEq(t.total(), 7)
32+
}

0 commit comments

Comments
 (0)