duratom 0.1.0 → 0.2.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 +7 -227
- package/dist/{chunk-GTI66UUE.js → chunk-7TXOG3GB.js} +180 -31
- package/dist/{chunk-LTA6HTAG.js → chunk-URKFBGPT.js} +40 -4
- package/dist/core/index.d.ts +49 -3
- package/dist/core/index.js +14 -4
- package/dist/index.d.ts +41 -5
- package/dist/index.js +81 -3
- package/dist/react/index.d.ts +2 -2
- package/dist/react/index.js +2 -2
- package/dist/{shape--EeyM-2X.d.ts → shape-CMa7XFjy.d.ts} +52 -1
- package/dist/{typed-atom-oZaH0A5l.d.ts → typed-atom-t7tvnXSS.d.ts} +36 -2
- package/llms-full.txt +309 -12
- package/llms.txt +1 -1
- package/package.json +16 -17
- package/skills/duratom/SKILL.md +13 -12
package/README.md
CHANGED
|
@@ -68,239 +68,19 @@ createRoot(document.getElementById('root')!).render(
|
|
|
68
68
|
|
|
69
69
|
<!-- /snippet -->
|
|
70
70
|
|
|
71
|
-
Add a sync provider to the doc ([sync recipe](
|
|
71
|
+
Add a sync provider to the doc ([sync recipe](https://duratom.karolnov.dev/recipes/sync)) and the same atom shows and edits the todo on every device, with no change to the atom or the component.
|
|
72
72
|
|
|
73
|
-
##
|
|
74
|
-
|
|
75
|
-
Four rules cover how duratom behaves; the [API reference](https://duratom.karolnov.dev/api) has the details.
|
|
76
|
-
|
|
77
|
-
- **One doc per store.** Atoms read the Y.Doc in `docAtom`, so set it once on each Jotai store before reading an atom. An app with several docs gives an atom its own doc atom: `typedAtom(locator, Schema, shape, { docAtom: otherDocAtom })`.
|
|
78
|
-
- **Patches merge.** Setting a typed atom with `{ priority: 'high' }` reads the node as it is in the doc at that moment, merges the patch into it and writes only what changed, so a peer's concurrent edit to another field survives. `set(prev => patch)` gets that fresh value as `prev`. On a node with no valid value, the patch is the whole value: that creates a missing node, or repairs an invalid one.
|
|
79
|
-
- **Results, not exceptions.** A typed atom reads as `undefined` while its node is missing, `{ issues }` while the node doesn't satisfy the schema, and `{ value }` otherwise. Setting it returns the schema's result; an invalid patch changes nothing and returns `{ issues }`. Only misconfiguration throws: an async schema (`AsyncValidationUnsupportedError`), a value a `Y.Map` can't store (`UnsupportedValueError`), or no doc in `docAtom`.
|
|
80
|
-
- **Subscribed atoms follow the doc.** An atom observes its node while something is subscribed to it (a component, or `store.sub`), and then only that node. Read with `store.get` while nothing is subscribed, it can show a value from before a peer's edit; it does see its own writes. Use `live` from `duratom/core` to read the doc as it is now outside Jotai.
|
|
81
|
-
|
|
82
|
-
## Shapes
|
|
83
|
-
|
|
84
|
-
A schema says what a value is; a shape says which of its fields are Yjs shared types, so that peers editing them at once merge their edits. Every field the shape doesn't name is a plain value, where the last writer wins.
|
|
85
|
-
|
|
86
|
-
- `'text'`: a `Y.Text`, read as a string. Two people typing in it both keep their edits.
|
|
87
|
-
- `'xml'`: a `Y.XmlFragment`, read as the fragment itself, for rich-text editors.
|
|
88
|
-
- `map(shape)`: a nested `Y.Map` with fixed keys, read as an object.
|
|
89
|
-
- `record(itemShape)`: a `Y.Map` of item maps with any keys, read as an object of objects.
|
|
90
|
-
- `array()`: a `Y.Array` of plain values, read as an array.
|
|
91
|
-
|
|
92
|
-
A shape's keys are checked against the schema's type, so a key the schema doesn't have, or a kind its field can't hold, is a type error. An empty shape, `{}`, makes every field plain.
|
|
93
|
-
|
|
94
|
-
## Collections
|
|
95
|
-
|
|
96
|
-
`collection` covers a map of items: an `ids` atom that changes only when an item is added or removed, a typed atom per item, and `create` and `remove`.
|
|
97
|
-
|
|
98
|
-
<!-- snippet: docs/snippets/collection.tsx -->
|
|
99
|
-
|
|
100
|
-
```tsx
|
|
101
|
-
import { collection, select } from 'duratom'
|
|
102
|
-
import { useAtom, useAtomValue, useSetAtom } from 'jotai'
|
|
103
|
-
import { z } from 'zod'
|
|
104
|
-
|
|
105
|
-
const Todo = z.object({ title: z.string().min(1), done: z.boolean() })
|
|
106
|
-
|
|
107
|
-
// Every todo in the root map `todos`, each one a Y.Map read through `Todo`.
|
|
108
|
-
const todos = collection(select.map('todos'), Todo, { title: 'text' })
|
|
109
|
-
|
|
110
|
-
export function TodoList() {
|
|
111
|
-
// Changes only when a todo is added or removed, not when one is edited.
|
|
112
|
-
const ids = useAtomValue(todos.ids)
|
|
113
|
-
const create = useSetAtom(todos.create)
|
|
114
|
-
const add = () => {
|
|
115
|
-
const result = create({ title: 'New todo', done: false })
|
|
116
|
-
if (result.issues) console.warn(result.issues)
|
|
117
|
-
}
|
|
118
|
-
return (
|
|
119
|
-
<>
|
|
120
|
-
<ul>
|
|
121
|
-
{ids.map((id) => (
|
|
122
|
-
<TodoItem key={id} id={id} />
|
|
123
|
-
))}
|
|
124
|
-
</ul>
|
|
125
|
-
<button onClick={add}>Add</button>
|
|
126
|
-
</>
|
|
127
|
-
)
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function TodoItem({ id }: { id: string }) {
|
|
131
|
-
// The same typed atom for the same id; it re-renders this row only.
|
|
132
|
-
const [todo, setTodo] = useAtom(todos.item(id))
|
|
133
|
-
const remove = useSetAtom(todos.remove)
|
|
134
|
-
if (!todo || todo.issues) return <li>Unreadable todo</li>
|
|
135
|
-
return (
|
|
136
|
-
<li>
|
|
137
|
-
<input
|
|
138
|
-
type="checkbox"
|
|
139
|
-
checked={todo.value.done}
|
|
140
|
-
onChange={(e) => setTodo({ done: e.target.checked })}
|
|
141
|
-
/>
|
|
142
|
-
{todo.value.title}
|
|
143
|
-
<button onClick={() => remove(id)}>Remove</button>
|
|
144
|
-
</li>
|
|
145
|
-
)
|
|
146
|
-
}
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
<!-- /snippet -->
|
|
150
|
-
|
|
151
|
-
## Typed atoms in a hook
|
|
152
|
-
|
|
153
|
-
`useTypedAtom` from `duratom/react` is `typedAtom` for a path that depends on props. It returns `[result, set]`, as `useAtom` does, and rebuilds the atom only when `deps` change.
|
|
154
|
-
|
|
155
|
-
<!-- snippet: docs/snippets/typed-hook.tsx -->
|
|
156
|
-
|
|
157
|
-
```tsx
|
|
158
|
-
import { select } from 'duratom'
|
|
159
|
-
import { useTypedAtom } from 'duratom/react'
|
|
160
|
-
import { z } from 'zod'
|
|
161
|
-
|
|
162
|
-
const Card = z.object({ title: z.string(), priority: z.enum(['low', 'high']) })
|
|
163
|
-
|
|
164
|
-
// The card's path depends on a prop: the hook rebuilds the atom only when `id` changes.
|
|
165
|
-
export function CardRow({ id }: { id: string }) {
|
|
166
|
-
const [card, setCard] = useTypedAtom(
|
|
167
|
-
() => select.map('cards').child(id),
|
|
168
|
-
Card,
|
|
169
|
-
{ title: 'text' },
|
|
170
|
-
[id],
|
|
171
|
-
)
|
|
172
|
-
if (!card) return <button onClick={() => setCard({ title: 'New', priority: 'low' })}>Add</button>
|
|
173
|
-
if (card.issues) return <span>Unreadable card</span>
|
|
174
|
-
return (
|
|
175
|
-
<label>
|
|
176
|
-
{card.value.title}
|
|
177
|
-
<input
|
|
178
|
-
type="checkbox"
|
|
179
|
-
checked={card.value.priority === 'high'}
|
|
180
|
-
onChange={(e) => setCard({ priority: e.target.checked ? 'high' : 'low' })}
|
|
181
|
-
/>
|
|
182
|
-
</label>
|
|
183
|
-
)
|
|
184
|
-
}
|
|
185
|
-
```
|
|
186
|
-
|
|
187
|
-
<!-- /snippet -->
|
|
188
|
-
|
|
189
|
-
## Custom projections
|
|
190
|
-
|
|
191
|
-
`liveAtom` is the read-only atom under `typedAtom`, for values a schema doesn't describe: one field, a count, anything computed from a node. `select` builds what it reads, and the atom re-renders only when the result changes.
|
|
192
|
-
|
|
193
|
-
<!-- snippet: docs/snippets/live-atom.tsx -->
|
|
194
|
-
|
|
195
|
-
```tsx
|
|
196
|
-
import { liveAtom, select } from 'duratom'
|
|
197
|
-
import { useLiveAtom } from 'duratom/react'
|
|
198
|
-
import { useAtomValue } from 'jotai'
|
|
199
|
-
import * as Y from 'yjs'
|
|
200
|
-
|
|
201
|
-
// One field, with a fallback for a missing map, a missing key or a value of another type.
|
|
202
|
-
const boardName = liveAtom(select.map('board').get('name', 'Untitled'))
|
|
203
|
-
|
|
204
|
-
// Your own projection: re-renders only when the counts change, not on every edit to a todo.
|
|
205
|
-
const progress = liveAtom(
|
|
206
|
-
select.map('todos').project((todos) => {
|
|
207
|
-
let done = 0
|
|
208
|
-
todos?.forEach((todo) => {
|
|
209
|
-
if (todo instanceof Y.Map && todo.get('done') === true) done++
|
|
210
|
-
})
|
|
211
|
-
return { done, total: todos?.size ?? 0 }
|
|
212
|
-
}),
|
|
213
|
-
)
|
|
214
|
-
|
|
215
|
-
export function Header() {
|
|
216
|
-
const name = useAtomValue(boardName)
|
|
217
|
-
const { done, total } = useAtomValue(progress)
|
|
218
|
-
return (
|
|
219
|
-
<h1>
|
|
220
|
-
{name}: {done} of {total} done
|
|
221
|
-
</h1>
|
|
222
|
-
)
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
// A selector that depends on props: the hook rebuilds the atom only when `id` changes.
|
|
226
|
-
export function Title({ id }: { id: string }) {
|
|
227
|
-
const title = useLiveAtom(() => select.map('todos').child(id).text('title'), [id])
|
|
228
|
-
return <span>{title}</span>
|
|
229
|
-
}
|
|
230
|
-
```
|
|
231
|
-
|
|
232
|
-
<!-- /snippet -->
|
|
233
|
-
|
|
234
|
-
## Several docs
|
|
235
|
-
|
|
236
|
-
Every atom takes a last argument, `{ docAtom }`, naming the atom that holds its doc. Atoms without it read `docAtom`.
|
|
237
|
-
|
|
238
|
-
<!-- snippet: docs/snippets/several-docs.ts -->
|
|
239
|
-
|
|
240
|
-
```ts
|
|
241
|
-
import { select, typedAtom } from 'duratom'
|
|
242
|
-
import { atom, createStore } from 'jotai'
|
|
243
|
-
import * as Y from 'yjs'
|
|
244
|
-
import { z } from 'zod'
|
|
245
|
-
|
|
246
|
-
// A second doc, beside the one in `docAtom`: settings kept apart from the shared board, say.
|
|
247
|
-
const settingsDocAtom = atom<Y.Doc | null>(null)
|
|
248
|
-
|
|
249
|
-
const Settings = z.object({ theme: z.enum(['light', 'dark']) })
|
|
250
|
-
const settingsAtom = typedAtom(
|
|
251
|
-
select.map('settings').child('user'),
|
|
252
|
-
Settings,
|
|
253
|
-
{},
|
|
254
|
-
{
|
|
255
|
-
docAtom: settingsDocAtom,
|
|
256
|
-
},
|
|
257
|
-
)
|
|
258
|
-
|
|
259
|
-
const store = createStore()
|
|
260
|
-
store.set(settingsDocAtom, new Y.Doc())
|
|
261
|
-
store.set(settingsAtom, { theme: 'dark' })
|
|
262
|
-
```
|
|
263
|
-
|
|
264
|
-
<!-- /snippet -->
|
|
265
|
-
|
|
266
|
-
## Without Jotai
|
|
267
|
-
|
|
268
|
-
`duratom/core` has the pieces under the atoms, for other state libraries or none: `live` follows one node of a doc, and `read` and `write` read and write a `Y.Map` through a schema.
|
|
269
|
-
|
|
270
|
-
<!-- snippet: docs/snippets/core.ts -->
|
|
271
|
-
|
|
272
|
-
```ts
|
|
273
|
-
import { live, read, select, write } from 'duratom/core'
|
|
274
|
-
import * as Y from 'yjs'
|
|
275
|
-
import { z } from 'zod'
|
|
276
|
-
|
|
277
|
-
const Todo = z.object({ title: z.string(), done: z.boolean() })
|
|
278
|
-
const doc = new Y.Doc()
|
|
279
|
-
const todo = doc.getMap('todos').set('first', new Y.Map())
|
|
280
|
-
|
|
281
|
-
// Validated writes and typed reads, on any Y.Map.
|
|
282
|
-
write(Todo, { title: 'text' }, todo, { title: 'Learn duratom', done: false })
|
|
283
|
-
const result = read(Todo, { title: 'text' }, todo) // { value: { title: 'Learn duratom', done: false } }
|
|
284
|
-
|
|
285
|
-
// A live value in the shape useSyncExternalStore takes, for any state library or none.
|
|
286
|
-
const done = live(doc, select.map('todos').child('first').get('done', false))
|
|
287
|
-
const stop = done.subscribe((value) => console.log('done is now', value))
|
|
288
|
-
write(Todo, { title: 'text' }, todo, { title: 'Learn duratom', done: true }) // logs: done is now true
|
|
289
|
-
stop()
|
|
73
|
+
## Docs
|
|
290
74
|
|
|
291
|
-
|
|
292
|
-
```
|
|
75
|
+
Everything else is at **[duratom.karolnov.dev](https://duratom.karolnov.dev)**: the guide, recipes for sync and offline storage, and the API reference.
|
|
293
76
|
|
|
294
|
-
|
|
77
|
+
For AI agents, the npm package ships [`llms-full.txt`](llms-full.txt) (every doc in one file) and an agent skill, [`skills/duratom/SKILL.md`](skills/duratom/SKILL.md), so an agent finds them in `node_modules/duratom`.
|
|
295
78
|
|
|
296
|
-
##
|
|
79
|
+
## Specs and blueprints
|
|
297
80
|
|
|
298
|
-
|
|
299
|
-
- Recipes: [sync between devices](docs/recipes/sync.md), [offline storage](docs/recipes/offline.md), [repair a broken node](docs/recipes/repair.md).
|
|
300
|
-
- Example apps: [a two-pane shared todo list](examples/todo), and [an offline-first todo list synced through a Durable Object](examples/sync), with end-to-end tests.
|
|
301
|
-
- For AI agents: [`llms.txt`](llms.txt), [`llms-full.txt`](llms-full.txt) (every doc in one file) and an agent skill, [`skills/duratom/SKILL.md`](skills/duratom/SKILL.md). The npm package ships `llms-full.txt` and the skill, so an agent finds them in `node_modules/duratom`.
|
|
81
|
+
What each part of duratom must do is written down as **capability specs**: requirements with GIVEN / WHEN / THEN scenarios, one page per capability (`core/observe`, `schema`, `atoms`, `docs`). A planned change is a **blueprint**: a brief with the decisions, the tasks and a spec delta, plus a review for people. When the change is built, its delta is merged into the specs.
|
|
302
82
|
|
|
303
|
-
|
|
83
|
+
These come from the [`/blueprint` skills](https://github.com/KarolNov/skills/tree/main/skills/blueprint) in [KarolNov/skills](https://github.com/KarolNov/skills). The specs live outside the repo; [`.blueprint/`](.blueprint) maps each capability to the code that implements it and records when the two last agreed. `python3 .blueprint/drift.py check` lists capabilities whose code changed since then.
|
|
304
84
|
|
|
305
85
|
## License
|
|
306
86
|
|
|
@@ -60,8 +60,8 @@ function observeLocated(doc, locator, options, onChange) {
|
|
|
60
60
|
let cleanups = [];
|
|
61
61
|
let node;
|
|
62
62
|
const accepts = (tr) => !origin || origin(tr.origin);
|
|
63
|
-
const
|
|
64
|
-
const counts = (event) =>
|
|
63
|
+
const touchesKeys2 = (event) => !keys || !(event instanceof Y2.YMapEvent) || deep && event.target === node || keys.some((k) => event.keysChanged.has(k));
|
|
64
|
+
const counts = (event) => touchesKeys2(event) && (!accepted || accepted(event));
|
|
65
65
|
const unbind = () => {
|
|
66
66
|
for (const fn of cleanups) fn();
|
|
67
67
|
cleanups = [];
|
|
@@ -158,13 +158,60 @@ function live(doc, spec) {
|
|
|
158
158
|
}
|
|
159
159
|
|
|
160
160
|
// src/core/select.ts
|
|
161
|
+
import * as Y4 from "yjs";
|
|
162
|
+
|
|
163
|
+
// src/core/key-value.ts
|
|
161
164
|
import * as Y3 from "yjs";
|
|
165
|
+
var isEntry = (item) => item !== null && typeof item === "object" && !Array.isArray(item) && !(item instanceof Y3.AbstractType) && typeof item.key === "string";
|
|
166
|
+
function currentEntry(array2, key) {
|
|
167
|
+
const items = array2.toArray();
|
|
168
|
+
for (let n = items.length - 1; n >= 0; n--) {
|
|
169
|
+
const item = items[n];
|
|
170
|
+
if (isEntry(item) && item.key === key) return item;
|
|
171
|
+
}
|
|
172
|
+
return void 0;
|
|
173
|
+
}
|
|
174
|
+
function currentEntries(array2) {
|
|
175
|
+
const seen = /* @__PURE__ */ new Map();
|
|
176
|
+
const items = array2.toArray();
|
|
177
|
+
for (let n = items.length - 1; n >= 0; n--) {
|
|
178
|
+
const item = items[n];
|
|
179
|
+
if (isEntry(item) && !seen.has(item.key)) seen.set(item.key, item);
|
|
180
|
+
}
|
|
181
|
+
return new Map([...seen].reverse());
|
|
182
|
+
}
|
|
183
|
+
var touchesKeys = (keys) => (event) => {
|
|
184
|
+
if (!(event instanceof Y3.YArrayEvent)) return true;
|
|
185
|
+
const { added, deleted } = event.changes;
|
|
186
|
+
for (const item of [...added, ...deleted]) {
|
|
187
|
+
const content = item.content?.getContent();
|
|
188
|
+
if (!content) return true;
|
|
189
|
+
if (content.some((entry) => isEntry(entry) && keys.includes(entry.key))) return true;
|
|
190
|
+
}
|
|
191
|
+
return false;
|
|
192
|
+
};
|
|
193
|
+
function setEntry(array2, key, val, equals) {
|
|
194
|
+
const items = array2.toArray();
|
|
195
|
+
const older = [];
|
|
196
|
+
items.forEach((item, n) => {
|
|
197
|
+
if (isEntry(item) && item.key === key) older.push(n);
|
|
198
|
+
});
|
|
199
|
+
if (older.length === 1 && equals(items[older[0]].val, val)) return;
|
|
200
|
+
const write2 = () => {
|
|
201
|
+
for (let n = older.length - 1; n >= 0; n--) array2.delete(older[n], 1);
|
|
202
|
+
array2.push([{ key, val }]);
|
|
203
|
+
};
|
|
204
|
+
if (array2.doc) array2.doc.transact(write2);
|
|
205
|
+
else write2();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// src/core/select.ts
|
|
162
209
|
var selector = (locator, is, project2, options = {}) => ({
|
|
163
210
|
locate: () => locator,
|
|
164
211
|
project: (node) => project2(is(node) ? node : void 0),
|
|
165
212
|
...options
|
|
166
213
|
});
|
|
167
|
-
var isText = (node) => node instanceof
|
|
214
|
+
var isText = (node) => node instanceof Y4.Text && !(node instanceof Y4.XmlText);
|
|
168
215
|
var asStored = (node) => true;
|
|
169
216
|
var kindOf = (value) => value === null ? "null" : Array.isArray(value) ? "array" : value instanceof Uint8Array ? "bytes" : isPlainObject(value) ? "object" : typeof value === "object" ? "other" : typeof value;
|
|
170
217
|
var sameKind = (value, fallback) => {
|
|
@@ -203,7 +250,9 @@ var MapPath = class _MapPath extends Path {
|
|
|
203
250
|
return isMap(node);
|
|
204
251
|
}
|
|
205
252
|
child(key, kind = "map") {
|
|
206
|
-
|
|
253
|
+
if (kind === "array") return new ArrayPath(this.locator.child(key));
|
|
254
|
+
if (kind === "keyValue") return new KeyValuePath(this.locator.child(key));
|
|
255
|
+
return new _MapPath(this.locator.child(key));
|
|
207
256
|
}
|
|
208
257
|
get(key, fallback) {
|
|
209
258
|
const safe = frozen(fallback);
|
|
@@ -256,7 +305,7 @@ var MapPath = class _MapPath extends Path {
|
|
|
256
305
|
};
|
|
257
306
|
var ArrayPath = class extends Path {
|
|
258
307
|
is(node) {
|
|
259
|
-
return node instanceof
|
|
308
|
+
return node instanceof Y4.Array;
|
|
260
309
|
}
|
|
261
310
|
/** The items as stored, `[]` when the array is missing. Compared shallowly. */
|
|
262
311
|
toArray() {
|
|
@@ -271,16 +320,66 @@ var ArrayPath = class extends Path {
|
|
|
271
320
|
return this.json([]);
|
|
272
321
|
}
|
|
273
322
|
};
|
|
323
|
+
var KeyValuePath = class extends Path {
|
|
324
|
+
is(node) {
|
|
325
|
+
return node instanceof Y4.Array;
|
|
326
|
+
}
|
|
327
|
+
/** The entry for `key`, for `typedAtom(path.key(k), schema)`. */
|
|
328
|
+
key(key) {
|
|
329
|
+
return new KeyPath(this.locator, key);
|
|
330
|
+
}
|
|
331
|
+
get(key, fallback) {
|
|
332
|
+
const safe = frozen(fallback);
|
|
333
|
+
return this.selector((node) => orFallback(node && currentEntry(node, key)?.val, safe), {
|
|
334
|
+
event: touchesKeys([key]),
|
|
335
|
+
equals: deepEqual
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
/** The keys that have an entry, sorted so every peer sees the same order. `[]` when missing. */
|
|
339
|
+
keys() {
|
|
340
|
+
return this.selector((node) => node ? [...currentEntries(node).keys()].sort() : [], {
|
|
341
|
+
equals: deepEqual
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
/** The current value of every key, as stored. `{}` when missing. Compared deeply. */
|
|
345
|
+
toJSON() {
|
|
346
|
+
return this.selector(
|
|
347
|
+
(node) => {
|
|
348
|
+
const out = {};
|
|
349
|
+
if (node)
|
|
350
|
+
for (const [key, entry] of currentEntries(node))
|
|
351
|
+
Object.defineProperty(out, key, {
|
|
352
|
+
value: entry.val,
|
|
353
|
+
enumerable: true,
|
|
354
|
+
writable: true,
|
|
355
|
+
configurable: true
|
|
356
|
+
});
|
|
357
|
+
return out;
|
|
358
|
+
},
|
|
359
|
+
{ equals: deepEqual }
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
var KeyPath = class {
|
|
364
|
+
constructor(locator, key) {
|
|
365
|
+
this.locator = locator;
|
|
366
|
+
this.key = key;
|
|
367
|
+
}
|
|
368
|
+
locator;
|
|
369
|
+
key;
|
|
370
|
+
};
|
|
274
371
|
var select = {
|
|
275
372
|
map: (name) => new MapPath(at.map(name)),
|
|
276
|
-
array: (name) => new ArrayPath(at.array(name))
|
|
373
|
+
array: (name) => new ArrayPath(at.array(name)),
|
|
374
|
+
/** A root `Y.Array` that holds a y-utility `YKeyValue`. */
|
|
375
|
+
keyValue: (name) => new KeyValuePath(at.array(name))
|
|
277
376
|
};
|
|
278
377
|
|
|
279
378
|
// src/schema/read.ts
|
|
280
|
-
import * as
|
|
379
|
+
import * as Y7 from "yjs";
|
|
281
380
|
|
|
282
381
|
// src/schema/copy.ts
|
|
283
|
-
import * as
|
|
382
|
+
import * as Y5 from "yjs";
|
|
284
383
|
var setField = (target, key, value) => Object.defineProperty(target, key, {
|
|
285
384
|
value,
|
|
286
385
|
enumerable: true,
|
|
@@ -292,7 +391,7 @@ var pathOf = (at2) => {
|
|
|
292
391
|
for (let link = at2; link; link = link.parent) keys.push(link.key);
|
|
293
392
|
return keys.reverse();
|
|
294
393
|
};
|
|
295
|
-
var isShared = (value) => value instanceof
|
|
394
|
+
var isShared = (value) => value instanceof Y5.AbstractType || value instanceof Y5.Doc;
|
|
296
395
|
function copy(stored, report2) {
|
|
297
396
|
if (!isContainer(stored)) return stored;
|
|
298
397
|
if (stored instanceof Uint8Array) return new Uint8Array(stored);
|
|
@@ -390,14 +489,14 @@ var isPlainObject2 = (value) => {
|
|
|
390
489
|
};
|
|
391
490
|
|
|
392
491
|
// src/schema/shape.ts
|
|
393
|
-
import * as
|
|
492
|
+
import * as Y6 from "yjs";
|
|
394
493
|
var map = (shape) => Object.freeze({ kind: "map", shape });
|
|
395
494
|
var record = (itemShape) => Object.freeze({ kind: "record", shape: itemShape });
|
|
396
495
|
var arrayKind = Object.freeze({ kind: "array" });
|
|
397
496
|
var array = () => arrayKind;
|
|
398
|
-
var isText2 = (stored) => stored instanceof
|
|
399
|
-
var isMap2 = (value) => value instanceof
|
|
400
|
-
var isXml = (value) => value instanceof
|
|
497
|
+
var isText2 = (stored) => stored instanceof Y6.Text && !(stored instanceof Y6.XmlText);
|
|
498
|
+
var isMap2 = (value) => value instanceof Y6.Map && !(value instanceof Y6.XmlHook);
|
|
499
|
+
var isXml = (value) => value instanceof Y6.XmlFragment && !(value instanceof Y6.XmlElement);
|
|
401
500
|
function kindOf2(declared) {
|
|
402
501
|
if (declared === "text" || declared === "xml") return declared;
|
|
403
502
|
if (declared === null || typeof declared !== "object") return void 0;
|
|
@@ -494,15 +593,15 @@ var yMap = "a Y.Map";
|
|
|
494
593
|
var yArray = "a Y.Array";
|
|
495
594
|
var yXmlFragment = "a Y.XmlFragment";
|
|
496
595
|
function describeFound(value) {
|
|
497
|
-
if (value instanceof
|
|
498
|
-
if (value instanceof
|
|
499
|
-
if (value instanceof
|
|
500
|
-
if (value instanceof
|
|
501
|
-
if (value instanceof
|
|
502
|
-
if (value instanceof
|
|
503
|
-
if (value instanceof
|
|
504
|
-
if (value instanceof
|
|
505
|
-
if (value instanceof
|
|
596
|
+
if (value instanceof Y7.Doc) return "a Y.Doc";
|
|
597
|
+
if (value instanceof Y7.XmlElement) return "a Y.XmlElement";
|
|
598
|
+
if (value instanceof Y7.XmlFragment) return yXmlFragment;
|
|
599
|
+
if (value instanceof Y7.XmlText) return "a Y.XmlText";
|
|
600
|
+
if (value instanceof Y7.XmlHook) return "a Y.XmlHook";
|
|
601
|
+
if (value instanceof Y7.Text) return yText;
|
|
602
|
+
if (value instanceof Y7.Map) return yMap;
|
|
603
|
+
if (value instanceof Y7.Array) return yArray;
|
|
604
|
+
if (value instanceof Y7.AbstractType) return "a shared type";
|
|
506
605
|
if (value === null) return "null";
|
|
507
606
|
if (Array.isArray(value)) return "an array";
|
|
508
607
|
if (value instanceof Uint8Array) return "bytes";
|
|
@@ -538,7 +637,7 @@ function project(shape, node) {
|
|
|
538
637
|
if (isXml(stored)) setField(value, key, stored);
|
|
539
638
|
else report(issues, mismatch(here, yXmlFragment, stored));
|
|
540
639
|
} else if (kind.kind === "array") {
|
|
541
|
-
if (stored instanceof
|
|
640
|
+
if (stored instanceof Y7.Array)
|
|
542
641
|
setField(value, key, copy(stored.toArray(), reportAt(here)));
|
|
543
642
|
else report(issues, mismatch(here, yArray, stored));
|
|
544
643
|
} else if (!isMap2(stored)) report(issues, mismatch(here, yMap, stored));
|
|
@@ -581,11 +680,11 @@ function project(shape, node) {
|
|
|
581
680
|
}
|
|
582
681
|
|
|
583
682
|
// src/schema/write.ts
|
|
584
|
-
import * as
|
|
683
|
+
import * as Y8 from "yjs";
|
|
585
684
|
function write(schema, shape, node, value) {
|
|
586
685
|
if (!isMap2(node))
|
|
587
686
|
throw new TypeError(
|
|
588
|
-
node instanceof
|
|
687
|
+
node instanceof Y8.AbstractType ? "write needs a Y.Map, not another shared type (a Y.XmlHook is one); write its parent to repair it." : "write needs a located Y.Map; write its parent to create a missing one."
|
|
589
688
|
);
|
|
590
689
|
const doc = node.doc;
|
|
591
690
|
if (!doc) throw new TypeError("write needs a Y.Map that is in a Y.Doc.");
|
|
@@ -627,7 +726,7 @@ var Target = class {
|
|
|
627
726
|
for (let target = this; !target.#map; target = target.parent) missing.push(target);
|
|
628
727
|
for (let n = missing.length - 1; n >= 0; n--) {
|
|
629
728
|
const target = missing[n];
|
|
630
|
-
target.#map = new
|
|
729
|
+
target.#map = new Y8.Map();
|
|
631
730
|
target.parent.#map.set(target.key, target.#map);
|
|
632
731
|
}
|
|
633
732
|
return this.#map;
|
|
@@ -667,11 +766,11 @@ function plan(shape, root, value) {
|
|
|
667
766
|
if (typeof field !== "string") throw notStorable(here, "a string, null or undefined");
|
|
668
767
|
if (isText2(current)) changes.push(() => splice(current, field));
|
|
669
768
|
else if (field !== "" || has)
|
|
670
|
-
changes.push(() => target.ensure().set(key, new
|
|
769
|
+
changes.push(() => target.ensure().set(key, new Y8.Text(field)));
|
|
671
770
|
} else if (kind.kind === "array") {
|
|
672
771
|
if (!Array.isArray(field)) throw notStorable(here, "an array, null or undefined");
|
|
673
772
|
const items = Array.from(field);
|
|
674
|
-
const array2 = current instanceof
|
|
773
|
+
const array2 = current instanceof Y8.Array ? current : void 0;
|
|
675
774
|
const { start, removed, inserted } = diff(array2?.toArray() ?? [], items);
|
|
676
775
|
inserted.forEach((item, n) => {
|
|
677
776
|
const itemAt = { key: start + n, parent: here };
|
|
@@ -686,7 +785,7 @@ function plan(shape, root, value) {
|
|
|
686
785
|
});
|
|
687
786
|
} else if (items.length > 0 || has)
|
|
688
787
|
changes.push(() => {
|
|
689
|
-
const created = new
|
|
788
|
+
const created = new Y8.Array();
|
|
690
789
|
target.ensure().set(key, created);
|
|
691
790
|
created.insert(0, items.map(copyItem));
|
|
692
791
|
});
|
|
@@ -718,7 +817,7 @@ function plan(shape, root, value) {
|
|
|
718
817
|
}
|
|
719
818
|
changes.push(() => {
|
|
720
819
|
for (const [target, key] of fragments)
|
|
721
|
-
if (target.map && !isXml(target.map.get(key))) target.map.set(key, new
|
|
820
|
+
if (target.map && !isXml(target.map.get(key))) target.map.set(key, new Y8.XmlFragment());
|
|
722
821
|
});
|
|
723
822
|
return changes;
|
|
724
823
|
}
|
|
@@ -800,7 +899,47 @@ function offsetsOf(text) {
|
|
|
800
899
|
var isHighSurrogate = (code) => code >= 55296 && code <= 56319;
|
|
801
900
|
var isLowSurrogate = (code) => code >= 56320 && code <= 57343;
|
|
802
901
|
|
|
902
|
+
// src/schema/key-value.ts
|
|
903
|
+
import * as Y9 from "yjs";
|
|
904
|
+
function readKey(schema, array2, key) {
|
|
905
|
+
if (!(array2 instanceof Y9.Array)) return void 0;
|
|
906
|
+
const entry = currentEntry(array2, key);
|
|
907
|
+
return entry && readValue(schema, entry.val, key);
|
|
908
|
+
}
|
|
909
|
+
function readValue(schema, stored, key) {
|
|
910
|
+
const issues = [];
|
|
911
|
+
const onShared = (_found, at3) => issues.push({
|
|
912
|
+
message: "Shape mismatch: expected a plain value, found a shared type",
|
|
913
|
+
// Relative to the value, like the schema's own issues: drop the key itself.
|
|
914
|
+
path: pathOf(at3).slice(1)
|
|
915
|
+
});
|
|
916
|
+
const at2 = { key, parent: void 0 };
|
|
917
|
+
if (isShared(stored)) onShared(stored, at2);
|
|
918
|
+
const value = copy(stored, { at: at2, onShared });
|
|
919
|
+
if (issues.length > 0) return { issues };
|
|
920
|
+
return validate(schema, value);
|
|
921
|
+
}
|
|
922
|
+
function writeKey(schema, array2, key, value) {
|
|
923
|
+
if (!(array2 instanceof Y9.Array)) throw new TypeError("writeKey needs a Y.Array.");
|
|
924
|
+
const result = validate(schema, value);
|
|
925
|
+
if (result.issues) return result;
|
|
926
|
+
const at2 = { key, parent: void 0 };
|
|
927
|
+
assertStorable(value, at2);
|
|
928
|
+
setEntry(array2, key, copy(value), deepEqual2);
|
|
929
|
+
return result;
|
|
930
|
+
}
|
|
931
|
+
function keyReader(schema, key) {
|
|
932
|
+
return {
|
|
933
|
+
project: (node) => readKey(schema, node, key),
|
|
934
|
+
deep: false,
|
|
935
|
+
event: touchesKeys([key]),
|
|
936
|
+
equals: deepEqual2
|
|
937
|
+
};
|
|
938
|
+
}
|
|
939
|
+
|
|
803
940
|
export {
|
|
941
|
+
copy,
|
|
942
|
+
deepEqual2 as deepEqual,
|
|
804
943
|
map,
|
|
805
944
|
record,
|
|
806
945
|
array,
|
|
@@ -812,13 +951,23 @@ export {
|
|
|
812
951
|
reader,
|
|
813
952
|
write,
|
|
814
953
|
create,
|
|
954
|
+
assertStorable,
|
|
955
|
+
currentEntries,
|
|
956
|
+
touchesKeys,
|
|
957
|
+
setEntry,
|
|
958
|
+
readKey,
|
|
959
|
+
readValue,
|
|
960
|
+
writeKey,
|
|
961
|
+
keyReader,
|
|
815
962
|
shallowEqual,
|
|
816
|
-
deepEqual,
|
|
963
|
+
deepEqual as deepEqual2,
|
|
817
964
|
Locator,
|
|
818
965
|
at,
|
|
819
966
|
observeLocated,
|
|
820
967
|
live,
|
|
821
968
|
MapPath,
|
|
822
969
|
ArrayPath,
|
|
970
|
+
KeyValuePath,
|
|
971
|
+
KeyPath,
|
|
823
972
|
select
|
|
824
973
|
};
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
|
+
KeyPath,
|
|
2
3
|
Locator,
|
|
3
4
|
MapPath,
|
|
4
5
|
create,
|
|
5
6
|
isMap,
|
|
7
|
+
keyReader,
|
|
6
8
|
live,
|
|
7
9
|
read,
|
|
8
10
|
reader,
|
|
9
|
-
write
|
|
10
|
-
|
|
11
|
+
write,
|
|
12
|
+
writeKey
|
|
13
|
+
} from "./chunk-7TXOG3GB.js";
|
|
11
14
|
|
|
12
15
|
// src/atoms/live-atom.ts
|
|
13
16
|
import { atom } from "jotai";
|
|
@@ -62,8 +65,41 @@ function versionedLiveAtom(spec, options, refreshers = []) {
|
|
|
62
65
|
|
|
63
66
|
// src/atoms/typed-atom.ts
|
|
64
67
|
import { atom as atom2 } from "jotai";
|
|
65
|
-
|
|
66
|
-
|
|
68
|
+
import * as Y from "yjs";
|
|
69
|
+
function typedAtom(locator, schema, shapeOrOptions, options = {}) {
|
|
70
|
+
if (locator instanceof KeyPath)
|
|
71
|
+
return typedKeyAtom(locator, schema, shapeOrOptions ?? {});
|
|
72
|
+
return refreshedTypedAtom(locator, schema, shapeOrOptions ?? {}, options, []);
|
|
73
|
+
}
|
|
74
|
+
var missingArray = "The key-value Y.Array is missing";
|
|
75
|
+
function typedKeyAtom(path, schema, options) {
|
|
76
|
+
const { locator, key } = path;
|
|
77
|
+
const { live: live2, version } = versionedLiveAtom(
|
|
78
|
+
{ locate: () => locator, ...keyReader(schema, key) },
|
|
79
|
+
options
|
|
80
|
+
);
|
|
81
|
+
return atom2(
|
|
82
|
+
(get) => get(live2),
|
|
83
|
+
(get, set, update) => {
|
|
84
|
+
const doc = readDoc(get, options);
|
|
85
|
+
let result;
|
|
86
|
+
doc.transact(() => {
|
|
87
|
+
const array = locator.resolve(doc);
|
|
88
|
+
if (!(array instanceof Y.Array))
|
|
89
|
+
return void (result = { issues: [{ message: missingArray }] });
|
|
90
|
+
let value = update;
|
|
91
|
+
if (typeof update === "function") {
|
|
92
|
+
const current = keyReader(schema, key).project(array);
|
|
93
|
+
value = update(
|
|
94
|
+
current && !current.issues ? current.value : void 0
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
result = writeKey(schema, array, key, value);
|
|
98
|
+
});
|
|
99
|
+
if (!result.issues) set(version, (n) => n + 1);
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
);
|
|
67
103
|
}
|
|
68
104
|
function refreshedTypedAtom(locator, schema, shape, options, refreshers) {
|
|
69
105
|
const located = locator instanceof MapPath ? locator.locator : locator;
|