r-state-tree 0.1.0-rc9 → 0.3.0
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.
- package/README.md +119 -0
- package/dist/r-state-tree.cjs +2419 -0
- package/dist/r-state-tree.js +2419 -0
- package/package.json +36 -28
- package/lib/api.d.ts +0 -8
- package/lib/computedProxy.d.ts +0 -2
- package/lib/decorators.d.ts +0 -10
- package/lib/graph.d.ts +0 -2
- package/lib/index.d.ts +0 -8
- package/lib/lobx.d.ts +0 -10
- package/lib/model/Model.d.ts +0 -14
- package/lib/model/ModelAdministration.d.ts +0 -40
- package/lib/model/idMap.d.ts +0 -7
- package/lib/r-state-tree.js +0 -1119
- package/lib/r-state-tree.js.map +0 -1
- package/lib/r-state-tree.module.js +0 -1086
- package/lib/r-state-tree.module.js.map +0 -1
- package/lib/store/Store.d.ts +0 -16
- package/lib/store/StoreAdministration.d.ts +0 -35
- package/lib/types.d.ts +0 -73
- package/lib/utils.d.ts +0 -2
package/README.md
CHANGED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# r-state-tree
|
|
2
|
+
|
|
3
|
+
Reactive state management featuring store trees, computed child stores, and snapshot utilities backed by `@preact/signals-core`.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add r-state-tree
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Stores
|
|
12
|
+
|
|
13
|
+
Stores describe reactive state containers composed into a tree.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import Store, { createStore, mount } from "r-state-tree";
|
|
17
|
+
import { child } from "r-state-tree/decorators";
|
|
18
|
+
|
|
19
|
+
class TodoStore extends Store {
|
|
20
|
+
props = { title: "" };
|
|
21
|
+
|
|
22
|
+
get title() {
|
|
23
|
+
return this.props.title;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
class AppStore extends Store {
|
|
28
|
+
@child get todo() {
|
|
29
|
+
return createStore(TodoStore, { title: "Write docs" });
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const app = mount(createStore(AppStore));
|
|
34
|
+
app.todo.title; // "Write docs"
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Updating props
|
|
38
|
+
|
|
39
|
+
Stores receive reactive `props` objects. Use `updateStore` to change them.
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { updateStore } from "r-state-tree";
|
|
43
|
+
|
|
44
|
+
updateStore(app.todo, { title: "Ship release" });
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Actions and batching
|
|
48
|
+
|
|
49
|
+
`runInBatch` groups updates to avoid redundant reactions.
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import { runInBatch } from "r-state-tree";
|
|
53
|
+
|
|
54
|
+
runInBatch(() => {
|
|
55
|
+
app.todo.props.title = "Refactor";
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Models and snapshots
|
|
60
|
+
|
|
61
|
+
Models capture persistent state with snapshot utilities.
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import Model from "r-state-tree/model/Model";
|
|
65
|
+
import {
|
|
66
|
+
applySnapshot,
|
|
67
|
+
onSnapshot,
|
|
68
|
+
onSnapshotDiff,
|
|
69
|
+
toSnapshot,
|
|
70
|
+
} from "r-state-tree";
|
|
71
|
+
|
|
72
|
+
class TodoModel extends Model {
|
|
73
|
+
static types = {
|
|
74
|
+
title: stateType,
|
|
75
|
+
assignee: modelRefType,
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
title = "";
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const todo = new TodoModel();
|
|
82
|
+
|
|
83
|
+
const stop = onSnapshot(todo, (snapshot) => {
|
|
84
|
+
console.log(snapshot.title);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
applySnapshot(todo, { title: "Learn signals" });
|
|
88
|
+
stop();
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Snapshot diffs
|
|
92
|
+
|
|
93
|
+
Use `onSnapshotDiff` to receive undo/redo payloads.
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
const off = onSnapshotDiff(todo, ({ undo, redo }) => {
|
|
97
|
+
// undo and redo contain patch-like snapshots
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Testing
|
|
102
|
+
|
|
103
|
+
The repository ships with Vitest suites covering stores, models, containers, and observable primitives.
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
pnpm test
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Build & publishing
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
pnpm build
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
The build emits CommonJS, ESM bundles, and type declarations under `dist/`.
|
|
116
|
+
|
|
117
|
+
## License
|
|
118
|
+
|
|
119
|
+
MIT
|