duratom 0.0.0-stage → 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 +306 -2
- package/dist/chunk-GTI66UUE.js +824 -0
- package/dist/chunk-LTA6HTAG.js +129 -0
- package/dist/core/index.d.ts +104 -0
- package/dist/core/index.js +34 -0
- package/dist/equality-AjtpnVrF.d.ts +20 -0
- package/dist/index.d.ts +66 -0
- package/dist/index.js +104 -0
- package/dist/react/index.d.ts +52 -0
- package/dist/react/index.js +43 -0
- package/dist/shape--EeyM-2X.d.ts +339 -0
- package/dist/typed-atom-oZaH0A5l.d.ts +93 -0
- package/llms-full.txt +1361 -0
- package/llms.txt +24 -0
- package/package.json +104 -4
- package/skills/duratom/SKILL.md +170 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Locator,
|
|
3
|
+
MapPath,
|
|
4
|
+
create,
|
|
5
|
+
isMap,
|
|
6
|
+
live,
|
|
7
|
+
read,
|
|
8
|
+
reader,
|
|
9
|
+
write
|
|
10
|
+
} from "./chunk-GTI66UUE.js";
|
|
11
|
+
|
|
12
|
+
// src/atoms/live-atom.ts
|
|
13
|
+
import { atom } from "jotai";
|
|
14
|
+
var docAtom = atom(null);
|
|
15
|
+
function readDoc(get, options) {
|
|
16
|
+
const holder = options.docAtom ?? docAtom;
|
|
17
|
+
const doc = get(holder);
|
|
18
|
+
if (!doc) {
|
|
19
|
+
throw new Error(
|
|
20
|
+
holder === docAtom ? "docAtom has not been set on this store" : "The doc atom passed to this atom holds no doc on this store"
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
return doc;
|
|
24
|
+
}
|
|
25
|
+
function liveAtom(spec, options = {}) {
|
|
26
|
+
return versionedLiveAtom(spec, options).live;
|
|
27
|
+
}
|
|
28
|
+
function versionedLiveAtom(spec, options, refreshers = []) {
|
|
29
|
+
const version = atom(0);
|
|
30
|
+
const getDoc = (get) => readDoc(get, options);
|
|
31
|
+
const sources = /* @__PURE__ */ new WeakMap();
|
|
32
|
+
const sourceFor = (doc) => {
|
|
33
|
+
let source = sources.get(doc);
|
|
34
|
+
if (!source) sources.set(doc, source = live(doc, spec));
|
|
35
|
+
return source;
|
|
36
|
+
};
|
|
37
|
+
const snapshot = atom(null);
|
|
38
|
+
const live2 = atom(
|
|
39
|
+
(get) => {
|
|
40
|
+
get(version);
|
|
41
|
+
for (const refresher of refreshers) get(refresher);
|
|
42
|
+
const snap = get(snapshot);
|
|
43
|
+
return snap ? snap.value : sourceFor(getDoc(get)).get();
|
|
44
|
+
},
|
|
45
|
+
(get, set, message) => {
|
|
46
|
+
if (message.type === "stop") return set(snapshot, null);
|
|
47
|
+
const source = sourceFor(getDoc(get));
|
|
48
|
+
message.register(source.subscribe((value) => set(snapshot, { value })));
|
|
49
|
+
set(snapshot, { value: source.get() });
|
|
50
|
+
}
|
|
51
|
+
);
|
|
52
|
+
live2.onMount = (send) => {
|
|
53
|
+
let stop;
|
|
54
|
+
send({ type: "start", register: (fn) => stop = fn });
|
|
55
|
+
return () => {
|
|
56
|
+
stop?.();
|
|
57
|
+
send({ type: "stop" });
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
return { live: live2, version };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// src/atoms/typed-atom.ts
|
|
64
|
+
import { atom as atom2 } from "jotai";
|
|
65
|
+
function typedAtom(locator, schema, shape, options = {}) {
|
|
66
|
+
return refreshedTypedAtom(locator, schema, shape, options, []);
|
|
67
|
+
}
|
|
68
|
+
function refreshedTypedAtom(locator, schema, shape, options, refreshers) {
|
|
69
|
+
const located = locator instanceof MapPath ? locator.locator : locator;
|
|
70
|
+
const last = located.steps.at(-1);
|
|
71
|
+
const parent = last && "key" in last ? { locator: new Locator(located.steps.slice(0, -1)), key: last.key } : void 0;
|
|
72
|
+
const { live: live2, version } = versionedLiveAtom(
|
|
73
|
+
{ locate: () => located, ...reader(schema, shape) },
|
|
74
|
+
options,
|
|
75
|
+
refreshers
|
|
76
|
+
);
|
|
77
|
+
const update = (doc, patch) => {
|
|
78
|
+
const node = located.resolve(doc);
|
|
79
|
+
if (node === void 0) {
|
|
80
|
+
const map = parent?.locator.resolve(doc);
|
|
81
|
+
if (!parent || !isMap(map)) return { issues: [{ message: missingParent }] };
|
|
82
|
+
const whole = typeof patch === "function" ? patch(void 0) : patch;
|
|
83
|
+
return create(schema, shape, map, parent.key, whole);
|
|
84
|
+
}
|
|
85
|
+
if (!isMap(node)) return read(schema, shape, node);
|
|
86
|
+
const current = read(schema, shape, node);
|
|
87
|
+
const prev = current.issues ? void 0 : current.value;
|
|
88
|
+
const changes = typeof patch === "function" ? patch(prev) : patch;
|
|
89
|
+
const merged = {
|
|
90
|
+
...unknownKeys(node, prev ?? changes, shape),
|
|
91
|
+
...prev,
|
|
92
|
+
...changes
|
|
93
|
+
};
|
|
94
|
+
return write(schema, shape, node, merged);
|
|
95
|
+
};
|
|
96
|
+
return atom2(
|
|
97
|
+
(get) => get(live2),
|
|
98
|
+
(get, set, patch) => {
|
|
99
|
+
const doc = readDoc(get, options);
|
|
100
|
+
let result;
|
|
101
|
+
doc.transact(() => result = update(doc, patch));
|
|
102
|
+
if (!result.issues) set(version, (n) => n + 1);
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
var missingParent = "The node is missing, and so is the Y.Map it would be created in";
|
|
108
|
+
function unknownKeys(node, value, shape) {
|
|
109
|
+
const out = {};
|
|
110
|
+
const known = (key) => Object.hasOwn(shape, key) || value !== null && typeof value === "object" && Object.hasOwn(value, key);
|
|
111
|
+
for (const [key, stored] of node.entries())
|
|
112
|
+
if (!known(key))
|
|
113
|
+
Object.defineProperty(out, key, {
|
|
114
|
+
value: stored,
|
|
115
|
+
enumerable: true,
|
|
116
|
+
writable: true,
|
|
117
|
+
configurable: true
|
|
118
|
+
});
|
|
119
|
+
return out;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export {
|
|
123
|
+
docAtom,
|
|
124
|
+
readDoc,
|
|
125
|
+
liveAtom,
|
|
126
|
+
versionedLiveAtom,
|
|
127
|
+
typedAtom,
|
|
128
|
+
refreshedTypedAtom
|
|
129
|
+
};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
export { d as deepEqual, s as shallowEqual } from '../equality-AjtpnVrF.js';
|
|
2
|
+
import { E as Exact } from '../shape--EeyM-2X.js';
|
|
3
|
+
export { A as ArrayPath, a as At, L as Live, b as LiveSpec, c as Locator, M as MapPath, O as ObserveOptions, P as ProjectOptions, d as Projection, S as Selector, e as at, l as live, o as observeLocated, s as select } from '../shape--EeyM-2X.js';
|
|
4
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
5
|
+
import * as Y from 'yjs';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Projects a located `Y.Map` following `shape` and validates it with `schema`, returning the
|
|
9
|
+
* schema's own result. An absent node returns `undefined` without validating, and a node with a
|
|
10
|
+
* shape mismatch, or a shared type that isn't a `Y.Map`, returns the layer's own issues without
|
|
11
|
+
* validating.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const Todo = z.object({ title: z.string(), done: z.boolean() })
|
|
15
|
+
* const result = read(Todo, { title: 'text' }, doc.getMap('todos').get(id) as Y.Map<unknown>)
|
|
16
|
+
* if (result?.issues) console.warn(result.issues)
|
|
17
|
+
* else if (result) console.log(result.value.title)
|
|
18
|
+
*/
|
|
19
|
+
declare function read<Schema extends StandardSchemaV1, const S extends object>(schema: Schema, shape: Exact<S, StandardSchemaV1.InferOutput<Schema>>, node: Y.AbstractType<any> | undefined): StandardSchemaV1.Result<StandardSchemaV1.InferOutput<Schema>> | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* `read` packaged as a store binding's projection: `project` reads a located node through
|
|
22
|
+
* `schema` and `shape`, `deep` observes every change under it, `event` skips edits inside an
|
|
23
|
+
* `'xml'` field, which a read returns as the fragment itself, and `equals` compares two results
|
|
24
|
+
* deeply, since each read returns a fresh tree. It has the shape `project` of a
|
|
25
|
+
* `select` selector and a `liveAtom` spec take, so the schema layer and the observation core
|
|
26
|
+
* stay independent of each other.
|
|
27
|
+
*/
|
|
28
|
+
interface Reader<T> {
|
|
29
|
+
project: (node: Y.AbstractType<any> | undefined) => StandardSchemaV1.Result<T> | undefined;
|
|
30
|
+
deep: true;
|
|
31
|
+
event: (event: Y.YEvent<any>) => boolean;
|
|
32
|
+
equals: (a: StandardSchemaV1.Result<T> | undefined, b: StandardSchemaV1.Result<T> | undefined) => boolean;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* `read(schema, shape, node)` as a `Reader`, for `select...project(reader(schema, shape))`.
|
|
36
|
+
* `typedAtom` reads through it; use it for a read-only typed value outside Jotai.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* const Todo = z.object({ title: z.string(), done: z.boolean() })
|
|
40
|
+
* const todo = live(doc, select.map('todos').child(id).project(reader(Todo, { title: 'text' })))
|
|
41
|
+
* todo.subscribe((result) => console.log(result?.issues ?? result?.value))
|
|
42
|
+
*/
|
|
43
|
+
declare function reader<Schema extends StandardSchemaV1, const S extends object>(schema: Schema, shape: Exact<S, StandardSchemaV1.InferOutput<Schema>>): Reader<StandardSchemaV1.InferOutput<Schema>>;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Validates `value` with `schema` and, if it's valid, merges it into the located `Y.Map` so only
|
|
47
|
+
* the differences change. Returns the schema's own result; an invalid value changes nothing.
|
|
48
|
+
* Fields the value lacks are deleted, so pass the whole value; a typed atom merges a patch first.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* const Todo = z.object({ title: z.string(), done: z.boolean() })
|
|
52
|
+
* const node = doc.getMap('todos').get(id) as Y.Map<unknown>
|
|
53
|
+
* const result = write(Todo, { title: 'text' }, node, { title: 'Ship it', done: true })
|
|
54
|
+
* if (result.issues) console.warn(result.issues) // the doc is unchanged
|
|
55
|
+
*/
|
|
56
|
+
declare function write<Schema extends StandardSchemaV1, const S extends object>(schema: Schema, shape: Exact<S, StandardSchemaV1.InferOutput<Schema>>, node: Y.Map<unknown>, value: StandardSchemaV1.InferOutput<Schema>): StandardSchemaV1.Result<StandardSchemaV1.InferOutput<Schema>>;
|
|
57
|
+
/**
|
|
58
|
+
* Validates `value` with `schema` and, if it's valid, stores it as a new `Y.Map` under `key` of
|
|
59
|
+
* `parent`, replacing whatever was stored there. Returns the schema's own result; an invalid
|
|
60
|
+
* value creates nothing, and a value the shape can't store throws before anything is created.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* const Todo = z.object({ title: z.string(), done: z.boolean() })
|
|
64
|
+
* create(Todo, { title: 'text' }, doc.getMap('todos'), 'todo-1', { title: 'Buy milk', done: false })
|
|
65
|
+
*/
|
|
66
|
+
declare function create<Schema extends StandardSchemaV1, const S extends object>(schema: Schema, shape: Exact<S, StandardSchemaV1.InferOutput<Schema>>, parent: Y.Map<unknown>, key: string, value: StandardSchemaV1.InferOutput<Schema>): StandardSchemaV1.Result<StandardSchemaV1.InferOutput<Schema>>;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Thrown by `read` and `write` when the schema's `~standard.validate` returns a Promise. The layer
|
|
70
|
+
* validates synchronously only, so an async schema is a configuration error, not bad data.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* const Slow = z.object({ title: z.string() }).refine(async () => true)
|
|
74
|
+
* try {
|
|
75
|
+
* store.set(typedAtom(select.map('cards').child('c1'), Slow, {}), { title: 'Hi' })
|
|
76
|
+
* } catch (error) {
|
|
77
|
+
* if (error instanceof AsyncValidationUnsupportedError) console.error(error.vendor) // 'zod'
|
|
78
|
+
* }
|
|
79
|
+
*/
|
|
80
|
+
declare class AsyncValidationUnsupportedError extends Error {
|
|
81
|
+
readonly vendor: string;
|
|
82
|
+
constructor(vendor: string);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Thrown by `write` when a plain field or `array()` item holds a value Yjs can't store faithfully: anything but
|
|
86
|
+
* JSON values, `undefined` and `Uint8Array` bytes, a cycle, a `__proto__` key, or a `constructor`
|
|
87
|
+
* key on the field's own object. The schema accepted it, so it's a schema that's too loose, not bad
|
|
88
|
+
* data. Nothing is written.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* const Event = z.object({ at: z.date() }) // a Date syncs as {}, so it can't be stored
|
|
92
|
+
* try {
|
|
93
|
+
* store.set(typedAtom(select.map('events').child('e1'), Event, {}), { at: new Date() })
|
|
94
|
+
* } catch (error) {
|
|
95
|
+
* if (error instanceof UnsupportedValueError) console.error(error.path) // ['at']
|
|
96
|
+
* }
|
|
97
|
+
*/
|
|
98
|
+
declare class UnsupportedValueError extends Error {
|
|
99
|
+
/** Where the value sits in the written value, starting at the field's key. */
|
|
100
|
+
readonly path: readonly PropertyKey[];
|
|
101
|
+
constructor(path: readonly PropertyKey[]);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export { AsyncValidationUnsupportedError, type Reader, UnsupportedValueError, create, read, reader, write };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ArrayPath,
|
|
3
|
+
AsyncValidationUnsupportedError,
|
|
4
|
+
Locator,
|
|
5
|
+
MapPath,
|
|
6
|
+
UnsupportedValueError,
|
|
7
|
+
at,
|
|
8
|
+
create,
|
|
9
|
+
deepEqual,
|
|
10
|
+
live,
|
|
11
|
+
observeLocated,
|
|
12
|
+
read,
|
|
13
|
+
reader,
|
|
14
|
+
select,
|
|
15
|
+
shallowEqual,
|
|
16
|
+
write
|
|
17
|
+
} from "../chunk-GTI66UUE.js";
|
|
18
|
+
export {
|
|
19
|
+
ArrayPath,
|
|
20
|
+
AsyncValidationUnsupportedError,
|
|
21
|
+
Locator,
|
|
22
|
+
MapPath,
|
|
23
|
+
UnsupportedValueError,
|
|
24
|
+
at,
|
|
25
|
+
create,
|
|
26
|
+
deepEqual,
|
|
27
|
+
live,
|
|
28
|
+
observeLocated,
|
|
29
|
+
read,
|
|
30
|
+
reader,
|
|
31
|
+
select,
|
|
32
|
+
shallowEqual,
|
|
33
|
+
write
|
|
34
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether two values are the same, or objects or arrays whose own keys hold the same values
|
|
3
|
+
* (`Object.is`). The default `equals` of a `LiveSpec`.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* shallowEqual({ done: 1, total: 2 }, { done: 1, total: 2 }) // true
|
|
7
|
+
* shallowEqual({ tags: ['a'] }, { tags: ['a'] }) // false: two different arrays
|
|
8
|
+
*/
|
|
9
|
+
declare const shallowEqual: (a: unknown, b: unknown) => boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Compares arrays and plain objects all the way down, and bytes byte by byte.
|
|
12
|
+
* Any other object (a `Map`, a `Set`, a `Date`, a shared type) equals only itself.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* deepEqual({ tags: ['a'] }, { tags: ['a'] }) // true
|
|
16
|
+
* const tags = liveAtom({ ...select.map('card').get('tags', []), equals: deepEqual })
|
|
17
|
+
*/
|
|
18
|
+
declare const deepEqual: (a: unknown, b: unknown) => boolean;
|
|
19
|
+
|
|
20
|
+
export { deepEqual as d, shallowEqual as s };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { c as Locator, M as MapPath, E as Exact } from './shape--EeyM-2X.js';
|
|
2
|
+
export { f as ArrayKind, A as ArrayPath, a as At, b as LiveSpec, g as MapKind, O as ObserveOptions, P as ProjectOptions, d as Projection, R as RecordKind, S as Selector, h as Shape, i as array, e as at, j as isMap, m as map, r as record, s as select } from './shape--EeyM-2X.js';
|
|
3
|
+
export { d as deepEqual, s as shallowEqual } from './equality-AjtpnVrF.js';
|
|
4
|
+
import { T as TypedAtom, A as AtomOptions } from './typed-atom-oZaH0A5l.js';
|
|
5
|
+
export { P as Patch, d as docAtom, l as liveAtom, t as typedAtom } from './typed-atom-oZaH0A5l.js';
|
|
6
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
7
|
+
import { Atom, WritableAtom } from 'jotai';
|
|
8
|
+
import { AtomFamily } from 'jotai-family';
|
|
9
|
+
import 'yjs';
|
|
10
|
+
|
|
11
|
+
/** What a collection's `create` returns: the schema's result, with the new item's id when valid. */
|
|
12
|
+
type CreateResult<T> = (StandardSchemaV1.SuccessResult<T> & {
|
|
13
|
+
readonly id: string;
|
|
14
|
+
}) | StandardSchemaV1.FailureResult;
|
|
15
|
+
/**
|
|
16
|
+
* Atoms over a `Y.Map` of items, each item a `Y.Map` read through one schema. Write with
|
|
17
|
+
* `store.set` or `useSetAtom`, like any Jotai atom.
|
|
18
|
+
*/
|
|
19
|
+
interface Collection<T> {
|
|
20
|
+
/**
|
|
21
|
+
* The ids of every entry of the map, sorted so every peer sees the same order; `[]` while the
|
|
22
|
+
* map is missing. Notifies only when an id is added or removed, never when an item's fields
|
|
23
|
+
* change.
|
|
24
|
+
*/
|
|
25
|
+
readonly ids: Atom<string[]>;
|
|
26
|
+
/**
|
|
27
|
+
* The typed atom of the item under `id`, the same atom for the same id: it reads `undefined`,
|
|
28
|
+
* `{ issues }` or `{ value }` and merges a patch when set, as `typedAtom` does; setting a
|
|
29
|
+
* missing item creates it. The collection's `remove` drops the item's atom from the family, and
|
|
30
|
+
* `item.remove(id)` or `item.setShouldRemove` can drop others; an atom a caller still holds keeps
|
|
31
|
+
* working after it's dropped.
|
|
32
|
+
*/
|
|
33
|
+
readonly item: AtomFamily<string, TypedAtom<T>>;
|
|
34
|
+
/**
|
|
35
|
+
* Validates a whole value and adds it as a new item under `id`, or under a random UUID when no
|
|
36
|
+
* id is given. Returns the schema's result with the id. An invalid value, an id already in use,
|
|
37
|
+
* or a missing map adds nothing and returns `{ issues }`: the value's own issues when it has
|
|
38
|
+
* any, else one saying why it couldn't be added.
|
|
39
|
+
*/
|
|
40
|
+
readonly create: WritableAtom<null, [value: T, id?: string], CreateResult<T>>;
|
|
41
|
+
/** Removes the item under `id`. Returns whether there was one. */
|
|
42
|
+
readonly remove: WritableAtom<null, [id: string], boolean>;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* A collection over the `Y.Map` at `locator`, whose every entry is an item `Y.Map` read through
|
|
46
|
+
* `schema` and `shape`. `ids` lists the items and stays quiet while their fields change, `item(id)`
|
|
47
|
+
* is a `typedAtom` per item, and `create` and `remove` add and delete items.
|
|
48
|
+
*
|
|
49
|
+
* Like any duratom atom, `ids` and `item(id)` can read stale while nothing is subscribed, except
|
|
50
|
+
* right after the collection's own `create` or `remove`, or a write through `item(id)` that
|
|
51
|
+
* created the item. An entry a peer stored as something other than a `Y.Map` is still listed in
|
|
52
|
+
* `ids`, so it can be found and repaired; its `item` atom reads it as `{ issues }`, or as
|
|
53
|
+
* `undefined` for a plain value.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* const Todo = z.object({ title: z.string(), done: z.boolean() })
|
|
57
|
+
* const todos = collection(select.map('todos'), Todo, { title: 'text' })
|
|
58
|
+
*
|
|
59
|
+
* const { id } = store.set(todos.create, { title: 'Buy milk', done: false }) as { id: string }
|
|
60
|
+
* store.get(todos.ids) // [id]
|
|
61
|
+
* store.set(todos.item(id), { done: true }) // changes only done
|
|
62
|
+
* store.set(todos.remove, id)
|
|
63
|
+
*/
|
|
64
|
+
declare function collection<Schema extends StandardSchemaV1, const S extends object>(locator: Locator<any> | MapPath<any>, schema: Schema, shape: Exact<S, StandardSchemaV1.InferOutput<Schema>>, options?: AtomOptions): Collection<StandardSchemaV1.InferOutput<Schema>>;
|
|
65
|
+
|
|
66
|
+
export { AtomOptions, type Collection, type CreateResult, Locator, MapPath, TypedAtom, collection };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import {
|
|
2
|
+
docAtom,
|
|
3
|
+
liveAtom,
|
|
4
|
+
readDoc,
|
|
5
|
+
refreshedTypedAtom,
|
|
6
|
+
typedAtom,
|
|
7
|
+
versionedLiveAtom
|
|
8
|
+
} from "./chunk-LTA6HTAG.js";
|
|
9
|
+
import {
|
|
10
|
+
ArrayPath,
|
|
11
|
+
Locator,
|
|
12
|
+
MapPath,
|
|
13
|
+
array,
|
|
14
|
+
at,
|
|
15
|
+
create,
|
|
16
|
+
deepEqual,
|
|
17
|
+
isMap,
|
|
18
|
+
map,
|
|
19
|
+
record,
|
|
20
|
+
select,
|
|
21
|
+
shallowEqual,
|
|
22
|
+
validate
|
|
23
|
+
} from "./chunk-GTI66UUE.js";
|
|
24
|
+
|
|
25
|
+
// src/atoms/collection.ts
|
|
26
|
+
import { atom } from "jotai";
|
|
27
|
+
import { atomFamily } from "jotai-family";
|
|
28
|
+
function collection(locator, schema, shape, options = {}) {
|
|
29
|
+
const path = locator instanceof MapPath ? locator : new MapPath(locator);
|
|
30
|
+
const membership = atom(0);
|
|
31
|
+
const changed = (set) => set(membership, (n) => n + 1);
|
|
32
|
+
const has = (get, id) => {
|
|
33
|
+
const map2 = path.locator.resolve(readDoc(get, options));
|
|
34
|
+
return isMap(map2) && map2.has(id);
|
|
35
|
+
};
|
|
36
|
+
const { live: ids } = versionedLiveAtom(path.keys(), options, [membership]);
|
|
37
|
+
const item = atomFamily((id) => {
|
|
38
|
+
const typed = refreshedTypedAtom(path.child(id), schema, shape, options, [membership]);
|
|
39
|
+
return atom(
|
|
40
|
+
(get) => get(typed),
|
|
41
|
+
(get, set, patch) => {
|
|
42
|
+
const existed = has(get, id);
|
|
43
|
+
const result = set(typed, patch);
|
|
44
|
+
if (!result.issues && !existed) changed(set);
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
);
|
|
48
|
+
});
|
|
49
|
+
const refuse = (value, message) => {
|
|
50
|
+
const checked = validate(schema, value);
|
|
51
|
+
return checked.issues ? checked : { issues: [{ message }] };
|
|
52
|
+
};
|
|
53
|
+
const create2 = atom(null, (get, set, value, id) => {
|
|
54
|
+
const doc = readDoc(get, options);
|
|
55
|
+
const key = id ?? randomId();
|
|
56
|
+
let result;
|
|
57
|
+
doc.transact(() => {
|
|
58
|
+
const map2 = path.locator.resolve(doc);
|
|
59
|
+
if (!isMap(map2)) result = refuse(value, missingMap);
|
|
60
|
+
else if (map2.has(key)) result = refuse(value, taken(key));
|
|
61
|
+
else result = create(schema, shape, map2, key, value);
|
|
62
|
+
});
|
|
63
|
+
if (result.issues) return result;
|
|
64
|
+
changed(set);
|
|
65
|
+
return { value: result.value, id: key };
|
|
66
|
+
});
|
|
67
|
+
const remove = atom(null, (get, set, id) => {
|
|
68
|
+
const doc = readDoc(get, options);
|
|
69
|
+
const map2 = path.locator.resolve(doc);
|
|
70
|
+
if (!isMap(map2) || !map2.has(id)) return false;
|
|
71
|
+
map2.delete(id);
|
|
72
|
+
changed(set);
|
|
73
|
+
item.remove(id);
|
|
74
|
+
return true;
|
|
75
|
+
});
|
|
76
|
+
return { ids, item, create: create2, remove };
|
|
77
|
+
}
|
|
78
|
+
var missingMap = "The collection's Y.Map is missing, so there is nowhere to create the item";
|
|
79
|
+
var taken = (id) => `An item with id ${JSON.stringify(id)} already exists`;
|
|
80
|
+
function randomId() {
|
|
81
|
+
if (typeof crypto.randomUUID === "function") return crypto.randomUUID();
|
|
82
|
+
const bytes = crypto.getRandomValues(new Uint8Array(16));
|
|
83
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
84
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
85
|
+
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
86
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
87
|
+
}
|
|
88
|
+
export {
|
|
89
|
+
ArrayPath,
|
|
90
|
+
Locator,
|
|
91
|
+
MapPath,
|
|
92
|
+
array,
|
|
93
|
+
at,
|
|
94
|
+
collection,
|
|
95
|
+
deepEqual,
|
|
96
|
+
docAtom,
|
|
97
|
+
isMap,
|
|
98
|
+
liveAtom,
|
|
99
|
+
map,
|
|
100
|
+
record,
|
|
101
|
+
select,
|
|
102
|
+
shallowEqual,
|
|
103
|
+
typedAtom
|
|
104
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
import { DependencyList } from 'react';
|
|
3
|
+
import * as Y from 'yjs';
|
|
4
|
+
import { A as AtomOptions, P as Patch } from '../typed-atom-oZaH0A5l.js';
|
|
5
|
+
import { b as LiveSpec, c as Locator, M as MapPath, E as Exact } from '../shape--EeyM-2X.js';
|
|
6
|
+
import 'jotai';
|
|
7
|
+
|
|
8
|
+
type SpecOptions<N extends Y.AbstractType<any>, T> = Omit<LiveSpec<N, T>, 'locate' | 'project'> & AtomOptions;
|
|
9
|
+
/**
|
|
10
|
+
* Hook form of `liveAtom`, taking a whole spec, such as a selector from `select`, or a function
|
|
11
|
+
* that builds it: `useLiveAtom(() => select..., [id])` builds it only when `deps` change, rather
|
|
12
|
+
* than on every render. The atom is rebuilt only when `deps` change, so list whatever the spec
|
|
13
|
+
* closes over (an id, say), as with useMemo. `deps` is required: a spec built inline from props
|
|
14
|
+
* would otherwise keep its first value. `options.docAtom` reads another doc than `docAtom`.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* const title = useLiveAtom(() => select.map('cards').child(id).get('title', ''), [id])
|
|
18
|
+
*/
|
|
19
|
+
declare function useLiveAtom<N extends Y.AbstractType<any>, T>(spec: LiveSpec<N, T> | (() => LiveSpec<N, T>), deps: DependencyList, options?: AtomOptions): T;
|
|
20
|
+
/**
|
|
21
|
+
* Hook form of `liveAtom`. The atom is rebuilt only when `deps` change, so list whatever
|
|
22
|
+
* `locate` and `project` close over (an id, say), as with useMemo. `options.docAtom` reads
|
|
23
|
+
* another doc than `docAtom`.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* const title = useLiveAtom(
|
|
27
|
+
* (at) => at.map('cards').child(id),
|
|
28
|
+
* (card: Y.Map<unknown> | undefined) => String(card?.get('title') ?? ''),
|
|
29
|
+
* { keys: ['title'] },
|
|
30
|
+
* [id],
|
|
31
|
+
* )
|
|
32
|
+
*/
|
|
33
|
+
declare function useLiveAtom<N extends Y.AbstractType<any>, T>(locate: LiveSpec<N, T>['locate'], project: LiveSpec<N, T>['project'], options?: SpecOptions<N, T>, deps?: DependencyList): T;
|
|
34
|
+
/**
|
|
35
|
+
* Hook form of `typedAtom`, returning `[result, set]` as `useAtom` does: `result` is `undefined`
|
|
36
|
+
* while the node is missing, `{ issues }` while it's invalid and `{ value }` otherwise, and
|
|
37
|
+
* `set(patch)` merges a patch and returns the schema's result. `locator` can be a function that
|
|
38
|
+
* builds it, so it's built only when `deps` change. The atom is rebuilt only when `deps` change,
|
|
39
|
+
* so list whatever the locator closes over (an id, say), as with useMemo; `schema` and `shape`
|
|
40
|
+
* are taken from the render that built it. `options.docAtom` reads another doc than `docAtom`.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* const Card = z.object({ title: z.string(), priority: z.enum(['low', 'high']) })
|
|
44
|
+
* const [card, setCard] = useTypedAtom(() => select.map('cards').child(id), Card, { title: 'text' }, [id])
|
|
45
|
+
* if (card && !card.issues) setCard({ priority: 'high' })
|
|
46
|
+
*/
|
|
47
|
+
declare function useTypedAtom<Schema extends StandardSchemaV1, const S extends object>(locator: Locator<any> | MapPath<any> | (() => Locator<any> | MapPath<any>), schema: Schema, shape: Exact<S, StandardSchemaV1.InferOutput<Schema>>, deps: DependencyList, options?: AtomOptions): [
|
|
48
|
+
result: StandardSchemaV1.Result<StandardSchemaV1.InferOutput<Schema>> | undefined,
|
|
49
|
+
set: (patch: Patch<StandardSchemaV1.InferOutput<Schema>>) => StandardSchemaV1.Result<StandardSchemaV1.InferOutput<Schema>>
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
export { useLiveAtom, useTypedAtom };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {
|
|
2
|
+
liveAtom,
|
|
3
|
+
typedAtom
|
|
4
|
+
} from "../chunk-LTA6HTAG.js";
|
|
5
|
+
import "../chunk-GTI66UUE.js";
|
|
6
|
+
|
|
7
|
+
// src/react/index.ts
|
|
8
|
+
import { useAtom, useAtomValue } from "jotai";
|
|
9
|
+
import { useMemo } from "react";
|
|
10
|
+
function useLiveAtom(...args) {
|
|
11
|
+
let spec;
|
|
12
|
+
let deps;
|
|
13
|
+
let docAtom;
|
|
14
|
+
if (typeof args[1] === "function") {
|
|
15
|
+
const { docAtom: own, ...rest } = args[2] ?? {};
|
|
16
|
+
spec = { locate: args[0], project: args[1], ...rest };
|
|
17
|
+
deps = args[3] ?? [];
|
|
18
|
+
docAtom = own;
|
|
19
|
+
} else {
|
|
20
|
+
spec = args[0];
|
|
21
|
+
deps = args[1];
|
|
22
|
+
docAtom = args[2]?.docAtom;
|
|
23
|
+
}
|
|
24
|
+
const a = useMemo(
|
|
25
|
+
() => liveAtom(typeof spec === "function" ? spec() : spec, { docAtom }),
|
|
26
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
27
|
+
[...deps, docAtom]
|
|
28
|
+
);
|
|
29
|
+
return useAtomValue(a);
|
|
30
|
+
}
|
|
31
|
+
function useTypedAtom(locator, schema, shape, deps, options) {
|
|
32
|
+
const docAtom = options?.docAtom;
|
|
33
|
+
const a = useMemo(
|
|
34
|
+
() => typedAtom(typeof locator === "function" ? locator() : locator, schema, shape, { docAtom }),
|
|
35
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
36
|
+
[...deps, docAtom]
|
|
37
|
+
);
|
|
38
|
+
return useAtom(a);
|
|
39
|
+
}
|
|
40
|
+
export {
|
|
41
|
+
useLiveAtom,
|
|
42
|
+
useTypedAtom
|
|
43
|
+
};
|