@liveblocks/yjs 2.18.2 → 2.18.4-uns1

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.js CHANGED
@@ -1,14 +1,14 @@
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 _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } 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; } var _class; var _class2; var _class3;// src/index.ts
2
- var _core = require('@liveblocks/core');
1
+ // src/index.ts
2
+ import { detectDupes } from "@liveblocks/core";
3
3
 
4
4
  // src/version.ts
5
5
  var PKG_NAME = "@liveblocks/yjs";
6
- var PKG_VERSION = "2.18.2";
7
- var PKG_FORMAT = "cjs";
6
+ var PKG_VERSION = "2.18.4-uns1";
7
+ var PKG_FORMAT = "esm";
8
8
 
9
9
  // src/provider.ts
10
-
11
- var _jsbase64 = require('js-base64');
10
+ import { ClientMsgCode, kInternal } from "@liveblocks/core";
11
+ import { Base64 as Base642 } from "js-base64";
12
12
 
13
13
  // ../../node_modules/lib0/map.js
14
14
  var create = () => /* @__PURE__ */ new Map();
@@ -81,27 +81,27 @@ var Observable = class {
81
81
  };
82
82
 
83
83
  // src/provider.ts
84
- var _yindexeddb = require('y-indexeddb');
85
- var _yjs = require('yjs'); var Y = _interopRequireWildcard(_yjs);
84
+ import { IndexeddbPersistence as IndexeddbPersistence2 } from "y-indexeddb";
85
+ import { parseUpdateMeta, PermanentUserData } from "yjs";
86
86
 
87
87
  // src/awareness.ts
88
88
  var Y_PRESENCE_KEY = "__yjs";
89
89
  var Y_PRESENCE_ID_KEY = "__yjs_clientid";
90
- var Awareness = (_class = class extends Observable {
91
-
92
-
93
- __init() {this.states = /* @__PURE__ */ new Map()}
90
+ var Awareness = class extends Observable {
91
+ room;
92
+ doc;
93
+ states = /* @__PURE__ */ new Map();
94
94
  // used to map liveblock's ActorId to Yjs ClientID, both unique numbers representing a client
95
- __init2() {this.actorToClientMap = /* @__PURE__ */ new Map()}
95
+ actorToClientMap = /* @__PURE__ */ new Map();
96
96
  // Meta is used to keep track and timeout users who disconnect. Liveblocks provides this for us, so we don't need to
97
97
  // manage it here. Unfortunately, it's expected to exist by various integrations, so it's an empty map.
98
- __init3() {this.meta = /* @__PURE__ */ new Map()}
98
+ meta = /* @__PURE__ */ new Map();
99
99
  // _checkInterval this would hold a timer to remove users, but Liveblock's presence already handles this
100
100
  // unfortunately it's typed by various integrations
101
- __init4() {this._checkInterval = 0}
102
-
101
+ _checkInterval = 0;
102
+ othersUnsub;
103
103
  constructor(doc, room) {
104
- super();_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this);_class.prototype.__init4.call(this);;
104
+ super();
105
105
  this.doc = doc;
106
106
  this.room = room;
107
107
  this.room.updatePresence({
@@ -165,7 +165,7 @@ var Awareness = (_class = class extends Observable {
165
165
  return presence[Y_PRESENCE_KEY];
166
166
  }
167
167
  setLocalState(state) {
168
- const presence = _optionalChain([this, 'access', _2 => _2.room, 'access', _3 => _3.getSelf, 'call', _4 => _4(), 'optionalAccess', _5 => _5.presence]);
168
+ const presence = this.room.getSelf()?.presence;
169
169
  if (state === null) {
170
170
  if (presence === void 0) {
171
171
  return;
@@ -177,7 +177,7 @@ var Awareness = (_class = class extends Observable {
177
177
  ]);
178
178
  return;
179
179
  }
180
- const yPresence = _optionalChain([presence, 'optionalAccess', _6 => _6[Y_PRESENCE_KEY]]);
180
+ const yPresence = presence?.[Y_PRESENCE_KEY];
181
181
  const added = yPresence === void 0 ? [this.doc.clientID] : [];
182
182
  const updated = yPresence === void 0 ? [] : [this.doc.clientID];
183
183
  this.room.updatePresence({
@@ -189,7 +189,7 @@ var Awareness = (_class = class extends Observable {
189
189
  this.emit("update", [{ added, updated, removed: [] }, "local"]);
190
190
  }
191
191
  setLocalStateField(field, value) {
192
- 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]]);
192
+ const presence = this.room.getSelf()?.presence[Y_PRESENCE_KEY];
193
193
  const update = { [field]: value };
194
194
  this.room.updatePresence({
195
195
  [Y_PRESENCE_KEY]: { ...presence || {}, ...update }
@@ -206,25 +206,25 @@ var Awareness = (_class = class extends Observable {
206
206
  }
207
207
  return acc;
208
208
  }, /* @__PURE__ */ new Map());
209
- const localPresence = _optionalChain([this, 'access', _12 => _12.room, 'access', _13 => _13.getSelf, 'call', _14 => _14(), 'optionalAccess', _15 => _15.presence, 'access', _16 => _16[Y_PRESENCE_KEY]]);
209
+ const localPresence = this.room.getSelf()?.presence[Y_PRESENCE_KEY];
210
210
  if (localPresence !== void 0) {
211
211
  states.set(this.doc.clientID, localPresence);
212
212
  }
213
213
  return states;
214
214
  }
215
- }, _class);
215
+ };
216
216
 
217
217
  // src/doc.ts
218
-
219
-
220
-
221
- var yDocHandler = (_class2 = class extends Observable {
222
- __init5() {this.unsubscribers = []}
223
- __init6() {this._synced = false}
224
-
225
-
226
-
227
-
218
+ import { Base64 } from "js-base64";
219
+ import { IndexeddbPersistence } from "y-indexeddb";
220
+ import * as Y from "yjs";
221
+ var yDocHandler = class extends Observable {
222
+ unsubscribers = [];
223
+ _synced = false;
224
+ doc;
225
+ updateRoomDoc;
226
+ fetchRoomDoc;
227
+ useV2Encoding;
228
228
  constructor({
229
229
  doc,
230
230
  isRoot,
@@ -232,7 +232,7 @@ var yDocHandler = (_class2 = class extends Observable {
232
232
  fetchDoc,
233
233
  useV2Encoding
234
234
  }) {
235
- super();_class2.prototype.__init5.call(this);_class2.prototype.__init6.call(this);_class2.prototype.__init7.call(this);_class2.prototype.__init8.call(this);_class2.prototype.__init9.call(this);;
235
+ super();
236
236
  this.doc = doc;
237
237
  this.useV2Encoding = useV2Encoding;
238
238
  this.doc.on(useV2Encoding ? "updateV2" : "update", this.updateHandler);
@@ -244,7 +244,7 @@ var yDocHandler = (_class2 = class extends Observable {
244
244
  };
245
245
  this.syncDoc();
246
246
  }
247
- __init7() {this.handleServerUpdate = ({
247
+ handleServerUpdate = ({
248
248
  update,
249
249
  stateVector,
250
250
  readOnly,
@@ -258,7 +258,7 @@ var yDocHandler = (_class2 = class extends Observable {
258
258
  const encodeUpdate = this.useV2Encoding ? Y.encodeStateAsUpdateV2 : Y.encodeStateAsUpdate;
259
259
  const localUpdate = encodeUpdate(
260
260
  this.doc,
261
- _jsbase64.Base64.toUint8Array(stateVector)
261
+ Base64.toUint8Array(stateVector)
262
262
  );
263
263
  this.updateRoomDoc(localUpdate);
264
264
  } catch (e) {
@@ -267,12 +267,12 @@ var yDocHandler = (_class2 = class extends Observable {
267
267
  }
268
268
  this.synced = true;
269
269
  }
270
- }}
271
- __init8() {this.syncDoc = () => {
270
+ };
271
+ syncDoc = () => {
272
272
  this.synced = false;
273
- const encodedVector = _jsbase64.Base64.fromUint8Array(Y.encodeStateVector(this.doc));
273
+ const encodedVector = Base64.fromUint8Array(Y.encodeStateVector(this.doc));
274
274
  this.fetchRoomDoc(encodedVector);
275
- }}
275
+ };
276
276
  // The sync'd property is required by some provider implementations
277
277
  get synced() {
278
278
  return this._synced;
@@ -284,35 +284,35 @@ var yDocHandler = (_class2 = class extends Observable {
284
284
  this.emit("sync", [state]);
285
285
  }
286
286
  }
287
- __init9() {this.updateHandler = (update, origin) => {
288
- const isFromLocal = origin instanceof _yindexeddb.IndexeddbPersistence;
287
+ updateHandler = (update, origin) => {
288
+ const isFromLocal = origin instanceof IndexeddbPersistence;
289
289
  if (origin !== "backend" && !isFromLocal) {
290
290
  this.updateRoomDoc(update);
291
291
  }
292
- }}
292
+ };
293
293
  destroy() {
294
294
  this.doc.off("update", this.updateHandler);
295
295
  this.unsubscribers.forEach((unsub) => unsub());
296
296
  this._observers = /* @__PURE__ */ new Map();
297
297
  this.doc.destroy();
298
298
  }
299
- }, _class2);
299
+ };
300
300
 
301
301
  // src/provider.ts
302
- var LiveblocksYjsProvider = (_class3 = class extends Observable {
303
-
304
-
305
-
306
- __init10() {this.indexeddbProvider = null}
307
- __init11() {this.isPaused = false}
308
- __init12() {this.unsubscribers = []}
309
-
310
-
311
- __init13() {this.subdocHandlers = /* @__PURE__ */ new Map()}
312
-
313
- __init14() {this.pending = []}
302
+ var LiveblocksYjsProvider = class extends Observable {
303
+ room;
304
+ rootDoc;
305
+ options;
306
+ indexeddbProvider = null;
307
+ isPaused = false;
308
+ unsubscribers = [];
309
+ awareness;
310
+ rootDocHandler;
311
+ subdocHandlers = /* @__PURE__ */ new Map();
312
+ permanentUserData;
313
+ pending = [];
314
314
  constructor(room, doc, options = {}) {
315
- super();_class3.prototype.__init10.call(this);_class3.prototype.__init11.call(this);_class3.prototype.__init12.call(this);_class3.prototype.__init13.call(this);_class3.prototype.__init14.call(this);_class3.prototype.__init15.call(this);_class3.prototype.__init16.call(this);_class3.prototype.__init17.call(this);_class3.prototype.__init18.call(this);_class3.prototype.__init19.call(this);_class3.prototype.__init20.call(this);_class3.prototype.__init21.call(this);_class3.prototype.__init22.call(this);;
315
+ super();
316
316
  this.rootDoc = doc;
317
317
  this.room = room;
318
318
  this.options = options;
@@ -321,12 +321,12 @@ var LiveblocksYjsProvider = (_class3 = class extends Observable {
321
321
  isRoot: true,
322
322
  updateDoc: this.updateDoc,
323
323
  fetchDoc: this.fetchDoc,
324
- useV2Encoding: _nullishCoalesce(this.options.useV2Encoding_experimental, () => ( false))
324
+ useV2Encoding: this.options.useV2Encoding_experimental ?? false
325
325
  });
326
326
  if (this.options.enablePermanentUserData) {
327
- this.permanentUserData = new (0, _yjs.PermanentUserData)(doc);
327
+ this.permanentUserData = new PermanentUserData(doc);
328
328
  }
329
- room[_core.kInternal].setYjsProvider(this);
329
+ room[kInternal].setYjsProvider(this);
330
330
  this.awareness = new Awareness(this.rootDoc, this.room);
331
331
  this.unsubscribers.push(
332
332
  this.room.events.status.subscribe((status) => {
@@ -341,12 +341,12 @@ var LiveblocksYjsProvider = (_class3 = class extends Observable {
341
341
  this.unsubscribers.push(
342
342
  this.room.events.ydoc.subscribe((message) => {
343
343
  const { type } = message;
344
- if (type === _core.ClientMsgCode.UPDATE_YDOC) {
344
+ if (type === ClientMsgCode.UPDATE_YDOC) {
345
345
  return;
346
346
  }
347
347
  const { stateVector, update: updateStr, guid, v2 } = message;
348
- const canWrite = _nullishCoalesce(_optionalChain([this, 'access', _34 => _34.room, 'access', _35 => _35.getSelf, 'call', _36 => _36(), 'optionalAccess', _37 => _37.canWrite]), () => ( true));
349
- const update = _jsbase64.Base64.toUint8Array(updateStr);
348
+ const canWrite = this.room.getSelf()?.canWrite ?? true;
349
+ const update = Base642.toUint8Array(updateStr);
350
350
  let foundPendingUpdate = false;
351
351
  const updateId = this.getUniqueUpdateId(update);
352
352
  this.pending = this.pending.filter((pendingUpdate) => {
@@ -358,12 +358,12 @@ var LiveblocksYjsProvider = (_class3 = class extends Observable {
358
358
  });
359
359
  if (!foundPendingUpdate) {
360
360
  if (guid !== void 0) {
361
- _optionalChain([this, 'access', _38 => _38.subdocHandlers, 'access', _39 => _39.get, 'call', _40 => _40(guid), 'optionalAccess', _41 => _41.handleServerUpdate, 'call', _42 => _42({
361
+ this.subdocHandlers.get(guid)?.handleServerUpdate({
362
362
  update,
363
363
  stateVector,
364
364
  readOnly: !canWrite,
365
365
  v2
366
- })]);
366
+ });
367
367
  } else {
368
368
  this.rootDocHandler.handleServerUpdate({
369
369
  update,
@@ -391,8 +391,8 @@ var LiveblocksYjsProvider = (_class3 = class extends Observable {
391
391
  this.rootDoc.on("subdocs", this.handleSubdocs);
392
392
  this.syncDoc();
393
393
  }
394
- __init15() {this.setupOfflineSupport = () => {
395
- this.indexeddbProvider = new (0, _yindexeddb.IndexeddbPersistence)(
394
+ setupOfflineSupport = () => {
395
+ this.indexeddbProvider = new IndexeddbPersistence2(
396
396
  this.room.id,
397
397
  this.rootDoc
398
398
  );
@@ -401,10 +401,10 @@ var LiveblocksYjsProvider = (_class3 = class extends Observable {
401
401
  };
402
402
  this.indexeddbProvider.on("synced", onIndexedDbSync);
403
403
  this.unsubscribers.push(() => {
404
- _optionalChain([this, 'access', _43 => _43.indexeddbProvider, 'optionalAccess', _44 => _44.off, 'call', _45 => _45("synced", onIndexedDbSync)]);
404
+ this.indexeddbProvider?.off("synced", onIndexedDbSync);
405
405
  });
406
- }}
407
- __init16() {this.handleSubdocs = ({
406
+ };
407
+ handleSubdocs = ({
408
408
  loaded,
409
409
  removed,
410
410
  added
@@ -419,34 +419,34 @@ var LiveblocksYjsProvider = (_class3 = class extends Observable {
419
419
  }
420
420
  for (const subdoc of removed) {
421
421
  if (this.subdocHandlers.has(subdoc.guid)) {
422
- _optionalChain([this, 'access', _46 => _46.subdocHandlers, 'access', _47 => _47.get, 'call', _48 => _48(subdoc.guid), 'optionalAccess', _49 => _49.destroy, 'call', _50 => _50()]);
422
+ this.subdocHandlers.get(subdoc.guid)?.destroy();
423
423
  this.subdocHandlers.delete(subdoc.guid);
424
424
  }
425
425
  }
426
- }}
427
- __init17() {this.getUniqueUpdateId = (update) => {
428
- const clock = _nullishCoalesce(_yjs.parseUpdateMeta.call(void 0, update).to.get(this.rootDoc.clientID), () => ( "-1"));
426
+ };
427
+ getUniqueUpdateId = (update) => {
428
+ const clock = parseUpdateMeta(update).to.get(this.rootDoc.clientID) ?? "-1";
429
429
  return this.rootDoc.clientID + ":" + clock;
430
- }}
431
- __init18() {this.updateDoc = (update, guid) => {
432
- const canWrite = _nullishCoalesce(_optionalChain([this, 'access', _51 => _51.room, 'access', _52 => _52.getSelf, 'call', _53 => _53(), 'optionalAccess', _54 => _54.canWrite]), () => ( true));
430
+ };
431
+ updateDoc = (update, guid) => {
432
+ const canWrite = this.room.getSelf()?.canWrite ?? true;
433
433
  if (canWrite && !this.isPaused) {
434
434
  const updateId = this.getUniqueUpdateId(update);
435
435
  this.pending.push(updateId);
436
436
  this.room.updateYDoc(
437
- _jsbase64.Base64.fromUint8Array(update),
437
+ Base642.fromUint8Array(update),
438
438
  guid,
439
439
  this.useV2Encoding
440
440
  );
441
441
  this.emit("status", [this.getStatus()]);
442
442
  }
443
- }}
444
- __init19() {this.fetchDoc = (vector, guid) => {
443
+ };
444
+ fetchDoc = (vector, guid) => {
445
445
  this.room.fetchYDoc(vector, guid, this.useV2Encoding);
446
- }}
447
- __init20() {this.createSubdocHandler = (subdoc) => {
446
+ };
447
+ createSubdocHandler = (subdoc) => {
448
448
  if (this.subdocHandlers.has(subdoc.guid)) {
449
- _optionalChain([this, 'access', _55 => _55.subdocHandlers, 'access', _56 => _56.get, 'call', _57 => _57(subdoc.guid), 'optionalAccess', _58 => _58.syncDoc, 'call', _59 => _59()]);
449
+ this.subdocHandlers.get(subdoc.guid)?.syncDoc();
450
450
  return;
451
451
  }
452
452
  const handler = new yDocHandler({
@@ -454,12 +454,12 @@ var LiveblocksYjsProvider = (_class3 = class extends Observable {
454
454
  isRoot: false,
455
455
  updateDoc: this.updateDoc,
456
456
  fetchDoc: this.fetchDoc,
457
- useV2Encoding: _nullishCoalesce(this.options.useV2Encoding_experimental, () => ( false))
457
+ useV2Encoding: this.options.useV2Encoding_experimental ?? false
458
458
  });
459
459
  this.subdocHandlers.set(subdoc.guid, handler);
460
- }}
460
+ };
461
461
  // attempt to load a subdoc of a given guid
462
- __init21() {this.loadSubdoc = (guid) => {
462
+ loadSubdoc = (guid) => {
463
463
  for (const subdoc of this.rootDoc.subdocs) {
464
464
  if (subdoc.guid === guid) {
465
465
  subdoc.load();
@@ -467,22 +467,22 @@ var LiveblocksYjsProvider = (_class3 = class extends Observable {
467
467
  }
468
468
  }
469
469
  return false;
470
- }}
471
- __init22() {this.syncDoc = () => {
470
+ };
471
+ syncDoc = () => {
472
472
  this.rootDocHandler.syncDoc();
473
473
  for (const [_, handler] of this.subdocHandlers) {
474
474
  handler.syncDoc();
475
475
  }
476
- }}
476
+ };
477
477
  get useV2Encoding() {
478
- return _nullishCoalesce(this.options.useV2Encoding_experimental, () => ( false));
478
+ return this.options.useV2Encoding_experimental ?? false;
479
479
  }
480
480
  // The sync'd property is required by some provider implementations
481
481
  get synced() {
482
482
  return this.rootDocHandler.synced;
483
483
  }
484
484
  async pause() {
485
- await _optionalChain([this, 'access', _60 => _60.indexeddbProvider, 'optionalAccess', _61 => _61.destroy, 'call', _62 => _62()]);
485
+ await this.indexeddbProvider?.destroy();
486
486
  this.indexeddbProvider = null;
487
487
  this.isPaused = true;
488
488
  }
@@ -522,17 +522,17 @@ var LiveblocksYjsProvider = (_class3 = class extends Observable {
522
522
  }
523
523
  connect() {
524
524
  }
525
- }, _class3);
525
+ };
526
526
 
527
527
  // src/providerContext.ts
528
-
528
+ import { Doc } from "yjs";
529
529
  var providersMap = /* @__PURE__ */ new WeakMap();
530
530
  var getYjsProviderForRoom = (room, options = {}) => {
531
531
  const provider = providersMap.get(room);
532
532
  if (provider !== void 0) {
533
533
  return provider;
534
534
  }
535
- const doc = new (0, _yjs.Doc)();
535
+ const doc = new Doc();
536
536
  const newProvider = new LiveblocksYjsProvider(room, doc, options);
537
537
  room.events.roomWillDestroy.subscribeOnce(() => {
538
538
  newProvider.destroy();
@@ -542,9 +542,9 @@ var getYjsProviderForRoom = (room, options = {}) => {
542
542
  };
543
543
 
544
544
  // src/index.ts
545
- _core.detectDupes.call(void 0, PKG_NAME, PKG_VERSION, PKG_FORMAT);
546
-
547
-
548
-
549
- exports.LiveblocksYjsProvider = LiveblocksYjsProvider; exports.getYjsProviderForRoom = getYjsProviderForRoom;
545
+ detectDupes(PKG_NAME, PKG_VERSION, PKG_FORMAT);
546
+ export {
547
+ LiveblocksYjsProvider,
548
+ getYjsProviderForRoom
549
+ };
550
550
  //# sourceMappingURL=index.js.map