jotai-state-tree 0.1.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/LICENSE +21 -0
- package/README.md +168 -0
- package/dist/chunk-XXZK62DD.mjs +931 -0
- package/dist/index.d.mts +1109 -0
- package/dist/index.d.ts +1109 -0
- package/dist/index.js +3579 -0
- package/dist/index.mjs +2625 -0
- package/dist/react.d.mts +144 -0
- package/dist/react.d.ts +144 -0
- package/dist/react.js +1259 -0
- package/dist/react.mjs +372 -0
- package/package.json +77 -0
- package/src/__tests__/index.test.ts +1371 -0
- package/src/__tests__/memory.test.ts +681 -0
- package/src/__tests__/performance.test.ts +667 -0
- package/src/__tests__/react.react.test.tsx +811 -0
- package/src/__tests__/registry.test.ts +589 -0
- package/src/array.ts +335 -0
- package/src/compat.ts +294 -0
- package/src/index.ts +647 -0
- package/src/lifecycle.ts +580 -0
- package/src/map.ts +276 -0
- package/src/model.ts +832 -0
- package/src/primitives.ts +400 -0
- package/src/react.ts +626 -0
- package/src/registry.ts +741 -0
- package/src/tree.ts +1275 -0
- package/src/types.ts +520 -0
- package/src/undo.ts +566 -0
- package/src/utilities.ts +616 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Brandon Martel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# jotai-state-tree
|
|
2
|
+
|
|
3
|
+
A MobX-State-Tree (MST) compatible state management library powered by [Jotai](https://jotai.org/).
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🌳 **MST-Compatible API** - Familiar `types.model`, `types.array`, `types.map` and more
|
|
8
|
+
- ⚛️ **Powered by Jotai** - Leverages Jotai's atomic state model for performance
|
|
9
|
+
- 🔄 **Snapshots & Patches** - Full support for `getSnapshot`, `applySnapshot`, `onPatch`
|
|
10
|
+
- 📍 **Tree Navigation** - `getRoot`, `getParent`, `getPath`, `resolvePath`
|
|
11
|
+
- 🔗 **References** - Type-safe references with `types.reference` and `types.safeReference`
|
|
12
|
+
- ⏪ **Undo/Redo** - Built-in undo manager and time-travel debugging
|
|
13
|
+
- ⚛️ **React Integration** - `observer` HOC and hooks for React
|
|
14
|
+
- 🔒 **TypeScript** - Full type safety with inference
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install jotai-state-tree jotai
|
|
20
|
+
# or
|
|
21
|
+
yarn add jotai-state-tree jotai
|
|
22
|
+
# or
|
|
23
|
+
pnpm add jotai-state-tree jotai
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { types, getSnapshot, applySnapshot } from 'jotai-state-tree';
|
|
30
|
+
|
|
31
|
+
// Define your models
|
|
32
|
+
const Todo = types
|
|
33
|
+
.model('Todo', {
|
|
34
|
+
id: types.identifier,
|
|
35
|
+
title: types.string,
|
|
36
|
+
done: types.optional(types.boolean, false),
|
|
37
|
+
})
|
|
38
|
+
.actions((self) => ({
|
|
39
|
+
toggle() {
|
|
40
|
+
self.done = !self.done;
|
|
41
|
+
},
|
|
42
|
+
}));
|
|
43
|
+
|
|
44
|
+
const TodoStore = types
|
|
45
|
+
.model('TodoStore', {
|
|
46
|
+
todos: types.array(Todo),
|
|
47
|
+
})
|
|
48
|
+
.views((self) => ({
|
|
49
|
+
get completedCount() {
|
|
50
|
+
return self.todos.filter((t) => t.done).length;
|
|
51
|
+
},
|
|
52
|
+
}))
|
|
53
|
+
.actions((self) => ({
|
|
54
|
+
addTodo(title: string) {
|
|
55
|
+
self.todos.push({ id: `${Date.now()}`, title });
|
|
56
|
+
},
|
|
57
|
+
}));
|
|
58
|
+
|
|
59
|
+
// Create and use
|
|
60
|
+
const store = TodoStore.create({ todos: [] });
|
|
61
|
+
store.addTodo('Learn jotai-state-tree');
|
|
62
|
+
store.todos[0].toggle();
|
|
63
|
+
console.log(getSnapshot(store));
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## React Integration
|
|
67
|
+
|
|
68
|
+
```tsx
|
|
69
|
+
import { observer, Provider, useStore } from 'jotai-state-tree/react';
|
|
70
|
+
|
|
71
|
+
const TodoList = observer(({ store }) => (
|
|
72
|
+
<ul>
|
|
73
|
+
{store.todos.map((todo) => (
|
|
74
|
+
<li key={todo.id} onClick={() => todo.toggle()}>
|
|
75
|
+
{todo.done ? '✓' : '○'} {todo.title}
|
|
76
|
+
</li>
|
|
77
|
+
))}
|
|
78
|
+
</ul>
|
|
79
|
+
));
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## API Reference
|
|
83
|
+
|
|
84
|
+
### Types
|
|
85
|
+
|
|
86
|
+
| Type | Description |
|
|
87
|
+
|------|-------------|
|
|
88
|
+
| `types.string` | String values |
|
|
89
|
+
| `types.number` | Number values |
|
|
90
|
+
| `types.boolean` | Boolean values |
|
|
91
|
+
| `types.integer` | Integer values |
|
|
92
|
+
| `types.Date` | Date objects |
|
|
93
|
+
| `types.identifier` | String identifier |
|
|
94
|
+
| `types.identifierNumber` | Number identifier |
|
|
95
|
+
| `types.model(name, props)` | Model type |
|
|
96
|
+
| `types.array(type)` | Observable array |
|
|
97
|
+
| `types.map(type)` | Observable map |
|
|
98
|
+
| `types.optional(type, default)` | Optional with default |
|
|
99
|
+
| `types.maybe(type)` | Type or undefined |
|
|
100
|
+
| `types.maybeNull(type)` | Type or null |
|
|
101
|
+
| `types.union(...types)` | Union type |
|
|
102
|
+
| `types.literal(value)` | Literal type |
|
|
103
|
+
| `types.enumeration(values)` | Enumeration |
|
|
104
|
+
| `types.frozen<T>()` | Frozen immutable |
|
|
105
|
+
| `types.late(() => type)` | Lazy/recursive type |
|
|
106
|
+
| `types.reference(type)` | Reference to model |
|
|
107
|
+
| `types.safeReference(type)` | Safe reference |
|
|
108
|
+
| `types.refinement(type, predicate)` | Refined type |
|
|
109
|
+
|
|
110
|
+
### Tree Utilities
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
getSnapshot(node) // Get snapshot
|
|
114
|
+
applySnapshot(node, snap) // Apply snapshot
|
|
115
|
+
onSnapshot(node, listener) // Listen to changes
|
|
116
|
+
onPatch(node, listener) // Listen to patches
|
|
117
|
+
applyPatch(node, patch) // Apply patch
|
|
118
|
+
getRoot(node) // Get root
|
|
119
|
+
getParent(node) // Get parent
|
|
120
|
+
getPath(node) // Get path string
|
|
121
|
+
getEnv(node) // Get environment
|
|
122
|
+
isAlive(node) // Check if alive
|
|
123
|
+
destroy(node) // Destroy node
|
|
124
|
+
clone(node) // Clone node
|
|
125
|
+
walk(node, visitor) // Walk tree
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Undo/Redo
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
import { createUndoManager } from 'jotai-state-tree';
|
|
132
|
+
|
|
133
|
+
const undoManager = createUndoManager(store);
|
|
134
|
+
undoManager.undo();
|
|
135
|
+
undoManager.redo();
|
|
136
|
+
undoManager.startGroup();
|
|
137
|
+
undoManager.endGroup();
|
|
138
|
+
undoManager.clear();
|
|
139
|
+
undoManager.dispose();
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Time Travel
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
import { createTimeTravelManager } from 'jotai-state-tree';
|
|
146
|
+
|
|
147
|
+
const timeTravel = createTimeTravelManager(store);
|
|
148
|
+
timeTravel.record();
|
|
149
|
+
timeTravel.goBack();
|
|
150
|
+
timeTravel.goForward();
|
|
151
|
+
timeTravel.goTo(index);
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Migration from MST
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
// Before (MST)
|
|
158
|
+
import { types } from 'mobx-state-tree';
|
|
159
|
+
import { observer } from 'mobx-react-lite';
|
|
160
|
+
|
|
161
|
+
// After (jotai-state-tree)
|
|
162
|
+
import { types } from 'jotai-state-tree';
|
|
163
|
+
import { observer } from 'jotai-state-tree/react';
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## License
|
|
167
|
+
|
|
168
|
+
MIT
|