@pyreon/state-tree 0.24.5 → 0.24.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,85 +0,0 @@
1
- import {
2
- renderApiReferenceEntries,
3
- renderLlmsFullSection,
4
- renderLlmsTxtLine,
5
- } from '@pyreon/manifest'
6
- import manifest from '../manifest'
7
-
8
- describe('gen-docs — state-tree snapshot', () => {
9
- it('renders to llms.txt bullet', () => {
10
- expect(renderLlmsTxtLine(manifest)).toMatchInlineSnapshot(`"- @pyreon/state-tree — Structured reactive state tree — composable models with snapshots, patches, and middleware. State mutations must go through actions — direct \`.set()\` calls on state signals bypass middleware and patch recording. The model enforces this in dev mode."`)
11
- })
12
-
13
- it('renders to llms-full.txt section', () => {
14
- expect(renderLlmsFullSection(manifest)).toMatchInlineSnapshot(`
15
- "## @pyreon/state-tree — State Tree
16
-
17
- MobX-State-Tree-inspired structured state management built on Pyreon signals. Models compose state (signals), views (computeds), and actions into self-contained units that support typed snapshots, JSON-patch record/replay, and action interception middleware. Models can nest other models for tree-shaped state, and \`.asHook(id)\` provides singleton instances scoped to a store-like registry.
18
-
19
- \`\`\`typescript
20
- import { model, getSnapshot, applySnapshot, onPatch, applyPatch, addMiddleware } from '@pyreon/state-tree'
21
-
22
- // Define a model — state (signals), views (derived), actions (mutations):
23
- const Todo = model({
24
- state: { title: '', done: false },
25
- views: (self) => ({
26
- summary: () => \`\${self.title()} [\${self.done() ? 'x' : ' '}]\`,
27
- }),
28
- actions: (self) => ({
29
- toggle: () => self.done.set(!self.done()),
30
- rename: (title: string) => self.title.set(title),
31
- }),
32
- })
33
-
34
- const TodoList = model({
35
- state: { todos: [] as ReturnType<typeof Todo.create>[] },
36
- actions: (self) => ({
37
- add: (title: string) => {
38
- const todo = Todo.create({ title, done: false })
39
- self.todos.update(list => [...list, todo])
40
- },
41
- }),
42
- })
43
-
44
- // Create instances:
45
- const list = TodoList.create({ todos: [] })
46
- list.add('Write tests')
47
- list.todos()[0].toggle()
48
-
49
- // Snapshots — typed recursive serialization:
50
- const snap = getSnapshot(list)
51
- applySnapshot(list, { todos: [{ title: 'Restored', done: true }] })
52
-
53
- // JSON patches — record/replay for undo, sync, debugging:
54
- const patches: Patch[] = []
55
- const dispose = onPatch(list, (patch) => patches.push(patch))
56
- list.add('New item')
57
- // Later: applyPatch(list, patches[0]) to replay
58
-
59
- // Middleware — intercept any action in the tree:
60
- addMiddleware(list, (call, next) => {
61
- console.log(\`Action: \${call.name}\`, call.args)
62
- return next(call)
63
- })
64
-
65
- // Singleton hook for app-wide state:
66
- const useTodoList = TodoList.asHook('todo-list')
67
- const { store } = useTodoList() // same instance on every call
68
- \`\`\`
69
-
70
- > **Actions only**: State mutations must go through actions — direct \`.set()\` calls on state signals bypass middleware and patch recording. The model enforces this in dev mode.
71
- >
72
- > **Snapshot serialization**: \`getSnapshot\` reads via \`.peek()\` so it does not subscribe to signals. The snapshot is a one-time read, not a reactive computed.
73
- >
74
- > **Devtools**: Import \`@pyreon/state-tree/devtools\` for a WeakRef-based registry of live model instances. Tree-shakeable — zero cost unless imported.
75
- "
76
- `)
77
- })
78
-
79
- it('renders to MCP api-reference entries', () => {
80
- const record = renderApiReferenceEntries(manifest)
81
- expect(Object.keys(record).length).toBe(6)
82
- expect(record['state-tree/model']!.notes).toContain('ModelDefinition')
83
- expect(record['state-tree/model']!.mistakes?.split('\n').length).toBe(3)
84
- })
85
- })