@liveblocks/yjs 1.5.3-test1 → 1.6.0-subdocs1

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.mts CHANGED
@@ -27,20 +27,52 @@ declare class Awareness extends Observable<unknown> {
27
27
  setLocalStateField(field: string, value: JsonObject | null): void;
28
28
  getStates(): Map<number, unknown>;
29
29
  }
30
+
31
+ declare class yDocHandler extends Observable<unknown> {
32
+ private unsubscribers;
33
+ private _synced;
34
+ private doc;
35
+ private updateRoomDoc;
36
+ private fetchRoomDoc;
37
+ constructor({ doc, isRoot, updateDoc, fetchDoc, }: {
38
+ doc: Y.Doc;
39
+ isRoot: boolean;
40
+ updateDoc: (update: string, guid?: string) => void;
41
+ fetchDoc: (vector: string, guid?: string) => void;
42
+ });
43
+ handleServerUpdate: ({ update, stateVector, }: {
44
+ update: string;
45
+ stateVector: string | null;
46
+ }) => void;
47
+ syncDoc: () => void;
48
+ get synced(): boolean;
49
+ set synced(state: boolean);
50
+ private updateHandler;
51
+ destroy(): void;
52
+ }
53
+
54
+ declare type ProviderOptions = {
55
+ autoloadSubdocs: boolean;
56
+ };
30
57
  declare class LiveblocksProvider<P extends JsonObject, S extends LsonObject, U extends BaseUserMeta, E extends Json> extends Observable<unknown> {
31
58
  private room;
32
- private doc;
59
+ private rootDoc;
60
+ private options;
33
61
  private unsubscribers;
34
62
  awareness: Awareness;
35
- private _synced;
36
- constructor(room: Room<P, S, U, E>, doc: Y.Doc);
63
+ rootDocHandler: yDocHandler;
64
+ subdocHandlers: Map<string, yDocHandler>;
65
+ constructor(room: Room<P, S, U, E>, doc: Y.Doc, options?: ProviderOptions | undefined);
66
+ private handleSubdocs;
67
+ private updateDoc;
68
+ private fetchDoc;
69
+ private createSubdocHandler;
70
+ loadSubdoc: (guid: string) => boolean;
37
71
  private syncDoc;
38
72
  get synced(): boolean;
39
- set synced(state: boolean);
40
- private updateHandler;
41
73
  destroy(): void;
42
74
  disconnect(): void;
43
75
  connect(): void;
44
76
  }
45
77
 
46
- export { Awareness, LiveblocksProvider as default };
78
+ export { LiveblocksProvider as default };
package/dist/index.d.ts CHANGED
@@ -27,20 +27,52 @@ declare class Awareness extends Observable<unknown> {
27
27
  setLocalStateField(field: string, value: JsonObject | null): void;
28
28
  getStates(): Map<number, unknown>;
29
29
  }
30
+
31
+ declare class yDocHandler extends Observable<unknown> {
32
+ private unsubscribers;
33
+ private _synced;
34
+ private doc;
35
+ private updateRoomDoc;
36
+ private fetchRoomDoc;
37
+ constructor({ doc, isRoot, updateDoc, fetchDoc, }: {
38
+ doc: Y.Doc;
39
+ isRoot: boolean;
40
+ updateDoc: (update: string, guid?: string) => void;
41
+ fetchDoc: (vector: string, guid?: string) => void;
42
+ });
43
+ handleServerUpdate: ({ update, stateVector, }: {
44
+ update: string;
45
+ stateVector: string | null;
46
+ }) => void;
47
+ syncDoc: () => void;
48
+ get synced(): boolean;
49
+ set synced(state: boolean);
50
+ private updateHandler;
51
+ destroy(): void;
52
+ }
53
+
54
+ declare type ProviderOptions = {
55
+ autoloadSubdocs: boolean;
56
+ };
30
57
  declare class LiveblocksProvider<P extends JsonObject, S extends LsonObject, U extends BaseUserMeta, E extends Json> extends Observable<unknown> {
31
58
  private room;
32
- private doc;
59
+ private rootDoc;
60
+ private options;
33
61
  private unsubscribers;
34
62
  awareness: Awareness;
35
- private _synced;
36
- constructor(room: Room<P, S, U, E>, doc: Y.Doc);
63
+ rootDocHandler: yDocHandler;
64
+ subdocHandlers: Map<string, yDocHandler>;
65
+ constructor(room: Room<P, S, U, E>, doc: Y.Doc, options?: ProviderOptions | undefined);
66
+ private handleSubdocs;
67
+ private updateDoc;
68
+ private fetchDoc;
69
+ private createSubdocHandler;
70
+ loadSubdoc: (guid: string) => boolean;
37
71
  private syncDoc;
38
72
  get synced(): boolean;
39
- set synced(state: boolean);
40
- private updateHandler;
41
73
  destroy(): void;
42
74
  disconnect(): void;
43
75
  connect(): void;
44
76
  }
45
77
 
46
- export { Awareness, LiveblocksProvider as default };
78
+ export { LiveblocksProvider as default };
package/dist/index.js CHANGED
@@ -1,6 +1,5 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/index.ts
2
2
  var _core = require('@liveblocks/core');
3
- var _jsbase64 = require('js-base64');
4
3
 
5
4
  // ../../node_modules/lib0/map.js
6
5
  var create = () => /* @__PURE__ */ new Map();
@@ -72,16 +71,7 @@ var Observable = class {
72
71
  }
73
72
  };
74
73
 
75
- // src/index.ts
76
- var _yjs = require('yjs'); var Y = _interopRequireWildcard(_yjs);
77
-
78
- // src/version.ts
79
- var PKG_NAME = "@liveblocks/yjs";
80
- var PKG_VERSION = "1.5.3-test1";
81
- var PKG_FORMAT = "cjs";
82
-
83
- // src/index.ts
84
- _core.detectDupes.call(void 0, PKG_NAME, PKG_VERSION, PKG_FORMAT);
74
+ // src/awareness.ts
85
75
  var Y_PRESENCE_KEY = "__yjs";
86
76
  var Awareness = class extends Observable {
87
77
  constructor(doc, room) {
@@ -131,13 +121,13 @@ var Awareness = class extends Observable {
131
121
  return presence[Y_PRESENCE_KEY];
132
122
  }
133
123
  setLocalState(state) {
134
- const presence = _optionalChain([this, 'access', _ => _.room, 'access', _2 => _2.getSelf, 'call', _3 => _3(), 'optionalAccess', _4 => _4.presence, 'access', _5 => _5[Y_PRESENCE_KEY]]);
124
+ const presence = _optionalChain([this, 'access', _2 => _2.room, 'access', _3 => _3.getSelf, 'call', _4 => _4(), 'optionalAccess', _5 => _5.presence, 'access', _6 => _6[Y_PRESENCE_KEY]]);
135
125
  this.room.updatePresence({
136
126
  __yjs: { ...presence || {}, ...state || {} }
137
127
  });
138
128
  }
139
129
  setLocalStateField(field, value) {
140
- const presence = _optionalChain([this, 'access', _6 => _6.room, 'access', _7 => _7.getSelf, 'call', _8 => _8(), 'optionalAccess', _9 => _9.presence, 'access', _10 => _10[Y_PRESENCE_KEY]]);
130
+ const presence = _optionalChain([this, 'access', _7 => _7.room, 'access', _8 => _8.getSelf, 'call', _9 => _9(), 'optionalAccess', _10 => _10.presence, 'access', _11 => _11[Y_PRESENCE_KEY]]);
141
131
  const update = { [field]: value };
142
132
  this.room.updatePresence({
143
133
  __yjs: { ...presence || {}, ...update }
@@ -158,80 +148,215 @@ var Awareness = class extends Observable {
158
148
  return states;
159
149
  }
160
150
  };
161
- var LiveblocksProvider = class extends Observable {
162
- constructor(room, doc) {
151
+
152
+ // src/doc.ts
153
+
154
+ var _jsbase64 = require('js-base64');
155
+ var _yjs = require('yjs'); var Y = _interopRequireWildcard(_yjs);
156
+
157
+ // src/version.ts
158
+ var PKG_NAME = "@liveblocks/yjs";
159
+ var PKG_VERSION = "1.6.0-subdocs1";
160
+ var PKG_FORMAT = "cjs";
161
+
162
+ // src/doc.ts
163
+ _core.detectDupes.call(void 0, PKG_NAME, PKG_VERSION, PKG_FORMAT);
164
+ var yDocHandler = class extends Observable {
165
+ constructor({
166
+ doc,
167
+ isRoot,
168
+ updateDoc,
169
+ fetchDoc
170
+ }) {
163
171
  super();
164
172
  this.unsubscribers = [];
165
173
  this._synced = false;
174
+ this.handleServerUpdate = ({
175
+ update,
176
+ stateVector
177
+ }) => {
178
+ Y.applyUpdate(this.doc, _jsbase64.Base64.toUint8Array(update), "backend");
179
+ if (stateVector) {
180
+ try {
181
+ const localUpdate = Y.encodeStateAsUpdate(
182
+ this.doc,
183
+ _jsbase64.Base64.toUint8Array(stateVector)
184
+ );
185
+ this.updateRoomDoc(_jsbase64.Base64.fromUint8Array(localUpdate));
186
+ } catch (e) {
187
+ console.warn(e);
188
+ }
189
+ this.synced = true;
190
+ }
191
+ };
166
192
  this.syncDoc = () => {
167
193
  this.synced = false;
168
- this.doc.clientID = _optionalChain([this, 'access', _11 => _11.room, 'access', _12 => _12.getSelf, 'call', _13 => _13(), 'optionalAccess', _14 => _14.connectionId]) || this.doc.clientID;
169
- this.awareness.clientID = this.doc.clientID;
170
194
  const encodedVector = _jsbase64.Base64.fromUint8Array(Y.encodeStateVector(this.doc));
171
- this.room.fetchYDoc(encodedVector);
195
+ this.fetchRoomDoc(encodedVector);
172
196
  };
173
197
  this.updateHandler = (update, origin) => {
174
198
  if (origin !== "backend") {
175
199
  const encodedUpdate = _jsbase64.Base64.fromUint8Array(update);
176
- this.room.updateYDoc(encodedUpdate);
200
+ this.updateRoomDoc(encodedUpdate);
177
201
  }
178
202
  };
179
203
  this.doc = doc;
204
+ this.doc.on("update", this.updateHandler);
205
+ this.updateRoomDoc = (update) => {
206
+ updateDoc(update, isRoot ? void 0 : this.doc.guid);
207
+ };
208
+ this.fetchRoomDoc = (vector) => {
209
+ fetchDoc(vector, isRoot ? void 0 : this.doc.guid);
210
+ };
211
+ this.syncDoc();
212
+ }
213
+ // The sync'd property is required by some provider implementations
214
+ get synced() {
215
+ return this._synced;
216
+ }
217
+ set synced(state) {
218
+ if (this._synced !== state) {
219
+ this._synced = state;
220
+ this.emit("synced", [state]);
221
+ this.emit("sync", [state]);
222
+ }
223
+ }
224
+ destroy() {
225
+ this.doc.off("update", this.updateHandler);
226
+ this.unsubscribers.forEach((unsub) => unsub());
227
+ this._observers = /* @__PURE__ */ new Map();
228
+ this.doc.destroy();
229
+ }
230
+ };
231
+
232
+ // src/index.ts
233
+ _core.detectDupes.call(void 0, PKG_NAME, PKG_VERSION, PKG_FORMAT);
234
+ var DefaultOptions = {
235
+ autoloadSubdocs: false
236
+ };
237
+ var LiveblocksProvider = class extends Observable {
238
+ constructor(room, doc, options = DefaultOptions) {
239
+ super();
240
+ this.unsubscribers = [];
241
+ this.subdocHandlers = /* @__PURE__ */ new Map();
242
+ this.handleSubdocs = ({
243
+ loaded,
244
+ removed,
245
+ added
246
+ }) => {
247
+ loaded.forEach(this.createSubdocHandler);
248
+ if (this.options.autoloadSubdocs) {
249
+ for (const subdoc of added) {
250
+ if (!this.subdocHandlers.has(subdoc.guid)) {
251
+ subdoc.load();
252
+ }
253
+ }
254
+ }
255
+ for (const subdoc of removed) {
256
+ if (this.subdocHandlers.has(subdoc.guid)) {
257
+ _optionalChain([this, 'access', _12 => _12.subdocHandlers, 'access', _13 => _13.get, 'call', _14 => _14(subdoc.guid), 'optionalAccess', _15 => _15.destroy, 'call', _16 => _16()]);
258
+ this.subdocHandlers.delete(subdoc.guid);
259
+ }
260
+ }
261
+ };
262
+ this.updateDoc = (update, guid) => {
263
+ this.room.updateYDoc(update, guid);
264
+ };
265
+ this.fetchDoc = (vector, guid) => {
266
+ this.room.fetchYDoc(vector, guid);
267
+ };
268
+ this.createSubdocHandler = (subdoc) => {
269
+ if (this.subdocHandlers.has(subdoc.guid)) {
270
+ _optionalChain([this, 'access', _17 => _17.subdocHandlers, 'access', _18 => _18.get, 'call', _19 => _19(subdoc.guid), 'optionalAccess', _20 => _20.syncDoc, 'call', _21 => _21()]);
271
+ return;
272
+ }
273
+ const handler = new yDocHandler({
274
+ doc: subdoc,
275
+ isRoot: false,
276
+ updateDoc: this.updateDoc,
277
+ fetchDoc: this.fetchDoc
278
+ });
279
+ this.subdocHandlers.set(subdoc.guid, handler);
280
+ };
281
+ // attempt to load a subdoc of a given guid
282
+ this.loadSubdoc = (guid) => {
283
+ for (const subdoc of this.rootDoc.subdocs) {
284
+ if (subdoc.guid === guid) {
285
+ subdoc.load();
286
+ return true;
287
+ }
288
+ }
289
+ return false;
290
+ };
291
+ this.syncDoc = () => {
292
+ this.rootDoc.clientID = _optionalChain([this, 'access', _22 => _22.room, 'access', _23 => _23.getSelf, 'call', _24 => _24(), 'optionalAccess', _25 => _25.connectionId]) || this.rootDoc.clientID;
293
+ this.awareness.clientID = this.rootDoc.clientID;
294
+ this.rootDocHandler.syncDoc();
295
+ for (const [_, handler] of this.subdocHandlers) {
296
+ handler.syncDoc();
297
+ }
298
+ };
299
+ this.rootDoc = doc;
180
300
  this.room = room;
181
- const connectionId = _optionalChain([this, 'access', _15 => _15.room, 'access', _16 => _16.getSelf, 'call', _17 => _17(), 'optionalAccess', _18 => _18.connectionId]);
301
+ this.options = options;
302
+ this.rootDocHandler = new yDocHandler({
303
+ doc,
304
+ isRoot: true,
305
+ updateDoc: this.updateDoc,
306
+ fetchDoc: this.fetchDoc
307
+ });
308
+ const connectionId = _optionalChain([this, 'access', _26 => _26.room, 'access', _27 => _27.getSelf, 'call', _28 => _28(), 'optionalAccess', _29 => _29.connectionId]);
182
309
  if (connectionId) {
183
- this.doc.clientID = connectionId;
310
+ this.rootDoc.clientID = connectionId;
184
311
  }
185
- this.awareness = new Awareness(this.doc, this.room);
186
- this.doc.on("update", this.updateHandler);
312
+ this.awareness = new Awareness(this.rootDoc, this.room);
187
313
  this.unsubscribers.push(
188
314
  this.room.events.status.subscribe((status) => {
189
315
  if (status === "connected") {
190
- this.syncDoc();
316
+ this.rootDocHandler.syncDoc();
191
317
  } else {
192
- this.synced = false;
318
+ this.rootDocHandler.synced = false;
193
319
  }
194
320
  })
195
321
  );
196
322
  this.unsubscribers.push(
197
323
  this.room.events.ydoc.subscribe((message) => {
198
- const { type, update } = message;
324
+ const { type } = message;
199
325
  if (type === _core.ClientMsgCode.UPDATE_YDOC) {
200
326
  return;
201
327
  }
202
- Y.applyUpdate(this.doc, _jsbase64.Base64.toUint8Array(update), "backend");
203
- const { stateVector } = message;
204
- if (stateVector) {
205
- try {
206
- const localUpdate = Y.encodeStateAsUpdate(
207
- this.doc,
208
- _jsbase64.Base64.toUint8Array(stateVector)
209
- );
210
- this.room.updateYDoc(_jsbase64.Base64.fromUint8Array(localUpdate));
211
- } catch (e) {
212
- console.warn(e);
213
- }
214
- this.synced = true;
328
+ const { stateVector, update, guid } = message;
329
+ if (guid !== void 0) {
330
+ _optionalChain([this, 'access', _30 => _30.subdocHandlers, 'access', _31 => _31.get, 'call', _32 => _32(guid), 'optionalAccess', _33 => _33.handleServerUpdate, 'call', _34 => _34({ update, stateVector })]);
331
+ } else {
332
+ this.rootDocHandler.handleServerUpdate({ update, stateVector });
215
333
  }
216
334
  })
217
335
  );
336
+ this.rootDocHandler.on("synced", () => {
337
+ const state = this.rootDocHandler.synced;
338
+ for (const [_, handler] of this.subdocHandlers) {
339
+ handler.syncDoc();
340
+ }
341
+ this.emit("synced", [state]);
342
+ this.emit("sync", [state]);
343
+ });
344
+ this.rootDoc.on("subdocs", this.handleSubdocs);
218
345
  this.syncDoc();
219
346
  }
220
347
  // The sync'd property is required by some provider implementations
221
348
  get synced() {
222
- return this._synced;
223
- }
224
- set synced(state) {
225
- if (this._synced !== state) {
226
- this._synced = state;
227
- this.emit("synced", [state]);
228
- this.emit("sync", [state]);
229
- }
349
+ return this.rootDocHandler.synced;
230
350
  }
231
351
  destroy() {
232
- this.doc.off("update", this.updateHandler);
233
352
  this.unsubscribers.forEach((unsub) => unsub());
234
353
  this.awareness.destroy();
354
+ this.rootDocHandler.destroy();
355
+ this._observers = /* @__PURE__ */ new Map();
356
+ for (const [_, handler] of this.subdocHandlers) {
357
+ handler.destroy();
358
+ }
359
+ this.subdocHandlers.clear();
235
360
  super.destroy();
236
361
  }
237
362
  // Some provider implementations expect to be able to call connect/disconnect, implement as noop
@@ -242,6 +367,5 @@ var LiveblocksProvider = class extends Observable {
242
367
  };
243
368
 
244
369
 
245
-
246
- exports.Awareness = Awareness; exports.default = LiveblocksProvider;
370
+ exports.default = LiveblocksProvider;
247
371
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../../../node_modules/lib0/map.js","../../../node_modules/lib0/set.js","../../../node_modules/lib0/array.js","../../../node_modules/lib0/observable.js","../src/version.ts"],"names":["create"],"mappings":";AAOA,SAAS,eAAe,mBAAmB;AAC3C,SAAS,cAAc;;;ACMhB,IAAM,SAAS,MAAM,oBAAI,IAAI;AAgC7B,IAAM,iBAAiB,CAAC,KAAK,KAAK,YAAY;AACnD,MAAI,MAAM,IAAI,IAAI,GAAG;AACrB,MAAI,QAAQ,QAAW;AACrB,QAAI,IAAI,KAAK,MAAM,QAAQ,CAAC;AAAA,EAC9B;AACA,SAAO;AACT;;;AC9CO,IAAMA,UAAS,MAAM,oBAAI,IAAI;;;AC6C7B,IAAM,OAAO,MAAM;AAgFnB,IAAM,UAAU,MAAM;;;ACpHtB,IAAM,aAAN,MAAiB;AAAA,EACtB,cAAe;AAKb,SAAK,aAAiB,OAAO;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,GAAI,MAAM,GAAG;AACX,IAAI,eAAe,KAAK,YAAY,MAAUA,OAAM,EAAE,IAAI,CAAC;AAAA,EAC7D;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,KAAM,MAAM,GAAG;AAIb,UAAM,KAAK,IAAI,SAAS;AACtB,WAAK,IAAI,MAAM,EAAE;AACjB,QAAE,GAAG,IAAI;AAAA,IACX;AACA,SAAK,GAAG,MAAM,EAAE;AAAA,EAClB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAK,MAAM,GAAG;AACZ,UAAM,YAAY,KAAK,WAAW,IAAI,IAAI;AAC1C,QAAI,cAAc,QAAW;AAC3B,gBAAU,OAAO,CAAC;AAClB,UAAI,UAAU,SAAS,GAAG;AACxB,aAAK,WAAW,OAAO,IAAI;AAAA,MAC7B;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,KAAM,MAAM,MAAM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AS,GAAP;AAEA,oBAAQ,KAAK,CAAC;AAAA,UAChB;AAGA,eAAK,SAAS;AAAA,QAChB;AAAA,MACF,CAAC;AAAA,IACH;AACA,SAAK,QAAQ;AAAA,EACf;AAAA;AAAA,EAmBA,IAAI,SAAkB;AACpB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAI,OAAO,OAAgB;AACzB,QAAI,KAAK,YAAY,OAAO;AAC1B,WAAK,UAAU;AACf,WAAK,KAAK,UAAU,CAAC,KAAK,CAAC;AAC3B,WAAK,KAAK,QAAQ,CAAC,KAAK,CAAC;AAAA,IAC3B;AAAA,EACF;AAAA,EASA,UAAgB;AACd,SAAK,IAAI,IAAI,UAAU,KAAK,aAAa;AACzC,SAAK,cAAc,QAAQ,CAAC,UAAU,MAAM,CAAC;AAC7C,SAAK,UAAU,QAAQ;AACvB,UAAM,QAAQ;AAAA,EAChB;AAAA;AAAA,EAGA,aAAmB;AAAA,EAEnB;AAAA,EAEA,UAAgB;AAAA,EAEhB;AACF","sourcesContent":["import type {\n BaseUserMeta,\n Json,\n JsonObject,\n LsonObject,\n Room,\n} from \"@liveblocks/client\";\nimport { ClientMsgCode, detectDupes } from \"@liveblocks/core\";\nimport { Base64 } from \"js-base64\";\nimport { Observable } from \"lib0/observable\";\nimport * as Y from \"yjs\";\n\nimport { PKG_FORMAT, PKG_NAME, PKG_VERSION } from \"./version\";\n2;\n\ndetectDupes(PKG_NAME, PKG_VERSION, PKG_FORMAT);\n\nconst Y_PRESENCE_KEY = \"__yjs\";\n\ntype MetaClientState = {\n clock: number;\n lastUpdated: number;\n};\n\n/**\n * This class will store Yjs awareness in Liveblock's presence under the __yjs key\n * IMPORTANT: The Yjs awareness protocol uses ydoc.clientId to reference users\n * to their respective documents. To avoid mapping Yjs clientIds to liveblock's connectionId,\n * we simply set the clientId of the doc to the connectionId. Then no further mapping is required\n */\nexport class Awareness extends Observable<unknown> {\n private room: Room<JsonObject, LsonObject, BaseUserMeta, Json>;\n public doc: Y.Doc;\n public clientID: number;\n public states: Map<number, unknown> = new Map();\n // Meta is used to keep track and timeout users who disconnect. Liveblocks provides this for us, so we don't need to\n // manage it here. Unfortunately, it's expected to exist by various integrations, so it's an empty map.\n public meta: Map<number, MetaClientState> = new Map();\n // _checkInterval this would hold a timer to remove users, but Liveblock's presence already handles this\n // unfortunately it's typed by various integrations\n public _checkInterval: number = 0;\n\n private othersUnsub: () => void;\n constructor(\n doc: Y.Doc,\n room: Room<JsonObject, LsonObject, BaseUserMeta, Json>\n ) {\n super();\n this.doc = doc;\n this.room = room;\n this.clientID = doc.clientID;\n this.othersUnsub = this.room.events.others.subscribe((event) => {\n // When others are changed, we emit an event that contains arrays added/updated/removed.\n if (event.type === \"leave\") {\n // REMOVED\n this.emit(\"change\", [\n { added: [], updated: [], removed: [event.user.connectionId] },\n \"local\",\n ]);\n }\n\n if (event.type === \"enter\") {\n // ADDED\n this.emit(\"change\", [\n { added: [event.user.connectionId], updated: [], removed: [] },\n \"local\",\n ]);\n }\n\n if (event.type === \"update\") {\n // UPDATED\n this.emit(\"change\", [\n { added: [], updated: [event.user.connectionId], removed: [] },\n \"local\",\n ]);\n }\n });\n }\n\n destroy(): void {\n this.emit(\"destroy\", [this]);\n this.othersUnsub();\n this.setLocalState(null);\n super.destroy();\n }\n\n getLocalState(): JsonObject | null {\n const presence = this.room.getPresence();\n if (\n Object.keys(presence).length === 0 ||\n typeof presence[Y_PRESENCE_KEY] === \"undefined\"\n ) {\n return null;\n }\n return presence[Y_PRESENCE_KEY] as JsonObject | null;\n }\n\n setLocalState(state: Partial<JsonObject> | null): void {\n const presence = this.room.getSelf()?.presence[Y_PRESENCE_KEY];\n this.room.updatePresence({\n __yjs: { ...((presence as JsonObject) || {}), ...(state || {}) },\n });\n }\n\n setLocalStateField(field: string, value: JsonObject | null): void {\n const presence = this.room.getSelf()?.presence[Y_PRESENCE_KEY];\n const update = { [field]: value } as Partial<JsonObject>;\n this.room.updatePresence({\n __yjs: { ...((presence as JsonObject) || {}), ...update },\n });\n }\n\n // Translate liveblocks presence to yjs awareness\n getStates(): Map<number, unknown> {\n const others = this.room.getOthers();\n const states = others.reduce((acc: Map<number, unknown>, currentValue) => {\n if (currentValue.connectionId) {\n // connectionId == actorId == yjs.clientId\n acc.set(\n currentValue.connectionId,\n currentValue.presence[Y_PRESENCE_KEY] || {}\n );\n }\n return acc;\n }, new Map());\n return states;\n }\n}\n\nexport default class LiveblocksProvider<\n P extends JsonObject,\n S extends LsonObject,\n U extends BaseUserMeta,\n E extends Json,\n> extends Observable<unknown> {\n private room: Room<P, S, U, E>;\n private doc: Y.Doc;\n\n private unsubscribers: Array<() => void> = [];\n\n public awareness: Awareness;\n\n private _synced = false;\n\n constructor(room: Room<P, S, U, E>, doc: Y.Doc) {\n super();\n this.doc = doc;\n this.room = room;\n\n // if we have a connectionId already during construction, use that\n const connectionId = this.room.getSelf()?.connectionId;\n if (connectionId) {\n this.doc.clientID = connectionId;\n }\n this.awareness = new Awareness(this.doc, this.room);\n this.doc.on(\"update\", this.updateHandler);\n\n this.unsubscribers.push(\n this.room.events.status.subscribe((status) => {\n if (status === \"connected\") {\n this.syncDoc();\n } else {\n this.synced = false;\n }\n })\n );\n\n this.unsubscribers.push(\n this.room.events.ydoc.subscribe((message) => {\n const { type, update } = message;\n if (type === ClientMsgCode.UPDATE_YDOC) {\n // don't apply updates that came from the client\n return;\n }\n // apply update from the server\n Y.applyUpdate(this.doc, Base64.toUint8Array(update), \"backend\");\n const { stateVector } = message;\n // if this update is the result of a fetch, the state vector is included\n if (stateVector) {\n // Use server state to calculate a diff and send it\n try {\n const localUpdate = Y.encodeStateAsUpdate(\n this.doc,\n Base64.toUint8Array(stateVector)\n );\n this.room.updateYDoc(Base64.fromUint8Array(localUpdate));\n } catch (e) {\n // something went wrong encoding local state to send to the server\n console.warn(e);\n }\n // now that we've sent our local and received from server, we're in sync\n // calling `syncDoc` again will sync up the documents\n this.synced = true;\n }\n })\n );\n this.syncDoc();\n }\n\n private syncDoc = () => {\n this.synced = false;\n /**\n * If the connection changes, set the new id, this is used by awareness.\n * yjs' only requirement for clientID is that it's truly unique and a number.\n * Liveblock's connectionID satisfies those constraints\n * */\n this.doc.clientID = this.room.getSelf()?.connectionId || this.doc.clientID;\n this.awareness.clientID = this.doc.clientID; // tell our awareness provider the new ID\n\n // The state vector is sent to the server so it knows what to send back\n // if you don't send it, it returns everything\n const encodedVector = Base64.fromUint8Array(Y.encodeStateVector(this.doc));\n this.room.fetchYDoc(encodedVector);\n };\n\n // The sync'd property is required by some provider implementations\n get synced(): boolean {\n return this._synced;\n }\n\n set synced(state: boolean) {\n if (this._synced !== state) {\n this._synced = state;\n this.emit(\"synced\", [state]);\n this.emit(\"sync\", [state]);\n }\n }\n\n private updateHandler = (update: Uint8Array, origin: string) => {\n if (origin !== \"backend\") {\n const encodedUpdate = Base64.fromUint8Array(update);\n this.room.updateYDoc(encodedUpdate);\n }\n };\n\n destroy(): void {\n this.doc.off(\"update\", this.updateHandler);\n this.unsubscribers.forEach((unsub) => unsub());\n this.awareness.destroy();\n super.destroy();\n }\n\n // Some provider implementations expect to be able to call connect/disconnect, implement as noop\n disconnect(): void {\n // This is a noop for liveblocks as connections are managed by the room\n }\n\n connect(): void {\n // This is a noop for liveblocks as connections are managed by the room\n }\n}\n","/**\n * Utility module to work with key-value stores.\n *\n * @module map\n */\n\n/**\n * Creates a new Map instance.\n *\n * @function\n * @return {Map<any, any>}\n *\n * @function\n */\nexport const create = () => new Map()\n\n/**\n * Copy a Map object into a fresh Map object.\n *\n * @function\n * @template X,Y\n * @param {Map<X,Y>} m\n * @return {Map<X,Y>}\n */\nexport const copy = m => {\n const r = create()\n m.forEach((v, k) => { r.set(k, v) })\n return r\n}\n\n/**\n * Get map property. Create T if property is undefined and set T on map.\n *\n * ```js\n * const listeners = map.setIfUndefined(events, 'eventName', set.create)\n * listeners.add(listener)\n * ```\n *\n * @function\n * @template V,K\n * @template {Map<K,V>} MAP\n * @param {MAP} map\n * @param {K} key\n * @param {function():V} createT\n * @return {V}\n */\nexport const setIfUndefined = (map, key, createT) => {\n let set = map.get(key)\n if (set === undefined) {\n map.set(key, set = createT())\n }\n return set\n}\n\n/**\n * Creates an Array and populates it with the content of all key-value pairs using the `f(value, key)` function.\n *\n * @function\n * @template K\n * @template V\n * @template R\n * @param {Map<K,V>} m\n * @param {function(V,K):R} f\n * @return {Array<R>}\n */\nexport const map = (m, f) => {\n const res = []\n for (const [key, value] of m) {\n res.push(f(value, key))\n }\n return res\n}\n\n/**\n * Tests whether any key-value pairs pass the test implemented by `f(value, key)`.\n *\n * @todo should rename to some - similarly to Array.some\n *\n * @function\n * @template K\n * @template V\n * @param {Map<K,V>} m\n * @param {function(V,K):boolean} f\n * @return {boolean}\n */\nexport const any = (m, f) => {\n for (const [key, value] of m) {\n if (f(value, key)) {\n return true\n }\n }\n return false\n}\n\n/**\n * Tests whether all key-value pairs pass the test implemented by `f(value, key)`.\n *\n * @function\n * @template K\n * @template V\n * @param {Map<K,V>} m\n * @param {function(V,K):boolean} f\n * @return {boolean}\n */\nexport const all = (m, f) => {\n for (const [key, value] of m) {\n if (!f(value, key)) {\n return false\n }\n }\n return true\n}\n","/**\n * Utility module to work with sets.\n *\n * @module set\n */\n\nexport const create = () => new Set()\n\n/**\n * @template T\n * @param {Set<T>} set\n * @return {Array<T>}\n */\nexport const toArray = set => Array.from(set)\n\n/**\n * @template T\n * @param {Set<T>} set\n * @return {T}\n */\nexport const first = set =>\n set.values().next().value || undefined\n\n/**\n * @template T\n * @param {Iterable<T>} entries\n * @return {Set<T>}\n */\nexport const from = entries => new Set(entries)\n","/**\n * Utility module to work with Arrays.\n *\n * @module array\n */\n\nimport * as set from './set.js'\n\n/**\n * Return the last element of an array. The element must exist\n *\n * @template L\n * @param {ArrayLike<L>} arr\n * @return {L}\n */\nexport const last = arr => arr[arr.length - 1]\n\n/**\n * @template C\n * @return {Array<C>}\n */\nexport const create = () => /** @type {Array<C>} */ ([])\n\n/**\n * @template D\n * @param {Array<D>} a\n * @return {Array<D>}\n */\nexport const copy = a => /** @type {Array<D>} */ (a.slice())\n\n/**\n * Append elements from src to dest\n *\n * @template M\n * @param {Array<M>} dest\n * @param {Array<M>} src\n */\nexport const appendTo = (dest, src) => {\n for (let i = 0; i < src.length; i++) {\n dest.push(src[i])\n }\n}\n\n/**\n * Transforms something array-like to an actual Array.\n *\n * @function\n * @template T\n * @param {ArrayLike<T>|Iterable<T>} arraylike\n * @return {T}\n */\nexport const from = Array.from\n\n/**\n * True iff condition holds on every element in the Array.\n *\n * @function\n * @template ITEM\n * @template {ArrayLike<ITEM>} ARR\n *\n * @param {ARR} arr\n * @param {function(ITEM, number, ARR):boolean} f\n * @return {boolean}\n */\nexport const every = (arr, f) => {\n for (let i = 0; i < arr.length; i++) {\n if (!f(arr[i], i, arr)) {\n return false\n }\n }\n return true\n}\n\n/**\n * True iff condition holds on some element in the Array.\n *\n * @function\n * @template S\n * @template {ArrayLike<S>} ARR\n * @param {ARR} arr\n * @param {function(S, number, ARR):boolean} f\n * @return {boolean}\n */\nexport const some = (arr, f) => {\n for (let i = 0; i < arr.length; i++) {\n if (f(arr[i], i, arr)) {\n return true\n }\n }\n return false\n}\n\n/**\n * @template ELEM\n *\n * @param {ArrayLike<ELEM>} a\n * @param {ArrayLike<ELEM>} b\n * @return {boolean}\n */\nexport const equalFlat = (a, b) => a.length === b.length && every(a, (item, index) => item === b[index])\n\n/**\n * @template ELEM\n * @param {Array<Array<ELEM>>} arr\n * @return {Array<ELEM>}\n */\nexport const flatten = arr => fold(arr, /** @type {Array<ELEM>} */ ([]), (acc, val) => acc.concat(val))\n\n/**\n * @template T\n * @param {number} len\n * @param {function(number, Array<T>):T} f\n * @return {Array<T>}\n */\nexport const unfold = (len, f) => {\n const array = new Array(len)\n for (let i = 0; i < len; i++) {\n array[i] = f(i, array)\n }\n return array\n}\n\n/**\n * @template T\n * @template RESULT\n * @param {Array<T>} arr\n * @param {RESULT} seed\n * @param {function(RESULT, T, number):RESULT} folder\n */\nexport const fold = (arr, seed, folder) => arr.reduce(folder, seed)\n\nexport const isArray = Array.isArray\n\n/**\n * @template T\n * @param {Array<T>} arr\n * @return {Array<T>}\n */\nexport const unique = arr => from(set.from(arr))\n\n/**\n * @template T\n * @template M\n * @param {ArrayLike<T>} arr\n * @param {function(T):M} mapper\n * @return {Array<T>}\n */\nexport const uniqueBy = (arr, mapper) => {\n /**\n * @type {Set<M>}\n */\n const happened = set.create()\n /**\n * @type {Array<T>}\n */\n const result = []\n for (let i = 0; i < arr.length; i++) {\n const el = arr[i]\n const mapped = mapper(el)\n if (!happened.has(mapped)) {\n happened.add(mapped)\n result.push(el)\n }\n }\n return result\n}\n","/**\n * Observable class prototype.\n *\n * @module observable\n */\n\nimport * as map from './map.js'\nimport * as set from './set.js'\nimport * as array from './array.js'\n\n/**\n * Handles named events.\n *\n * @template N\n */\nexport class Observable {\n constructor () {\n /**\n * Some desc.\n * @type {Map<N, any>}\n */\n this._observers = map.create()\n }\n\n /**\n * @param {N} name\n * @param {function} f\n */\n on (name, f) {\n map.setIfUndefined(this._observers, name, set.create).add(f)\n }\n\n /**\n * @param {N} name\n * @param {function} f\n */\n once (name, f) {\n /**\n * @param {...any} args\n */\n const _f = (...args) => {\n this.off(name, _f)\n f(...args)\n }\n this.on(name, _f)\n }\n\n /**\n * @param {N} name\n * @param {function} f\n */\n off (name, f) {\n const observers = this._observers.get(name)\n if (observers !== undefined) {\n observers.delete(f)\n if (observers.size === 0) {\n this._observers.delete(name)\n }\n }\n }\n\n /**\n * Emit a named event. All registered event listeners that listen to the\n * specified name will receive the event.\n *\n * @todo This should catch exceptions\n *\n * @param {N} name The event name.\n * @param {Array<any>} args The arguments that are applied to the event listener.\n */\n emit (name, args) {\n // copy all listeners to an array first to make sure that no event is emitted to listeners that are subscribed while the event handler is called.\n return array.from((this._observers.get(name) || map.create()).values()).forEach(f => f(...args))\n }\n\n destroy () {\n this._observers = map.create()\n }\n}\n","declare const __VERSION__: string;\ndeclare const TSUP_FORMAT: string;\n\nexport const PKG_NAME = \"@liveblocks/yjs\";\nexport const PKG_VERSION = typeof __VERSION__ === \"string\" && __VERSION__;\nexport const PKG_FORMAT = typeof TSUP_FORMAT === \"string\" && TSUP_FORMAT;\n"]}
1
+ {"version":3,"sources":["../src/index.ts","../../../node_modules/lib0/map.js","../../../node_modules/lib0/set.js","../../../node_modules/lib0/array.js","../../../node_modules/lib0/observable.js","../src/awareness.ts","../src/doc.ts","../src/version.ts"],"names":["detectDupes","create"],"mappings":";AAOA,SAAS,eAAe,eAAAA,oBAAmB;;;ACOpC,IAAM,SAAS,MAAM,oBAAI,IAAI;AAgC7B,IAAM,iBAAiB,CAAC,KAAK,KAAK,YAAY;AACnD,MAAI,MAAM,IAAI,IAAI,GAAG;AACrB,MAAI,QAAQ,QAAW;AACrB,QAAI,IAAI,KAAK,MAAM,QAAQ,CAAC;AAAA,EAC9B;AACA,SAAO;AACT;;;AC9CO,IAAMC,UAAS,MAAM,oBAAI,IAAI;;;AC6C7B,IAAM,OAAO,MAAM;AAgFnB,IAAM,UAAU,MAAM;;;ACpHtB,IAAM,aAAN,MAAiB;AAAA,EACtB,cAAe;AAKb,SAAK,aAAiB,OAAO;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,GAAI,MAAM,GAAG;AACX,IAAI,eAAe,KAAK,YAAY,MAAUA,OAAM,EAAE,IAAI,CAAC;AAAA,EAC7D;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,KAAM,MAAM,GAAG;AAIb,UAAM,KAAK,IAAI,SAAS;AACtB,WAAK,IAAI,MAAM,EAAE;AACjB,QAAE,GAAG,IAAI;AAAA,IACX;AACA,SAAK,GAAG,MAAM,EAAE;AAAA,EAClB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAK,MAAM,GAAG;AACZ,UAAM,YAAY,KAAK,WAAW,IAAI,IAAI;AAC1C,QAAI,cAAc,QAAW;AAC3B,gBAAU,OAAO,CAAC;AAClB,UAAI,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,EAGA,aAAmB;AAAA,EAEnB;AAAA,EAEA,UAAgB;AAAA,EAEhB;AACF","sourcesContent":["import type {\n BaseUserMeta,\n Json,\n JsonObject,\n LsonObject,\n Room,\n} from \"@liveblocks/client\";\nimport { ClientMsgCode, detectDupes } from \"@liveblocks/core\";\nimport { Observable } from \"lib0/observable\";\nimport type * as Y from \"yjs\";\n\nimport { Awareness } from \"./awareness\";\nimport yDocHandler from \"./doc\";\nimport { PKG_FORMAT, PKG_NAME, PKG_VERSION } from \"./version\";\n2;\n\ndetectDupes(PKG_NAME, PKG_VERSION, PKG_FORMAT);\n\ntype ProviderOptions = {\n autoloadSubdocs: boolean;\n};\n\nconst DefaultOptions: ProviderOptions = {\n autoloadSubdocs: false,\n};\n\nexport default class LiveblocksProvider<\n P extends JsonObject,\n S extends LsonObject,\n U extends BaseUserMeta,\n E extends Json,\n> extends Observable<unknown> {\n private room: Room<P, S, U, E>;\n private rootDoc: Y.Doc;\n private options: ProviderOptions;\n\n private unsubscribers: Array<() => void> = [];\n\n public awareness: Awareness;\n\n public rootDocHandler: yDocHandler;\n public subdocHandlers: Map<string, yDocHandler> = new Map();\n\n constructor(\n room: Room<P, S, U, E>,\n doc: Y.Doc,\n options: ProviderOptions | undefined = DefaultOptions\n ) {\n super();\n this.rootDoc = doc;\n this.room = room;\n this.options = options;\n this.rootDocHandler = new yDocHandler({\n doc,\n isRoot: true,\n updateDoc: this.updateDoc,\n fetchDoc: this.fetchDoc,\n });\n // if we have a connectionId already during construction, use that\n const connectionId = this.room.getSelf()?.connectionId;\n if (connectionId) {\n this.rootDoc.clientID = connectionId;\n }\n this.awareness = new Awareness(this.rootDoc, this.room);\n\n this.unsubscribers.push(\n this.room.events.status.subscribe((status) => {\n if (status === \"connected\") {\n this.rootDocHandler.syncDoc();\n } else {\n this.rootDocHandler.synced = false;\n }\n })\n );\n\n this.unsubscribers.push(\n this.room.events.ydoc.subscribe((message) => {\n const { type } = message;\n if (type === ClientMsgCode.UPDATE_YDOC) {\n // don't apply updates that came from the client\n return;\n }\n const { stateVector, update, guid } = message;\n // find the right doc and update\n if (guid !== undefined) {\n this.subdocHandlers\n .get(guid as string)\n ?.handleServerUpdate({ update, stateVector });\n } else {\n this.rootDocHandler.handleServerUpdate({ update, stateVector });\n }\n })\n );\n\n this.rootDocHandler.on(\"synced\", () => {\n const state = this.rootDocHandler.synced;\n for (const [_, handler] of this.subdocHandlers) {\n handler.syncDoc();\n }\n this.emit(\"synced\", [state]);\n this.emit(\"sync\", [state]);\n });\n this.rootDoc.on(\"subdocs\", this.handleSubdocs);\n this.syncDoc();\n }\n\n private handleSubdocs = ({\n loaded,\n removed,\n added,\n }: {\n loaded: Y.Doc[];\n removed: Y.Doc[];\n added: Y.Doc[];\n }) => {\n loaded.forEach(this.createSubdocHandler);\n if (this.options.autoloadSubdocs) {\n for (const subdoc of added) {\n if (!this.subdocHandlers.has(subdoc.guid)) {\n subdoc.load();\n }\n }\n }\n for (const subdoc of removed) {\n if (this.subdocHandlers.has(subdoc.guid)) {\n this.subdocHandlers.get(subdoc.guid)?.destroy();\n this.subdocHandlers.delete(subdoc.guid);\n }\n }\n };\n\n private updateDoc = (update: string, guid?: string) => {\n this.room.updateYDoc(update, guid);\n };\n\n private fetchDoc = (vector: string, guid?: string) => {\n this.room.fetchYDoc(vector, guid);\n };\n\n private createSubdocHandler = (subdoc: Y.Doc): void => {\n if (this.subdocHandlers.has(subdoc.guid)) {\n // if we already handle this subdoc, just fetch it again\n this.subdocHandlers.get(subdoc.guid)?.syncDoc();\n return;\n }\n const handler = new yDocHandler({\n doc: subdoc,\n isRoot: false,\n updateDoc: this.updateDoc,\n fetchDoc: this.fetchDoc,\n });\n this.subdocHandlers.set(subdoc.guid, handler);\n };\n\n // attempt to load a subdoc of a given guid\n public loadSubdoc = (guid: string): boolean => {\n for (const subdoc of this.rootDoc.subdocs) {\n if (subdoc.guid === guid) {\n subdoc.load();\n return true;\n }\n }\n // should we throw instead?\n return false;\n };\n\n private syncDoc = () => {\n /**\n * If the connection changes, set the new id, this is used by awareness.\n * yjs' only requirement for clientID is that it's truly unique and a number.\n * Liveblock's connectionID satisfies those constraints\n * */\n this.rootDoc.clientID =\n this.room.getSelf()?.connectionId || this.rootDoc.clientID;\n this.awareness.clientID = this.rootDoc.clientID; // tell our awareness provider the new ID\n\n this.rootDocHandler.syncDoc();\n for (const [_, handler] of this.subdocHandlers) {\n handler.syncDoc();\n }\n };\n\n // The sync'd property is required by some provider implementations\n get synced(): boolean {\n return this.rootDocHandler.synced;\n }\n\n destroy(): void {\n this.unsubscribers.forEach((unsub) => unsub());\n this.awareness.destroy();\n this.rootDocHandler.destroy();\n this._observers = new Map();\n for (const [_, handler] of this.subdocHandlers) {\n handler.destroy();\n }\n this.subdocHandlers.clear();\n super.destroy();\n }\n\n // Some provider implementations expect to be able to call connect/disconnect, implement as noop\n disconnect(): void {\n // This is a noop for liveblocks as connections are managed by the room\n }\n\n connect(): void {\n // This is a noop for liveblocks as connections are managed by the room\n }\n}\n","/**\n * Utility module to work with key-value stores.\n *\n * @module map\n */\n\n/**\n * Creates a new Map instance.\n *\n * @function\n * @return {Map<any, any>}\n *\n * @function\n */\nexport const create = () => new Map()\n\n/**\n * Copy a Map object into a fresh Map object.\n *\n * @function\n * @template X,Y\n * @param {Map<X,Y>} m\n * @return {Map<X,Y>}\n */\nexport const copy = m => {\n const r = create()\n m.forEach((v, k) => { r.set(k, v) })\n return r\n}\n\n/**\n * Get map property. Create T if property is undefined and set T on map.\n *\n * ```js\n * const listeners = map.setIfUndefined(events, 'eventName', set.create)\n * listeners.add(listener)\n * ```\n *\n * @function\n * @template V,K\n * @template {Map<K,V>} MAP\n * @param {MAP} map\n * @param {K} key\n * @param {function():V} createT\n * @return {V}\n */\nexport const setIfUndefined = (map, key, createT) => {\n let set = map.get(key)\n if (set === undefined) {\n map.set(key, set = createT())\n }\n return set\n}\n\n/**\n * Creates an Array and populates it with the content of all key-value pairs using the `f(value, key)` function.\n *\n * @function\n * @template K\n * @template V\n * @template R\n * @param {Map<K,V>} m\n * @param {function(V,K):R} f\n * @return {Array<R>}\n */\nexport const map = (m, f) => {\n const res = []\n for (const [key, value] of m) {\n res.push(f(value, key))\n }\n return res\n}\n\n/**\n * Tests whether any key-value pairs pass the test implemented by `f(value, key)`.\n *\n * @todo should rename to some - similarly to Array.some\n *\n * @function\n * @template K\n * @template V\n * @param {Map<K,V>} m\n * @param {function(V,K):boolean} f\n * @return {boolean}\n */\nexport const any = (m, f) => {\n for (const [key, value] of m) {\n if (f(value, key)) {\n return true\n }\n }\n return false\n}\n\n/**\n * Tests whether all key-value pairs pass the test implemented by `f(value, key)`.\n *\n * @function\n * @template K\n * @template V\n * @param {Map<K,V>} m\n * @param {function(V,K):boolean} f\n * @return {boolean}\n */\nexport const all = (m, f) => {\n for (const [key, value] of m) {\n if (!f(value, key)) {\n return false\n }\n }\n return true\n}\n","/**\n * Utility module to work with sets.\n *\n * @module set\n */\n\nexport const create = () => new Set()\n\n/**\n * @template T\n * @param {Set<T>} set\n * @return {Array<T>}\n */\nexport const toArray = set => Array.from(set)\n\n/**\n * @template T\n * @param {Set<T>} set\n * @return {T}\n */\nexport const first = set =>\n set.values().next().value || undefined\n\n/**\n * @template T\n * @param {Iterable<T>} entries\n * @return {Set<T>}\n */\nexport const from = entries => new Set(entries)\n","/**\n * Utility module to work with Arrays.\n *\n * @module array\n */\n\nimport * as set from './set.js'\n\n/**\n * Return the last element of an array. The element must exist\n *\n * @template L\n * @param {ArrayLike<L>} arr\n * @return {L}\n */\nexport const last = arr => arr[arr.length - 1]\n\n/**\n * @template C\n * @return {Array<C>}\n */\nexport const create = () => /** @type {Array<C>} */ ([])\n\n/**\n * @template D\n * @param {Array<D>} a\n * @return {Array<D>}\n */\nexport const copy = a => /** @type {Array<D>} */ (a.slice())\n\n/**\n * Append elements from src to dest\n *\n * @template M\n * @param {Array<M>} dest\n * @param {Array<M>} src\n */\nexport const appendTo = (dest, src) => {\n for (let i = 0; i < src.length; i++) {\n dest.push(src[i])\n }\n}\n\n/**\n * Transforms something array-like to an actual Array.\n *\n * @function\n * @template T\n * @param {ArrayLike<T>|Iterable<T>} arraylike\n * @return {T}\n */\nexport const from = Array.from\n\n/**\n * True iff condition holds on every element in the Array.\n *\n * @function\n * @template ITEM\n * @template {ArrayLike<ITEM>} ARR\n *\n * @param {ARR} arr\n * @param {function(ITEM, number, ARR):boolean} f\n * @return {boolean}\n */\nexport const every = (arr, f) => {\n for (let i = 0; i < arr.length; i++) {\n if (!f(arr[i], i, arr)) {\n return false\n }\n }\n return true\n}\n\n/**\n * True iff condition holds on some element in the Array.\n *\n * @function\n * @template S\n * @template {ArrayLike<S>} ARR\n * @param {ARR} arr\n * @param {function(S, number, ARR):boolean} f\n * @return {boolean}\n */\nexport const some = (arr, f) => {\n for (let i = 0; i < arr.length; i++) {\n if (f(arr[i], i, arr)) {\n return true\n }\n }\n return false\n}\n\n/**\n * @template ELEM\n *\n * @param {ArrayLike<ELEM>} a\n * @param {ArrayLike<ELEM>} b\n * @return {boolean}\n */\nexport const equalFlat = (a, b) => a.length === b.length && every(a, (item, index) => item === b[index])\n\n/**\n * @template ELEM\n * @param {Array<Array<ELEM>>} arr\n * @return {Array<ELEM>}\n */\nexport const flatten = arr => fold(arr, /** @type {Array<ELEM>} */ ([]), (acc, val) => acc.concat(val))\n\n/**\n * @template T\n * @param {number} len\n * @param {function(number, Array<T>):T} f\n * @return {Array<T>}\n */\nexport const unfold = (len, f) => {\n const array = new Array(len)\n for (let i = 0; i < len; i++) {\n array[i] = f(i, array)\n }\n return array\n}\n\n/**\n * @template T\n * @template RESULT\n * @param {Array<T>} arr\n * @param {RESULT} seed\n * @param {function(RESULT, T, number):RESULT} folder\n */\nexport const fold = (arr, seed, folder) => arr.reduce(folder, seed)\n\nexport const isArray = Array.isArray\n\n/**\n * @template T\n * @param {Array<T>} arr\n * @return {Array<T>}\n */\nexport const unique = arr => from(set.from(arr))\n\n/**\n * @template T\n * @template M\n * @param {ArrayLike<T>} arr\n * @param {function(T):M} mapper\n * @return {Array<T>}\n */\nexport const uniqueBy = (arr, mapper) => {\n /**\n * @type {Set<M>}\n */\n const happened = set.create()\n /**\n * @type {Array<T>}\n */\n const result = []\n for (let i = 0; i < arr.length; i++) {\n const el = arr[i]\n const mapped = mapper(el)\n if (!happened.has(mapped)) {\n happened.add(mapped)\n result.push(el)\n }\n }\n return result\n}\n","/**\n * Observable class prototype.\n *\n * @module observable\n */\n\nimport * as map from './map.js'\nimport * as set from './set.js'\nimport * as array from './array.js'\n\n/**\n * Handles named events.\n *\n * @template N\n */\nexport class Observable {\n constructor () {\n /**\n * Some desc.\n * @type {Map<N, any>}\n */\n this._observers = map.create()\n }\n\n /**\n * @param {N} name\n * @param {function} f\n */\n on (name, f) {\n map.setIfUndefined(this._observers, name, set.create).add(f)\n }\n\n /**\n * @param {N} name\n * @param {function} f\n */\n once (name, f) {\n /**\n * @param {...any} args\n */\n const _f = (...args) => {\n this.off(name, _f)\n f(...args)\n }\n this.on(name, _f)\n }\n\n /**\n * @param {N} name\n * @param {function} f\n */\n off (name, f) {\n const observers = this._observers.get(name)\n if (observers !== undefined) {\n observers.delete(f)\n if (observers.size === 0) {\n this._observers.delete(name)\n }\n }\n }\n\n /**\n * Emit a named event. All registered event listeners that listen to the\n * specified name will receive the event.\n *\n * @todo This should catch exceptions\n *\n * @param {N} name The event name.\n * @param {Array<any>} args The arguments that are applied to the event listener.\n */\n emit (name, args) {\n // copy all listeners to an array first to make sure that no event is emitted to listeners that are subscribed while the event handler is called.\n return array.from((this._observers.get(name) || map.create()).values()).forEach(f => f(...args))\n }\n\n destroy () {\n this._observers = map.create()\n }\n}\n","// TODO: apparently Yjs is full of anys or something, see if we can fix this\n/* eslint-disable @typescript-eslint/no-unsafe-call */\n/* eslint-disable @typescript-eslint/no-unsafe-member-access */\n/* eslint-disable @typescript-eslint/no-unsafe-assignment */\nimport type {\n BaseUserMeta,\n Json,\n JsonObject,\n LsonObject,\n Room,\n} from \"@liveblocks/client\";\nimport { Observable } from \"lib0/observable\";\nimport type * as Y from \"yjs\";\n\nconst Y_PRESENCE_KEY = \"__yjs\";\n\ntype MetaClientState = {\n clock: number;\n lastUpdated: number;\n};\n\n/**\n * This class will store Yjs awareness in Liveblock's presence under the __yjs key\n * IMPORTANT: The Yjs awareness protocol uses ydoc.clientId to reference users\n * to their respective documents. To avoid mapping Yjs clientIds to liveblock's connectionId,\n * we simply set the clientId of the doc to the connectionId. Then no further mapping is required\n */\nexport class Awareness extends Observable<unknown> {\n private room: Room<JsonObject, LsonObject, BaseUserMeta, Json>;\n public doc: Y.Doc;\n public clientID: number;\n public states: Map<number, unknown> = new Map();\n // Meta is used to keep track and timeout users who disconnect. Liveblocks provides this for us, so we don't need to\n // manage it here. Unfortunately, it's expected to exist by various integrations, so it's an empty map.\n public meta: Map<number, MetaClientState> = new Map();\n // _checkInterval this would hold a timer to remove users, but Liveblock's presence already handles this\n // unfortunately it's typed by various integrations\n public _checkInterval: number = 0;\n\n private othersUnsub: () => void;\n constructor(\n doc: Y.Doc,\n room: Room<JsonObject, LsonObject, BaseUserMeta, Json>\n ) {\n super();\n this.doc = doc;\n this.room = room;\n this.clientID = doc.clientID;\n this.othersUnsub = this.room.events.others.subscribe((event) => {\n // When others are changed, we emit an event that contains arrays added/updated/removed.\n if (event.type === \"leave\") {\n // REMOVED\n this.emit(\"change\", [\n { added: [], updated: [], removed: [event.user.connectionId] },\n \"local\",\n ]);\n }\n\n if (event.type === \"enter\") {\n // ADDED\n this.emit(\"change\", [\n { added: [event.user.connectionId], updated: [], removed: [] },\n \"local\",\n ]);\n }\n\n if (event.type === \"update\") {\n // UPDATED\n this.emit(\"change\", [\n { added: [], updated: [event.user.connectionId], removed: [] },\n \"local\",\n ]);\n }\n });\n }\n\n destroy(): void {\n this.emit(\"destroy\", [this]);\n this.othersUnsub();\n this.setLocalState(null);\n super.destroy();\n }\n\n getLocalState(): JsonObject | null {\n const presence = this.room.getPresence();\n if (\n Object.keys(presence).length === 0 ||\n typeof presence[Y_PRESENCE_KEY] === \"undefined\"\n ) {\n return null;\n }\n return presence[Y_PRESENCE_KEY] as JsonObject | null;\n }\n\n setLocalState(state: Partial<JsonObject> | null): void {\n const presence = this.room.getSelf()?.presence[Y_PRESENCE_KEY];\n this.room.updatePresence({\n __yjs: { ...((presence as JsonObject) || {}), ...(state || {}) },\n });\n }\n\n setLocalStateField(field: string, value: JsonObject | null): void {\n const presence = this.room.getSelf()?.presence[Y_PRESENCE_KEY];\n const update = { [field]: value } as Partial<JsonObject>;\n this.room.updatePresence({\n __yjs: { ...((presence as JsonObject) || {}), ...update },\n });\n }\n\n // Translate liveblocks presence to yjs awareness\n getStates(): Map<number, unknown> {\n const others = this.room.getOthers();\n const states = others.reduce((acc: Map<number, unknown>, currentValue) => {\n if (currentValue.connectionId) {\n // connectionId == actorId == yjs.clientId\n acc.set(\n currentValue.connectionId,\n currentValue.presence[Y_PRESENCE_KEY] || {}\n );\n }\n return acc;\n }, new Map());\n return states;\n }\n}\n","import { detectDupes } from \"@liveblocks/core\";\nimport { Base64 } from \"js-base64\";\nimport { Observable } from \"lib0/observable\";\nimport * as Y from \"yjs\";\n\nimport { PKG_FORMAT, PKG_NAME, PKG_VERSION } from \"./version\";\n\ndetectDupes(PKG_NAME, PKG_VERSION, PKG_FORMAT);\n\nexport default class yDocHandler extends Observable<unknown> {\n private unsubscribers: Array<() => void> = [];\n\n private _synced = false;\n private doc: Y.Doc;\n private updateRoomDoc: (update: string) => void;\n private fetchRoomDoc: (vector: string) => void;\n\n constructor({\n doc,\n isRoot,\n updateDoc,\n fetchDoc,\n }: {\n doc: Y.Doc;\n isRoot: boolean;\n updateDoc: (update: string, guid?: string) => void;\n fetchDoc: (vector: string, guid?: string) => void;\n }) {\n super();\n this.doc = doc;\n // this.doc.load(); // this just emits a load event, it doesn't actually load anything\n this.doc.on(\"update\", this.updateHandler);\n this.updateRoomDoc = (update: string) => {\n updateDoc(update, isRoot ? undefined : this.doc.guid);\n };\n this.fetchRoomDoc = (vector: string) => {\n fetchDoc(vector, isRoot ? undefined : this.doc.guid);\n };\n\n this.syncDoc();\n }\n\n public handleServerUpdate = ({\n update,\n stateVector,\n }: {\n update: string;\n stateVector: string | null;\n }): void => {\n // apply update from the server\n Y.applyUpdate(this.doc, Base64.toUint8Array(update), \"backend\");\n // if this update is the result of a fetch, the state vector is included\n if (stateVector) {\n // Use server state to calculate a diff and send it\n try {\n const localUpdate = Y.encodeStateAsUpdate(\n this.doc,\n Base64.toUint8Array(stateVector)\n );\n this.updateRoomDoc(Base64.fromUint8Array(localUpdate));\n } catch (e) {\n // something went wrong encoding local state to send to the server\n console.warn(e);\n }\n // now that we've sent our local and received from server, we're in sync\n // calling `syncDoc` again will sync up the documents\n this.synced = true;\n }\n };\n\n public syncDoc = (): void => {\n this.synced = false;\n\n // The state vector is sent to the server so it knows what to send back\n // if you don't send it, it returns everything\n const encodedVector = Base64.fromUint8Array(Y.encodeStateVector(this.doc));\n this.fetchRoomDoc(encodedVector);\n };\n\n // The sync'd property is required by some provider implementations\n get synced(): boolean {\n return this._synced;\n }\n\n set synced(state: boolean) {\n if (this._synced !== state) {\n this._synced = state;\n this.emit(\"synced\", [state]);\n this.emit(\"sync\", [state]);\n }\n }\n\n private updateHandler = (update: Uint8Array, origin: string) => {\n if (origin !== \"backend\") {\n const encodedUpdate = Base64.fromUint8Array(update);\n this.updateRoomDoc(encodedUpdate);\n }\n };\n\n destroy(): void {\n this.doc.off(\"update\", this.updateHandler);\n this.unsubscribers.forEach((unsub) => unsub());\n this._observers = new Map();\n this.doc.destroy();\n }\n}\n","declare const __VERSION__: string;\ndeclare const TSUP_FORMAT: string;\n\nexport const PKG_NAME = \"@liveblocks/yjs\";\nexport const PKG_VERSION = typeof __VERSION__ === \"string\" && __VERSION__;\nexport const PKG_FORMAT = typeof TSUP_FORMAT === \"string\" && TSUP_FORMAT;\n"]}
package/dist/index.mjs CHANGED
@@ -1,6 +1,5 @@
1
1
  // src/index.ts
2
- import { ClientMsgCode, detectDupes } from "@liveblocks/core";
3
- import { Base64 } from "js-base64";
2
+ import { ClientMsgCode, detectDupes as detectDupes2 } from "@liveblocks/core";
4
3
 
5
4
  // ../../node_modules/lib0/map.js
6
5
  var create = () => /* @__PURE__ */ new Map();
@@ -72,16 +71,7 @@ var Observable = class {
72
71
  }
73
72
  };
74
73
 
75
- // src/index.ts
76
- import * as Y from "yjs";
77
-
78
- // src/version.ts
79
- var PKG_NAME = "@liveblocks/yjs";
80
- var PKG_VERSION = "1.5.3-test1";
81
- var PKG_FORMAT = "esm";
82
-
83
- // src/index.ts
84
- detectDupes(PKG_NAME, PKG_VERSION, PKG_FORMAT);
74
+ // src/awareness.ts
85
75
  var Y_PRESENCE_KEY = "__yjs";
86
76
  var Awareness = class extends Observable {
87
77
  constructor(doc, room) {
@@ -158,80 +148,215 @@ var Awareness = class extends Observable {
158
148
  return states;
159
149
  }
160
150
  };
161
- var LiveblocksProvider = class extends Observable {
162
- constructor(room, doc) {
151
+
152
+ // src/doc.ts
153
+ import { detectDupes } from "@liveblocks/core";
154
+ import { Base64 } from "js-base64";
155
+ import * as Y from "yjs";
156
+
157
+ // src/version.ts
158
+ var PKG_NAME = "@liveblocks/yjs";
159
+ var PKG_VERSION = "1.6.0-subdocs1";
160
+ var PKG_FORMAT = "esm";
161
+
162
+ // src/doc.ts
163
+ detectDupes(PKG_NAME, PKG_VERSION, PKG_FORMAT);
164
+ var yDocHandler = class extends Observable {
165
+ constructor({
166
+ doc,
167
+ isRoot,
168
+ updateDoc,
169
+ fetchDoc
170
+ }) {
163
171
  super();
164
172
  this.unsubscribers = [];
165
173
  this._synced = false;
174
+ this.handleServerUpdate = ({
175
+ update,
176
+ stateVector
177
+ }) => {
178
+ Y.applyUpdate(this.doc, Base64.toUint8Array(update), "backend");
179
+ if (stateVector) {
180
+ try {
181
+ const localUpdate = Y.encodeStateAsUpdate(
182
+ this.doc,
183
+ Base64.toUint8Array(stateVector)
184
+ );
185
+ this.updateRoomDoc(Base64.fromUint8Array(localUpdate));
186
+ } catch (e) {
187
+ console.warn(e);
188
+ }
189
+ this.synced = true;
190
+ }
191
+ };
166
192
  this.syncDoc = () => {
167
193
  this.synced = false;
168
- this.doc.clientID = this.room.getSelf()?.connectionId || this.doc.clientID;
169
- this.awareness.clientID = this.doc.clientID;
170
194
  const encodedVector = Base64.fromUint8Array(Y.encodeStateVector(this.doc));
171
- this.room.fetchYDoc(encodedVector);
195
+ this.fetchRoomDoc(encodedVector);
172
196
  };
173
197
  this.updateHandler = (update, origin) => {
174
198
  if (origin !== "backend") {
175
199
  const encodedUpdate = Base64.fromUint8Array(update);
176
- this.room.updateYDoc(encodedUpdate);
200
+ this.updateRoomDoc(encodedUpdate);
177
201
  }
178
202
  };
179
203
  this.doc = doc;
204
+ this.doc.on("update", this.updateHandler);
205
+ this.updateRoomDoc = (update) => {
206
+ updateDoc(update, isRoot ? void 0 : this.doc.guid);
207
+ };
208
+ this.fetchRoomDoc = (vector) => {
209
+ fetchDoc(vector, isRoot ? void 0 : this.doc.guid);
210
+ };
211
+ this.syncDoc();
212
+ }
213
+ // The sync'd property is required by some provider implementations
214
+ get synced() {
215
+ return this._synced;
216
+ }
217
+ set synced(state) {
218
+ if (this._synced !== state) {
219
+ this._synced = state;
220
+ this.emit("synced", [state]);
221
+ this.emit("sync", [state]);
222
+ }
223
+ }
224
+ destroy() {
225
+ this.doc.off("update", this.updateHandler);
226
+ this.unsubscribers.forEach((unsub) => unsub());
227
+ this._observers = /* @__PURE__ */ new Map();
228
+ this.doc.destroy();
229
+ }
230
+ };
231
+
232
+ // src/index.ts
233
+ detectDupes2(PKG_NAME, PKG_VERSION, PKG_FORMAT);
234
+ var DefaultOptions = {
235
+ autoloadSubdocs: false
236
+ };
237
+ var LiveblocksProvider = class extends Observable {
238
+ constructor(room, doc, options = DefaultOptions) {
239
+ super();
240
+ this.unsubscribers = [];
241
+ this.subdocHandlers = /* @__PURE__ */ new Map();
242
+ this.handleSubdocs = ({
243
+ loaded,
244
+ removed,
245
+ added
246
+ }) => {
247
+ loaded.forEach(this.createSubdocHandler);
248
+ if (this.options.autoloadSubdocs) {
249
+ for (const subdoc of added) {
250
+ if (!this.subdocHandlers.has(subdoc.guid)) {
251
+ subdoc.load();
252
+ }
253
+ }
254
+ }
255
+ for (const subdoc of removed) {
256
+ if (this.subdocHandlers.has(subdoc.guid)) {
257
+ this.subdocHandlers.get(subdoc.guid)?.destroy();
258
+ this.subdocHandlers.delete(subdoc.guid);
259
+ }
260
+ }
261
+ };
262
+ this.updateDoc = (update, guid) => {
263
+ this.room.updateYDoc(update, guid);
264
+ };
265
+ this.fetchDoc = (vector, guid) => {
266
+ this.room.fetchYDoc(vector, guid);
267
+ };
268
+ this.createSubdocHandler = (subdoc) => {
269
+ if (this.subdocHandlers.has(subdoc.guid)) {
270
+ this.subdocHandlers.get(subdoc.guid)?.syncDoc();
271
+ return;
272
+ }
273
+ const handler = new yDocHandler({
274
+ doc: subdoc,
275
+ isRoot: false,
276
+ updateDoc: this.updateDoc,
277
+ fetchDoc: this.fetchDoc
278
+ });
279
+ this.subdocHandlers.set(subdoc.guid, handler);
280
+ };
281
+ // attempt to load a subdoc of a given guid
282
+ this.loadSubdoc = (guid) => {
283
+ for (const subdoc of this.rootDoc.subdocs) {
284
+ if (subdoc.guid === guid) {
285
+ subdoc.load();
286
+ return true;
287
+ }
288
+ }
289
+ return false;
290
+ };
291
+ this.syncDoc = () => {
292
+ this.rootDoc.clientID = this.room.getSelf()?.connectionId || this.rootDoc.clientID;
293
+ this.awareness.clientID = this.rootDoc.clientID;
294
+ this.rootDocHandler.syncDoc();
295
+ for (const [_, handler] of this.subdocHandlers) {
296
+ handler.syncDoc();
297
+ }
298
+ };
299
+ this.rootDoc = doc;
180
300
  this.room = room;
301
+ this.options = options;
302
+ this.rootDocHandler = new yDocHandler({
303
+ doc,
304
+ isRoot: true,
305
+ updateDoc: this.updateDoc,
306
+ fetchDoc: this.fetchDoc
307
+ });
181
308
  const connectionId = this.room.getSelf()?.connectionId;
182
309
  if (connectionId) {
183
- this.doc.clientID = connectionId;
310
+ this.rootDoc.clientID = connectionId;
184
311
  }
185
- this.awareness = new Awareness(this.doc, this.room);
186
- this.doc.on("update", this.updateHandler);
312
+ this.awareness = new Awareness(this.rootDoc, this.room);
187
313
  this.unsubscribers.push(
188
314
  this.room.events.status.subscribe((status) => {
189
315
  if (status === "connected") {
190
- this.syncDoc();
316
+ this.rootDocHandler.syncDoc();
191
317
  } else {
192
- this.synced = false;
318
+ this.rootDocHandler.synced = false;
193
319
  }
194
320
  })
195
321
  );
196
322
  this.unsubscribers.push(
197
323
  this.room.events.ydoc.subscribe((message) => {
198
- const { type, update } = message;
324
+ const { type } = message;
199
325
  if (type === ClientMsgCode.UPDATE_YDOC) {
200
326
  return;
201
327
  }
202
- Y.applyUpdate(this.doc, Base64.toUint8Array(update), "backend");
203
- const { stateVector } = message;
204
- if (stateVector) {
205
- try {
206
- const localUpdate = Y.encodeStateAsUpdate(
207
- this.doc,
208
- Base64.toUint8Array(stateVector)
209
- );
210
- this.room.updateYDoc(Base64.fromUint8Array(localUpdate));
211
- } catch (e) {
212
- console.warn(e);
213
- }
214
- this.synced = true;
328
+ const { stateVector, update, guid } = message;
329
+ if (guid !== void 0) {
330
+ this.subdocHandlers.get(guid)?.handleServerUpdate({ update, stateVector });
331
+ } else {
332
+ this.rootDocHandler.handleServerUpdate({ update, stateVector });
215
333
  }
216
334
  })
217
335
  );
336
+ this.rootDocHandler.on("synced", () => {
337
+ const state = this.rootDocHandler.synced;
338
+ for (const [_, handler] of this.subdocHandlers) {
339
+ handler.syncDoc();
340
+ }
341
+ this.emit("synced", [state]);
342
+ this.emit("sync", [state]);
343
+ });
344
+ this.rootDoc.on("subdocs", this.handleSubdocs);
218
345
  this.syncDoc();
219
346
  }
220
347
  // The sync'd property is required by some provider implementations
221
348
  get synced() {
222
- return this._synced;
223
- }
224
- set synced(state) {
225
- if (this._synced !== state) {
226
- this._synced = state;
227
- this.emit("synced", [state]);
228
- this.emit("sync", [state]);
229
- }
349
+ return this.rootDocHandler.synced;
230
350
  }
231
351
  destroy() {
232
- this.doc.off("update", this.updateHandler);
233
352
  this.unsubscribers.forEach((unsub) => unsub());
234
353
  this.awareness.destroy();
354
+ this.rootDocHandler.destroy();
355
+ this._observers = /* @__PURE__ */ new Map();
356
+ for (const [_, handler] of this.subdocHandlers) {
357
+ handler.destroy();
358
+ }
359
+ this.subdocHandlers.clear();
235
360
  super.destroy();
236
361
  }
237
362
  // Some provider implementations expect to be able to call connect/disconnect, implement as noop
@@ -241,7 +366,6 @@ var LiveblocksProvider = class extends Observable {
241
366
  }
242
367
  };
243
368
  export {
244
- Awareness,
245
369
  LiveblocksProvider as default
246
370
  };
247
371
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../../../node_modules/lib0/map.js","../../../node_modules/lib0/set.js","../../../node_modules/lib0/array.js","../../../node_modules/lib0/observable.js","../src/version.ts"],"sourcesContent":["import type {\n BaseUserMeta,\n Json,\n JsonObject,\n LsonObject,\n Room,\n} from \"@liveblocks/client\";\nimport { ClientMsgCode, detectDupes } from \"@liveblocks/core\";\nimport { Base64 } from \"js-base64\";\nimport { Observable } from \"lib0/observable\";\nimport * as Y from \"yjs\";\n\nimport { PKG_FORMAT, PKG_NAME, PKG_VERSION } from \"./version\";\n2;\n\ndetectDupes(PKG_NAME, PKG_VERSION, PKG_FORMAT);\n\nconst Y_PRESENCE_KEY = \"__yjs\";\n\ntype MetaClientState = {\n clock: number;\n lastUpdated: number;\n};\n\n/**\n * This class will store Yjs awareness in Liveblock's presence under the __yjs key\n * IMPORTANT: The Yjs awareness protocol uses ydoc.clientId to reference users\n * to their respective documents. To avoid mapping Yjs clientIds to liveblock's connectionId,\n * we simply set the clientId of the doc to the connectionId. Then no further mapping is required\n */\nexport class Awareness extends Observable<unknown> {\n private room: Room<JsonObject, LsonObject, BaseUserMeta, Json>;\n public doc: Y.Doc;\n public clientID: number;\n public states: Map<number, unknown> = new Map();\n // Meta is used to keep track and timeout users who disconnect. Liveblocks provides this for us, so we don't need to\n // manage it here. Unfortunately, it's expected to exist by various integrations, so it's an empty map.\n public meta: Map<number, MetaClientState> = new Map();\n // _checkInterval this would hold a timer to remove users, but Liveblock's presence already handles this\n // unfortunately it's typed by various integrations\n public _checkInterval: number = 0;\n\n private othersUnsub: () => void;\n constructor(\n doc: Y.Doc,\n room: Room<JsonObject, LsonObject, BaseUserMeta, Json>\n ) {\n super();\n this.doc = doc;\n this.room = room;\n this.clientID = doc.clientID;\n this.othersUnsub = this.room.events.others.subscribe((event) => {\n // When others are changed, we emit an event that contains arrays added/updated/removed.\n if (event.type === \"leave\") {\n // REMOVED\n this.emit(\"change\", [\n { added: [], updated: [], removed: [event.user.connectionId] },\n \"local\",\n ]);\n }\n\n if (event.type === \"enter\") {\n // ADDED\n this.emit(\"change\", [\n { added: [event.user.connectionId], updated: [], removed: [] },\n \"local\",\n ]);\n }\n\n if (event.type === \"update\") {\n // UPDATED\n this.emit(\"change\", [\n { added: [], updated: [event.user.connectionId], removed: [] },\n \"local\",\n ]);\n }\n });\n }\n\n destroy(): void {\n this.emit(\"destroy\", [this]);\n this.othersUnsub();\n this.setLocalState(null);\n super.destroy();\n }\n\n getLocalState(): JsonObject | null {\n const presence = this.room.getPresence();\n if (\n Object.keys(presence).length === 0 ||\n typeof presence[Y_PRESENCE_KEY] === \"undefined\"\n ) {\n return null;\n }\n return presence[Y_PRESENCE_KEY] as JsonObject | null;\n }\n\n setLocalState(state: Partial<JsonObject> | null): void {\n const presence = this.room.getSelf()?.presence[Y_PRESENCE_KEY];\n this.room.updatePresence({\n __yjs: { ...((presence as JsonObject) || {}), ...(state || {}) },\n });\n }\n\n setLocalStateField(field: string, value: JsonObject | null): void {\n const presence = this.room.getSelf()?.presence[Y_PRESENCE_KEY];\n const update = { [field]: value } as Partial<JsonObject>;\n this.room.updatePresence({\n __yjs: { ...((presence as JsonObject) || {}), ...update },\n });\n }\n\n // Translate liveblocks presence to yjs awareness\n getStates(): Map<number, unknown> {\n const others = this.room.getOthers();\n const states = others.reduce((acc: Map<number, unknown>, currentValue) => {\n if (currentValue.connectionId) {\n // connectionId == actorId == yjs.clientId\n acc.set(\n currentValue.connectionId,\n currentValue.presence[Y_PRESENCE_KEY] || {}\n );\n }\n return acc;\n }, new Map());\n return states;\n }\n}\n\nexport default class LiveblocksProvider<\n P extends JsonObject,\n S extends LsonObject,\n U extends BaseUserMeta,\n E extends Json,\n> extends Observable<unknown> {\n private room: Room<P, S, U, E>;\n private doc: Y.Doc;\n\n private unsubscribers: Array<() => void> = [];\n\n public awareness: Awareness;\n\n private _synced = false;\n\n constructor(room: Room<P, S, U, E>, doc: Y.Doc) {\n super();\n this.doc = doc;\n this.room = room;\n\n // if we have a connectionId already during construction, use that\n const connectionId = this.room.getSelf()?.connectionId;\n if (connectionId) {\n this.doc.clientID = connectionId;\n }\n this.awareness = new Awareness(this.doc, this.room);\n this.doc.on(\"update\", this.updateHandler);\n\n this.unsubscribers.push(\n this.room.events.status.subscribe((status) => {\n if (status === \"connected\") {\n this.syncDoc();\n } else {\n this.synced = false;\n }\n })\n );\n\n this.unsubscribers.push(\n this.room.events.ydoc.subscribe((message) => {\n const { type, update } = message;\n if (type === ClientMsgCode.UPDATE_YDOC) {\n // don't apply updates that came from the client\n return;\n }\n // apply update from the server\n Y.applyUpdate(this.doc, Base64.toUint8Array(update), \"backend\");\n const { stateVector } = message;\n // if this update is the result of a fetch, the state vector is included\n if (stateVector) {\n // Use server state to calculate a diff and send it\n try {\n const localUpdate = Y.encodeStateAsUpdate(\n this.doc,\n Base64.toUint8Array(stateVector)\n );\n this.room.updateYDoc(Base64.fromUint8Array(localUpdate));\n } catch (e) {\n // something went wrong encoding local state to send to the server\n console.warn(e);\n }\n // now that we've sent our local and received from server, we're in sync\n // calling `syncDoc` again will sync up the documents\n this.synced = true;\n }\n })\n );\n this.syncDoc();\n }\n\n private syncDoc = () => {\n this.synced = false;\n /**\n * If the connection changes, set the new id, this is used by awareness.\n * yjs' only requirement for clientID is that it's truly unique and a number.\n * Liveblock's connectionID satisfies those constraints\n * */\n this.doc.clientID = this.room.getSelf()?.connectionId || this.doc.clientID;\n this.awareness.clientID = this.doc.clientID; // tell our awareness provider the new ID\n\n // The state vector is sent to the server so it knows what to send back\n // if you don't send it, it returns everything\n const encodedVector = Base64.fromUint8Array(Y.encodeStateVector(this.doc));\n this.room.fetchYDoc(encodedVector);\n };\n\n // The sync'd property is required by some provider implementations\n get synced(): boolean {\n return this._synced;\n }\n\n set synced(state: boolean) {\n if (this._synced !== state) {\n this._synced = state;\n this.emit(\"synced\", [state]);\n this.emit(\"sync\", [state]);\n }\n }\n\n private updateHandler = (update: Uint8Array, origin: string) => {\n if (origin !== \"backend\") {\n const encodedUpdate = Base64.fromUint8Array(update);\n this.room.updateYDoc(encodedUpdate);\n }\n };\n\n destroy(): void {\n this.doc.off(\"update\", this.updateHandler);\n this.unsubscribers.forEach((unsub) => unsub());\n this.awareness.destroy();\n super.destroy();\n }\n\n // Some provider implementations expect to be able to call connect/disconnect, implement as noop\n disconnect(): void {\n // This is a noop for liveblocks as connections are managed by the room\n }\n\n connect(): void {\n // This is a noop for liveblocks as connections are managed by the room\n }\n}\n","/**\n * Utility module to work with key-value stores.\n *\n * @module map\n */\n\n/**\n * Creates a new Map instance.\n *\n * @function\n * @return {Map<any, any>}\n *\n * @function\n */\nexport const create = () => new Map()\n\n/**\n * Copy a Map object into a fresh Map object.\n *\n * @function\n * @template X,Y\n * @param {Map<X,Y>} m\n * @return {Map<X,Y>}\n */\nexport const copy = m => {\n const r = create()\n m.forEach((v, k) => { r.set(k, v) })\n return r\n}\n\n/**\n * Get map property. Create T if property is undefined and set T on map.\n *\n * ```js\n * const listeners = map.setIfUndefined(events, 'eventName', set.create)\n * listeners.add(listener)\n * ```\n *\n * @function\n * @template V,K\n * @template {Map<K,V>} MAP\n * @param {MAP} map\n * @param {K} key\n * @param {function():V} createT\n * @return {V}\n */\nexport const setIfUndefined = (map, key, createT) => {\n let set = map.get(key)\n if (set === undefined) {\n map.set(key, set = createT())\n }\n return set\n}\n\n/**\n * Creates an Array and populates it with the content of all key-value pairs using the `f(value, key)` function.\n *\n * @function\n * @template K\n * @template V\n * @template R\n * @param {Map<K,V>} m\n * @param {function(V,K):R} f\n * @return {Array<R>}\n */\nexport const map = (m, f) => {\n const res = []\n for (const [key, value] of m) {\n res.push(f(value, key))\n }\n return res\n}\n\n/**\n * Tests whether any key-value pairs pass the test implemented by `f(value, key)`.\n *\n * @todo should rename to some - similarly to Array.some\n *\n * @function\n * @template K\n * @template V\n * @param {Map<K,V>} m\n * @param {function(V,K):boolean} f\n * @return {boolean}\n */\nexport const any = (m, f) => {\n for (const [key, value] of m) {\n if (f(value, key)) {\n return true\n }\n }\n return false\n}\n\n/**\n * Tests whether all key-value pairs pass the test implemented by `f(value, key)`.\n *\n * @function\n * @template K\n * @template V\n * @param {Map<K,V>} m\n * @param {function(V,K):boolean} f\n * @return {boolean}\n */\nexport const all = (m, f) => {\n for (const [key, value] of m) {\n if (!f(value, key)) {\n return false\n }\n }\n return true\n}\n","/**\n * Utility module to work with sets.\n *\n * @module set\n */\n\nexport const create = () => new Set()\n\n/**\n * @template T\n * @param {Set<T>} set\n * @return {Array<T>}\n */\nexport const toArray = set => Array.from(set)\n\n/**\n * @template T\n * @param {Set<T>} set\n * @return {T}\n */\nexport const first = set =>\n set.values().next().value || undefined\n\n/**\n * @template T\n * @param {Iterable<T>} entries\n * @return {Set<T>}\n */\nexport const from = entries => new Set(entries)\n","/**\n * Utility module to work with Arrays.\n *\n * @module array\n */\n\nimport * as set from './set.js'\n\n/**\n * Return the last element of an array. The element must exist\n *\n * @template L\n * @param {ArrayLike<L>} arr\n * @return {L}\n */\nexport const last = arr => arr[arr.length - 1]\n\n/**\n * @template C\n * @return {Array<C>}\n */\nexport const create = () => /** @type {Array<C>} */ ([])\n\n/**\n * @template D\n * @param {Array<D>} a\n * @return {Array<D>}\n */\nexport const copy = a => /** @type {Array<D>} */ (a.slice())\n\n/**\n * Append elements from src to dest\n *\n * @template M\n * @param {Array<M>} dest\n * @param {Array<M>} src\n */\nexport const appendTo = (dest, src) => {\n for (let i = 0; i < src.length; i++) {\n dest.push(src[i])\n }\n}\n\n/**\n * Transforms something array-like to an actual Array.\n *\n * @function\n * @template T\n * @param {ArrayLike<T>|Iterable<T>} arraylike\n * @return {T}\n */\nexport const from = Array.from\n\n/**\n * True iff condition holds on every element in the Array.\n *\n * @function\n * @template ITEM\n * @template {ArrayLike<ITEM>} ARR\n *\n * @param {ARR} arr\n * @param {function(ITEM, number, ARR):boolean} f\n * @return {boolean}\n */\nexport const every = (arr, f) => {\n for (let i = 0; i < arr.length; i++) {\n if (!f(arr[i], i, arr)) {\n return false\n }\n }\n return true\n}\n\n/**\n * True iff condition holds on some element in the Array.\n *\n * @function\n * @template S\n * @template {ArrayLike<S>} ARR\n * @param {ARR} arr\n * @param {function(S, number, ARR):boolean} f\n * @return {boolean}\n */\nexport const some = (arr, f) => {\n for (let i = 0; i < arr.length; i++) {\n if (f(arr[i], i, arr)) {\n return true\n }\n }\n return false\n}\n\n/**\n * @template ELEM\n *\n * @param {ArrayLike<ELEM>} a\n * @param {ArrayLike<ELEM>} b\n * @return {boolean}\n */\nexport const equalFlat = (a, b) => a.length === b.length && every(a, (item, index) => item === b[index])\n\n/**\n * @template ELEM\n * @param {Array<Array<ELEM>>} arr\n * @return {Array<ELEM>}\n */\nexport const flatten = arr => fold(arr, /** @type {Array<ELEM>} */ ([]), (acc, val) => acc.concat(val))\n\n/**\n * @template T\n * @param {number} len\n * @param {function(number, Array<T>):T} f\n * @return {Array<T>}\n */\nexport const unfold = (len, f) => {\n const array = new Array(len)\n for (let i = 0; i < len; i++) {\n array[i] = f(i, array)\n }\n return array\n}\n\n/**\n * @template T\n * @template RESULT\n * @param {Array<T>} arr\n * @param {RESULT} seed\n * @param {function(RESULT, T, number):RESULT} folder\n */\nexport const fold = (arr, seed, folder) => arr.reduce(folder, seed)\n\nexport const isArray = Array.isArray\n\n/**\n * @template T\n * @param {Array<T>} arr\n * @return {Array<T>}\n */\nexport const unique = arr => from(set.from(arr))\n\n/**\n * @template T\n * @template M\n * @param {ArrayLike<T>} arr\n * @param {function(T):M} mapper\n * @return {Array<T>}\n */\nexport const uniqueBy = (arr, mapper) => {\n /**\n * @type {Set<M>}\n */\n const happened = set.create()\n /**\n * @type {Array<T>}\n */\n const result = []\n for (let i = 0; i < arr.length; i++) {\n const el = arr[i]\n const mapped = mapper(el)\n if (!happened.has(mapped)) {\n happened.add(mapped)\n result.push(el)\n }\n }\n return result\n}\n","/**\n * Observable class prototype.\n *\n * @module observable\n */\n\nimport * as map from './map.js'\nimport * as set from './set.js'\nimport * as array from './array.js'\n\n/**\n * Handles named events.\n *\n * @template N\n */\nexport class Observable {\n constructor () {\n /**\n * Some desc.\n * @type {Map<N, any>}\n */\n this._observers = map.create()\n }\n\n /**\n * @param {N} name\n * @param {function} f\n */\n on (name, f) {\n map.setIfUndefined(this._observers, name, set.create).add(f)\n }\n\n /**\n * @param {N} name\n * @param {function} f\n */\n once (name, f) {\n /**\n * @param {...any} args\n */\n const _f = (...args) => {\n this.off(name, _f)\n f(...args)\n }\n this.on(name, _f)\n }\n\n /**\n * @param {N} name\n * @param {function} f\n */\n off (name, f) {\n const observers = this._observers.get(name)\n if (observers !== undefined) {\n observers.delete(f)\n if (observers.size === 0) {\n this._observers.delete(name)\n }\n }\n }\n\n /**\n * Emit a named event. All registered event listeners that listen to the\n * specified name will receive the event.\n *\n * @todo This should catch exceptions\n *\n * @param {N} name The event name.\n * @param {Array<any>} args The arguments that are applied to the event listener.\n */\n emit (name, args) {\n // copy all listeners to an array first to make sure that no event is emitted to listeners that are subscribed while the event handler is called.\n return array.from((this._observers.get(name) || map.create()).values()).forEach(f => f(...args))\n }\n\n destroy () {\n this._observers = map.create()\n }\n}\n","declare const __VERSION__: string;\ndeclare const TSUP_FORMAT: string;\n\nexport const PKG_NAME = \"@liveblocks/yjs\";\nexport const PKG_VERSION = typeof __VERSION__ === \"string\" && __VERSION__;\nexport const PKG_FORMAT = typeof TSUP_FORMAT === \"string\" && TSUP_FORMAT;\n"],"mappings":";AAOA,SAAS,eAAe,mBAAmB;AAC3C,SAAS,cAAc;;;ACMhB,IAAM,SAAS,MAAM,oBAAI,IAAI;AAgC7B,IAAM,iBAAiB,CAAC,KAAK,KAAK,YAAY;AACnD,MAAI,MAAM,IAAI,IAAI,GAAG;AACrB,MAAI,QAAQ,QAAW;AACrB,QAAI,IAAI,KAAK,MAAM,QAAQ,CAAC;AAAA,EAC9B;AACA,SAAO;AACT;;;AC9CO,IAAMA,UAAS,MAAM,oBAAI,IAAI;;;AC6C7B,IAAM,OAAO,MAAM;AAgFnB,IAAM,UAAU,MAAM;;;ACpHtB,IAAM,aAAN,MAAiB;AAAA,EACtB,cAAe;AAKb,SAAK,aAAiB,OAAO;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,GAAI,MAAM,GAAG;AACX,IAAI,eAAe,KAAK,YAAY,MAAUC,OAAM,EAAE,IAAI,CAAC;AAAA,EAC7D;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,KAAM,MAAM,GAAG;AAIb,UAAM,KAAK,IAAI,SAAS;AACtB,WAAK,IAAI,MAAM,EAAE;AACjB,QAAE,GAAG,IAAI;AAAA,IACX;AACA,SAAK,GAAG,MAAM,EAAE;AAAA,EAClB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAK,MAAM,GAAG;AACZ,UAAM,YAAY,KAAK,WAAW,IAAI,IAAI;AAC1C,QAAI,cAAc,QAAW;AAC3B,gBAAU,OAAO,CAAC;AAClB,UAAI,UAAU,SAAS,GAAG;AACxB,aAAK,WAAW,OAAO,IAAI;AAAA,MAC7B;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,KAAM,MAAM,M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,SAAS,GAAP;AAEA,oBAAQ,KAAK,CAAC;AAAA,UAChB;AAGA,eAAK,SAAS;AAAA,QAChB;AAAA,MACF,CAAC;AAAA,IACH;AACA,SAAK,QAAQ;AAAA,EACf;AAAA;AAAA,EAmBA,IAAI,SAAkB;AACpB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAI,OAAO,OAAgB;AACzB,QAAI,KAAK,YAAY,OAAO;AAC1B,WAAK,UAAU;AACf,WAAK,KAAK,UAAU,CAAC,KAAK,CAAC;AAC3B,WAAK,KAAK,QAAQ,CAAC,KAAK,CAAC;AAAA,IAC3B;AAAA,EACF;AAAA,EASA,UAAgB;AACd,SAAK,IAAI,IAAI,UAAU,KAAK,aAAa;AACzC,SAAK,cAAc,QAAQ,CAAC,UAAU,MAAM,CAAC;AAC7C,SAAK,UAAU,QAAQ;AACvB,UAAM,QAAQ;AAAA,EAChB;AAAA;AAAA,EAGA,aAAmB;AAAA,EAEnB;AAAA,EAEA,UAAgB;AAAA,EAEhB;AACF;","names":["create","create"]}
1
+ {"version":3,"sources":["../src/index.ts","../../../node_modules/lib0/map.js","../../../node_modules/lib0/set.js","../../../node_modules/lib0/array.js","../../../node_modules/lib0/observable.js","../src/awareness.ts","../src/doc.ts","../src/version.ts"],"sourcesContent":["import type {\n BaseUserMeta,\n Json,\n JsonObject,\n LsonObject,\n Room,\n} from \"@liveblocks/client\";\nimport { ClientMsgCode, detectDupes } from \"@liveblocks/core\";\nimport { Observable } from \"lib0/observable\";\nimport type * as Y from \"yjs\";\n\nimport { Awareness } from \"./awareness\";\nimport yDocHandler from \"./doc\";\nimport { PKG_FORMAT, PKG_NAME, PKG_VERSION } from \"./version\";\n2;\n\ndetectDupes(PKG_NAME, PKG_VERSION, PKG_FORMAT);\n\ntype ProviderOptions = {\n autoloadSubdocs: boolean;\n};\n\nconst DefaultOptions: ProviderOptions = {\n autoloadSubdocs: false,\n};\n\nexport default class LiveblocksProvider<\n P extends JsonObject,\n S extends LsonObject,\n U extends BaseUserMeta,\n E extends Json,\n> extends Observable<unknown> {\n private room: Room<P, S, U, E>;\n private rootDoc: Y.Doc;\n private options: ProviderOptions;\n\n private unsubscribers: Array<() => void> = [];\n\n public awareness: Awareness;\n\n public rootDocHandler: yDocHandler;\n public subdocHandlers: Map<string, yDocHandler> = new Map();\n\n constructor(\n room: Room<P, S, U, E>,\n doc: Y.Doc,\n options: ProviderOptions | undefined = DefaultOptions\n ) {\n super();\n this.rootDoc = doc;\n this.room = room;\n this.options = options;\n this.rootDocHandler = new yDocHandler({\n doc,\n isRoot: true,\n updateDoc: this.updateDoc,\n fetchDoc: this.fetchDoc,\n });\n // if we have a connectionId already during construction, use that\n const connectionId = this.room.getSelf()?.connectionId;\n if (connectionId) {\n this.rootDoc.clientID = connectionId;\n }\n this.awareness = new Awareness(this.rootDoc, this.room);\n\n this.unsubscribers.push(\n this.room.events.status.subscribe((status) => {\n if (status === \"connected\") {\n this.rootDocHandler.syncDoc();\n } else {\n this.rootDocHandler.synced = false;\n }\n })\n );\n\n this.unsubscribers.push(\n this.room.events.ydoc.subscribe((message) => {\n const { type } = message;\n if (type === ClientMsgCode.UPDATE_YDOC) {\n // don't apply updates that came from the client\n return;\n }\n const { stateVector, update, guid } = message;\n // find the right doc and update\n if (guid !== undefined) {\n this.subdocHandlers\n .get(guid as string)\n ?.handleServerUpdate({ update, stateVector });\n } else {\n this.rootDocHandler.handleServerUpdate({ update, stateVector });\n }\n })\n );\n\n this.rootDocHandler.on(\"synced\", () => {\n const state = this.rootDocHandler.synced;\n for (const [_, handler] of this.subdocHandlers) {\n handler.syncDoc();\n }\n this.emit(\"synced\", [state]);\n this.emit(\"sync\", [state]);\n });\n this.rootDoc.on(\"subdocs\", this.handleSubdocs);\n this.syncDoc();\n }\n\n private handleSubdocs = ({\n loaded,\n removed,\n added,\n }: {\n loaded: Y.Doc[];\n removed: Y.Doc[];\n added: Y.Doc[];\n }) => {\n loaded.forEach(this.createSubdocHandler);\n if (this.options.autoloadSubdocs) {\n for (const subdoc of added) {\n if (!this.subdocHandlers.has(subdoc.guid)) {\n subdoc.load();\n }\n }\n }\n for (const subdoc of removed) {\n if (this.subdocHandlers.has(subdoc.guid)) {\n this.subdocHandlers.get(subdoc.guid)?.destroy();\n this.subdocHandlers.delete(subdoc.guid);\n }\n }\n };\n\n private updateDoc = (update: string, guid?: string) => {\n this.room.updateYDoc(update, guid);\n };\n\n private fetchDoc = (vector: string, guid?: string) => {\n this.room.fetchYDoc(vector, guid);\n };\n\n private createSubdocHandler = (subdoc: Y.Doc): void => {\n if (this.subdocHandlers.has(subdoc.guid)) {\n // if we already handle this subdoc, just fetch it again\n this.subdocHandlers.get(subdoc.guid)?.syncDoc();\n return;\n }\n const handler = new yDocHandler({\n doc: subdoc,\n isRoot: false,\n updateDoc: this.updateDoc,\n fetchDoc: this.fetchDoc,\n });\n this.subdocHandlers.set(subdoc.guid, handler);\n };\n\n // attempt to load a subdoc of a given guid\n public loadSubdoc = (guid: string): boolean => {\n for (const subdoc of this.rootDoc.subdocs) {\n if (subdoc.guid === guid) {\n subdoc.load();\n return true;\n }\n }\n // should we throw instead?\n return false;\n };\n\n private syncDoc = () => {\n /**\n * If the connection changes, set the new id, this is used by awareness.\n * yjs' only requirement for clientID is that it's truly unique and a number.\n * Liveblock's connectionID satisfies those constraints\n * */\n this.rootDoc.clientID =\n this.room.getSelf()?.connectionId || this.rootDoc.clientID;\n this.awareness.clientID = this.rootDoc.clientID; // tell our awareness provider the new ID\n\n this.rootDocHandler.syncDoc();\n for (const [_, handler] of this.subdocHandlers) {\n handler.syncDoc();\n }\n };\n\n // The sync'd property is required by some provider implementations\n get synced(): boolean {\n return this.rootDocHandler.synced;\n }\n\n destroy(): void {\n this.unsubscribers.forEach((unsub) => unsub());\n this.awareness.destroy();\n this.rootDocHandler.destroy();\n this._observers = new Map();\n for (const [_, handler] of this.subdocHandlers) {\n handler.destroy();\n }\n this.subdocHandlers.clear();\n super.destroy();\n }\n\n // Some provider implementations expect to be able to call connect/disconnect, implement as noop\n disconnect(): void {\n // This is a noop for liveblocks as connections are managed by the room\n }\n\n connect(): void {\n // This is a noop for liveblocks as connections are managed by the room\n }\n}\n","/**\n * Utility module to work with key-value stores.\n *\n * @module map\n */\n\n/**\n * Creates a new Map instance.\n *\n * @function\n * @return {Map<any, any>}\n *\n * @function\n */\nexport const create = () => new Map()\n\n/**\n * Copy a Map object into a fresh Map object.\n *\n * @function\n * @template X,Y\n * @param {Map<X,Y>} m\n * @return {Map<X,Y>}\n */\nexport const copy = m => {\n const r = create()\n m.forEach((v, k) => { r.set(k, v) })\n return r\n}\n\n/**\n * Get map property. Create T if property is undefined and set T on map.\n *\n * ```js\n * const listeners = map.setIfUndefined(events, 'eventName', set.create)\n * listeners.add(listener)\n * ```\n *\n * @function\n * @template V,K\n * @template {Map<K,V>} MAP\n * @param {MAP} map\n * @param {K} key\n * @param {function():V} createT\n * @return {V}\n */\nexport const setIfUndefined = (map, key, createT) => {\n let set = map.get(key)\n if (set === undefined) {\n map.set(key, set = createT())\n }\n return set\n}\n\n/**\n * Creates an Array and populates it with the content of all key-value pairs using the `f(value, key)` function.\n *\n * @function\n * @template K\n * @template V\n * @template R\n * @param {Map<K,V>} m\n * @param {function(V,K):R} f\n * @return {Array<R>}\n */\nexport const map = (m, f) => {\n const res = []\n for (const [key, value] of m) {\n res.push(f(value, key))\n }\n return res\n}\n\n/**\n * Tests whether any key-value pairs pass the test implemented by `f(value, key)`.\n *\n * @todo should rename to some - similarly to Array.some\n *\n * @function\n * @template K\n * @template V\n * @param {Map<K,V>} m\n * @param {function(V,K):boolean} f\n * @return {boolean}\n */\nexport const any = (m, f) => {\n for (const [key, value] of m) {\n if (f(value, key)) {\n return true\n }\n }\n return false\n}\n\n/**\n * Tests whether all key-value pairs pass the test implemented by `f(value, key)`.\n *\n * @function\n * @template K\n * @template V\n * @param {Map<K,V>} m\n * @param {function(V,K):boolean} f\n * @return {boolean}\n */\nexport const all = (m, f) => {\n for (const [key, value] of m) {\n if (!f(value, key)) {\n return false\n }\n }\n return true\n}\n","/**\n * Utility module to work with sets.\n *\n * @module set\n */\n\nexport const create = () => new Set()\n\n/**\n * @template T\n * @param {Set<T>} set\n * @return {Array<T>}\n */\nexport const toArray = set => Array.from(set)\n\n/**\n * @template T\n * @param {Set<T>} set\n * @return {T}\n */\nexport const first = set =>\n set.values().next().value || undefined\n\n/**\n * @template T\n * @param {Iterable<T>} entries\n * @return {Set<T>}\n */\nexport const from = entries => new Set(entries)\n","/**\n * Utility module to work with Arrays.\n *\n * @module array\n */\n\nimport * as set from './set.js'\n\n/**\n * Return the last element of an array. The element must exist\n *\n * @template L\n * @param {ArrayLike<L>} arr\n * @return {L}\n */\nexport const last = arr => arr[arr.length - 1]\n\n/**\n * @template C\n * @return {Array<C>}\n */\nexport const create = () => /** @type {Array<C>} */ ([])\n\n/**\n * @template D\n * @param {Array<D>} a\n * @return {Array<D>}\n */\nexport const copy = a => /** @type {Array<D>} */ (a.slice())\n\n/**\n * Append elements from src to dest\n *\n * @template M\n * @param {Array<M>} dest\n * @param {Array<M>} src\n */\nexport const appendTo = (dest, src) => {\n for (let i = 0; i < src.length; i++) {\n dest.push(src[i])\n }\n}\n\n/**\n * Transforms something array-like to an actual Array.\n *\n * @function\n * @template T\n * @param {ArrayLike<T>|Iterable<T>} arraylike\n * @return {T}\n */\nexport const from = Array.from\n\n/**\n * True iff condition holds on every element in the Array.\n *\n * @function\n * @template ITEM\n * @template {ArrayLike<ITEM>} ARR\n *\n * @param {ARR} arr\n * @param {function(ITEM, number, ARR):boolean} f\n * @return {boolean}\n */\nexport const every = (arr, f) => {\n for (let i = 0; i < arr.length; i++) {\n if (!f(arr[i], i, arr)) {\n return false\n }\n }\n return true\n}\n\n/**\n * True iff condition holds on some element in the Array.\n *\n * @function\n * @template S\n * @template {ArrayLike<S>} ARR\n * @param {ARR} arr\n * @param {function(S, number, ARR):boolean} f\n * @return {boolean}\n */\nexport const some = (arr, f) => {\n for (let i = 0; i < arr.length; i++) {\n if (f(arr[i], i, arr)) {\n return true\n }\n }\n return false\n}\n\n/**\n * @template ELEM\n *\n * @param {ArrayLike<ELEM>} a\n * @param {ArrayLike<ELEM>} b\n * @return {boolean}\n */\nexport const equalFlat = (a, b) => a.length === b.length && every(a, (item, index) => item === b[index])\n\n/**\n * @template ELEM\n * @param {Array<Array<ELEM>>} arr\n * @return {Array<ELEM>}\n */\nexport const flatten = arr => fold(arr, /** @type {Array<ELEM>} */ ([]), (acc, val) => acc.concat(val))\n\n/**\n * @template T\n * @param {number} len\n * @param {function(number, Array<T>):T} f\n * @return {Array<T>}\n */\nexport const unfold = (len, f) => {\n const array = new Array(len)\n for (let i = 0; i < len; i++) {\n array[i] = f(i, array)\n }\n return array\n}\n\n/**\n * @template T\n * @template RESULT\n * @param {Array<T>} arr\n * @param {RESULT} seed\n * @param {function(RESULT, T, number):RESULT} folder\n */\nexport const fold = (arr, seed, folder) => arr.reduce(folder, seed)\n\nexport const isArray = Array.isArray\n\n/**\n * @template T\n * @param {Array<T>} arr\n * @return {Array<T>}\n */\nexport const unique = arr => from(set.from(arr))\n\n/**\n * @template T\n * @template M\n * @param {ArrayLike<T>} arr\n * @param {function(T):M} mapper\n * @return {Array<T>}\n */\nexport const uniqueBy = (arr, mapper) => {\n /**\n * @type {Set<M>}\n */\n const happened = set.create()\n /**\n * @type {Array<T>}\n */\n const result = []\n for (let i = 0; i < arr.length; i++) {\n const el = arr[i]\n const mapped = mapper(el)\n if (!happened.has(mapped)) {\n happened.add(mapped)\n result.push(el)\n }\n }\n return result\n}\n","/**\n * Observable class prototype.\n *\n * @module observable\n */\n\nimport * as map from './map.js'\nimport * as set from './set.js'\nimport * as array from './array.js'\n\n/**\n * Handles named events.\n *\n * @template N\n */\nexport class Observable {\n constructor () {\n /**\n * Some desc.\n * @type {Map<N, any>}\n */\n this._observers = map.create()\n }\n\n /**\n * @param {N} name\n * @param {function} f\n */\n on (name, f) {\n map.setIfUndefined(this._observers, name, set.create).add(f)\n }\n\n /**\n * @param {N} name\n * @param {function} f\n */\n once (name, f) {\n /**\n * @param {...any} args\n */\n const _f = (...args) => {\n this.off(name, _f)\n f(...args)\n }\n this.on(name, _f)\n }\n\n /**\n * @param {N} name\n * @param {function} f\n */\n off (name, f) {\n const observers = this._observers.get(name)\n if (observers !== undefined) {\n observers.delete(f)\n if (observers.size === 0) {\n this._observers.delete(name)\n }\n }\n }\n\n /**\n * Emit a named event. All registered event listeners that listen to the\n * specified name will receive the event.\n *\n * @todo This should catch exceptions\n *\n * @param {N} name The event name.\n * @param {Array<any>} args The arguments that are applied to the event listener.\n */\n emit (name, args) {\n // copy all listeners to an array first to make sure that no event is emitted to listeners that are subscribed while the event handler is called.\n return array.from((this._observers.get(name) || map.create()).values()).forEach(f => f(...args))\n }\n\n destroy () {\n this._observers = map.create()\n }\n}\n","// TODO: apparently Yjs is full of anys or something, see if we can fix this\n/* eslint-disable @typescript-eslint/no-unsafe-call */\n/* eslint-disable @typescript-eslint/no-unsafe-member-access */\n/* eslint-disable @typescript-eslint/no-unsafe-assignment */\nimport type {\n BaseUserMeta,\n Json,\n JsonObject,\n LsonObject,\n Room,\n} from \"@liveblocks/client\";\nimport { Observable } from \"lib0/observable\";\nimport type * as Y from \"yjs\";\n\nconst Y_PRESENCE_KEY = \"__yjs\";\n\ntype MetaClientState = {\n clock: number;\n lastUpdated: number;\n};\n\n/**\n * This class will store Yjs awareness in Liveblock's presence under the __yjs key\n * IMPORTANT: The Yjs awareness protocol uses ydoc.clientId to reference users\n * to their respective documents. To avoid mapping Yjs clientIds to liveblock's connectionId,\n * we simply set the clientId of the doc to the connectionId. Then no further mapping is required\n */\nexport class Awareness extends Observable<unknown> {\n private room: Room<JsonObject, LsonObject, BaseUserMeta, Json>;\n public doc: Y.Doc;\n public clientID: number;\n public states: Map<number, unknown> = new Map();\n // Meta is used to keep track and timeout users who disconnect. Liveblocks provides this for us, so we don't need to\n // manage it here. Unfortunately, it's expected to exist by various integrations, so it's an empty map.\n public meta: Map<number, MetaClientState> = new Map();\n // _checkInterval this would hold a timer to remove users, but Liveblock's presence already handles this\n // unfortunately it's typed by various integrations\n public _checkInterval: number = 0;\n\n private othersUnsub: () => void;\n constructor(\n doc: Y.Doc,\n room: Room<JsonObject, LsonObject, BaseUserMeta, Json>\n ) {\n super();\n this.doc = doc;\n this.room = room;\n this.clientID = doc.clientID;\n this.othersUnsub = this.room.events.others.subscribe((event) => {\n // When others are changed, we emit an event that contains arrays added/updated/removed.\n if (event.type === \"leave\") {\n // REMOVED\n this.emit(\"change\", [\n { added: [], updated: [], removed: [event.user.connectionId] },\n \"local\",\n ]);\n }\n\n if (event.type === \"enter\") {\n // ADDED\n this.emit(\"change\", [\n { added: [event.user.connectionId], updated: [], removed: [] },\n \"local\",\n ]);\n }\n\n if (event.type === \"update\") {\n // UPDATED\n this.emit(\"change\", [\n { added: [], updated: [event.user.connectionId], removed: [] },\n \"local\",\n ]);\n }\n });\n }\n\n destroy(): void {\n this.emit(\"destroy\", [this]);\n this.othersUnsub();\n this.setLocalState(null);\n super.destroy();\n }\n\n getLocalState(): JsonObject | null {\n const presence = this.room.getPresence();\n if (\n Object.keys(presence).length === 0 ||\n typeof presence[Y_PRESENCE_KEY] === \"undefined\"\n ) {\n return null;\n }\n return presence[Y_PRESENCE_KEY] as JsonObject | null;\n }\n\n setLocalState(state: Partial<JsonObject> | null): void {\n const presence = this.room.getSelf()?.presence[Y_PRESENCE_KEY];\n this.room.updatePresence({\n __yjs: { ...((presence as JsonObject) || {}), ...(state || {}) },\n });\n }\n\n setLocalStateField(field: string, value: JsonObject | null): void {\n const presence = this.room.getSelf()?.presence[Y_PRESENCE_KEY];\n const update = { [field]: value } as Partial<JsonObject>;\n this.room.updatePresence({\n __yjs: { ...((presence as JsonObject) || {}), ...update },\n });\n }\n\n // Translate liveblocks presence to yjs awareness\n getStates(): Map<number, unknown> {\n const others = this.room.getOthers();\n const states = others.reduce((acc: Map<number, unknown>, currentValue) => {\n if (currentValue.connectionId) {\n // connectionId == actorId == yjs.clientId\n acc.set(\n currentValue.connectionId,\n currentValue.presence[Y_PRESENCE_KEY] || {}\n );\n }\n return acc;\n }, new Map());\n return states;\n }\n}\n","import { detectDupes } from \"@liveblocks/core\";\nimport { Base64 } from \"js-base64\";\nimport { Observable } from \"lib0/observable\";\nimport * as Y from \"yjs\";\n\nimport { PKG_FORMAT, PKG_NAME, PKG_VERSION } from \"./version\";\n\ndetectDupes(PKG_NAME, PKG_VERSION, PKG_FORMAT);\n\nexport default class yDocHandler extends Observable<unknown> {\n private unsubscribers: Array<() => void> = [];\n\n private _synced = false;\n private doc: Y.Doc;\n private updateRoomDoc: (update: string) => void;\n private fetchRoomDoc: (vector: string) => void;\n\n constructor({\n doc,\n isRoot,\n updateDoc,\n fetchDoc,\n }: {\n doc: Y.Doc;\n isRoot: boolean;\n updateDoc: (update: string, guid?: string) => void;\n fetchDoc: (vector: string, guid?: string) => void;\n }) {\n super();\n this.doc = doc;\n // this.doc.load(); // this just emits a load event, it doesn't actually load anything\n this.doc.on(\"update\", this.updateHandler);\n this.updateRoomDoc = (update: string) => {\n updateDoc(update, isRoot ? undefined : this.doc.guid);\n };\n this.fetchRoomDoc = (vector: string) => {\n fetchDoc(vector, isRoot ? undefined : this.doc.guid);\n };\n\n this.syncDoc();\n }\n\n public handleServerUpdate = ({\n update,\n stateVector,\n }: {\n update: string;\n stateVector: string | null;\n }): void => {\n // apply update from the server\n Y.applyUpdate(this.doc, Base64.toUint8Array(update), \"backend\");\n // if this update is the result of a fetch, the state vector is included\n if (stateVector) {\n // Use server state to calculate a diff and send it\n try {\n const localUpdate = Y.encodeStateAsUpdate(\n this.doc,\n Base64.toUint8Array(stateVector)\n );\n this.updateRoomDoc(Base64.fromUint8Array(localUpdate));\n } catch (e) {\n // something went wrong encoding local state to send to the server\n console.warn(e);\n }\n // now that we've sent our local and received from server, we're in sync\n // calling `syncDoc` again will sync up the documents\n this.synced = true;\n }\n };\n\n public syncDoc = (): void => {\n this.synced = false;\n\n // The state vector is sent to the server so it knows what to send back\n // if you don't send it, it returns everything\n const encodedVector = Base64.fromUint8Array(Y.encodeStateVector(this.doc));\n this.fetchRoomDoc(encodedVector);\n };\n\n // The sync'd property is required by some provider implementations\n get synced(): boolean {\n return this._synced;\n }\n\n set synced(state: boolean) {\n if (this._synced !== state) {\n this._synced = state;\n this.emit(\"synced\", [state]);\n this.emit(\"sync\", [state]);\n }\n }\n\n private updateHandler = (update: Uint8Array, origin: string) => {\n if (origin !== \"backend\") {\n const encodedUpdate = Base64.fromUint8Array(update);\n this.updateRoomDoc(encodedUpdate);\n }\n };\n\n destroy(): void {\n this.doc.off(\"update\", this.updateHandler);\n this.unsubscribers.forEach((unsub) => unsub());\n this._observers = new Map();\n this.doc.destroy();\n }\n}\n","declare const __VERSION__: string;\ndeclare const TSUP_FORMAT: string;\n\nexport const PKG_NAME = \"@liveblocks/yjs\";\nexport const PKG_VERSION = typeof __VERSION__ === \"string\" && __VERSION__;\nexport const PKG_FORMAT = typeof TSUP_FORMAT === \"string\" && TSUP_FORMAT;\n"],"mappings":";AAOA,SAAS,eAAe,eAAAA,oBAAmB;;;ACOpC,IAAM,SAAS,MAAM,oBAAI,IAAI;AAgC7B,IAAM,iBAAiB,CAAC,KAAK,KAAK,YAAY;AACnD,MAAI,MAAM,IAAI,IAAI,GAAG;AACrB,MAAI,QAAQ,QAAW;AACrB,QAAI,IAAI,KAAK,MAAM,QAAQ,CAAC;AAAA,EAC9B;AACA,SAAO;AACT;;;AC9CO,IAAMC,UAAS,MAAM,oBAAI,IAAI;;;AC6C7B,IAAM,OAAO,MAAM;AAgFnB,IAAM,UAAU,MAAM;;;ACpHtB,IAAM,aAAN,MAAiB;AAAA,EACtB,cAAe;AAKb,SAAK,aAAiB,OAAO;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,GAAI,MAAM,GAAG;AACX,IAAI,eAAe,KAAK,YAAY,MAAUC,OAAM,EAAE,IAAI,CAAC;AAAA,EAC7D;AAAA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AkB;AACpB,WAAO,KAAK,eAAe;AAAA,EAC7B;AAAA,EAEA,UAAgB;AACd,SAAK,cAAc,QAAQ,CAAC,UAAU,MAAM,CAAC;AAC7C,SAAK,UAAU,QAAQ;AACvB,SAAK,eAAe,QAAQ;AAC5B,SAAK,aAAa,oBAAI,IAAI;AAC1B,eAAW,CAAC,GAAG,OAAO,KAAK,KAAK,gBAAgB;AAC9C,cAAQ,QAAQ;AAAA,IAClB;AACA,SAAK,eAAe,MAAM;AAC1B,UAAM,QAAQ;AAAA,EAChB;AAAA;AAAA,EAGA,aAAmB;AAAA,EAEnB;AAAA,EAEA,UAAgB;AAAA,EAEhB;AACF;","names":["detectDupes","create","create","detectDupes"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liveblocks/yjs",
3
- "version": "1.5.3-test1",
3
+ "version": "1.6.0-subdocs1",
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",
@@ -32,8 +32,8 @@
32
32
  "test:watch": "jest --silent --verbose --color=always --watch"
33
33
  },
34
34
  "dependencies": {
35
- "@liveblocks/client": "1.5.3-test1",
36
- "@liveblocks/core": "1.5.3-test1",
35
+ "@liveblocks/client": "1.6.0-subdocs1",
36
+ "@liveblocks/core": "1.6.0-subdocs1",
37
37
  "js-base64": "^3.7.5"
38
38
  },
39
39
  "peerDependencies": {