@kyneta/yjs-schema 1.1.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 CHANGED
@@ -1,7 +1,7 @@
1
- import { Version, Schema, Ref, SubstratePayload, BoundSchema, Path, ChangeBase, Op, Reader, Substrate, SubstrateFactory, Segment, AnnotatedSchema } from '@kyneta/schema';
2
- export { Changeset, Op, Ref, Schema, SubstratePayload, applyChanges, change, subscribe, subscribeNode } from '@kyneta/schema';
1
+ import { Version, Schema, Ref, SubstratePayload, SubstrateNamespace, CrdtStrategy, Path, ChangeBase, Op, Reader, Substrate, ReplicaFactory, SubstrateFactory, Segment } from '@kyneta/schema';
2
+ export { Op, Ref, Schema, SubstratePayload, applyChanges, change, subscribe, subscribeNode } from '@kyneta/schema';
3
3
  import * as Y from 'yjs';
4
- import { Doc } from 'yjs';
4
+ export { Changeset } from '@kyneta/changefeed';
5
5
 
6
6
  /**
7
7
  * A Version wrapping a Yjs state vector.
@@ -41,6 +41,16 @@ declare class YjsVersion implements Version {
41
41
  * Throws if `other` is not a `YjsVersion`.
42
42
  */
43
43
  compare(other: Version): "behind" | "equal" | "ahead" | "concurrent";
44
+ /**
45
+ * Greatest lower bound (lattice meet) of two Yjs versions.
46
+ *
47
+ * Decodes both state vectors, computes the component-wise minimum
48
+ * via the shared `versionVectorMeet` utility, and encodes the result
49
+ * back to a Yjs state vector.
50
+ *
51
+ * @throws If `other` is not a `YjsVersion`.
52
+ */
53
+ meet(other: Version): YjsVersion;
44
54
  /**
45
55
  * Parse a serialized YjsVersion string back into a YjsVersion.
46
56
  *
@@ -159,35 +169,23 @@ declare function exportSince(doc: object, since: YjsVersion): SubstratePayload |
159
169
  declare function merge(doc: object, payload: SubstratePayload, origin?: string): void;
160
170
 
161
171
  /**
162
- * Bind a schema to the Yjs CRDT substrate with causal merge strategy.
163
- *
164
- * This is the recommended way to declare a Yjs-backed document type.
165
- * The factory builder injects a deterministic numeric Yjs clientID derived
166
- * from the exchange's string peerId, ensuring consistent change attribution
167
- * across all documents and sessions.
172
+ * The Yjs CRDT substrate namespace.
168
173
  *
169
- * **Unsupported annotations:** Yjs has no native counter, movable list,
170
- * or tree types. Schemas passed to `bindYjs` must not contain
171
- * `Schema.annotated("counter")`, `Schema.annotated("movable")`, or
172
- * `Schema.annotated("tree")`. These will throw at construction time.
174
+ * - `yjs.bind(schema)` collaborative sync (default)
175
+ * - `yjs.bind(schema, "ephemeral")` ephemeral/presence broadcast
176
+ * - `yjs.replica()` — collaborative replication (default)
177
+ * - `yjs.replica("ephemeral")` ephemeral replication
178
+ * - `yjs.unwrap(ref)` — access the underlying Y.Doc
173
179
  *
174
- * @example
175
- * ```ts
176
- * import { bindYjs } from "@kyneta/yjs-schema"
177
- * import { Schema } from "@kyneta/schema"
178
- *
179
- * const TodoDoc = bindYjs(Schema.doc({
180
- * title: Schema.annotated("text"),
181
- * items: Schema.list(Schema.struct({
182
- * name: Schema.string(),
183
- * done: Schema.boolean(),
184
- * })),
185
- * }))
186
- *
187
- * const doc = exchange.get("my-todos", TodoDoc)
188
- * ```
180
+ * Strategy is constrained to `CrdtStrategy` (`"collaborative" | "ephemeral"`).
181
+ * Passing `"authoritative"` is a compile error.
189
182
  */
190
- declare function bindYjs<S extends Schema>(schema: S): BoundSchema<S>;
183
+ /** The closed set of capability tags that the Yjs substrate supports. */
184
+ type YjsCaps = "text" | "json";
185
+ declare const yjs: SubstrateNamespace<CrdtStrategy, YjsCaps> & {
186
+ /** Access the underlying `Y.Doc` backing a ref. */
187
+ unwrap(ref: object): Y.Doc;
188
+ };
191
189
 
192
190
  /**
193
191
  * Apply a kyneta Change to the Yjs shared type tree imperatively.
@@ -215,15 +213,15 @@ declare function applyChangeToYjs(rootMap: Y.Map<any>, rootSchema: Schema, path:
215
213
  *
216
214
  * @param events - The events from the `observeDeep` callback
217
215
  */
218
- declare function eventsToOps(events: Y.YEvent<any>[]): Op[];
216
+ declare function eventsToOps(events: Y.YEvent<any>[], schema: Schema): Op[];
219
217
 
220
218
  /**
221
219
  * Ensure that a Y.Doc's root map contains the correct Yjs shared types
222
220
  * matching the schema structure.
223
221
  *
224
- * Obtains the root map via `doc.getMap("root")`, unwraps the root product
225
- * schema, and creates empty containers for each field within a single
226
- * `doc.transact()` call for atomicity.
222
+ * Obtains the root map via `doc.getMap("root")`, reads the root product
223
+ * schema's fields, and creates empty containers for each field within a
224
+ * single `doc.transact()` call for atomicity.
227
225
  *
228
226
  * When `conditional` is true, fields that already exist in the root map
229
227
  * are skipped. This is the correct mode after hydration — containers
@@ -240,7 +238,7 @@ declare function eventsToOps(events: Y.YEvent<any>[]): Op[];
240
238
  * structural ops across all peers, enabling Yjs deduplication on merge.
241
239
  *
242
240
  * @param doc - The Y.Doc to prepare
243
- * @param schema - The root document schema (typically annotated("doc", product))
241
+ * @param schema - The root document schema (a ProductSchema)
244
242
  * @param conditional - If true, skip fields that already exist in the root map.
245
243
  * Context: jj:smmulzkm (two-phase substrate construction)
246
244
  */
@@ -281,35 +279,9 @@ declare function yjsReader(doc: Y.Doc, schema: Schema): Reader;
281
279
  * @param schema - The root schema for the document.
282
280
  */
283
281
  declare function createYjsSubstrate(doc: Y.Doc, schema: Schema): Substrate<YjsVersion>;
282
+ declare const yjsReplicaFactory: ReplicaFactory<YjsVersion>;
284
283
  declare const yjsSubstrateFactory: SubstrateFactory<YjsVersion>;
285
284
 
286
- /**
287
- * Returns the `Y.Doc` backing the given ref.
288
- *
289
- * This is the Yjs-specific escape hatch for accessing substrate-level
290
- * capabilities: raw Yjs API, y-prosemirror/y-codemirror bindings,
291
- * undo manager, awareness protocol, Yjs providers (y-websocket,
292
- * y-indexeddb, y-webrtc, Hocuspocus, Liveblocks), etc.
293
- *
294
- * Currently supports root document refs only. Child-level resolution
295
- * (e.g. `yjs(doc.title)` → `Y.Text`) is future work.
296
- *
297
- * @param ref - A root document ref backed by a Yjs substrate
298
- * @returns The `Y.Doc` backing the ref
299
- * @throws If the ref is not backed by a Yjs substrate
300
- *
301
- * @example
302
- * ```ts
303
- * import { yjs } from "@kyneta/yjs-schema"
304
- *
305
- * const doc = exchange.get("my-doc", TodoDoc)
306
- * const yjsDoc = yjs(doc)
307
- * console.log(yjsDoc.getMap("root").toJSON()) // raw state
308
- * console.log(yjsDoc.clientID) // client ID
309
- * ```
310
- */
311
- declare function yjs(ref: object): Doc;
312
-
313
285
  /**
314
286
  * Navigate one step deeper into the Yjs shared type tree.
315
287
  *
@@ -338,16 +310,4 @@ declare function stepIntoYjs(current: unknown, segment: Segment): unknown;
338
310
  */
339
311
  declare function resolveYjsType(rootMap: Y.Map<any>, rootSchema: Schema, path: Path): unknown;
340
312
 
341
- /**
342
- * Collaborative text (CRDT). Produces `annotated("text")`.
343
- *
344
- * The annotation implies scalar string semantics for reads,
345
- * but the Yjs substrate provides collaborative editing (insert, delete)
346
- * via Y.Text.
347
- *
348
- * This is a convenience re-export so that `@kyneta/yjs-schema` users
349
- * don't need to import `LoroSchema` just for `text()`.
350
- */
351
- declare function text(): AnnotatedSchema<"text", undefined>;
352
-
353
- export { YjsVersion, applyChangeToYjs, bindYjs, createYjsDoc, createYjsDocFromEntirety, createYjsSubstrate, ensureContainers, eventsToOps, exportEntirety, exportSince, merge, resolveYjsType, stepIntoYjs, text, version, yjs, yjsReader, yjsSubstrateFactory };
313
+ export { type YjsCaps, YjsVersion, applyChangeToYjs, createYjsDoc, createYjsDocFromEntirety, createYjsSubstrate, ensureContainers, eventsToOps, exportEntirety, exportSince, merge, resolveYjsType, stepIntoYjs, version, yjs, yjsReader, yjsReplicaFactory, yjsSubstrateFactory };