duratom 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +86 -2
- package/dist/chunk-7TXOG3GB.js +973 -0
- package/dist/chunk-URKFBGPT.js +165 -0
- package/dist/core/index.d.ts +150 -0
- package/dist/core/index.js +44 -0
- package/dist/equality-AjtpnVrF.d.ts +20 -0
- package/dist/index.d.ts +102 -0
- package/dist/index.js +182 -0
- package/dist/react/index.d.ts +52 -0
- package/dist/react/index.js +43 -0
- package/dist/shape-CMa7XFjy.d.ts +390 -0
- package/dist/typed-atom-t7tvnXSS.d.ts +127 -0
- package/llms-full.txt +1658 -0
- package/llms.txt +24 -0
- package/package.json +102 -3
- package/skills/duratom/SKILL.md +171 -0
|
@@ -0,0 +1,973 @@
|
|
|
1
|
+
// src/core/equality.ts
|
|
2
|
+
var shallowEqual = (a, b) => {
|
|
3
|
+
if (Object.is(a, b)) return true;
|
|
4
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) return false;
|
|
5
|
+
if (Array.isArray(a) !== Array.isArray(b)) return false;
|
|
6
|
+
const ka = Object.keys(a);
|
|
7
|
+
const kb = Object.keys(b);
|
|
8
|
+
return ka.length === kb.length && ka.every((k) => Object.hasOwn(b, k) && Object.is(a[k], b[k]));
|
|
9
|
+
};
|
|
10
|
+
var isPlainObject = (value) => {
|
|
11
|
+
if (value === null || typeof value !== "object") return false;
|
|
12
|
+
const proto = Object.getPrototypeOf(value);
|
|
13
|
+
return proto === Object.prototype || proto === null;
|
|
14
|
+
};
|
|
15
|
+
var deepEqual = (a, b) => {
|
|
16
|
+
if (Object.is(a, b)) return true;
|
|
17
|
+
if (a instanceof Uint8Array && b instanceof Uint8Array)
|
|
18
|
+
return a.length === b.length && a.every((byte, n) => byte === b[n]);
|
|
19
|
+
const arrays = Array.isArray(a) && Array.isArray(b);
|
|
20
|
+
if (!arrays && !(isPlainObject(a) && isPlainObject(b))) return false;
|
|
21
|
+
const ka = Object.keys(a);
|
|
22
|
+
return ka.length === Object.keys(b).length && ka.every((k) => Object.hasOwn(b, k) && deepEqual(a[k], b[k]));
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// src/core/locator.ts
|
|
26
|
+
import * as Y from "yjs";
|
|
27
|
+
var isMap = (node) => node instanceof Y.Map && !(node instanceof Y.XmlHook);
|
|
28
|
+
var Locator = class _Locator {
|
|
29
|
+
constructor(steps) {
|
|
30
|
+
this.steps = steps;
|
|
31
|
+
}
|
|
32
|
+
steps;
|
|
33
|
+
/** Descend into the Y type stored under `key` of a map. */
|
|
34
|
+
child(key) {
|
|
35
|
+
return new _Locator([...this.steps, { key }]);
|
|
36
|
+
}
|
|
37
|
+
resolve(doc) {
|
|
38
|
+
let node;
|
|
39
|
+
for (const step of this.steps) {
|
|
40
|
+
if ("root" in step)
|
|
41
|
+
node = step.kind === "map" ? doc.getMap(step.root) : doc.getArray(step.root);
|
|
42
|
+
else {
|
|
43
|
+
const next = isMap(node) ? node.get(step.key) : void 0;
|
|
44
|
+
node = next instanceof Y.AbstractType ? next : void 0;
|
|
45
|
+
}
|
|
46
|
+
if (!node) return void 0;
|
|
47
|
+
}
|
|
48
|
+
return node;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
var at = {
|
|
52
|
+
map: (name) => new Locator([{ root: name, kind: "map" }]),
|
|
53
|
+
array: (name) => new Locator([{ root: name, kind: "array" }])
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// src/core/observe.ts
|
|
57
|
+
import * as Y2 from "yjs";
|
|
58
|
+
function observeLocated(doc, locator, options, onChange) {
|
|
59
|
+
const { deep = false, keys, origin, event: accepted } = options;
|
|
60
|
+
let cleanups = [];
|
|
61
|
+
let node;
|
|
62
|
+
const accepts = (tr) => !origin || origin(tr.origin);
|
|
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
|
+
const unbind = () => {
|
|
66
|
+
for (const fn of cleanups) fn();
|
|
67
|
+
cleanups = [];
|
|
68
|
+
};
|
|
69
|
+
const bind = () => {
|
|
70
|
+
unbind();
|
|
71
|
+
let current;
|
|
72
|
+
node = void 0;
|
|
73
|
+
for (const step of locator.steps) {
|
|
74
|
+
if ("root" in step) {
|
|
75
|
+
current = step.kind === "map" ? doc.getMap(step.root) : doc.getArray(step.root);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (!isMap(current)) return;
|
|
79
|
+
const parent = current;
|
|
80
|
+
const handler = (event, tr) => {
|
|
81
|
+
if (!event.keysChanged.has(step.key)) return;
|
|
82
|
+
bind();
|
|
83
|
+
if (accepts(tr)) onChange();
|
|
84
|
+
};
|
|
85
|
+
parent.observe(handler);
|
|
86
|
+
cleanups.push(() => parent.unobserve(handler));
|
|
87
|
+
const next = parent.get(step.key);
|
|
88
|
+
if (!(next instanceof Y2.AbstractType)) return;
|
|
89
|
+
current = next;
|
|
90
|
+
}
|
|
91
|
+
const target = current;
|
|
92
|
+
if (!target) return;
|
|
93
|
+
node = target;
|
|
94
|
+
if (deep) {
|
|
95
|
+
const handler = (events, tr) => {
|
|
96
|
+
if (accepts(tr) && events.some(counts)) onChange();
|
|
97
|
+
};
|
|
98
|
+
target.observeDeep(handler);
|
|
99
|
+
cleanups.push(() => target.unobserveDeep(handler));
|
|
100
|
+
} else {
|
|
101
|
+
const handler = (event, tr) => {
|
|
102
|
+
if (accepts(tr) && counts(event)) onChange();
|
|
103
|
+
};
|
|
104
|
+
target.observe(handler);
|
|
105
|
+
cleanups.push(() => target.unobserve(handler));
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
bind();
|
|
109
|
+
return unbind;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// src/core/live.ts
|
|
113
|
+
function live(doc, spec) {
|
|
114
|
+
const equals = spec.equals ?? shallowEqual;
|
|
115
|
+
const locator = spec.locate(at);
|
|
116
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
117
|
+
let current;
|
|
118
|
+
let stop;
|
|
119
|
+
const refresh = () => {
|
|
120
|
+
const next = spec.project(locator.resolve(doc));
|
|
121
|
+
if (current && equals(current.value, next)) return false;
|
|
122
|
+
current = { value: next };
|
|
123
|
+
return true;
|
|
124
|
+
};
|
|
125
|
+
return {
|
|
126
|
+
get() {
|
|
127
|
+
if (!stop) refresh();
|
|
128
|
+
return current.value;
|
|
129
|
+
},
|
|
130
|
+
subscribe(listener) {
|
|
131
|
+
const entry = (value, prev) => listener(value, prev);
|
|
132
|
+
listeners.add(entry);
|
|
133
|
+
if (!stop) {
|
|
134
|
+
try {
|
|
135
|
+
stop = observeLocated(doc, locator, spec, () => {
|
|
136
|
+
const prev = current.value;
|
|
137
|
+
if (!refresh()) return;
|
|
138
|
+
const value = current.value;
|
|
139
|
+
for (const fn of [...listeners]) if (listeners.has(fn)) fn(value, prev);
|
|
140
|
+
});
|
|
141
|
+
refresh();
|
|
142
|
+
} catch (error) {
|
|
143
|
+
listeners.delete(entry);
|
|
144
|
+
stop?.();
|
|
145
|
+
stop = void 0;
|
|
146
|
+
throw error;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return () => {
|
|
150
|
+
if (!listeners.delete(entry) || listeners.size > 0) return;
|
|
151
|
+
stop?.();
|
|
152
|
+
stop = void 0;
|
|
153
|
+
};
|
|
154
|
+
},
|
|
155
|
+
resolve: () => locator.resolve(doc),
|
|
156
|
+
write: (fn, origin) => doc.transact(() => fn(locator.resolve(doc), doc), origin)
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// src/core/select.ts
|
|
161
|
+
import * as Y4 from "yjs";
|
|
162
|
+
|
|
163
|
+
// src/core/key-value.ts
|
|
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
|
|
209
|
+
var selector = (locator, is, project2, options = {}) => ({
|
|
210
|
+
locate: () => locator,
|
|
211
|
+
project: (node) => project2(is(node) ? node : void 0),
|
|
212
|
+
...options
|
|
213
|
+
});
|
|
214
|
+
var isText = (node) => node instanceof Y4.Text && !(node instanceof Y4.XmlText);
|
|
215
|
+
var asStored = (node) => true;
|
|
216
|
+
var kindOf = (value) => value === null ? "null" : Array.isArray(value) ? "array" : value instanceof Uint8Array ? "bytes" : isPlainObject(value) ? "object" : typeof value === "object" ? "other" : typeof value;
|
|
217
|
+
var sameKind = (value, fallback) => {
|
|
218
|
+
const kind = kindOf(value);
|
|
219
|
+
return kind !== "other" && kind === kindOf(fallback);
|
|
220
|
+
};
|
|
221
|
+
var frozen = (value) => Array.isArray(value) ? Object.freeze(value.map(frozen)) : isPlainObject(value) ? Object.freeze(
|
|
222
|
+
Object.fromEntries(Object.entries(value).map(([k, v]) => [k, frozen(v)]))
|
|
223
|
+
) : value;
|
|
224
|
+
var orFallback = (value, fallback) => fallback === void 0 || sameKind(value, fallback) ? value : fallback instanceof Uint8Array ? fallback.slice() : fallback;
|
|
225
|
+
var observing = ({
|
|
226
|
+
equals = deepEqual,
|
|
227
|
+
keys,
|
|
228
|
+
deep = keys === void 0,
|
|
229
|
+
event
|
|
230
|
+
}) => ({ equals, deep, keys, event });
|
|
231
|
+
var Path = class {
|
|
232
|
+
constructor(locator) {
|
|
233
|
+
this.locator = locator;
|
|
234
|
+
}
|
|
235
|
+
locator;
|
|
236
|
+
selector(project2, options) {
|
|
237
|
+
return selector(this.locator, (node) => this.is(node), project2, options);
|
|
238
|
+
}
|
|
239
|
+
/** `toJSON()` of the node and everything under it, `empty` when missing. Observes deeply; compared deeply. */
|
|
240
|
+
json(empty) {
|
|
241
|
+
return this.selector((node) => node ? node.toJSON() : empty, observing({}));
|
|
242
|
+
}
|
|
243
|
+
project(projection, options = {}) {
|
|
244
|
+
if (typeof projection === "function") return this.selector(projection, observing(options));
|
|
245
|
+
return selector(this.locator, asStored, projection.project, observing(projection));
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
var MapPath = class _MapPath extends Path {
|
|
249
|
+
is(node) {
|
|
250
|
+
return isMap(node);
|
|
251
|
+
}
|
|
252
|
+
child(key, kind = "map") {
|
|
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));
|
|
256
|
+
}
|
|
257
|
+
get(key, fallback) {
|
|
258
|
+
const safe = frozen(fallback);
|
|
259
|
+
return this.selector((node) => orFallback(node?.get(key), safe), {
|
|
260
|
+
keys: [key]
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* An object with each key of `defaults`, read as `get(key, defaults[key])`
|
|
265
|
+
* reads it. Observes only those keys; compared shallowly.
|
|
266
|
+
*/
|
|
267
|
+
pick(defaults) {
|
|
268
|
+
const safe = frozen(defaults);
|
|
269
|
+
const keys = Object.keys(safe);
|
|
270
|
+
return this.selector(
|
|
271
|
+
(node) => {
|
|
272
|
+
const out = {};
|
|
273
|
+
for (const k of keys) out[k] = orFallback(node?.get(k), safe[k]);
|
|
274
|
+
return out;
|
|
275
|
+
},
|
|
276
|
+
{ keys }
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* The string of the `Y.Text` under `key`, `''` when it is missing or not a
|
|
281
|
+
* `Y.Text` (a `Y.XmlText` included). Follows edits to the text.
|
|
282
|
+
*/
|
|
283
|
+
text(key) {
|
|
284
|
+
return selector(
|
|
285
|
+
this.locator.child(key),
|
|
286
|
+
isText,
|
|
287
|
+
(node) => node ? node.toString() : ""
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* The map's keys, sorted so every peer sees the same order. `[]` when the
|
|
292
|
+
* map is missing. Only a key added or deleted counts, not a value replaced.
|
|
293
|
+
* Its own `event` filter does that, so a spec that adds an `event` filter
|
|
294
|
+
* calls it from its own: `{ ...ids, event: (e) => ids.event!(e) && mine(e) }`.
|
|
295
|
+
*/
|
|
296
|
+
keys() {
|
|
297
|
+
return this.selector((node) => node ? [...node.keys()].sort() : [], {
|
|
298
|
+
event: (event) => [...event.changes.keys.values()].some((c) => c.action !== "update")
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
/** `toJSON()` of the map and everything under it, `{}` when missing. Observes deeply; compared deeply. */
|
|
302
|
+
toJSON() {
|
|
303
|
+
return this.json({});
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
var ArrayPath = class extends Path {
|
|
307
|
+
is(node) {
|
|
308
|
+
return node instanceof Y4.Array;
|
|
309
|
+
}
|
|
310
|
+
/** The items as stored, `[]` when the array is missing. Compared shallowly. */
|
|
311
|
+
toArray() {
|
|
312
|
+
return this.selector((node) => node ? node.toArray() : []);
|
|
313
|
+
}
|
|
314
|
+
/** The number of items, 0 when the array is missing. */
|
|
315
|
+
length() {
|
|
316
|
+
return this.selector((node) => node?.length ?? 0);
|
|
317
|
+
}
|
|
318
|
+
/** `toJSON()` of the array and everything under it, `[]` when missing. Observes deeply; compared deeply. */
|
|
319
|
+
toJSON() {
|
|
320
|
+
return this.json([]);
|
|
321
|
+
}
|
|
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
|
+
};
|
|
371
|
+
var select = {
|
|
372
|
+
map: (name) => new MapPath(at.map(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))
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
// src/schema/read.ts
|
|
379
|
+
import * as Y7 from "yjs";
|
|
380
|
+
|
|
381
|
+
// src/schema/copy.ts
|
|
382
|
+
import * as Y5 from "yjs";
|
|
383
|
+
var setField = (target, key, value) => Object.defineProperty(target, key, {
|
|
384
|
+
value,
|
|
385
|
+
enumerable: true,
|
|
386
|
+
writable: true,
|
|
387
|
+
configurable: true
|
|
388
|
+
});
|
|
389
|
+
var pathOf = (at2) => {
|
|
390
|
+
const keys = [];
|
|
391
|
+
for (let link = at2; link; link = link.parent) keys.push(link.key);
|
|
392
|
+
return keys.reverse();
|
|
393
|
+
};
|
|
394
|
+
var isShared = (value) => value instanceof Y5.AbstractType || value instanceof Y5.Doc;
|
|
395
|
+
function copy(stored, report2) {
|
|
396
|
+
if (!isContainer(stored)) return stored;
|
|
397
|
+
if (stored instanceof Uint8Array) return new Uint8Array(stored);
|
|
398
|
+
const onPath = /* @__PURE__ */ new Map();
|
|
399
|
+
const root = {};
|
|
400
|
+
const steps = [
|
|
401
|
+
{
|
|
402
|
+
value: stored,
|
|
403
|
+
target: root,
|
|
404
|
+
key: "value",
|
|
405
|
+
pathKey: report2?.at.key ?? "value",
|
|
406
|
+
parent: report2?.at.parent
|
|
407
|
+
}
|
|
408
|
+
];
|
|
409
|
+
for (let step = steps.pop(); step; step = steps.pop()) {
|
|
410
|
+
if ("leave" in step) {
|
|
411
|
+
onPath.delete(step.leave);
|
|
412
|
+
continue;
|
|
413
|
+
}
|
|
414
|
+
const { value, target, key, pathKey, parent } = step;
|
|
415
|
+
if (report2 && isShared(value)) report2.onShared(value, { key: pathKey, parent });
|
|
416
|
+
else if (!isContainer(value)) setField(target, key, value);
|
|
417
|
+
else if (onPath.has(value)) setField(target, key, onPath.get(value));
|
|
418
|
+
else if (value instanceof Uint8Array) setField(target, key, new Uint8Array(value));
|
|
419
|
+
else {
|
|
420
|
+
const here = report2 && { key: pathKey, parent };
|
|
421
|
+
const children = [];
|
|
422
|
+
let copied;
|
|
423
|
+
if (Array.isArray(value)) {
|
|
424
|
+
copied = new Array(value.length);
|
|
425
|
+
for (let n = 0; n < value.length; n++)
|
|
426
|
+
children.push({
|
|
427
|
+
value: value[n],
|
|
428
|
+
target: copied,
|
|
429
|
+
key: String(n),
|
|
430
|
+
pathKey: n,
|
|
431
|
+
parent: here
|
|
432
|
+
});
|
|
433
|
+
} else {
|
|
434
|
+
copied = {};
|
|
435
|
+
for (const [field, item] of Object.entries(value))
|
|
436
|
+
children.push({
|
|
437
|
+
value: item,
|
|
438
|
+
target: copied,
|
|
439
|
+
key: field,
|
|
440
|
+
pathKey: field,
|
|
441
|
+
parent: here
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
setField(target, key, copied);
|
|
445
|
+
onPath.set(value, copied);
|
|
446
|
+
steps.push({ leave: value });
|
|
447
|
+
for (let n = children.length - 1; n >= 0; n--) steps.push(children[n]);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
return root.value;
|
|
451
|
+
}
|
|
452
|
+
var isContainer = (value) => value !== null && typeof value === "object" && !isShared(value);
|
|
453
|
+
|
|
454
|
+
// src/schema/equal.ts
|
|
455
|
+
function deepEqual2(a, b) {
|
|
456
|
+
const pairs = [[a, b]];
|
|
457
|
+
const seen = /* @__PURE__ */ new Map();
|
|
458
|
+
for (let pair = pairs.pop(); pair; pair = pairs.pop()) {
|
|
459
|
+
const [x, y] = pair;
|
|
460
|
+
if (Object.is(x, y)) continue;
|
|
461
|
+
if (x !== null && typeof x === "object" && y !== null && typeof y === "object") {
|
|
462
|
+
const partners = seen.get(x) ?? seen.set(x, /* @__PURE__ */ new Set()).get(x);
|
|
463
|
+
if (partners.has(y)) continue;
|
|
464
|
+
partners.add(y);
|
|
465
|
+
}
|
|
466
|
+
if (x instanceof Uint8Array && y instanceof Uint8Array) {
|
|
467
|
+
if (x.length !== y.length || x.some((byte, n) => byte !== y[n])) return false;
|
|
468
|
+
continue;
|
|
469
|
+
}
|
|
470
|
+
if (Array.isArray(x) && Array.isArray(y)) {
|
|
471
|
+
if (x.length !== y.length) return false;
|
|
472
|
+
for (let n = 0; n < x.length; n++) pairs.push([x[n], y[n]]);
|
|
473
|
+
continue;
|
|
474
|
+
}
|
|
475
|
+
if (!isPlainObject2(x) || !isPlainObject2(y)) return false;
|
|
476
|
+
const keys = Object.keys(x);
|
|
477
|
+
if (keys.length !== Object.keys(y).length) return false;
|
|
478
|
+
for (const key of keys) {
|
|
479
|
+
if (!Object.hasOwn(y, key)) return false;
|
|
480
|
+
pairs.push([x[key], y[key]]);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
return true;
|
|
484
|
+
}
|
|
485
|
+
var isPlainObject2 = (value) => {
|
|
486
|
+
if (value === null || typeof value !== "object") return false;
|
|
487
|
+
const proto = Object.getPrototypeOf(value);
|
|
488
|
+
return proto === Object.prototype || proto === null;
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
// src/schema/shape.ts
|
|
492
|
+
import * as Y6 from "yjs";
|
|
493
|
+
var map = (shape) => Object.freeze({ kind: "map", shape });
|
|
494
|
+
var record = (itemShape) => Object.freeze({ kind: "record", shape: itemShape });
|
|
495
|
+
var arrayKind = Object.freeze({ kind: "array" });
|
|
496
|
+
var array = () => arrayKind;
|
|
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);
|
|
500
|
+
function kindOf2(declared) {
|
|
501
|
+
if (declared === "text" || declared === "xml") return declared;
|
|
502
|
+
if (declared === null || typeof declared !== "object") return void 0;
|
|
503
|
+
const { kind, shape } = declared;
|
|
504
|
+
if (kind === "array") return declared;
|
|
505
|
+
if ((kind === "map" || kind === "record") && shape !== null && typeof shape === "object")
|
|
506
|
+
return declared;
|
|
507
|
+
return void 0;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
// src/schema/errors.ts
|
|
511
|
+
var AsyncValidationUnsupportedError = class extends Error {
|
|
512
|
+
vendor;
|
|
513
|
+
constructor(vendor) {
|
|
514
|
+
super(
|
|
515
|
+
`The ${vendor} schema validated asynchronously, which duratom doesn't support. Use a schema with no async refinements or transforms.`
|
|
516
|
+
);
|
|
517
|
+
this.name = "AsyncValidationUnsupportedError";
|
|
518
|
+
this.vendor = vendor;
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
var UnsupportedValueError = class extends Error {
|
|
522
|
+
/** Where the value sits in the written value, starting at the field's key. */
|
|
523
|
+
path;
|
|
524
|
+
constructor(path) {
|
|
525
|
+
super(
|
|
526
|
+
`A Y.Map can't store the value written at ${path.map(String).join(".")} as written, so nothing was written. Use a schema that accepts only JSON values and bytes.`
|
|
527
|
+
);
|
|
528
|
+
this.name = "UnsupportedValueError";
|
|
529
|
+
this.path = path;
|
|
530
|
+
}
|
|
531
|
+
};
|
|
532
|
+
|
|
533
|
+
// src/schema/validate.ts
|
|
534
|
+
function validate(schema, value) {
|
|
535
|
+
const result = schema["~standard"].validate(value);
|
|
536
|
+
if (isThenable(result)) {
|
|
537
|
+
Promise.resolve(result).catch(() => {
|
|
538
|
+
});
|
|
539
|
+
throw new AsyncValidationUnsupportedError(schema["~standard"].vendor);
|
|
540
|
+
}
|
|
541
|
+
return result;
|
|
542
|
+
}
|
|
543
|
+
var isThenable = (value) => typeof value?.then === "function";
|
|
544
|
+
|
|
545
|
+
// src/schema/read.ts
|
|
546
|
+
function read(schema, shape, node) {
|
|
547
|
+
if (node === void 0) return void 0;
|
|
548
|
+
if (!isMap2(node)) return { issues: [mismatch(void 0, yMap, node)] };
|
|
549
|
+
if (!node.doc) throw new TypeError("read needs a Y.Map that is in a Y.Doc.");
|
|
550
|
+
const { value, issues } = project(shape, node);
|
|
551
|
+
if (issues.length > 0) return { issues };
|
|
552
|
+
return validate(schema, value);
|
|
553
|
+
}
|
|
554
|
+
function reader(schema, shape) {
|
|
555
|
+
return {
|
|
556
|
+
project: (node) => read(schema, shape, node),
|
|
557
|
+
deep: true,
|
|
558
|
+
event: (event) => !insideXml(shape, event.path),
|
|
559
|
+
equals: deepEqual2
|
|
560
|
+
};
|
|
561
|
+
}
|
|
562
|
+
function insideXml(shape, path) {
|
|
563
|
+
let current = shape;
|
|
564
|
+
for (let n = 0; n < path.length; n++) {
|
|
565
|
+
const key = path[n];
|
|
566
|
+
if (typeof key !== "string" || !Object.hasOwn(current, key)) return false;
|
|
567
|
+
const kind = kindOf2(current[key]);
|
|
568
|
+
if (kind === "xml") return true;
|
|
569
|
+
if (kind === void 0 || kind === "text" || kind.kind === "array") return false;
|
|
570
|
+
if (kind.kind === "record") n++;
|
|
571
|
+
current = kind.shape;
|
|
572
|
+
}
|
|
573
|
+
return false;
|
|
574
|
+
}
|
|
575
|
+
var maxIssues = 100;
|
|
576
|
+
var TooManyIssues = class extends Error {
|
|
577
|
+
};
|
|
578
|
+
var tooManyIssues = new TooManyIssues();
|
|
579
|
+
function report(issues, issue) {
|
|
580
|
+
if (issues.length < maxIssues) return void issues.push(issue);
|
|
581
|
+
issues.push({
|
|
582
|
+
message: `Shape mismatch: more than ${maxIssues} mismatches, the rest left out`
|
|
583
|
+
});
|
|
584
|
+
throw tooManyIssues;
|
|
585
|
+
}
|
|
586
|
+
var mismatch = (at2, expected, found) => ({
|
|
587
|
+
message: `Shape mismatch: expected ${expected}, found ${describeFound(found)}`,
|
|
588
|
+
path: pathOf(at2)
|
|
589
|
+
});
|
|
590
|
+
var plain = "a plain value";
|
|
591
|
+
var yText = "a Y.Text";
|
|
592
|
+
var yMap = "a Y.Map";
|
|
593
|
+
var yArray = "a Y.Array";
|
|
594
|
+
var yXmlFragment = "a Y.XmlFragment";
|
|
595
|
+
function describeFound(value) {
|
|
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";
|
|
605
|
+
if (value === null) return "null";
|
|
606
|
+
if (Array.isArray(value)) return "an array";
|
|
607
|
+
if (value instanceof Uint8Array) return "bytes";
|
|
608
|
+
if (typeof value === "object") return "an object";
|
|
609
|
+
if (value === void 0) return "undefined";
|
|
610
|
+
return `a ${typeof value}`;
|
|
611
|
+
}
|
|
612
|
+
function project(shape, node) {
|
|
613
|
+
const root = {};
|
|
614
|
+
const issues = [];
|
|
615
|
+
const onShared = (found, at2) => report(issues, mismatch(at2, plain, found));
|
|
616
|
+
const reportAt = (at2) => ({ at: at2, onShared });
|
|
617
|
+
const steps = [{ shape, node, value: root, at: void 0, missing: [] }];
|
|
618
|
+
const later = (step, target, key) => {
|
|
619
|
+
const value = {};
|
|
620
|
+
setField(target, key, value);
|
|
621
|
+
steps.push({ ...step, value });
|
|
622
|
+
};
|
|
623
|
+
try {
|
|
624
|
+
for (let step = steps.pop(); step; step = steps.pop()) {
|
|
625
|
+
const { shape: shape2, node: node2, value, at: at2, missing } = step;
|
|
626
|
+
if (node2)
|
|
627
|
+
for (const [key, stored] of node2.entries()) {
|
|
628
|
+
const kind = Object.hasOwn(shape2, key) ? kindOf2(shape2[key]) : void 0;
|
|
629
|
+
const here = { key, parent: at2 };
|
|
630
|
+
if (kind === void 0) {
|
|
631
|
+
if (isShared(stored)) report(issues, mismatch(here, plain, stored));
|
|
632
|
+
else setField(value, key, copy(stored, reportAt(here)));
|
|
633
|
+
} else if (kind === "text") {
|
|
634
|
+
if (isText2(stored)) setField(value, key, stored.toString());
|
|
635
|
+
else report(issues, mismatch(here, yText, stored));
|
|
636
|
+
} else if (kind === "xml") {
|
|
637
|
+
if (isXml(stored)) setField(value, key, stored);
|
|
638
|
+
else report(issues, mismatch(here, yXmlFragment, stored));
|
|
639
|
+
} else if (kind.kind === "array") {
|
|
640
|
+
if (stored instanceof Y7.Array)
|
|
641
|
+
setField(value, key, copy(stored.toArray(), reportAt(here)));
|
|
642
|
+
else report(issues, mismatch(here, yArray, stored));
|
|
643
|
+
} else if (!isMap2(stored)) report(issues, mismatch(here, yMap, stored));
|
|
644
|
+
else if (kind.kind === "map")
|
|
645
|
+
later({ shape: kind.shape, node: stored, at: here, missing: [] }, value, key);
|
|
646
|
+
else {
|
|
647
|
+
const items = {};
|
|
648
|
+
setField(value, key, items);
|
|
649
|
+
for (const [id, item] of stored.entries()) {
|
|
650
|
+
const itemAt = { key: id, parent: here };
|
|
651
|
+
if (isMap2(item))
|
|
652
|
+
later({ shape: kind.shape, node: item, at: itemAt, missing: [] }, items, id);
|
|
653
|
+
else report(issues, mismatch(itemAt, yMap, item));
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
for (const [key, declared] of Object.entries(shape2)) {
|
|
658
|
+
const kind = kindOf2(declared);
|
|
659
|
+
if (kind === void 0 || kind === "xml" || node2?.has(key)) continue;
|
|
660
|
+
if (kind === "text") setField(value, key, "");
|
|
661
|
+
else if (kind.kind === "array") setField(value, key, []);
|
|
662
|
+
else if (kind.kind === "record" || missing.includes(kind.shape)) setField(value, key, {});
|
|
663
|
+
else
|
|
664
|
+
later(
|
|
665
|
+
{
|
|
666
|
+
shape: kind.shape,
|
|
667
|
+
node: void 0,
|
|
668
|
+
at: { key, parent: at2 },
|
|
669
|
+
missing: [...missing, kind.shape]
|
|
670
|
+
},
|
|
671
|
+
value,
|
|
672
|
+
key
|
|
673
|
+
);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
} catch (error) {
|
|
677
|
+
if (error !== tooManyIssues) throw error;
|
|
678
|
+
}
|
|
679
|
+
return { value: root, issues };
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
// src/schema/write.ts
|
|
683
|
+
import * as Y8 from "yjs";
|
|
684
|
+
function write(schema, shape, node, value) {
|
|
685
|
+
if (!isMap2(node))
|
|
686
|
+
throw new TypeError(
|
|
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."
|
|
688
|
+
);
|
|
689
|
+
const doc = node.doc;
|
|
690
|
+
if (!doc) throw new TypeError("write needs a Y.Map that is in a Y.Doc.");
|
|
691
|
+
return apply(schema, shape, doc, new Target(node), value);
|
|
692
|
+
}
|
|
693
|
+
function create(schema, shape, parent, key, value) {
|
|
694
|
+
if (!isMap2(parent)) throw new TypeError("create needs a Y.Map to create the node in.");
|
|
695
|
+
const doc = parent.doc;
|
|
696
|
+
if (!doc) throw new TypeError("create needs a Y.Map that is in a Y.Doc.");
|
|
697
|
+
const root = new Target(void 0, new Target(parent), key);
|
|
698
|
+
return apply(schema, shape, doc, root, value);
|
|
699
|
+
}
|
|
700
|
+
function apply(schema, shape, doc, root, value) {
|
|
701
|
+
const result = validate(schema, value);
|
|
702
|
+
if (result.issues) return result;
|
|
703
|
+
if (!isPlainObject2(value))
|
|
704
|
+
throw new TypeError("write needs a field object as its value, since a Y.Map holds fields.");
|
|
705
|
+
const changes = plan(shape, root, value);
|
|
706
|
+
doc.transact(() => {
|
|
707
|
+
root.ensure();
|
|
708
|
+
for (const change of changes) change();
|
|
709
|
+
});
|
|
710
|
+
return result;
|
|
711
|
+
}
|
|
712
|
+
var Target = class {
|
|
713
|
+
constructor(stored, parent, key) {
|
|
714
|
+
this.stored = stored;
|
|
715
|
+
this.parent = parent;
|
|
716
|
+
this.key = key;
|
|
717
|
+
this.#map = stored;
|
|
718
|
+
}
|
|
719
|
+
stored;
|
|
720
|
+
parent;
|
|
721
|
+
key;
|
|
722
|
+
#map;
|
|
723
|
+
/** The `Y.Map`, created along with any missing parents, replacing whatever was stored there. */
|
|
724
|
+
ensure() {
|
|
725
|
+
const missing = [];
|
|
726
|
+
for (let target = this; !target.#map; target = target.parent) missing.push(target);
|
|
727
|
+
for (let n = missing.length - 1; n >= 0; n--) {
|
|
728
|
+
const target = missing[n];
|
|
729
|
+
target.#map = new Y8.Map();
|
|
730
|
+
target.parent.#map.set(target.key, target.#map);
|
|
731
|
+
}
|
|
732
|
+
return this.#map;
|
|
733
|
+
}
|
|
734
|
+
/** The `Y.Map` if it's stored or has been created, without creating it. */
|
|
735
|
+
get map() {
|
|
736
|
+
return this.#map;
|
|
737
|
+
}
|
|
738
|
+
};
|
|
739
|
+
function plan(shape, root, value) {
|
|
740
|
+
const changes = [];
|
|
741
|
+
const fragments = [];
|
|
742
|
+
const steps = [{ shape, target: root, value, at: void 0 }];
|
|
743
|
+
for (let step = steps.pop(); step; step = steps.pop()) {
|
|
744
|
+
const { shape: shape2, target, value: value2, at: at2 } = step;
|
|
745
|
+
const { stored } = target;
|
|
746
|
+
const kindAt = (key) => Object.hasOwn(shape2, key) ? kindOf2(shape2[key]) : void 0;
|
|
747
|
+
const absent = stored ? [...stored.keys()].filter((key) => !Object.hasOwn(value2, key) && kindAt(key) !== "xml") : [];
|
|
748
|
+
if (absent.length > 0) changes.push(() => absent.forEach((key) => stored.delete(key)));
|
|
749
|
+
for (const key of Object.keys(shape2))
|
|
750
|
+
if (kindAt(key) === "xml" && !isXml(stored?.get(key))) fragments.push([target, key]);
|
|
751
|
+
for (const [key, field] of Object.entries(value2)) {
|
|
752
|
+
const kind = kindAt(key);
|
|
753
|
+
const here = { key, parent: at2 };
|
|
754
|
+
const has = stored?.has(key) ?? false;
|
|
755
|
+
const current = stored?.get(key);
|
|
756
|
+
if (kind === void 0) {
|
|
757
|
+
if (has && deepEqual2(current, field)) continue;
|
|
758
|
+
if (field === void 0 && !stored) continue;
|
|
759
|
+
assertStorable(field, here);
|
|
760
|
+
changes.push(() => target.ensure().set(key, copy(field)));
|
|
761
|
+
} else if (kind === "xml") {
|
|
762
|
+
continue;
|
|
763
|
+
} else if (field === void 0 || field === null) {
|
|
764
|
+
if (has) changes.push(() => stored.delete(key));
|
|
765
|
+
} else if (kind === "text") {
|
|
766
|
+
if (typeof field !== "string") throw notStorable(here, "a string, null or undefined");
|
|
767
|
+
if (isText2(current)) changes.push(() => splice(current, field));
|
|
768
|
+
else if (field !== "" || has)
|
|
769
|
+
changes.push(() => target.ensure().set(key, new Y8.Text(field)));
|
|
770
|
+
} else if (kind.kind === "array") {
|
|
771
|
+
if (!Array.isArray(field)) throw notStorable(here, "an array, null or undefined");
|
|
772
|
+
const items = Array.from(field);
|
|
773
|
+
const array2 = current instanceof Y8.Array ? current : void 0;
|
|
774
|
+
const { start, removed, inserted } = diff(array2?.toArray() ?? [], items);
|
|
775
|
+
inserted.forEach((item, n) => {
|
|
776
|
+
const itemAt = { key: start + n, parent: here };
|
|
777
|
+
if (item === void 0) throw new UnsupportedValueError(pathOf(itemAt));
|
|
778
|
+
assertStorable(item, itemAt);
|
|
779
|
+
});
|
|
780
|
+
if (array2) {
|
|
781
|
+
if (removed > 0 || inserted.length > 0)
|
|
782
|
+
changes.push(() => {
|
|
783
|
+
if (removed > 0) array2.delete(start, removed);
|
|
784
|
+
if (inserted.length > 0) array2.insert(start, inserted.map(copyItem));
|
|
785
|
+
});
|
|
786
|
+
} else if (items.length > 0 || has)
|
|
787
|
+
changes.push(() => {
|
|
788
|
+
const created = new Y8.Array();
|
|
789
|
+
target.ensure().set(key, created);
|
|
790
|
+
created.insert(0, items.map(copyItem));
|
|
791
|
+
});
|
|
792
|
+
} else {
|
|
793
|
+
if (!isPlainObject2(field)) throw notStorable(here, "an object, null or undefined");
|
|
794
|
+
const child = new Target(isMap2(current) ? current : void 0, target, key);
|
|
795
|
+
if (has && !child.stored) changes.push(() => child.ensure());
|
|
796
|
+
if (kind.kind === "map") {
|
|
797
|
+
steps.push({ shape: kind.shape, target: child, value: field, at: here });
|
|
798
|
+
continue;
|
|
799
|
+
}
|
|
800
|
+
const items = child.stored;
|
|
801
|
+
const gone = items ? [...items.keys()].filter((id) => !Object.hasOwn(field, id)) : [];
|
|
802
|
+
if (gone.length > 0) changes.push(() => gone.forEach((id) => items.delete(id)));
|
|
803
|
+
for (const [id, item] of Object.entries(field)) {
|
|
804
|
+
const itemAt = { key: id, parent: here };
|
|
805
|
+
if (item === void 0 || item === null) {
|
|
806
|
+
if (items?.has(id)) changes.push(() => items.delete(id));
|
|
807
|
+
continue;
|
|
808
|
+
}
|
|
809
|
+
if (!isPlainObject2(item)) throw notStorable(itemAt, "an object");
|
|
810
|
+
const storedItem = items?.get(id);
|
|
811
|
+
const itemTarget = new Target(isMap2(storedItem) ? storedItem : void 0, child, id);
|
|
812
|
+
if (!itemTarget.stored) changes.push(() => itemTarget.ensure());
|
|
813
|
+
steps.push({ shape: kind.shape, target: itemTarget, value: item, at: itemAt });
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
changes.push(() => {
|
|
819
|
+
for (const [target, key] of fragments)
|
|
820
|
+
if (target.map && !isXml(target.map.get(key))) target.map.set(key, new Y8.XmlFragment());
|
|
821
|
+
});
|
|
822
|
+
return changes;
|
|
823
|
+
}
|
|
824
|
+
var copyItem = (item) => copy(item);
|
|
825
|
+
var notStorable = (at2, expected) => new TypeError(
|
|
826
|
+
`write can't store ${JSON.stringify(pathOf(at2))} as the shape declares it: only ${expected}.`
|
|
827
|
+
);
|
|
828
|
+
function diff(current, next) {
|
|
829
|
+
const end = Math.min(current.length, next.length);
|
|
830
|
+
let start = 0;
|
|
831
|
+
while (start < end && deepEqual2(current[start], next[start])) start++;
|
|
832
|
+
let suffix = 0;
|
|
833
|
+
while (suffix < end - start && deepEqual2(current[current.length - 1 - suffix], next[next.length - 1 - suffix]))
|
|
834
|
+
suffix++;
|
|
835
|
+
return {
|
|
836
|
+
start,
|
|
837
|
+
removed: current.length - start - suffix,
|
|
838
|
+
inserted: next.slice(start, next.length - suffix)
|
|
839
|
+
};
|
|
840
|
+
}
|
|
841
|
+
function assertStorable(field, root) {
|
|
842
|
+
const fail = (at2) => {
|
|
843
|
+
throw new UnsupportedValueError(pathOf(at2));
|
|
844
|
+
};
|
|
845
|
+
const onPath = /* @__PURE__ */ new Set();
|
|
846
|
+
const steps = [{ value: field, at: root }];
|
|
847
|
+
for (let step = steps.pop(); step; step = steps.pop()) {
|
|
848
|
+
if ("leave" in step) {
|
|
849
|
+
onPath.delete(step.leave);
|
|
850
|
+
continue;
|
|
851
|
+
}
|
|
852
|
+
const { value, at: at2 } = step;
|
|
853
|
+
if (value === null || value === void 0) continue;
|
|
854
|
+
if (["string", "number", "boolean"].includes(typeof value)) continue;
|
|
855
|
+
if (value instanceof Uint8Array) continue;
|
|
856
|
+
if (typeof value !== "object" || onPath.has(value)) fail(at2);
|
|
857
|
+
const items = Array.isArray(value) ? (
|
|
858
|
+
// Array.from reads a hole as undefined, as Yjs stores it.
|
|
859
|
+
Array.from(value, (item, n) => [n, item])
|
|
860
|
+
) : isPlainObject2(value) ? Object.entries(value) : fail(at2);
|
|
861
|
+
onPath.add(value);
|
|
862
|
+
steps.push({ leave: value });
|
|
863
|
+
for (const [key, item] of items) {
|
|
864
|
+
const child = { key, parent: at2 };
|
|
865
|
+
if (key === "__proto__" || key === "constructor" && at2 === root) fail(child);
|
|
866
|
+
steps.push({ value: item, at: child });
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
function splice(text, next) {
|
|
871
|
+
const current = text.toString();
|
|
872
|
+
const end = Math.min(current.length, next.length);
|
|
873
|
+
let start = 0;
|
|
874
|
+
while (start < end && current[start] === next[start]) start++;
|
|
875
|
+
if (start > 0 && isHighSurrogate(current.charCodeAt(start - 1))) start--;
|
|
876
|
+
let suffix = 0;
|
|
877
|
+
while (suffix < end - start && current[current.length - 1 - suffix] === next[next.length - 1 - suffix])
|
|
878
|
+
suffix++;
|
|
879
|
+
if (suffix > 0 && isLowSurrogate(current.charCodeAt(current.length - suffix))) suffix--;
|
|
880
|
+
const removed = current.length - start - suffix;
|
|
881
|
+
const inserted = next.slice(start, next.length - suffix);
|
|
882
|
+
if (removed === 0 && !inserted) return;
|
|
883
|
+
const offsets = text.length === current.length ? void 0 : offsetsOf(text);
|
|
884
|
+
const at2 = (index) => offsets?.[index] ?? index;
|
|
885
|
+
const from = at2(start);
|
|
886
|
+
if (removed > 0) text.delete(from, at2(start + removed - 1) + 1 - from);
|
|
887
|
+
if (inserted) text.insert(from, inserted);
|
|
888
|
+
}
|
|
889
|
+
function offsetsOf(text) {
|
|
890
|
+
const offsets = [];
|
|
891
|
+
let offset = 0;
|
|
892
|
+
for (const { insert } of text.toDelta()) {
|
|
893
|
+
if (typeof insert === "string") for (let n = 0; n < insert.length; n++) offsets.push(offset++);
|
|
894
|
+
else offset++;
|
|
895
|
+
}
|
|
896
|
+
offsets.push(text.length);
|
|
897
|
+
return offsets;
|
|
898
|
+
}
|
|
899
|
+
var isHighSurrogate = (code) => code >= 55296 && code <= 56319;
|
|
900
|
+
var isLowSurrogate = (code) => code >= 56320 && code <= 57343;
|
|
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
|
+
|
|
940
|
+
export {
|
|
941
|
+
copy,
|
|
942
|
+
deepEqual2 as deepEqual,
|
|
943
|
+
map,
|
|
944
|
+
record,
|
|
945
|
+
array,
|
|
946
|
+
isMap2 as isMap,
|
|
947
|
+
AsyncValidationUnsupportedError,
|
|
948
|
+
UnsupportedValueError,
|
|
949
|
+
validate,
|
|
950
|
+
read,
|
|
951
|
+
reader,
|
|
952
|
+
write,
|
|
953
|
+
create,
|
|
954
|
+
assertStorable,
|
|
955
|
+
currentEntries,
|
|
956
|
+
touchesKeys,
|
|
957
|
+
setEntry,
|
|
958
|
+
readKey,
|
|
959
|
+
readValue,
|
|
960
|
+
writeKey,
|
|
961
|
+
keyReader,
|
|
962
|
+
shallowEqual,
|
|
963
|
+
deepEqual as deepEqual2,
|
|
964
|
+
Locator,
|
|
965
|
+
at,
|
|
966
|
+
observeLocated,
|
|
967
|
+
live,
|
|
968
|
+
MapPath,
|
|
969
|
+
ArrayPath,
|
|
970
|
+
KeyValuePath,
|
|
971
|
+
KeyPath,
|
|
972
|
+
select
|
|
973
|
+
};
|