@stratasync/y-doc 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 ADDED
@@ -0,0 +1,44 @@
1
+ # @stratasync/y-doc
2
+
3
+ Yjs CRDT utilities and integration for collaborative editing in the Done.
4
+
5
+ ## Overview
6
+
7
+ sync-yjs provides Yjs document management and protocol utilities:
8
+
9
+ - **Document management** — create and manage Yjs documents for collaborative fields
10
+ - **Awareness protocol** — presence and cursor tracking across clients
11
+ - **Delta serialization** — encode/decode Yjs updates for transport
12
+ - **Sync protocol** — state vector exchange and update application
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ npm install @stratasync/y-doc
18
+ ```
19
+
20
+ Dependency: `yjs` ^13.6.21
21
+
22
+ ## Usage
23
+
24
+ ```typescript
25
+ import { createYjsManager } from "@stratasync/y-doc";
26
+
27
+ const yjsManager = createYjsManager();
28
+
29
+ // Create a document for a collaborative field
30
+ const doc = yjsManager.getOrCreateDoc("Task", taskId, "description");
31
+
32
+ // Encode state for transport
33
+ const update = Y.encodeStateAsUpdate(doc);
34
+
35
+ // Apply remote update
36
+ Y.applyUpdate(doc, remoteUpdate);
37
+ ```
38
+
39
+ ## Concepts
40
+
41
+ - **One Yjs document per collaborative field** — e.g., `Task.description` gets its own doc
42
+ - **Awareness** — separate protocol for presence (cursors, selections, user info)
43
+ - **Binary encoding** — Yjs uses efficient binary encoding, not JSON
44
+ - **Conflict-free** — CRDT guarantees eventual consistency without server coordination
@@ -0,0 +1,91 @@
1
+ /**
2
+ * YjsDocumentManager - Manages local Y.Doc instances for collaborative editing.
3
+ *
4
+ * Responsibilities:
5
+ * - Create and manage Y.Doc instances per document field
6
+ * - Handle Yjs sync protocol (state vector exchange)
7
+ * - Apply local and remote updates
8
+ * - Track connection state per document
9
+ * - Generate derived content from Y.Doc
10
+ */
11
+ import * as Y from "yjs";
12
+ import type { ConnectOptions, DocumentConnectionState, DocumentKey, YjsDocumentManagerConfig, YjsTransport } from "./types.js";
13
+ /**
14
+ * Manages Y.Doc instances for collaborative editing.
15
+ */
16
+ export declare class YjsDocumentManager {
17
+ private readonly docs;
18
+ private readonly remoteUpdateOrigin;
19
+ private readonly config;
20
+ private readonly liveEditingRetryConfig;
21
+ private persistenceKeyPrefix;
22
+ private transport;
23
+ private transportConnectionState;
24
+ private unsubscribeTransportMessage;
25
+ private unsubscribeTransportConnection;
26
+ private readonly connectionStateCallbacks;
27
+ private readonly contentCallbacks;
28
+ constructor(config: YjsDocumentManagerConfig);
29
+ setPersistenceKeyPrefix(prefix: string): void;
30
+ clearPersistedDocuments(): void;
31
+ setTransport(transport: YjsTransport): void;
32
+ getDocument(docKey: DocumentKey): Y.Doc;
33
+ /**
34
+ * Connect to a document for collaborative editing.
35
+ */
36
+ connect(docKey: DocumentKey, options?: ConnectOptions): void;
37
+ disconnect(docKey: DocumentKey): void;
38
+ getConnectionState(docKey: DocumentKey): DocumentConnectionState;
39
+ onConnectionStateChange(docKey: DocumentKey, callback: (state: DocumentConnectionState) => void): () => void;
40
+ onContentChange(docKey: DocumentKey, callback: (content: string) => void): () => void;
41
+ /**
42
+ * Apply a remote update to a document.
43
+ */
44
+ applyRemoteUpdate(docKey: DocumentKey, update: Uint8Array): void;
45
+ /**
46
+ * Apply a snapshot (full state) to a document.
47
+ * Semantically identical to applyRemoteUpdate — a Yjs snapshot
48
+ * is applied using the same Y.applyUpdate mechanism.
49
+ */
50
+ applySnapshot(docKey: DocumentKey, snapshot: Uint8Array): void;
51
+ getDerivedContent(docKey: DocumentKey): string;
52
+ getStateVector(docKey: DocumentKey): Uint8Array;
53
+ getUpdatesSince(docKey: DocumentKey, stateVector: Uint8Array): Uint8Array | null;
54
+ getEncodedState(docKey: DocumentKey): Uint8Array | null;
55
+ destroy(docKey: DocumentKey): void;
56
+ destroyAll(): void;
57
+ private getOrCreateState;
58
+ private static createDocumentState;
59
+ private handleTransportConnectionStateChange;
60
+ private markActiveDocumentsAsConnecting;
61
+ private replaySyncStep1ForActiveDocuments;
62
+ private requestSyncStep1;
63
+ /**
64
+ * Seed pending initial content into the Y.Doc. Returns true if content was seeded.
65
+ *
66
+ * Uses `origin` so the local update handler ignores the insert (prevents
67
+ * seeded content from being buffered as a pending local update).
68
+ */
69
+ private static seedPendingContent;
70
+ private static clearRetryTimer;
71
+ private static resetRetryState;
72
+ private scheduleRetrySyncStep1;
73
+ private attachLocalUpdateHandler;
74
+ private static detachLocalUpdateHandler;
75
+ private static isDocActive;
76
+ private shouldApplyRemoteMessage;
77
+ private sendIfConnected;
78
+ private setConnectionState;
79
+ private notifyContentChange;
80
+ private sendSyncStep1;
81
+ private sendUpdate;
82
+ private flushPendingLocalUpdates;
83
+ private handleMessage;
84
+ private handleSyncStep2;
85
+ private handleUpdate;
86
+ private handleError;
87
+ private getPersistedDocumentKey;
88
+ private restorePersistedDocument;
89
+ private persistDocument;
90
+ }
91
+ //# sourceMappingURL=document-manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"document-manager.d.ts","sourceRoot":"","sources":["../src/document-manager.ts"],"names":[],"mappings":"AACA;;;;;;;;;GASG;AAGH,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAMzB,OAAO,KAAK,EAEV,cAAc,EACd,uBAAuB,EACvB,WAAW,EAIX,wBAAwB,EAExB,YAAY,EAGb,MAAM,YAAY,CAAC;AAqQpB;;GAEG;AACH,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAoC;IACzD,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAiC;IACpE,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA2B;IAClD,OAAO,CAAC,QAAQ,CAAC,sBAAsB,CAAyB;IAChE,OAAO,CAAC,oBAAoB,CAAS;IACrC,OAAO,CAAC,SAAS,CAA6B;IAC9C,OAAO,CAAC,wBAAwB,CACf;IACjB,OAAO,CAAC,2BAA2B,CAA6B;IAChE,OAAO,CAAC,8BAA8B,C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