@liveblocks/yjs 1.1.0-yjs4 → 1.1.1-yjs1

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
@@ -9,6 +9,12 @@ declare type MetaClientState = {
9
9
  clock: number;
10
10
  lastUpdated: number;
11
11
  };
12
+ /**
13
+ * This class will store YJS awareness in Liveblock's presence under the __yjs key
14
+ * IMPORTANT: The yjs awareness protocol uses ydoc.clientId to reference users
15
+ * to their respective documents. To avoid mapping yjs clientIds to liveblock's connectionId,
16
+ * we simply set the clientId of the doc to the connectionId. Then no further mapping is required
17
+ */
12
18
  declare class Awareness extends Observable<any> {
13
19
  private room;
14
20
  doc: Y.Doc;
@@ -32,6 +38,7 @@ declare class LiveblocksProvider<P extends JsonObject, S extends LsonObject, U e
32
38
  private unsubscribers;
33
39
  awareness: Awareness;
34
40
  constructor(room: Room<P, S, U, E>, doc: Y.Doc, config?: LiveblocksYjsOptions);
41
+ private syncDoc;
35
42
  private updateHandler;
36
43
  private resyncHttp;
37
44
  destroy(): void;
package/dist/index.js CHANGED
@@ -191,10 +191,17 @@ var LiveblocksProvider = class {
191
191
  constructor(room, doc, config) {
192
192
  this.lastUpdateDate = null;
193
193
  this.unsubscribers = [];
194
+ this.syncDoc = () => {
195
+ var _a;
196
+ this.doc.clientID = ((_a = this.room.getSelf()) == null ? void 0 : _a.connectionId) || this.doc.clientID;
197
+ this.awareness.clientID = this.doc.clientID;
198
+ const encodedVector = _jsbase64.Base64.fromUint8Array(Y.encodeStateVector(this.doc));
199
+ this.room.getYDoc(encodedVector);
200
+ };
194
201
  this.updateHandler = (update, origin) => __async(this, null, function* () {
195
202
  if (origin !== "backend") {
196
203
  const encodedUpdate = _jsbase64.Base64.fromUint8Array(update);
197
- this.room.updateDoc(encodedUpdate);
204
+ this.room.updateYDoc(encodedUpdate);
198
205
  if (this.httpEndpoint) {
199
206
  yield fetch(this.httpEndpoint, {
200
207
  method: "POST",
@@ -214,22 +221,14 @@ var LiveblocksProvider = class {
214
221
  this.doc.on("update", this.updateHandler);
215
222
  this.unsubscribers.push(
216
223
  this.room.events.connection.subscribe((e) => {
217
- var _a2;
218
224
  if (e === "open") {
219
- this.doc.clientID = ((_a2 = this.room.getSelf()) == null ? void 0 : _a2.connectionId) || this.doc.clientID;
220
- this.awareness.clientID = this.doc.clientID;
221
- const encodedVector = _jsbase64.Base64.fromUint8Array(
222
- Y.encodeStateVector(this.doc)
223
- );
224
- this.room.getDoc(encodedVector);
225
+ this.syncDoc();
225
226
  }
226
227
  })
227
228
  );
228
229
  this.unsubscribers.push(
229
- this.room.events.docUpdated.subscribe((updates) => {
230
- const decodedUpdates = updates.map(_jsbase64.Base64.toUint8Array);
231
- const update = Y.mergeUpdates(decodedUpdates);
232
- Y.applyUpdate(this.doc, update, "backend");
230
+ this.room.events.docUpdated.subscribe((update) => {
231
+ Y.applyUpdate(this.doc, _jsbase64.Base64.toUint8Array(update), "backend");
233
232
  })
234
233
  );
235
234
  if (config == null ? void 0 : config.httpEndpoint) {
@@ -243,7 +242,7 @@ var LiveblocksProvider = class {
243
242
  );
244
243
  void this.resyncHttp();
245
244
  }
246
- this.room.getDoc();
245
+ this.syncDoc();
247
246
  }
248
247
  resyncHttp() {
249
248
  return __async(this, null, function* () {
package/dist/index.mjs CHANGED
@@ -191,10 +191,17 @@ var LiveblocksProvider = class {
191
191
  constructor(room, doc, config) {
192
192
  this.lastUpdateDate = null;
193
193
  this.unsubscribers = [];
194
+ this.syncDoc = () => {
195
+ var _a;
196
+ this.doc.clientID = ((_a = this.room.getSelf()) == null ? void 0 : _a.connectionId) || this.doc.clientID;
197
+ this.awareness.clientID = this.doc.clientID;
198
+ const encodedVector = Base64.fromUint8Array(Y.encodeStateVector(this.doc));
199
+ this.room.getYDoc(encodedVector);
200
+ };
194
201
  this.updateHandler = (update, origin) => __async(this, null, function* () {
195
202
  if (origin !== "backend") {
196
203
  const encodedUpdate = Base64.fromUint8Array(update);
197
- this.room.updateDoc(encodedUpdate);
204
+ this.room.updateYDoc(encodedUpdate);
198
205
  if (this.httpEndpoint) {
199
206
  yield fetch(this.httpEndpoint, {
200
207
  method: "POST",
@@ -214,22 +221,14 @@ var LiveblocksProvider = class {
214
221
  this.doc.on("update", this.updateHandler);
215
222
  this.unsubscribers.push(
216
223
  this.room.events.connection.subscribe((e) => {
217
- var _a2;
218
224
  if (e === "open") {
219
- this.doc.clientID = ((_a2 = this.room.getSelf()) == null ? void 0 : _a2.connectionId) || this.doc.clientID;
220
- this.awareness.clientID = this.doc.clientID;
221
- const encodedVector = Base64.fromUint8Array(
222
- Y.encodeStateVector(this.doc)
223
- );
224
- this.room.getDoc(encodedVector);
225
+ this.syncDoc();
225
226
  }
226
227
  })
227
228
  );
228
229
  this.unsubscribers.push(
229
- this.room.events.docUpdated.subscribe((updates) => {
230
- const decodedUpdates = updates.map(Base64.toUint8Array);
231
- const update = Y.mergeUpdates(decodedUpdates);
232
- Y.applyUpdate(this.doc, update, "backend");
230
+ this.room.events.docUpdated.subscribe((update) => {
231
+ Y.applyUpdate(this.doc, Base64.toUint8Array(update), "backend");
233
232
  })
234
233
  );
235
234
  if (config == null ? void 0 : config.httpEndpoint) {
@@ -243,7 +242,7 @@ var LiveblocksProvider = class {
243
242
  );
244
243
  void this.resyncHttp();
245
244
  }
246
- this.room.getDoc();
245
+ this.syncDoc();
247
246
  }
248
247
  resyncHttp() {
249
248
  return __async(this, null, function* () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liveblocks/yjs",
3
- "version": "1.1.0-yjs4",
3
+ "version": "1.1.1-yjs1",
4
4
  "description": "An integration with . Liveblocks is the all-in-one toolkit to build collaborative products like Figma, Notion, and more.",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./dist/index.js",
@@ -26,8 +26,8 @@
26
26
  }
27
27
  },
28
28
  "dependencies": {
29
- "@liveblocks/client": "1.1.0-yjs4",
30
- "@liveblocks/core": "1.1.0-yjs4",
29
+ "@liveblocks/client": "1.1.1-yjs1",
30
+ "@liveblocks/core": "1.1.1-yjs1",
31
31
  "js-base64": "^3.7.5"
32
32
  },
33
33
  "peerDependencies": {