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/llms-full.txt
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# duratom: full documentation
|
|
2
2
|
|
|
3
|
-
Every duratom doc in one file: the
|
|
3
|
+
Every duratom doc in one file: the guide, the recipes and the API reference.
|
|
4
4
|
|
|
5
|
-
<!--
|
|
5
|
+
<!-- docs/guide.md -->
|
|
6
6
|
|
|
7
7
|
## duratom
|
|
8
8
|
|
|
@@ -70,7 +70,7 @@ createRoot(document.getElementById('root')!).render(
|
|
|
70
70
|
)
|
|
71
71
|
```
|
|
72
72
|
|
|
73
|
-
Add a sync provider to the doc ([sync recipe](
|
|
73
|
+
Add a sync provider to the doc ([sync recipe](recipes/sync.md)) and the same atom shows and edits the todo on every device, with no change to the atom or the component.
|
|
74
74
|
|
|
75
75
|
### Rules
|
|
76
76
|
|
|
@@ -180,6 +180,56 @@ export function CardRow({ id }: { id: string }) {
|
|
|
180
180
|
}
|
|
181
181
|
```
|
|
182
182
|
|
|
183
|
+
### Key-value stores
|
|
184
|
+
|
|
185
|
+
State that is written very often in alternating order, such as knobs and XY pads, makes a `Y.Map` grow. y-utility's [`YKeyValue`](https://github.com/yjs/y-utility) keeps it small: `{ key, val }` entries in a `Y.Array`, where the rightmost entry per key is the current one and a write deletes the older ones. `select.keyValue(name)` reads and writes the same entries, so duratom and a `YKeyValue` can share a doc, and duratom needs no y-utility of its own.
|
|
186
|
+
|
|
187
|
+
`typedAtom(path.key(k), schema)` is one validated key: it re-renders only when that key's current value changes, and setting it takes the whole value, since a key holds a plain value rather than fields. `keyValueAtom(path, schemas)` reads several keys with a schema each. A current value the schema rejects reads as `{ issues }`, like an invalid map field, and writing a valid value repairs it.
|
|
188
|
+
|
|
189
|
+
```tsx
|
|
190
|
+
import { keyValueAtom, select, typedAtom } from 'duratom'
|
|
191
|
+
import { useAtom, useAtomValue } from 'jotai'
|
|
192
|
+
import { z } from 'zod'
|
|
193
|
+
|
|
194
|
+
// The root array `master` holds a y-utility YKeyValue: { key, val } entries, the rightmost wins.
|
|
195
|
+
const master = select.keyValue('master')
|
|
196
|
+
|
|
197
|
+
// One key: re-renders only when `tone` changes, not when another knob moves.
|
|
198
|
+
const toneAtom = typedAtom(master.key('tone'), z.number().min(-1).max(1))
|
|
199
|
+
|
|
200
|
+
export function ToneKnob() {
|
|
201
|
+
const [tone, setTone] = useAtom(toneAtom)
|
|
202
|
+
const value = tone && !tone.issues ? tone.value : 0
|
|
203
|
+
return (
|
|
204
|
+
<input
|
|
205
|
+
type="range"
|
|
206
|
+
min={-1}
|
|
207
|
+
max={1}
|
|
208
|
+
step={0.01}
|
|
209
|
+
value={value}
|
|
210
|
+
// Validated, then written as YKeyValue#set writes it: append, delete the older entry.
|
|
211
|
+
onChange={(e) => setTone(Number(e.target.value))}
|
|
212
|
+
/>
|
|
213
|
+
)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Several keys at once, a schema per key; keys it doesn't name are ignored.
|
|
217
|
+
const XY = z.object({ x: z.number(), y: z.number() })
|
|
218
|
+
const masterAtom = keyValueAtom(master, { tone: z.number(), filter: XY })
|
|
219
|
+
|
|
220
|
+
export function MasterSummary() {
|
|
221
|
+
const { tone, filter } = useAtomValue(masterAtom)
|
|
222
|
+
return (
|
|
223
|
+
<p>
|
|
224
|
+
tone {tone && !tone.issues ? tone.value : '–'}, filter{' '}
|
|
225
|
+
{filter && !filter.issues ? `${filter.value.x}, ${filter.value.y}` : '–'}
|
|
226
|
+
</p>
|
|
227
|
+
)
|
|
228
|
+
}
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
`.get(key, fallback)`, `.keys()` and `.toJSON()` read a key-value store without a schema, and `readKey`, `writeKey` and `keyReader` from `duratom/core` work without Jotai. Values are plain only: a `YKeyValue` holds no shared types.
|
|
232
|
+
|
|
183
233
|
### Custom projections
|
|
184
234
|
|
|
185
235
|
`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.
|
|
@@ -278,16 +328,12 @@ export { result }
|
|
|
278
328
|
### Docs
|
|
279
329
|
|
|
280
330
|
- [API reference](https://duratom.karolnov.dev/api): every export, generated from the TSDoc.
|
|
281
|
-
- Recipes: [sync between devices](
|
|
282
|
-
- 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.
|
|
283
|
-
- 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`.
|
|
331
|
+
- Recipes: [sync between devices](recipes/sync.md), [offline storage](recipes/offline.md), [repair a broken node](recipes/repair.md).
|
|
332
|
+
- Example apps: [a two-pane shared todo list](https://github.com/KarolNov/duratom/tree/main/examples/todo), and [an offline-first todo list synced through a Durable Object](https://github.com/KarolNov/duratom/tree/main/examples/sync), with end-to-end tests.
|
|
333
|
+
- For AI agents: [`llms.txt`](https://duratom.karolnov.dev/llms.txt), [`llms-full.txt`](https://duratom.karolnov.dev/llms-full.txt) (every doc in one file) and an agent skill, [`skills/duratom/SKILL.md`](https://github.com/KarolNov/duratom/tree/main/skills/duratom/SKILL.md). The npm package ships `llms-full.txt` and the skill, so an agent finds them in `node_modules/duratom`.
|
|
284
334
|
|
|
285
335
|
Every code snippet in these docs is compiled against the source in CI, and so is every `@example` in the TSDoc.
|
|
286
336
|
|
|
287
|
-
### License
|
|
288
|
-
|
|
289
|
-
MIT
|
|
290
|
-
|
|
291
337
|
<!-- docs/recipes/sync.md -->
|
|
292
338
|
|
|
293
339
|
## Sync between devices
|
|
@@ -587,6 +633,79 @@ deepEqual({ tags: ['a'] }, { tags: ['a'] }) // true
|
|
|
587
633
|
const tags = liveAtom({ ...select.map('card').get('tags', []), equals: deepEqual })
|
|
588
634
|
```
|
|
589
635
|
|
|
636
|
+
##### `KeyPath`
|
|
637
|
+
|
|
638
|
+
```ts
|
|
639
|
+
class KeyPath
|
|
640
|
+
```
|
|
641
|
+
|
|
642
|
+
One key of a key-value array (see `KeyValuePath`): `select.keyValue(name).key(k)`. `typedAtom`
|
|
643
|
+
takes one for a validated atom over that key's current value.
|
|
644
|
+
|
|
645
|
+
```ts
|
|
646
|
+
const tone = select.keyValue('master').key('tone')
|
|
647
|
+
const toneAtom = typedAtom(tone, z.number())
|
|
648
|
+
```
|
|
649
|
+
|
|
650
|
+
- `constructor(/** Where the key-value array is. */ readonly locator: Locator<Y.Array<unknown>>, readonly key: string)`
|
|
651
|
+
- `readonly locator: Locator<Y.Array<unknown>>`
|
|
652
|
+
|
|
653
|
+
Where the key-value array is.
|
|
654
|
+
|
|
655
|
+
- `readonly key: string`
|
|
656
|
+
|
|
657
|
+
##### `KeyValuePath`
|
|
658
|
+
|
|
659
|
+
```ts
|
|
660
|
+
class KeyValuePath<V = unknown>
|
|
661
|
+
```
|
|
662
|
+
|
|
663
|
+
A path to a `Y.Array` that holds y-utility's `YKeyValue`: `{ key, val }` entries where the
|
|
664
|
+
rightmost entry per key is the current one. duratom reads and writes the same entries as
|
|
665
|
+
`YKeyValue`, so the two can share a doc. Items that aren't entries are skipped. End it with a
|
|
666
|
+
projection, or pass `key(k)` to `typedAtom` for a validated, writable atom over one key.
|
|
667
|
+
|
|
668
|
+
```ts
|
|
669
|
+
const master = select.keyValue('master')
|
|
670
|
+
const tone = liveAtom(master.get('tone', 0))
|
|
671
|
+
const toneAtom = typedAtom(master.key('tone'), z.number().min(-1).max(1))
|
|
672
|
+
```
|
|
673
|
+
|
|
674
|
+
- `key(key: string): KeyPath`
|
|
675
|
+
|
|
676
|
+
The entry for `key`, for `typedAtom(path.key(k), schema)`.
|
|
677
|
+
|
|
678
|
+
- `get(key: string): Selector<V | undefined>`
|
|
679
|
+
|
|
680
|
+
The current value of `key` as stored, `undefined` when it has no entry or the array is
|
|
681
|
+
missing. Notifies only when an entry for `key` is added or deleted and the value changed.
|
|
682
|
+
|
|
683
|
+
- `get<D>(key: string, fallback: D): Selector<undefined extends D ? V | Checked<D> : Checked<D>>`
|
|
684
|
+
|
|
685
|
+
The current value of `key` when it has `fallback`'s kind, else `fallback`, as `MapPath#get`
|
|
686
|
+
checks it. Notifies only when an entry for `key` changes the value.
|
|
687
|
+
|
|
688
|
+
- `keys(): Selector<string[]>`
|
|
689
|
+
|
|
690
|
+
The keys that have an entry, sorted so every peer sees the same order. `[]` when missing.
|
|
691
|
+
|
|
692
|
+
- `toJSON(): Selector<Record<string, V>>`
|
|
693
|
+
|
|
694
|
+
The current value of every key, as stored. `{}` when missing. Compared deeply.
|
|
695
|
+
|
|
696
|
+
- `readonly locator: Locator<Y.Array<unknown>>`
|
|
697
|
+
- `project<T>(project: (node: Y.Array<unknown> | undefined) => T, options?: ProjectOptions<T>): Selector<T>`
|
|
698
|
+
|
|
699
|
+
Your own projection of the node, `undefined` when it is missing or of
|
|
700
|
+
another kind. Observes deeply and compares with `deepEqual` unless
|
|
701
|
+
`options` say otherwise; `keys` alone observes only those keys.
|
|
702
|
+
|
|
703
|
+
- `project<T>(projection: Projection<T>): Selector<T>`
|
|
704
|
+
|
|
705
|
+
A packaged projection, such as `reader(schema, shape)`, with its own
|
|
706
|
+
observe options and `equals`. It gets the node as stored, whatever its kind.
|
|
707
|
+
|
|
708
|
+
|
|
590
709
|
##### `Locator`
|
|
591
710
|
|
|
592
711
|
```ts
|
|
@@ -635,6 +754,10 @@ const flags = liveAtom(card.pick({ done: false, starred: false }))
|
|
|
635
754
|
|
|
636
755
|
Descend into the `Y.Array` stored under `key`.
|
|
637
756
|
|
|
757
|
+
- `child<W = unknown>(key: string, kind: 'keyValue'): KeyValuePath<W>`
|
|
758
|
+
|
|
759
|
+
Descend into the `Y.Array` under `key` that holds a y-utility `YKeyValue`.
|
|
760
|
+
|
|
638
761
|
- `get(key: string): Selector<V | undefined>`
|
|
639
762
|
|
|
640
763
|
The value under `key`, or `undefined` when it or the map is missing. Observes only `key`.
|
|
@@ -689,6 +812,7 @@ const flags = liveAtom(card.pick({ done: false, starred: false }))
|
|
|
689
812
|
const select: {
|
|
690
813
|
map: <V = unknown>(name: string) => MapPath<V>;
|
|
691
814
|
array: <V = unknown>(name: string) => ArrayPath<V>;
|
|
815
|
+
keyValue: <V = unknown>(name: string) => KeyValuePath<V>;
|
|
692
816
|
}
|
|
693
817
|
```
|
|
694
818
|
|
|
@@ -699,6 +823,7 @@ at a root type of the doc, like `at`.
|
|
|
699
823
|
const title = liveAtom(select.map('cards').child('card-1').get('title', ''))
|
|
700
824
|
const ids = liveAtom(select.map('cards').keys())
|
|
701
825
|
const order = liveAtom(select.array<string>('order').toArray())
|
|
826
|
+
const tone = liveAtom(select.keyValue('master').get('tone', 0))
|
|
702
827
|
```
|
|
703
828
|
|
|
704
829
|
##### `shallowEqual`
|
|
@@ -794,8 +919,13 @@ function typedAtom<Schema extends StandardSchemaV1, const S extends object>(
|
|
|
794
919
|
locator: Locator<any> | MapPath<any>,
|
|
795
920
|
schema: Schema,
|
|
796
921
|
shape: Exact<S, StandardSchemaV1.InferOutput<Schema>>,
|
|
797
|
-
options
|
|
922
|
+
options?: AtomOptions,
|
|
798
923
|
): TypedAtom<StandardSchemaV1.InferOutput<Schema>>
|
|
924
|
+
function typedAtom<Schema extends StandardSchemaV1>(
|
|
925
|
+
key: KeyPath,
|
|
926
|
+
schema: Schema,
|
|
927
|
+
options?: AtomOptions,
|
|
928
|
+
): TypedKeyAtom<StandardSchemaV1.InferOutput<Schema>>
|
|
799
929
|
```
|
|
800
930
|
|
|
801
931
|
A typed, writable atom over the `Y.Map` at `locator`, read through `schema` and `shape`: a
|
|
@@ -818,6 +948,17 @@ an invalid node a valid value replaces what the schema rejected, keeping keys ne
|
|
|
818
948
|
nor the value names: that is how a node a peer broke is repaired. A shared type of another kind
|
|
819
949
|
where the map belongs reads as issues, and only writing its parent can replace it.
|
|
820
950
|
|
|
951
|
+
A typed, writable atom over one key of a key-value array (y-utility's `YKeyValue`), read
|
|
952
|
+
through `schema`: `select.keyValue(name).key(k)`. It reads the key's current entry (the
|
|
953
|
+
rightmost) as `undefined`, `{ issues }` or `{ value }`, and re-renders only when an entry for
|
|
954
|
+
that key changes its result, not when other keys change or a stale entry is cleaned up. A
|
|
955
|
+
current value the schema rejects reads as `{ issues }`; it does not fall back to an older entry.
|
|
956
|
+
|
|
957
|
+
Setting it validates the whole value, then writes it as `YKeyValue#set` does: pushes
|
|
958
|
+
`{ key, val }` and deletes the key's older entries, so a `YKeyValue` on the same doc reads it.
|
|
959
|
+
A function gets the key's valid value at write time, or `undefined`. `{ issues }` changes
|
|
960
|
+
nothing, and so does a missing array. Values are plain: a `YKeyValue` holds no shared types.
|
|
961
|
+
|
|
821
962
|
```ts
|
|
822
963
|
const Card = z.object({ title: z.string(), priority: z.enum(['low', 'high']) })
|
|
823
964
|
const cardAtom = typedAtom(select.map('cards').child('card-1'), Card, { title: 'text' })
|
|
@@ -827,6 +968,43 @@ store.set(cardAtom, { title: 'Ship it', priority: 'low' }) // creates the card
|
|
|
827
968
|
store.set(cardAtom, { priority: 'high' }) // changes only priority
|
|
828
969
|
```
|
|
829
970
|
|
|
971
|
+
```ts
|
|
972
|
+
const toneAtom = typedAtom(select.keyValue('master').key('tone'), z.number().min(-1).max(1))
|
|
973
|
+
|
|
974
|
+
store.get(toneAtom) // undefined, { issues } or { value }
|
|
975
|
+
store.set(toneAtom, -0.6)
|
|
976
|
+
store.set(toneAtom, (prev) => Math.min(1, (prev ?? 0) + 0.1))
|
|
977
|
+
```
|
|
978
|
+
|
|
979
|
+
##### `keyValueAtom`
|
|
980
|
+
|
|
981
|
+
```ts
|
|
982
|
+
function keyValueAtom<const S extends Schemas>(
|
|
983
|
+
path: KeyValuePath<any>,
|
|
984
|
+
schemas: S,
|
|
985
|
+
options: AtomOptions = {},
|
|
986
|
+
): KeyValueAtom<S>
|
|
987
|
+
```
|
|
988
|
+
|
|
989
|
+
A typed, writable atom over a whole key-value array (y-utility's `YKeyValue`), with one schema
|
|
990
|
+
per key. Keys `schemas` doesn't name are left out of the read and never written. It re-renders
|
|
991
|
+
only when an entry for one of its keys changes a result; for one key alone, use
|
|
992
|
+
`typedAtom(path.key(k), schema)`, which also ignores the other keys.
|
|
993
|
+
|
|
994
|
+
Setting it validates every key of the patch first: when any is invalid, nothing is written and
|
|
995
|
+
the result holds their issues, each path starting at its key. Otherwise each key is written as
|
|
996
|
+
`YKeyValue#set` does, in one transaction. A missing array changes nothing.
|
|
997
|
+
|
|
998
|
+
```ts
|
|
999
|
+
const master = keyValueAtom(select.keyValue('master'), {
|
|
1000
|
+
tone: z.number().min(-1).max(1),
|
|
1001
|
+
filter: z.object({ x: z.number(), y: z.number() }),
|
|
1002
|
+
})
|
|
1003
|
+
|
|
1004
|
+
store.get(master).tone // undefined, { issues } or { value }
|
|
1005
|
+
store.set(master, { tone: -0.6 })
|
|
1006
|
+
```
|
|
1007
|
+
|
|
830
1008
|
#### Types
|
|
831
1009
|
|
|
832
1010
|
##### `ArrayKind`
|
|
@@ -1023,6 +1201,15 @@ type CreateResult<T> =
|
|
|
1023
1201
|
|
|
1024
1202
|
What a collection's `create` returns: the schema's result, with the new item's id when valid.
|
|
1025
1203
|
|
|
1204
|
+
##### `KeyUpdate`
|
|
1205
|
+
|
|
1206
|
+
```ts
|
|
1207
|
+
type KeyUpdate<T> = T | ((prev: T | undefined) => T)
|
|
1208
|
+
```
|
|
1209
|
+
|
|
1210
|
+
What a key atom's setter takes: the whole new value, or a function of the key's value as it is
|
|
1211
|
+
in the doc at write time (`undefined` while it has no valid value) returning one.
|
|
1212
|
+
|
|
1026
1213
|
##### `Patch`
|
|
1027
1214
|
|
|
1028
1215
|
```ts
|
|
@@ -1046,6 +1233,36 @@ A Jotai atom over one `Y.Map` of the doc, read through a schema. It reads as `un
|
|
|
1046
1233
|
the node is missing, `{ issues }` while it doesn't satisfy the schema, and `{ value }`
|
|
1047
1234
|
otherwise. Setting it merges a patch and returns the schema's result.
|
|
1048
1235
|
|
|
1236
|
+
##### `TypedKeyAtom`
|
|
1237
|
+
|
|
1238
|
+
```ts
|
|
1239
|
+
type TypedKeyAtom<T> = WritableAtom<
|
|
1240
|
+
StandardSchemaV1.Result<T> | undefined,
|
|
1241
|
+
[update: KeyUpdate<T>],
|
|
1242
|
+
StandardSchemaV1.Result<T>
|
|
1243
|
+
>
|
|
1244
|
+
```
|
|
1245
|
+
|
|
1246
|
+
A Jotai atom over one key of a key-value array (y-utility's `YKeyValue`), read through a schema.
|
|
1247
|
+
It reads as `undefined` while the key has no entry, `{ issues }` while its current value doesn't
|
|
1248
|
+
satisfy the schema, and `{ value }` otherwise. Setting it validates and writes the whole value,
|
|
1249
|
+
and returns the schema's result.
|
|
1250
|
+
|
|
1251
|
+
##### `KeyValueAtom`
|
|
1252
|
+
|
|
1253
|
+
```ts
|
|
1254
|
+
type KeyValueAtom<S extends Schemas> = WritableAtom<
|
|
1255
|
+
{ [K in keyof S]: StandardSchemaV1.Result<Values<S>[K]> | undefined },
|
|
1256
|
+
[patch: Partial<Values<S>> | ((prev: Partial<Values<S>>) => Partial<Values<S>>)],
|
|
1257
|
+
StandardSchemaV1.Result<Partial<Values<S>>>
|
|
1258
|
+
>
|
|
1259
|
+
```
|
|
1260
|
+
|
|
1261
|
+
A Jotai atom over the keys of a key-value array that `schemas` names, each read through its
|
|
1262
|
+
own schema. It reads as an object with one result per key: `undefined` while the key has no
|
|
1263
|
+
entry, `{ issues }` while its value is invalid, `{ value }` otherwise. Setting it takes a patch
|
|
1264
|
+
of the keys to change, or a function of their valid values returning one.
|
|
1265
|
+
|
|
1049
1266
|
### `duratom/react`
|
|
1050
1267
|
|
|
1051
1268
|
#### Functions and values
|
|
@@ -1121,7 +1338,7 @@ if (card && !card.issues) setCard({ priority: 'high' })
|
|
|
1121
1338
|
|
|
1122
1339
|
### `duratom/core`
|
|
1123
1340
|
|
|
1124
|
-
Also exports `deepEqual`, `shallowEqual`, `LiveSpec`, `at`, `Locator`, `At`, `ObserveOptions`, `ArrayPath`, `MapPath`, `select`, `Projection`, `ProjectOptions`, `Selector`, documented under `duratom`.
|
|
1341
|
+
Also exports `deepEqual`, `shallowEqual`, `LiveSpec`, `at`, `Locator`, `At`, `ObserveOptions`, `ArrayPath`, `KeyPath`, `KeyValuePath`, `MapPath`, `select`, `Projection`, `ProjectOptions`, `Selector`, documented under `duratom`.
|
|
1125
1342
|
|
|
1126
1343
|
#### Functions and values
|
|
1127
1344
|
|
|
@@ -1219,6 +1436,24 @@ const Todo = z.object({ title: z.string(), done: z.boolean() })
|
|
|
1219
1436
|
create(Todo, { title: 'text' }, doc.getMap('todos'), 'todo-1', { title: 'Buy milk', done: false })
|
|
1220
1437
|
```
|
|
1221
1438
|
|
|
1439
|
+
##### `keyReader`
|
|
1440
|
+
|
|
1441
|
+
```ts
|
|
1442
|
+
function keyReader<Schema extends StandardSchemaV1>(
|
|
1443
|
+
schema: Schema,
|
|
1444
|
+
key: string,
|
|
1445
|
+
): KeyReader<StandardSchemaV1.InferOutput<Schema>>
|
|
1446
|
+
```
|
|
1447
|
+
|
|
1448
|
+
`readKey(schema, node, key)` packaged as a projection for `select.keyValue(name).project(...)`:
|
|
1449
|
+
it counts only changes that add or delete an entry for `key`, and compares results deeply.
|
|
1450
|
+
`typedAtom(path.key(k), schema)` reads through it.
|
|
1451
|
+
|
|
1452
|
+
```ts
|
|
1453
|
+
const tone = live(doc, select.keyValue('master').project(keyReader(z.number(), 'tone')))
|
|
1454
|
+
tone.subscribe((result) => console.log(result?.issues ?? result?.value))
|
|
1455
|
+
```
|
|
1456
|
+
|
|
1222
1457
|
##### `read`
|
|
1223
1458
|
|
|
1224
1459
|
```ts
|
|
@@ -1259,6 +1494,30 @@ const todo = live(doc, select.map('todos').child(id).project(reader(Todo, { titl
|
|
|
1259
1494
|
todo.subscribe((result) => console.log(result?.issues ?? result?.value))
|
|
1260
1495
|
```
|
|
1261
1496
|
|
|
1497
|
+
##### `readKey`
|
|
1498
|
+
|
|
1499
|
+
```ts
|
|
1500
|
+
function readKey<Schema extends StandardSchemaV1>(
|
|
1501
|
+
schema: Schema,
|
|
1502
|
+
array: Y.AbstractType<any> | undefined,
|
|
1503
|
+
key: string,
|
|
1504
|
+
): Result<Schema> | undefined
|
|
1505
|
+
```
|
|
1506
|
+
|
|
1507
|
+
Reads the current value of `key` in a key-value array (y-utility's `YKeyValue`: the rightmost
|
|
1508
|
+
`{ key, val }` entry per key wins) and validates it with `schema`, returning the schema's own
|
|
1509
|
+
result. A missing array or a key with no entry returns `undefined` without validating. A value
|
|
1510
|
+
holding a shared type returns the layer's own issue without validating, since a `YKeyValue`
|
|
1511
|
+
holds plain values only.
|
|
1512
|
+
|
|
1513
|
+
A current value the schema rejects reads as `{ issues }`, like an invalid map field: it does not
|
|
1514
|
+
fall back to an older entry, which a `YKeyValue` deletes anyway. Writing a valid value repairs it.
|
|
1515
|
+
|
|
1516
|
+
```ts
|
|
1517
|
+
const result = readKey(z.number(), doc.getArray('master'), 'tone')
|
|
1518
|
+
if (result && !result.issues) console.log(result.value)
|
|
1519
|
+
```
|
|
1520
|
+
|
|
1262
1521
|
##### `UnsupportedValueError`
|
|
1263
1522
|
|
|
1264
1523
|
```ts
|
|
@@ -1311,6 +1570,28 @@ const result = write(Todo, { title: 'text' }, node, { title: 'Ship it', done: tr
|
|
|
1311
1570
|
if (result.issues) console.warn(result.issues) // the doc is unchanged
|
|
1312
1571
|
```
|
|
1313
1572
|
|
|
1573
|
+
##### `writeKey`
|
|
1574
|
+
|
|
1575
|
+
```ts
|
|
1576
|
+
function writeKey<Schema extends StandardSchemaV1>(
|
|
1577
|
+
schema: Schema,
|
|
1578
|
+
array: Y.Array<any>,
|
|
1579
|
+
key: string,
|
|
1580
|
+
value: StandardSchemaV1.InferOutput<Schema>,
|
|
1581
|
+
): Result<Schema>
|
|
1582
|
+
```
|
|
1583
|
+
|
|
1584
|
+
Validates `value` with `schema` and, if it's valid, sets `key` of the key-value array as
|
|
1585
|
+
`YKeyValue#set` does: pushes `{ key, val }` to its end and deletes every older entry for `key`,
|
|
1586
|
+
in one transaction. Returns the schema's own result; an invalid value changes nothing, and so
|
|
1587
|
+
does a value equal to the current one. Throws `UnsupportedValueError` for a value Yjs can't store
|
|
1588
|
+
as written, since the schema accepted it.
|
|
1589
|
+
|
|
1590
|
+
```ts
|
|
1591
|
+
const result = writeKey(z.number().min(-1).max(1), doc.getArray('master'), 'tone', -0.6)
|
|
1592
|
+
if (result.issues) console.warn(result.issues) // the doc is unchanged
|
|
1593
|
+
```
|
|
1594
|
+
|
|
1314
1595
|
#### Types
|
|
1315
1596
|
|
|
1316
1597
|
##### `Live`
|
|
@@ -1339,6 +1620,22 @@ interface Live<T, N extends Y.AbstractType<any> = Y.AbstractType<any>> {
|
|
|
1339
1620
|
|
|
1340
1621
|
A read-only value over one node of a doc, in the shape `useSyncExternalStore` takes.
|
|
1341
1622
|
|
|
1623
|
+
##### `KeyReader`
|
|
1624
|
+
|
|
1625
|
+
```ts
|
|
1626
|
+
interface KeyReader<T> {
|
|
1627
|
+
project: (node: Y.AbstractType<any> | undefined) => StandardSchemaV1.Result<T> | undefined
|
|
1628
|
+
deep: false
|
|
1629
|
+
event: (event: Y.YEvent<any>) => boolean
|
|
1630
|
+
equals: (
|
|
1631
|
+
a: StandardSchemaV1.Result<T> | undefined,
|
|
1632
|
+
b: StandardSchemaV1.Result<T> | undefined,
|
|
1633
|
+
) => boolean
|
|
1634
|
+
}
|
|
1635
|
+
```
|
|
1636
|
+
|
|
1637
|
+
What `keyReader` returns: a projection of one key's validated value, with its event filter.
|
|
1638
|
+
|
|
1342
1639
|
##### `Reader`
|
|
1343
1640
|
|
|
1344
1641
|
```ts
|
package/llms.txt
CHANGED
|
@@ -8,7 +8,7 @@ The npm package ships `llms-full.txt` (every doc in one file) and an agent skill
|
|
|
8
8
|
|
|
9
9
|
## Docs
|
|
10
10
|
|
|
11
|
-
- [
|
|
11
|
+
- [Guide](https://duratom.karolnov.dev/): install, quick start, the main API by example
|
|
12
12
|
- [API reference](https://duratom.karolnov.dev/api): every export of `duratom`, `duratom/react` and `duratom/core`
|
|
13
13
|
- [Agent skill](https://github.com/KarolNov/duratom/blob/main/skills/duratom/SKILL.md): how to build with duratom, for coding agents
|
|
14
14
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "duratom",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Durable, live Jotai atoms over a Y.Doc: typed reads, validated writes, re-renders only on change.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"atoms",
|
|
@@ -47,20 +47,6 @@
|
|
|
47
47
|
"publishConfig": {
|
|
48
48
|
"access": "public"
|
|
49
49
|
},
|
|
50
|
-
"scripts": {
|
|
51
|
-
"build": "tsup",
|
|
52
|
-
"typecheck": "tsc --noEmit",
|
|
53
|
-
"test": "vitest run",
|
|
54
|
-
"format": "oxfmt --write",
|
|
55
|
-
"format:check": "oxfmt --check",
|
|
56
|
-
"docs": "node scripts/docs.ts",
|
|
57
|
-
"docs:check": "node scripts/docs.ts --check && tsc -p docs/tsconfig.json && tsc -p docs/snippets/server/tsconfig.json",
|
|
58
|
-
"site:build": "node scripts/site.ts",
|
|
59
|
-
"site:dev": "pnpm site:build && wrangler dev --config site/wrangler.jsonc",
|
|
60
|
-
"site:deploy": "pnpm site:build && wrangler deploy --config site/wrangler.jsonc",
|
|
61
|
-
"example:sync:deploy": "pnpm build && pnpm --filter duratom-example-sync run deploy",
|
|
62
|
-
"prepublishOnly": "pnpm build"
|
|
63
|
-
},
|
|
64
50
|
"dependencies": {
|
|
65
51
|
"@standard-schema/spec": "^1.0.0"
|
|
66
52
|
},
|
|
@@ -88,6 +74,7 @@
|
|
|
88
74
|
"wrangler": "4.156.0",
|
|
89
75
|
"y-indexeddb": "9.0.12",
|
|
90
76
|
"y-partyserver": "2.2.0",
|
|
77
|
+
"y-utility": "0.1.4",
|
|
91
78
|
"yjs": "^13.6.33",
|
|
92
79
|
"zod": "^4.6.7"
|
|
93
80
|
},
|
|
@@ -102,5 +89,17 @@
|
|
|
102
89
|
"optional": true
|
|
103
90
|
}
|
|
104
91
|
},
|
|
105
|
-
"
|
|
106
|
-
|
|
92
|
+
"scripts": {
|
|
93
|
+
"build": "tsup",
|
|
94
|
+
"typecheck": "tsc --noEmit",
|
|
95
|
+
"test": "vitest run",
|
|
96
|
+
"format": "oxfmt --write",
|
|
97
|
+
"format:check": "oxfmt --check",
|
|
98
|
+
"docs": "node scripts/docs.ts",
|
|
99
|
+
"docs:check": "node scripts/docs.ts --check && tsc -p docs/tsconfig.json && tsc -p docs/snippets/server/tsconfig.json",
|
|
100
|
+
"site:build": "node scripts/site.ts",
|
|
101
|
+
"site:dev": "pnpm site:build && wrangler dev --config site/wrangler.jsonc",
|
|
102
|
+
"site:deploy": "pnpm site:build && wrangler deploy --config site/wrangler.jsonc",
|
|
103
|
+
"example:sync:deploy": "pnpm build && pnpm --filter duratom-example-sync run deploy"
|
|
104
|
+
}
|
|
105
|
+
}
|
package/skills/duratom/SKILL.md
CHANGED
|
@@ -7,7 +7,7 @@ description: Build React apps whose state lives in a Yjs Y.Doc, through duratom'
|
|
|
7
7
|
|
|
8
8
|
duratom turns nodes of a Yjs `Y.Doc` into Jotai atoms. A typed atom reads one `Y.Map` through a Standard Schema validator (zod, valibot, arktype) and writes validated patches into it; a collection covers a map of such items; a live atom reads any projection of a node. Each atom re-renders only when its own value changes. Sync and offline storage come from Yjs providers attached to the doc, never from duratom.
|
|
9
9
|
|
|
10
|
-
The whole documentation is in `node_modules/duratom/llms-full.txt`: the
|
|
10
|
+
The whole documentation is in `node_modules/duratom/llms-full.txt`: the guide, the recipes and the API reference with every signature. Read it when this file doesn't answer a question; don't read duratom's source to find out how to use it.
|
|
11
11
|
|
|
12
12
|
## Rules
|
|
13
13
|
|
|
@@ -21,17 +21,18 @@ The whole documentation is in `node_modules/duratom/llms-full.txt`: the README,
|
|
|
21
21
|
|
|
22
22
|
## Which API
|
|
23
23
|
|
|
24
|
-
| Need
|
|
25
|
-
|
|
|
26
|
-
| One typed object at a known path
|
|
27
|
-
| A list of items with ids
|
|
28
|
-
|
|
|
29
|
-
|
|
|
30
|
-
| A
|
|
31
|
-
|
|
|
32
|
-
|
|
|
33
|
-
|
|
34
|
-
|
|
24
|
+
| Need | Use |
|
|
25
|
+
| -------------------------------------------- | --------------------------------------------------------------------------------------------------------- |
|
|
26
|
+
| One typed object at a known path | `typedAtom(select.map('root').child('key'), Schema, shape)` |
|
|
27
|
+
| A list of items with ids | `collection(select.map('items'), Schema, shape)`: `ids`, `item(id)`, `create`, `remove` |
|
|
28
|
+
| Often-written knobs in a y-utility YKeyValue | `typedAtom(select.keyValue('m').key('k'), Schema)` or `keyValueAtom(select.keyValue('m'), { k: Schema })` |
|
|
29
|
+
| One field, a count, a derived value | `liveAtom(select.map('m').get('field', fallback))` or `.project(fn)` |
|
|
30
|
+
| A selector that depends on props, in a hook | `useLiveAtom(() => select..., [deps])` from `duratom/react` |
|
|
31
|
+
| A typed object whose path depends on props | `useTypedAtom(() => select..., Schema, shape, [deps])` from `duratom/react` |
|
|
32
|
+
| No Jotai | `live`, `read`, `write` from `duratom/core` |
|
|
33
|
+
| Values derived from typed atoms | a plain Jotai `atom((get) => ...)` over them |
|
|
34
|
+
|
|
35
|
+
Paths: `select.map(name)` and `select.array(name)` start at a root type; `.child(key)` descends into a nested map, `.child(key, 'array')` into a nested array. Selectors end a path: `.get(key, fallback)`, `.pick(defaults)`, `.text(key)`, `.keys()`, `.toJSON()`, `.project(fn)` on a map; `.toArray()`, `.length()`, `.toJSON()` on an array. `select.keyValue(name)` (or `.child(key, 'keyValue')`) is a y-utility `YKeyValue` array: `.get(key, fallback)`, `.keys()`, `.toJSON()`, and `.key(k)` for `typedAtom`.
|
|
35
36
|
|
|
36
37
|
## Setup
|
|
37
38
|
|