@kyneta/yjs-schema 1.0.0 → 1.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/dist/index.d.ts +109 -147
- package/dist/index.js +321 -210
- package/dist/index.js.map +1 -1
- package/package.json +5 -3
- package/src/__tests__/bind-constraints.test.ts +333 -0
- package/src/__tests__/bind-yjs.test.ts +53 -55
- package/src/__tests__/create.test.ts +71 -62
- package/src/__tests__/{store-reader.test.ts → reader.test.ts} +64 -90
- package/src/__tests__/record-text-spike.test.ts +38 -31
- package/src/__tests__/structural-merge.test.ts +362 -0
- package/src/__tests__/substrate.test.ts +65 -84
- package/src/__tests__/version.test.ts +82 -16
- package/src/bind-yjs.ts +115 -64
- package/src/change-mapping.ts +60 -84
- package/src/create.ts +33 -28
- package/src/index.ts +32 -51
- package/src/populate.ts +87 -92
- package/src/{store-reader.ts → reader.ts} +7 -12
- package/src/substrate.ts +186 -42
- package/src/sync.ts +26 -26
- package/src/version.ts +57 -4
- package/src/yjs-resolve.ts +5 -21
- package/src/yjs-escape.ts +0 -100
package/src/yjs-escape.ts
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
// yjs-escape — Yjs-specific escape hatch for accessing the Y.Doc
|
|
2
|
-
// backing a ref.
|
|
3
|
-
//
|
|
4
|
-
// `yjs(ref)` returns the `Y.Doc` backing a root document ref.
|
|
5
|
-
//
|
|
6
|
-
// The mapping is maintained via a WeakMap from Substrate → Y.Doc,
|
|
7
|
-
// populated by `registerYjsSubstrate()` (called during substrate
|
|
8
|
-
// creation). The `yjs()` function uses `unwrap()` from `@kyneta/schema`
|
|
9
|
-
// to get the substrate, then looks up the Y.Doc.
|
|
10
|
-
//
|
|
11
|
-
// This two-step approach (ref → substrate → Y.Doc) avoids duplicating
|
|
12
|
-
// the ref-tracking WeakMap and composes cleanly with the general
|
|
13
|
-
// `unwrap()` escape hatch.
|
|
14
|
-
//
|
|
15
|
-
// Usage:
|
|
16
|
-
// import { yjs } from "@kyneta/yjs-schema"
|
|
17
|
-
//
|
|
18
|
-
// const doc = exchange.get("my-doc", TodoDoc)
|
|
19
|
-
// const yjsDoc = yjs(doc) // Y.Doc
|
|
20
|
-
// yjsDoc.getMap("root").toJSON() // raw Yjs inspection
|
|
21
|
-
|
|
22
|
-
import type { Doc as YDoc } from "yjs"
|
|
23
|
-
import type { Substrate } from "@kyneta/schema"
|
|
24
|
-
import { unwrap } from "@kyneta/schema"
|
|
25
|
-
|
|
26
|
-
// ---------------------------------------------------------------------------
|
|
27
|
-
// Substrate → Y.Doc mapping
|
|
28
|
-
// ---------------------------------------------------------------------------
|
|
29
|
-
|
|
30
|
-
const substrateToYjsDoc = new WeakMap<Substrate<any>, YDoc>()
|
|
31
|
-
|
|
32
|
-
// ---------------------------------------------------------------------------
|
|
33
|
-
// registerYjsSubstrate — called during substrate creation
|
|
34
|
-
// ---------------------------------------------------------------------------
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Register the Y.Doc backing a Yjs substrate.
|
|
38
|
-
*
|
|
39
|
-
* Called by `createYjsSubstrate()` and by `bindYjs`'s factory builder
|
|
40
|
-
* to enable the `yjs()` escape hatch. Must be called once per substrate
|
|
41
|
-
* at construction time.
|
|
42
|
-
*/
|
|
43
|
-
export function registerYjsSubstrate(
|
|
44
|
-
substrate: Substrate<any>,
|
|
45
|
-
doc: YDoc,
|
|
46
|
-
): void {
|
|
47
|
-
substrateToYjsDoc.set(substrate, doc)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// ---------------------------------------------------------------------------
|
|
51
|
-
// yjs — Yjs-specific escape hatch
|
|
52
|
-
// ---------------------------------------------------------------------------
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Returns the `Y.Doc` backing the given ref.
|
|
56
|
-
*
|
|
57
|
-
* This is the Yjs-specific escape hatch for accessing substrate-level
|
|
58
|
-
* capabilities: raw Yjs API, y-prosemirror/y-codemirror bindings,
|
|
59
|
-
* undo manager, awareness protocol, Yjs providers (y-websocket,
|
|
60
|
-
* y-indexeddb, y-webrtc, Hocuspocus, Liveblocks), etc.
|
|
61
|
-
*
|
|
62
|
-
* Currently supports root document refs only. Child-level resolution
|
|
63
|
-
* (e.g. `yjs(doc.title)` → `Y.Text`) is future work.
|
|
64
|
-
*
|
|
65
|
-
* @param ref - A root document ref backed by a Yjs substrate
|
|
66
|
-
* @returns The `Y.Doc` backing the ref
|
|
67
|
-
* @throws If the ref is not backed by a Yjs substrate
|
|
68
|
-
*
|
|
69
|
-
* @example
|
|
70
|
-
* ```ts
|
|
71
|
-
* import { yjs } from "@kyneta/yjs-schema"
|
|
72
|
-
*
|
|
73
|
-
* const doc = exchange.get("my-doc", TodoDoc)
|
|
74
|
-
* const yjsDoc = yjs(doc)
|
|
75
|
-
* console.log(yjsDoc.getMap("root").toJSON()) // raw state
|
|
76
|
-
* console.log(yjsDoc.clientID) // client ID
|
|
77
|
-
* ```
|
|
78
|
-
*/
|
|
79
|
-
export function yjs(ref: object): YDoc {
|
|
80
|
-
let substrate: Substrate<any>
|
|
81
|
-
try {
|
|
82
|
-
substrate = unwrap(ref)
|
|
83
|
-
} catch {
|
|
84
|
-
throw new Error(
|
|
85
|
-
"yjs() requires a ref backed by a Yjs substrate. " +
|
|
86
|
-
"Use a doc created by exchange.get() with a bindYjs() schema, " +
|
|
87
|
-
"or by createYjsDoc().",
|
|
88
|
-
)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const doc = substrateToYjsDoc.get(substrate)
|
|
92
|
-
if (!doc) {
|
|
93
|
-
throw new Error(
|
|
94
|
-
"yjs() requires a ref backed by a Yjs substrate. " +
|
|
95
|
-
"The ref has a substrate but it is not a Yjs substrate. " +
|
|
96
|
-
"Use a doc created with a bindYjs() schema or createYjsDoc().",
|
|
97
|
-
)
|
|
98
|
-
}
|
|
99
|
-
return doc
|
|
100
|
-
}
|