@liveblocks/yjs 2.18.3 → 2.18.4-uns2
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.mjs → index.cjs} +99 -99
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +98 -98
- package/dist/index.js.map +1 -1
- package/package.json +11 -11
- package/dist/index.mjs.map +0 -1
- /package/dist/{index.d.mts → index.d.cts} +0 -0
package/dist/index.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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.
|
|
7
|
-
var PKG_FORMAT = "
|
|
6
|
+
var PKG_VERSION = "2.18.4-uns2";
|
|
7
|
+
var PKG_FORMAT = "esm";
|
|
8
8
|
|
|
9
9
|
// src/provider.ts
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
85
|
-
|
|
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 =
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
102
|
-
|
|
101
|
+
_checkInterval = 0;
|
|
102
|
+
othersUnsub;
|
|
103
103
|
constructor(doc, room) {
|
|
104
|
-
super();
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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
|
-
}
|
|
215
|
+
};
|
|
216
216
|
|
|
217
217
|
// src/doc.ts
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
var yDocHandler =
|
|
222
|
-
|
|
223
|
-
|
|
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();
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
270
|
+
};
|
|
271
|
+
syncDoc = () => {
|
|
272
272
|
this.synced = false;
|
|
273
|
-
const encodedVector =
|
|
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
|
-
|
|
288
|
-
const isFromLocal = origin instanceof
|
|
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
|
-
}
|
|
299
|
+
};
|
|
300
300
|
|
|
301
301
|
// src/provider.ts
|
|
302
|
-
var LiveblocksYjsProvider =
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
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();
|
|
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:
|
|
324
|
+
useV2Encoding: this.options.useV2Encoding_experimental ?? false
|
|
325
325
|
});
|
|
326
326
|
if (this.options.enablePermanentUserData) {
|
|
327
|
-
this.permanentUserData = new
|
|
327
|
+
this.permanentUserData = new PermanentUserData(doc);
|
|
328
328
|
}
|
|
329
|
-
room[
|
|
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 ===
|
|
344
|
+
if (type === ClientMsgCode.UPDATE_YDOC) {
|
|
345
345
|
return;
|
|
346
346
|
}
|
|
347
347
|
const { stateVector, update: updateStr, guid, v2 } = message;
|
|
348
|
-
const canWrite =
|
|
349
|
-
const update =
|
|
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
|
-
|
|
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
|
-
|
|
395
|
-
this.indexeddbProvider = new (
|
|
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
|
-
|
|
404
|
+
this.indexeddbProvider?.off("synced", onIndexedDbSync);
|
|
405
405
|
});
|
|
406
|
-
}
|
|
407
|
-
|
|
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
|
-
|
|
422
|
+
this.subdocHandlers.get(subdoc.guid)?.destroy();
|
|
423
423
|
this.subdocHandlers.delete(subdoc.guid);
|
|
424
424
|
}
|
|
425
425
|
}
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
const clock =
|
|
426
|
+
};
|
|
427
|
+
getUniqueUpdateId = (update) => {
|
|
428
|
+
const clock = parseUpdateMeta(update).to.get(this.rootDoc.clientID) ?? "-1";
|
|
429
429
|
return this.rootDoc.clientID + ":" + clock;
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
const canWrite =
|
|
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
|
-
|
|
437
|
+
Base642.fromUint8Array(update),
|
|
438
438
|
guid,
|
|
439
439
|
this.useV2Encoding
|
|
440
440
|
);
|
|
441
441
|
this.emit("status", [this.getStatus()]);
|
|
442
442
|
}
|
|
443
|
-
}
|
|
444
|
-
|
|
443
|
+
};
|
|
444
|
+
fetchDoc = (vector, guid) => {
|
|
445
445
|
this.room.fetchYDoc(vector, guid, this.useV2Encoding);
|
|
446
|
-
}
|
|
447
|
-
|
|
446
|
+
};
|
|
447
|
+
createSubdocHandler = (subdoc) => {
|
|
448
448
|
if (this.subdocHandlers.has(subdoc.guid)) {
|
|
449
|
-
|
|
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:
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
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
|
-
}
|
|
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
|
|
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
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
545
|
+
detectDupes(PKG_NAME, PKG_VERSION, PKG_FORMAT);
|
|
546
|
+
export {
|
|
547
|
+
LiveblocksYjsProvider,
|
|
548
|
+
getYjsProviderForRoom
|
|
549
|
+
};
|
|
550
550
|
//# sourceMappingURL=index.js.map
|