@liveblocks/yjs 2.13.3-emails1 → 2.14.0-v2encoding
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 +7 -2
- package/dist/index.d.ts +7 -2
- package/dist/index.js +156 -128
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +146 -118
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -36,16 +36,19 @@ declare class yDocHandler extends Observable<unknown> {
|
|
|
36
36
|
private doc;
|
|
37
37
|
private updateRoomDoc;
|
|
38
38
|
private fetchRoomDoc;
|
|
39
|
-
|
|
39
|
+
private useV2Updates;
|
|
40
|
+
constructor({ doc, isRoot, updateDoc, fetchDoc, useV2Updates, }: {
|
|
40
41
|
doc: Y.Doc;
|
|
41
42
|
isRoot: boolean;
|
|
42
43
|
updateDoc: (update: Uint8Array, guid?: string) => void;
|
|
43
44
|
fetchDoc: (vector: string, guid?: string) => void;
|
|
45
|
+
useV2Updates: boolean;
|
|
44
46
|
});
|
|
45
|
-
handleServerUpdate: ({ update, stateVector, readOnly, }: {
|
|
47
|
+
handleServerUpdate: ({ update, stateVector, readOnly, v2, }: {
|
|
46
48
|
update: Uint8Array;
|
|
47
49
|
stateVector: string | null;
|
|
48
50
|
readOnly: boolean;
|
|
51
|
+
v2?: boolean | undefined;
|
|
49
52
|
}) => void;
|
|
50
53
|
syncDoc: () => void;
|
|
51
54
|
get synced(): boolean;
|
|
@@ -57,6 +60,7 @@ declare class yDocHandler extends Observable<unknown> {
|
|
|
57
60
|
declare type ProviderOptions = {
|
|
58
61
|
autoloadSubdocs?: boolean;
|
|
59
62
|
offlineSupport_experimental?: boolean;
|
|
63
|
+
useV2Updates_experimental?: boolean;
|
|
60
64
|
};
|
|
61
65
|
declare class LiveblocksYjsProvider<P extends JsonObject = DP, S extends LsonObject = DS, U extends BaseUserMeta = DU, E extends Json = DE, M extends BaseMetadata$1 = DM> extends Observable<unknown> implements IYjsProvider {
|
|
62
66
|
private room;
|
|
@@ -76,6 +80,7 @@ declare class LiveblocksYjsProvider<P extends JsonObject = DP, S extends LsonObj
|
|
|
76
80
|
private createSubdocHandler;
|
|
77
81
|
loadSubdoc: (guid: string) => boolean;
|
|
78
82
|
private syncDoc;
|
|
83
|
+
get useV2Updates(): boolean;
|
|
79
84
|
get synced(): boolean;
|
|
80
85
|
getStatus(): YjsSyncStatus;
|
|
81
86
|
destroy(): void;
|
package/dist/index.d.ts
CHANGED
|
@@ -36,16 +36,19 @@ declare class yDocHandler extends Observable<unknown> {
|
|
|
36
36
|
private doc;
|
|
37
37
|
private updateRoomDoc;
|
|
38
38
|
private fetchRoomDoc;
|
|
39
|
-
|
|
39
|
+
private useV2Updates;
|
|
40
|
+
constructor({ doc, isRoot, updateDoc, fetchDoc, useV2Updates, }: {
|
|
40
41
|
doc: Y.Doc;
|
|
41
42
|
isRoot: boolean;
|
|
42
43
|
updateDoc: (update: Uint8Array, guid?: string) => void;
|
|
43
44
|
fetchDoc: (vector: string, guid?: string) => void;
|
|
45
|
+
useV2Updates: boolean;
|
|
44
46
|
});
|
|
45
|
-
handleServerUpdate: ({ update, stateVector, readOnly, }: {
|
|
47
|
+
handleServerUpdate: ({ update, stateVector, readOnly, v2, }: {
|
|
46
48
|
update: Uint8Array;
|
|
47
49
|
stateVector: string | null;
|
|
48
50
|
readOnly: boolean;
|
|
51
|
+
v2?: boolean | undefined;
|
|
49
52
|
}) => void;
|
|
50
53
|
syncDoc: () => void;
|
|
51
54
|
get synced(): boolean;
|
|
@@ -57,6 +60,7 @@ declare class yDocHandler extends Observable<unknown> {
|
|
|
57
60
|
declare type ProviderOptions = {
|
|
58
61
|
autoloadSubdocs?: boolean;
|
|
59
62
|
offlineSupport_experimental?: boolean;
|
|
63
|
+
useV2Updates_experimental?: boolean;
|
|
60
64
|
};
|
|
61
65
|
declare class LiveblocksYjsProvider<P extends JsonObject = DP, S extends LsonObject = DS, U extends BaseUserMeta = DU, E extends Json = DE, M extends BaseMetadata$1 = DM> extends Observable<unknown> implements IYjsProvider {
|
|
62
66
|
private room;
|
|
@@ -76,6 +80,7 @@ declare class LiveblocksYjsProvider<P extends JsonObject = DP, S extends LsonObj
|
|
|
76
80
|
private createSubdocHandler;
|
|
77
81
|
loadSubdoc: (guid: string) => boolean;
|
|
78
82
|
private syncDoc;
|
|
83
|
+
get useV2Updates(): boolean;
|
|
79
84
|
get synced(): boolean;
|
|
80
85
|
getStatus(): YjsSyncStatus;
|
|
81
86
|
destroy(): void;
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
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; }
|
|
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
2
|
var _core = require('@liveblocks/core');
|
|
3
3
|
var _jsbase64 = require('js-base64');
|
|
4
4
|
|
|
@@ -79,18 +79,21 @@ var _yjs = require('yjs'); var Y2 = _interopRequireWildcard(_yjs); var Y = _inte
|
|
|
79
79
|
// src/awareness.ts
|
|
80
80
|
var Y_PRESENCE_KEY = "__yjs";
|
|
81
81
|
var Y_PRESENCE_ID_KEY = "__yjs_clientid";
|
|
82
|
-
var Awareness = class extends Observable {
|
|
82
|
+
var Awareness = (_class = class extends Observable {
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
__init() {this.states = /* @__PURE__ */ new Map()}
|
|
86
|
+
// used to map liveblock's ActorId to Yjs ClientID, both unique numbers representing a client
|
|
87
|
+
__init2() {this.actorToClientMap = /* @__PURE__ */ new Map()}
|
|
88
|
+
// Meta is used to keep track and timeout users who disconnect. Liveblocks provides this for us, so we don't need to
|
|
89
|
+
// manage it here. Unfortunately, it's expected to exist by various integrations, so it's an empty map.
|
|
90
|
+
__init3() {this.meta = /* @__PURE__ */ new Map()}
|
|
91
|
+
// _checkInterval this would hold a timer to remove users, but Liveblock's presence already handles this
|
|
92
|
+
// unfortunately it's typed by various integrations
|
|
93
|
+
__init4() {this._checkInterval = 0}
|
|
94
|
+
|
|
83
95
|
constructor(doc, room) {
|
|
84
|
-
super();
|
|
85
|
-
this.states = /* @__PURE__ */ new Map();
|
|
86
|
-
// used to map liveblock's ActorId to Yjs ClientID, both unique numbers representing a client
|
|
87
|
-
this.actorToClientMap = /* @__PURE__ */ new Map();
|
|
88
|
-
// Meta is used to keep track and timeout users who disconnect. Liveblocks provides this for us, so we don't need to
|
|
89
|
-
// manage it here. Unfortunately, it's expected to exist by various integrations, so it's an empty map.
|
|
90
|
-
this.meta = /* @__PURE__ */ new Map();
|
|
91
|
-
// _checkInterval this would hold a timer to remove users, but Liveblock's presence already handles this
|
|
92
|
-
// unfortunately it's typed by various integrations
|
|
93
|
-
this._checkInterval = 0;
|
|
96
|
+
super();_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this);_class.prototype.__init4.call(this);;
|
|
94
97
|
this.doc = doc;
|
|
95
98
|
this.room = room;
|
|
96
99
|
this.room.updatePresence({
|
|
@@ -201,55 +204,29 @@ var Awareness = class extends Observable {
|
|
|
201
204
|
}
|
|
202
205
|
return states;
|
|
203
206
|
}
|
|
204
|
-
};
|
|
207
|
+
}, _class);
|
|
205
208
|
|
|
206
209
|
// src/doc.ts
|
|
207
210
|
|
|
208
211
|
|
|
209
212
|
|
|
210
|
-
var yDocHandler = class extends Observable {
|
|
213
|
+
var yDocHandler = (_class2 = class extends Observable {
|
|
214
|
+
__init5() {this.unsubscribers = []}
|
|
215
|
+
__init6() {this._synced = false}
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
211
220
|
constructor({
|
|
212
221
|
doc,
|
|
213
222
|
isRoot,
|
|
214
223
|
updateDoc,
|
|
215
|
-
fetchDoc
|
|
224
|
+
fetchDoc,
|
|
225
|
+
useV2Updates
|
|
216
226
|
}) {
|
|
217
|
-
super();
|
|
218
|
-
this.unsubscribers = [];
|
|
219
|
-
this._synced = false;
|
|
220
|
-
this.handleServerUpdate = ({
|
|
221
|
-
update,
|
|
222
|
-
stateVector,
|
|
223
|
-
readOnly
|
|
224
|
-
}) => {
|
|
225
|
-
Y.applyUpdate(this.doc, update, "backend");
|
|
226
|
-
if (stateVector) {
|
|
227
|
-
if (!readOnly) {
|
|
228
|
-
try {
|
|
229
|
-
const localUpdate = Y.encodeStateAsUpdate(
|
|
230
|
-
this.doc,
|
|
231
|
-
_jsbase64.Base64.toUint8Array(stateVector)
|
|
232
|
-
);
|
|
233
|
-
this.updateRoomDoc(localUpdate);
|
|
234
|
-
} catch (e) {
|
|
235
|
-
console.warn(e);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
this.synced = true;
|
|
239
|
-
}
|
|
240
|
-
};
|
|
241
|
-
this.syncDoc = () => {
|
|
242
|
-
this.synced = false;
|
|
243
|
-
const encodedVector = _jsbase64.Base64.fromUint8Array(Y.encodeStateVector(this.doc));
|
|
244
|
-
this.fetchRoomDoc(encodedVector);
|
|
245
|
-
};
|
|
246
|
-
this.updateHandler = (update, origin) => {
|
|
247
|
-
const isFromLocal = origin instanceof _yindexeddb.IndexeddbPersistence;
|
|
248
|
-
if (origin !== "backend" && !isFromLocal) {
|
|
249
|
-
this.updateRoomDoc(update);
|
|
250
|
-
}
|
|
251
|
-
};
|
|
227
|
+
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);;
|
|
252
228
|
this.doc = doc;
|
|
229
|
+
this.useV2Updates = useV2Updates;
|
|
253
230
|
this.doc.on("update", this.updateHandler);
|
|
254
231
|
this.updateRoomDoc = (update) => {
|
|
255
232
|
updateDoc(update, isRoot ? void 0 : this.doc.guid);
|
|
@@ -259,6 +236,35 @@ var yDocHandler = class extends Observable {
|
|
|
259
236
|
};
|
|
260
237
|
this.syncDoc();
|
|
261
238
|
}
|
|
239
|
+
__init7() {this.handleServerUpdate = ({
|
|
240
|
+
update,
|
|
241
|
+
stateVector,
|
|
242
|
+
readOnly,
|
|
243
|
+
v2
|
|
244
|
+
}) => {
|
|
245
|
+
const applyUpdate2 = v2 ? Y.applyUpdateV2 : Y.applyUpdate;
|
|
246
|
+
applyUpdate2(this.doc, update, "backend");
|
|
247
|
+
if (stateVector) {
|
|
248
|
+
if (!readOnly) {
|
|
249
|
+
try {
|
|
250
|
+
const encodeUpdate = this.useV2Updates ? Y.encodeStateAsUpdateV2 : Y.encodeStateAsUpdate;
|
|
251
|
+
const localUpdate = encodeUpdate(
|
|
252
|
+
this.doc,
|
|
253
|
+
_jsbase64.Base64.toUint8Array(stateVector)
|
|
254
|
+
);
|
|
255
|
+
this.updateRoomDoc(localUpdate);
|
|
256
|
+
} catch (e) {
|
|
257
|
+
console.warn(e);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
this.synced = true;
|
|
261
|
+
}
|
|
262
|
+
}}
|
|
263
|
+
__init8() {this.syncDoc = () => {
|
|
264
|
+
this.synced = false;
|
|
265
|
+
const encodedVector = _jsbase64.Base64.fromUint8Array(Y.encodeStateVector(this.doc));
|
|
266
|
+
this.fetchRoomDoc(encodedVector);
|
|
267
|
+
}}
|
|
262
268
|
// The sync'd property is required by some provider implementations
|
|
263
269
|
get synced() {
|
|
264
270
|
return this._synced;
|
|
@@ -270,93 +276,39 @@ var yDocHandler = class extends Observable {
|
|
|
270
276
|
this.emit("sync", [state]);
|
|
271
277
|
}
|
|
272
278
|
}
|
|
279
|
+
__init9() {this.updateHandler = (update, origin) => {
|
|
280
|
+
const isFromLocal = origin instanceof _yindexeddb.IndexeddbPersistence;
|
|
281
|
+
if (origin !== "backend" && !isFromLocal) {
|
|
282
|
+
this.updateRoomDoc(update);
|
|
283
|
+
}
|
|
284
|
+
}}
|
|
273
285
|
destroy() {
|
|
274
286
|
this.doc.off("update", this.updateHandler);
|
|
275
287
|
this.unsubscribers.forEach((unsub) => unsub());
|
|
276
288
|
this._observers = /* @__PURE__ */ new Map();
|
|
277
289
|
this.doc.destroy();
|
|
278
290
|
}
|
|
279
|
-
};
|
|
291
|
+
}, _class2);
|
|
280
292
|
|
|
281
293
|
// src/version.ts
|
|
282
294
|
var PKG_NAME = "@liveblocks/yjs";
|
|
283
|
-
var PKG_VERSION = "2.
|
|
295
|
+
var PKG_VERSION = "2.14.0-v2encoding";
|
|
284
296
|
var PKG_FORMAT = "cjs";
|
|
285
297
|
|
|
286
298
|
// src/index.ts
|
|
287
299
|
_core.detectDupes.call(void 0, PKG_NAME, PKG_VERSION, PKG_FORMAT);
|
|
288
|
-
var LiveblocksYjsProvider = class extends Observable {
|
|
300
|
+
var LiveblocksYjsProvider = (_class3 = class extends Observable {
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
__init10() {this.indexeddbProvider = null}
|
|
305
|
+
__init11() {this.unsubscribers = []}
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
__init12() {this.subdocHandlers = /* @__PURE__ */ new Map()}
|
|
309
|
+
__init13() {this.pending = []}
|
|
289
310
|
constructor(room, doc, options = {}) {
|
|
290
|
-
super();
|
|
291
|
-
this.indexeddbProvider = null;
|
|
292
|
-
this.unsubscribers = [];
|
|
293
|
-
this.subdocHandlers = /* @__PURE__ */ new Map();
|
|
294
|
-
this.pending = [];
|
|
295
|
-
this.handleSubdocs = ({
|
|
296
|
-
loaded,
|
|
297
|
-
removed,
|
|
298
|
-
added
|
|
299
|
-
}) => {
|
|
300
|
-
loaded.forEach(this.createSubdocHandler);
|
|
301
|
-
if (this.options.autoloadSubdocs) {
|
|
302
|
-
for (const subdoc of added) {
|
|
303
|
-
if (!this.subdocHandlers.has(subdoc.guid)) {
|
|
304
|
-
subdoc.load();
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
for (const subdoc of removed) {
|
|
309
|
-
if (this.subdocHandlers.has(subdoc.guid)) {
|
|
310
|
-
_optionalChain([this, 'access', _17 => _17.subdocHandlers, 'access', _18 => _18.get, 'call', _19 => _19(subdoc.guid), 'optionalAccess', _20 => _20.destroy, 'call', _21 => _21()]);
|
|
311
|
-
this.subdocHandlers.delete(subdoc.guid);
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
};
|
|
315
|
-
this.getUniqueUpdateId = (update) => {
|
|
316
|
-
const clock = _nullishCoalesce(Y2.parseUpdateMeta(update).to.get(this.rootDoc.clientID), () => ( "-1"));
|
|
317
|
-
return this.rootDoc.clientID + ":" + clock;
|
|
318
|
-
};
|
|
319
|
-
this.updateDoc = (update, guid) => {
|
|
320
|
-
const canWrite = _nullishCoalesce(_optionalChain([this, 'access', _22 => _22.room, 'access', _23 => _23.getSelf, 'call', _24 => _24(), 'optionalAccess', _25 => _25.canWrite]), () => ( true));
|
|
321
|
-
if (canWrite) {
|
|
322
|
-
const updateId = this.getUniqueUpdateId(update);
|
|
323
|
-
this.pending.push(updateId);
|
|
324
|
-
this.room.updateYDoc(_jsbase64.Base64.fromUint8Array(update), guid);
|
|
325
|
-
this.emit("status", [this.getStatus()]);
|
|
326
|
-
}
|
|
327
|
-
};
|
|
328
|
-
this.fetchDoc = (vector, guid) => {
|
|
329
|
-
this.room.fetchYDoc(vector, guid);
|
|
330
|
-
};
|
|
331
|
-
this.createSubdocHandler = (subdoc) => {
|
|
332
|
-
if (this.subdocHandlers.has(subdoc.guid)) {
|
|
333
|
-
_optionalChain([this, 'access', _26 => _26.subdocHandlers, 'access', _27 => _27.get, 'call', _28 => _28(subdoc.guid), 'optionalAccess', _29 => _29.syncDoc, 'call', _30 => _30()]);
|
|
334
|
-
return;
|
|
335
|
-
}
|
|
336
|
-
const handler = new yDocHandler({
|
|
337
|
-
doc: subdoc,
|
|
338
|
-
isRoot: false,
|
|
339
|
-
updateDoc: this.updateDoc,
|
|
340
|
-
fetchDoc: this.fetchDoc
|
|
341
|
-
});
|
|
342
|
-
this.subdocHandlers.set(subdoc.guid, handler);
|
|
343
|
-
};
|
|
344
|
-
// attempt to load a subdoc of a given guid
|
|
345
|
-
this.loadSubdoc = (guid) => {
|
|
346
|
-
for (const subdoc of this.rootDoc.subdocs) {
|
|
347
|
-
if (subdoc.guid === guid) {
|
|
348
|
-
subdoc.load();
|
|
349
|
-
return true;
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
return false;
|
|
353
|
-
};
|
|
354
|
-
this.syncDoc = () => {
|
|
355
|
-
this.rootDocHandler.syncDoc();
|
|
356
|
-
for (const [_, handler] of this.subdocHandlers) {
|
|
357
|
-
handler.syncDoc();
|
|
358
|
-
}
|
|
359
|
-
};
|
|
311
|
+
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);;
|
|
360
312
|
this.rootDoc = doc;
|
|
361
313
|
this.room = room;
|
|
362
314
|
this.options = options;
|
|
@@ -364,7 +316,8 @@ var LiveblocksYjsProvider = class extends Observable {
|
|
|
364
316
|
doc,
|
|
365
317
|
isRoot: true,
|
|
366
318
|
updateDoc: this.updateDoc,
|
|
367
|
-
fetchDoc: this.fetchDoc
|
|
319
|
+
fetchDoc: this.fetchDoc,
|
|
320
|
+
useV2Updates: _nullishCoalesce(this.options.useV2Updates_experimental, () => ( false))
|
|
368
321
|
});
|
|
369
322
|
room[_core.kInternal].setYjsProvider(this);
|
|
370
323
|
this.awareness = new Awareness(this.rootDoc, this.room);
|
|
@@ -384,7 +337,7 @@ var LiveblocksYjsProvider = class extends Observable {
|
|
|
384
337
|
if (type === _core.ClientMsgCode.UPDATE_YDOC) {
|
|
385
338
|
return;
|
|
386
339
|
}
|
|
387
|
-
const { stateVector, update: updateStr, guid } = message;
|
|
340
|
+
const { stateVector, update: updateStr, guid, v2 } = message;
|
|
388
341
|
const canWrite = _nullishCoalesce(_optionalChain([this, 'access', _31 => _31.room, 'access', _32 => _32.getSelf, 'call', _33 => _33(), 'optionalAccess', _34 => _34.canWrite]), () => ( true));
|
|
389
342
|
const update = _jsbase64.Base64.toUint8Array(updateStr);
|
|
390
343
|
let foundPendingUpdate = false;
|
|
@@ -401,13 +354,15 @@ var LiveblocksYjsProvider = class extends Observable {
|
|
|
401
354
|
_optionalChain([this, 'access', _35 => _35.subdocHandlers, 'access', _36 => _36.get, 'call', _37 => _37(guid), 'optionalAccess', _38 => _38.handleServerUpdate, 'call', _39 => _39({
|
|
402
355
|
update,
|
|
403
356
|
stateVector,
|
|
404
|
-
readOnly: !canWrite
|
|
357
|
+
readOnly: !canWrite,
|
|
358
|
+
v2
|
|
405
359
|
})]);
|
|
406
360
|
} else {
|
|
407
361
|
this.rootDocHandler.handleServerUpdate({
|
|
408
362
|
update,
|
|
409
363
|
stateVector,
|
|
410
|
-
readOnly: !canWrite
|
|
364
|
+
readOnly: !canWrite,
|
|
365
|
+
v2
|
|
411
366
|
});
|
|
412
367
|
}
|
|
413
368
|
}
|
|
@@ -436,6 +391,79 @@ var LiveblocksYjsProvider = class extends Observable {
|
|
|
436
391
|
this.rootDoc.on("subdocs", this.handleSubdocs);
|
|
437
392
|
this.syncDoc();
|
|
438
393
|
}
|
|
394
|
+
__init14() {this.handleSubdocs = ({
|
|
395
|
+
loaded,
|
|
396
|
+
removed,
|
|
397
|
+
added
|
|
398
|
+
}) => {
|
|
399
|
+
loaded.forEach(this.createSubdocHandler);
|
|
400
|
+
if (this.options.autoloadSubdocs) {
|
|
401
|
+
for (const subdoc of added) {
|
|
402
|
+
if (!this.subdocHandlers.has(subdoc.guid)) {
|
|
403
|
+
subdoc.load();
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
for (const subdoc of removed) {
|
|
408
|
+
if (this.subdocHandlers.has(subdoc.guid)) {
|
|
409
|
+
_optionalChain([this, 'access', _43 => _43.subdocHandlers, 'access', _44 => _44.get, 'call', _45 => _45(subdoc.guid), 'optionalAccess', _46 => _46.destroy, 'call', _47 => _47()]);
|
|
410
|
+
this.subdocHandlers.delete(subdoc.guid);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}}
|
|
414
|
+
__init15() {this.getUniqueUpdateId = (update) => {
|
|
415
|
+
const clock = _nullishCoalesce(Y2.parseUpdateMeta(update).to.get(this.rootDoc.clientID), () => ( "-1"));
|
|
416
|
+
return this.rootDoc.clientID + ":" + clock;
|
|
417
|
+
}}
|
|
418
|
+
__init16() {this.updateDoc = (update, guid) => {
|
|
419
|
+
const canWrite = _nullishCoalesce(_optionalChain([this, 'access', _48 => _48.room, 'access', _49 => _49.getSelf, 'call', _50 => _50(), 'optionalAccess', _51 => _51.canWrite]), () => ( true));
|
|
420
|
+
if (canWrite) {
|
|
421
|
+
const updateId = this.getUniqueUpdateId(update);
|
|
422
|
+
this.pending.push(updateId);
|
|
423
|
+
this.room.updateYDoc(
|
|
424
|
+
_jsbase64.Base64.fromUint8Array(update),
|
|
425
|
+
guid,
|
|
426
|
+
this.useV2Updates
|
|
427
|
+
);
|
|
428
|
+
this.emit("status", [this.getStatus()]);
|
|
429
|
+
}
|
|
430
|
+
}}
|
|
431
|
+
__init17() {this.fetchDoc = (vector, guid) => {
|
|
432
|
+
this.room.fetchYDoc(vector, guid, this.useV2Updates);
|
|
433
|
+
}}
|
|
434
|
+
__init18() {this.createSubdocHandler = (subdoc) => {
|
|
435
|
+
if (this.subdocHandlers.has(subdoc.guid)) {
|
|
436
|
+
_optionalChain([this, 'access', _52 => _52.subdocHandlers, 'access', _53 => _53.get, 'call', _54 => _54(subdoc.guid), 'optionalAccess', _55 => _55.syncDoc, 'call', _56 => _56()]);
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
const handler = new yDocHandler({
|
|
440
|
+
doc: subdoc,
|
|
441
|
+
isRoot: false,
|
|
442
|
+
updateDoc: this.updateDoc,
|
|
443
|
+
fetchDoc: this.fetchDoc,
|
|
444
|
+
useV2Updates: _nullishCoalesce(this.options.useV2Updates_experimental, () => ( false))
|
|
445
|
+
});
|
|
446
|
+
this.subdocHandlers.set(subdoc.guid, handler);
|
|
447
|
+
}}
|
|
448
|
+
// attempt to load a subdoc of a given guid
|
|
449
|
+
__init19() {this.loadSubdoc = (guid) => {
|
|
450
|
+
for (const subdoc of this.rootDoc.subdocs) {
|
|
451
|
+
if (subdoc.guid === guid) {
|
|
452
|
+
subdoc.load();
|
|
453
|
+
return true;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return false;
|
|
457
|
+
}}
|
|
458
|
+
__init20() {this.syncDoc = () => {
|
|
459
|
+
this.rootDocHandler.syncDoc();
|
|
460
|
+
for (const [_, handler] of this.subdocHandlers) {
|
|
461
|
+
handler.syncDoc();
|
|
462
|
+
}
|
|
463
|
+
}}
|
|
464
|
+
get useV2Updates() {
|
|
465
|
+
return _nullishCoalesce(this.options.useV2Updates_experimental, () => ( false));
|
|
466
|
+
}
|
|
439
467
|
// The sync'd property is required by some provider implementations
|
|
440
468
|
get synced() {
|
|
441
469
|
return this.rootDocHandler.synced;
|
|
@@ -469,7 +497,7 @@ var LiveblocksYjsProvider = class extends Observable {
|
|
|
469
497
|
}
|
|
470
498
|
connect() {
|
|
471
499
|
}
|
|
472
|
-
};
|
|
500
|
+
}, _class3);
|
|
473
501
|
|
|
474
502
|
|
|
475
503
|
exports.LiveblocksYjsProvider = LiveblocksYjsProvider;
|
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/awareness.ts","../src/doc.ts","../src/version.ts"],"names":["Base64","create","IndexeddbPersistence","Y"],"mappings":";AAiBA,SAAS,eAAe,aAAa,iBAAiB;AACtD,SAAS,UAAAA,eAAc;;;ACJhB,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,MA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G,OAAO,KAAK,KAAK,gBAAgB;AAC9C,gBAAQ,QAAQ;AAAA,MAClB;AACA,WAAK,KAAK,UAAU,CAAC,KAAK,CAAC;AAC3B,WAAK,KAAK,QAAQ,CAAC,KAAK,CAAC;AACzB,WAAK,KAAK,UAAU,CAAC,KAAK,UAAU,CAAC,CAAC;AAAA,IACxC,CAAC;AACD,SAAK,QAAQ,GAAG,WAAW,KAAK,aAAa;AAC7C,SAAK,QAAQ;AAAA,EACf;AAAA;AAAA,EAkFA,IAAI,SAAkB;AACpB,WAAO,KAAK,eAAe;AAAA,EAC7B;AAAA,EAEO,YAA2B;AAChC,QAAI,CAAC,KAAK,QAAQ;AAChB,aAAO;AAAA,IACT;AACA,WAAO,KAAK,QAAQ,WAAW,IAAI,iBAAiB;AAAA,EACtD;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,EAEA,MAAM,mBAAkC;AACtC,QAAI,CAAC,KAAK,kBAAmB;AAC7B,WAAO,KAAK,kBAAkB,UAAU;AAAA,EAC1C;AAAA,EAEA,UAAiB;AACf,WAAO,KAAK;AAAA,EACd;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 type {\n BaseMetadata,\n DE,\n DM,\n DP,\n DS,\n DU,\n IYjsProvider,\n YjsSyncStatus,\n} from \"@liveblocks/core\";\nimport { ClientMsgCode, detectDupes, kInternal } from \"@liveblocks/core\";\nimport { Base64 } from \"js-base64\";\nimport { Observable } from \"lib0/observable\";\nimport { IndexeddbPersistence } from \"y-indexeddb\";\nimport * as Y from \"yjs\";\n\nimport { Awareness } from \"./awareness\";\nimport yDocHandler from \"./doc\";\nimport { PKG_FORMAT, PKG_NAME, PKG_VERSION } from \"./version\";\n\ndetectDupes(PKG_NAME, PKG_VERSION, PKG_FORMAT);\n\ntype ProviderOptions = {\n autoloadSubdocs?: boolean;\n offlineSupport_experimental?: boolean;\n};\n\nexport class LiveblocksYjsProvider<\n P extends JsonObject = DP,\n S extends LsonObject = DS,\n U extends BaseUserMeta = DU,\n E extends Json = DE,\n M extends BaseMetadata = DM,\n >\n extends Observable<unknown>\n implements IYjsProvider\n{\n private room: Room<P, S, U, E, M>;\n private rootDoc: Y.Doc;\n private options: ProviderOptions;\n private indexeddbProvider: IndexeddbPersistence | null = null;\n\n private unsubscribers: Array<() => void> = [];\n\n public awareness: Awareness<P, S, U, E, M>;\n\n public rootDocHandler: yDocHandler;\n public subdocHandlers: Map<string, yDocHandler> = new Map();\n\n private pending: string[] = [];\n\n constructor(\n room: Room<P, S, U, E, M>,\n doc: Y.Doc,\n options: ProviderOptions | undefined = {}\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\n // TODO: Display a warning if a YjsProvider is already attached to the room\n room[kInternal].setYjsProvider(this);\n\n // if we have a connectionId already during construction, use that\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 this.emit(\"status\", [this.getStatus()]);\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: updateStr, guid } = message;\n const canWrite = this.room.getSelf()?.canWrite ?? true;\n const update = Base64.toUint8Array(updateStr);\n let foundPendingUpdate = false;\n const updateId = this.getUniqueUpdateId(update);\n this.pending = this.pending.filter((pendingUpdate) => {\n if (pendingUpdate === updateId) {\n foundPendingUpdate = true;\n return false;\n }\n return true;\n });\n // if we found this update in our queue, we don't need to apply it\n if (!foundPendingUpdate) {\n // find the right doc and update\n if (guid !== undefined) {\n this.subdocHandlers.get(guid)?.handleServerUpdate({\n update,\n stateVector,\n readOnly: !canWrite,\n });\n } else {\n this.rootDocHandler.handleServerUpdate({\n update,\n stateVector,\n readOnly: !canWrite,\n });\n }\n }\n\n // notify any listeners that the status has changed\n this.emit(\"status\", [this.getStatus()]);\n })\n );\n\n if (options.offlineSupport_experimental) {\n this.indexeddbProvider = new IndexeddbPersistence(room.id, this.rootDoc);\n const onIndexedDbSync = () => {\n this.rootDocHandler.synced = true;\n };\n this.indexeddbProvider.on(\"synced\", onIndexedDbSync);\n\n this.unsubscribers.push(() => {\n this.indexeddbProvider?.off(\"synced\", onIndexedDbSync);\n });\n }\n\n // different consumers listen to sync and synced\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 this.emit(\"status\", [this.getStatus()]);\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: Set<Y.Doc>;\n removed: Set<Y.Doc>;\n added: Set<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 getUniqueUpdateId = (update: Uint8Array) => {\n const clock =\n Y.parseUpdateMeta(update).to.get(this.rootDoc.clientID) ?? \"-1\";\n return this.rootDoc.clientID + \":\" + clock;\n };\n\n private updateDoc = (update: Uint8Array, guid?: string) => {\n const canWrite = this.room.getSelf()?.canWrite ?? true;\n if (canWrite) {\n const updateId = this.getUniqueUpdateId(update);\n this.pending.push(updateId);\n this.room.updateYDoc(Base64.fromUint8Array(update), guid);\n this.emit(\"status\", [this.getStatus()]);\n }\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 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 public getStatus(): YjsSyncStatus {\n if (!this.synced) {\n return \"loading\";\n }\n return this.pending.length === 0 ? \"synchronized\" : \"synchronizing\";\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 async clearOfflineData(): Promise<void> {\n if (!this.indexeddbProvider) return;\n return this.indexeddbProvider.clearData();\n }\n\n getYDoc(): Y.Doc {\n return this.rootDoc;\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 K,V\n * @param {Map<K,V>} m\n * @return {Map<K,V>}\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 {Map<any, any>} MAP\n * @template {MAP extends Map<any,infer V> ? function():V : unknown} CF\n * @param {MAP} map\n * @param {MAP extends Map<infer K,any> ? K : unknown} key\n * @param {CF} createT\n * @return {ReturnType<CF>}\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/**\n * @template {ArrayLike<any>} ARR\n * @template {function(ARR extends ArrayLike<infer T> ? T : never, number, ARR):any} MAPPER\n * @param {ARR} arr\n * @param {MAPPER} mapper\n * @return {Array<MAPPER extends function(...any): infer M ? M : never>}\n */\nexport const map = (arr, mapper) => {\n /**\n * @type {Array<any>}\n */\n const res = Array(arr.length)\n for (let i = 0; i < arr.length; i++) {\n res[i] = mapper(/** @type {any} */ (arr[i]), i, /** @type {any} */ (arr))\n }\n return /** @type {any} */ (res)\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 * @experimental\n *\n * This is basically a (better typed) duplicate of Observable, which will replace Observable in the\n * next release.\n *\n * @template {{[key in keyof EVENTS]: function(...any):void}} EVENTS\n */\nexport class ObservableV2 {\n constructor () {\n /**\n * Some desc.\n * @type {Map<string, Set<any>>}\n */\n this._observers = map.create()\n }\n\n /**\n * @template {keyof EVENTS & string} NAME\n * @param {NAME} name\n * @param {EVENTS[NAME]} f\n */\n on (name, f) {\n map.setIfUndefined(this._observers, /** @type {string} */ (name), set.create).add(f)\n return f\n }\n\n /**\n * @template {keyof EVENTS & string} NAME\n * @param {NAME} name\n * @param {EVENTS[NAME]} f\n */\n once (name, f) {\n /**\n * @param {...any} args\n */\n const _f = (...args) => {\n this.off(name, /** @type {any} */ (_f))\n f(...args)\n }\n this.on(name, /** @type {any} */ (_f))\n }\n\n /**\n * @template {keyof EVENTS & string} NAME\n * @param {NAME} name\n * @param {EVENTS[NAME]} 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 * @template {keyof EVENTS & string} NAME\n * @param {NAME} name The event name.\n * @param {Parameters<EVENTS[NAME]>} 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\n/* c8 ignore start */\n/**\n * Handles named events.\n *\n * @deprecated\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/* c8 ignore end */\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 BaseMetadata,\n BaseUserMeta,\n Json,\n JsonObject,\n LsonObject,\n Room,\n User,\n} from \"@liveblocks/client\";\nimport { Observable } from \"lib0/observable\";\nimport type * as Y from \"yjs\";\n\nconst Y_PRESENCE_KEY = \"__yjs\";\nconst Y_PRESENCE_ID_KEY = \"__yjs_clientid\";\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<\n P extends JsonObject,\n S extends LsonObject,\n U extends BaseUserMeta,\n E extends Json,\n M extends BaseMetadata,\n> extends Observable<unknown> {\n private room: Room<P, S, U, E, M>;\n public doc: Y.Doc;\n public states: Map<number, unknown> = new Map();\n // used to map liveblock's ActorId to Yjs ClientID, both unique numbers representing a client\n public actorToClientMap: Map<number, number> = 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(doc: Y.Doc, room: Room<P, S, U, E, M>) {\n super();\n this.doc = doc;\n this.room = room;\n // Add the clientId to presence so we can map it to connectionId later\n this.room.updatePresence({\n [Y_PRESENCE_ID_KEY]: this.doc.clientID,\n } as unknown as Partial<P>);\n this.othersUnsub = this.room.events.others.subscribe((event) => {\n let updates:\n | { added: number[]; updated: number[]; removed: number[] }\n | undefined;\n\n // When others are changed, we emit an event that contains arrays added/updated/removed.\n if (event.type === \"leave\") {\n const targetClientId = this.actorToClientMap.get(\n event.user.connectionId\n );\n if (targetClientId !== undefined) {\n updates = { added: [], updated: [], removed: [targetClientId] };\n }\n // rebuild after the user leaves so we can get the ID of the user who left\n this.rebuildActorToClientMap(event.others);\n }\n if (event.type === \"enter\" || event.type === \"update\") {\n this.rebuildActorToClientMap(event.others);\n const targetClientId = this.actorToClientMap.get(\n event.user.connectionId\n );\n if (targetClientId !== undefined) {\n updates = {\n added: event.type === \"enter\" ? [targetClientId] : [],\n updated: event.type === \"update\" ? [targetClientId] : [],\n removed: [],\n };\n }\n }\n if (event.type === \"reset\") {\n this.rebuildActorToClientMap(event.others);\n }\n if (updates !== undefined) {\n this.emit(\"change\", [updates, \"presence\"]);\n this.emit(\"update\", [updates, \"presence\"]);\n }\n });\n }\n\n rebuildActorToClientMap(\n others: readonly User<JsonObject, BaseUserMeta>[]\n ): void {\n this.actorToClientMap.clear();\n others.forEach((user) => {\n if (user.presence[Y_PRESENCE_ID_KEY] !== undefined) {\n this.actorToClientMap.set(\n user.connectionId,\n user.presence[Y_PRESENCE_ID_KEY] as number\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;\n if (state === null) {\n if (presence === undefined) {\n // if presence is already undefined, we don't need to change anything here\n return;\n }\n this.room.updatePresence({ ...presence, [Y_PRESENCE_KEY]: null });\n this.emit(\"update\", [\n { added: [], updated: [], removed: [this.doc.clientID] },\n \"local\",\n ]);\n return;\n }\n // if presence was undefined, it's added, if not, it's updated\n const yPresence = presence?.[Y_PRESENCE_KEY];\n const added = yPresence === undefined ? [this.doc.clientID] : [];\n const updated = yPresence === undefined ? [] : [this.doc.clientID];\n this.room.updatePresence({\n [Y_PRESENCE_KEY]: {\n ...((yPresence as JsonObject) || {}),\n ...(state || {}),\n },\n } as unknown as Partial<P>);\n this.emit(\"update\", [{ added, updated, removed: [] }, \"local\"]);\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 [Y_PRESENCE_KEY]: { ...((presence as JsonObject) || {}), ...update },\n } as unknown as Partial<P>);\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>, otherUser) => {\n const otherPresence = otherUser.presence[Y_PRESENCE_KEY];\n const otherClientId = otherUser.presence[Y_PRESENCE_ID_KEY] as\n | number\n | undefined;\n if (otherPresence !== undefined && otherClientId !== undefined) {\n // set states of map clientId to yjs presence\n acc.set(otherClientId, otherPresence || {});\n }\n return acc;\n }, new Map());\n\n // add this client's yjs presence to states (local client not represented in others)\n const localPresence = this.room.getSelf()?.presence[Y_PRESENCE_KEY];\n if (localPresence !== undefined) {\n states.set(this.doc.clientID, localPresence);\n }\n return states;\n }\n}\n","import { Base64 } from \"js-base64\";\nimport { Observable } from \"lib0/observable\";\nimport { IndexeddbPersistence } from \"y-indexeddb\";\nimport * as Y from \"yjs\";\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: Uint8Array) => 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: Uint8Array, 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: Uint8Array) => {\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 readOnly,\n }: {\n update: Uint8Array;\n stateVector: string | null;\n readOnly: boolean;\n }): void => {\n // apply update from the server\n Y.applyUpdate(this.doc, update, \"backend\");\n // if this update is the result of a fetch, the state vector is included\n if (stateVector) {\n if (!readOnly) {\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(localUpdate);\n } catch (e) {\n // something went wrong encoding local state to send to the server\n console.warn(e);\n }\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 = (\n update: Uint8Array,\n origin: string | IndexeddbPersistence\n ) => {\n // don't send updates from indexedb, those will get handled by sync\n const isFromLocal = origin instanceof IndexeddbPersistence;\n if (origin !== \"backend\" && !isFromLocal) {\n this.updateRoomDoc(update);\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"]}
|
|
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":["Base64","create","IndexeddbPersistence","Y","applyUpdate"],"mappings":";AAiBA,SAAS,eAAe,aAAa,iBAAiB;AACtD,SAAS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,iBAAiB;AAAA,EACtD;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,EAEA,MAAM,mBAAkC;AACtC,QAAI,CAAC,KAAK,kBAAmB;AAC7B,WAAO,KAAK,kBAAkB,UAAU;AAAA,EAC1C;AAAA,EAEA,UAAiB;AACf,WAAO,KAAK;AAAA,EACd;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 type {\n BaseMetadata,\n DE,\n DM,\n DP,\n DS,\n DU,\n IYjsProvider,\n YjsSyncStatus,\n} from \"@liveblocks/core\";\nimport { ClientMsgCode, detectDupes, kInternal } from \"@liveblocks/core\";\nimport { Base64 } from \"js-base64\";\nimport { Observable } from \"lib0/observable\";\nimport { IndexeddbPersistence } from \"y-indexeddb\";\nimport * as Y from \"yjs\";\n\nimport { Awareness } from \"./awareness\";\nimport yDocHandler from \"./doc\";\nimport { PKG_FORMAT, PKG_NAME, PKG_VERSION } from \"./version\";\n\ndetectDupes(PKG_NAME, PKG_VERSION, PKG_FORMAT);\n\ntype ProviderOptions = {\n autoloadSubdocs?: boolean;\n offlineSupport_experimental?: boolean;\n useV2Updates_experimental?: boolean;\n};\n\nexport class LiveblocksYjsProvider<\n P extends JsonObject = DP,\n S extends LsonObject = DS,\n U extends BaseUserMeta = DU,\n E extends Json = DE,\n M extends BaseMetadata = DM,\n >\n extends Observable<unknown>\n implements IYjsProvider\n{\n private room: Room<P, S, U, E, M>;\n private rootDoc: Y.Doc;\n private options: ProviderOptions;\n private indexeddbProvider: IndexeddbPersistence | null = null;\n\n private unsubscribers: Array<() => void> = [];\n\n public awareness: Awareness<P, S, U, E, M>;\n\n public rootDocHandler: yDocHandler;\n public subdocHandlers: Map<string, yDocHandler> = new Map();\n\n private pending: string[] = [];\n\n constructor(\n room: Room<P, S, U, E, M>,\n doc: Y.Doc,\n options: ProviderOptions | undefined = {}\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 useV2Updates: this.options.useV2Updates_experimental ?? false,\n });\n\n // TODO: Display a warning if a YjsProvider is already attached to the room\n room[kInternal].setYjsProvider(this);\n\n // if we have a connectionId already during construction, use that\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 this.emit(\"status\", [this.getStatus()]);\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: updateStr, guid, v2 } = message;\n const canWrite = this.room.getSelf()?.canWrite ?? true;\n const update = Base64.toUint8Array(updateStr);\n let foundPendingUpdate = false;\n const updateId = this.getUniqueUpdateId(update);\n this.pending = this.pending.filter((pendingUpdate) => {\n if (pendingUpdate === updateId) {\n foundPendingUpdate = true;\n return false;\n }\n return true;\n });\n // if we found this update in our queue, we don't need to apply it\n if (!foundPendingUpdate) {\n // find the right doc and update\n if (guid !== undefined) {\n this.subdocHandlers.get(guid)?.handleServerUpdate({\n update,\n stateVector,\n readOnly: !canWrite,\n v2,\n });\n } else {\n this.rootDocHandler.handleServerUpdate({\n update,\n stateVector,\n readOnly: !canWrite,\n v2,\n });\n }\n }\n\n // notify any listeners that the status has changed\n this.emit(\"status\", [this.getStatus()]);\n })\n );\n\n if (options.offlineSupport_experimental) {\n this.indexeddbProvider = new IndexeddbPersistence(room.id, this.rootDoc);\n const onIndexedDbSync = () => {\n this.rootDocHandler.synced = true;\n };\n this.indexeddbProvider.on(\"synced\", onIndexedDbSync);\n\n this.unsubscribers.push(() => {\n this.indexeddbProvider?.off(\"synced\", onIndexedDbSync);\n });\n }\n\n // different consumers listen to sync and synced\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 this.emit(\"status\", [this.getStatus()]);\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: Set<Y.Doc>;\n removed: Set<Y.Doc>;\n added: Set<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 getUniqueUpdateId = (update: Uint8Array) => {\n const clock =\n Y.parseUpdateMeta(update).to.get(this.rootDoc.clientID) ?? \"-1\";\n return this.rootDoc.clientID + \":\" + clock;\n };\n\n private updateDoc = (update: Uint8Array, guid?: string) => {\n const canWrite = this.room.getSelf()?.canWrite ?? true;\n if (canWrite) {\n const updateId = this.getUniqueUpdateId(update);\n this.pending.push(updateId);\n this.room.updateYDoc(\n Base64.fromUint8Array(update),\n guid,\n this.useV2Updates\n );\n this.emit(\"status\", [this.getStatus()]);\n }\n };\n\n private fetchDoc = (vector: string, guid?: string) => {\n this.room.fetchYDoc(vector, guid, this.useV2Updates);\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 useV2Updates: this.options.useV2Updates_experimental ?? false,\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 this.rootDocHandler.syncDoc();\n for (const [_, handler] of this.subdocHandlers) {\n handler.syncDoc();\n }\n };\n\n get useV2Updates(): boolean {\n return this.options.useV2Updates_experimental ?? false;\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 public getStatus(): YjsSyncStatus {\n if (!this.synced) {\n return \"loading\";\n }\n return this.pending.length === 0 ? \"synchronized\" : \"synchronizing\";\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 async clearOfflineData(): Promise<void> {\n if (!this.indexeddbProvider) return;\n return this.indexeddbProvider.clearData();\n }\n\n getYDoc(): Y.Doc {\n return this.rootDoc;\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 K,V\n * @param {Map<K,V>} m\n * @return {Map<K,V>}\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 {Map<any, any>} MAP\n * @template {MAP extends Map<any,infer V> ? function():V : unknown} CF\n * @param {MAP} map\n * @param {MAP extends Map<infer K,any> ? K : unknown} key\n * @param {CF} createT\n * @return {ReturnType<CF>}\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/**\n * @template {ArrayLike<any>} ARR\n * @template {function(ARR extends ArrayLike<infer T> ? T : never, number, ARR):any} MAPPER\n * @param {ARR} arr\n * @param {MAPPER} mapper\n * @return {Array<MAPPER extends function(...any): infer M ? M : never>}\n */\nexport const map = (arr, mapper) => {\n /**\n * @type {Array<any>}\n */\n const res = Array(arr.length)\n for (let i = 0; i < arr.length; i++) {\n res[i] = mapper(/** @type {any} */ (arr[i]), i, /** @type {any} */ (arr))\n }\n return /** @type {any} */ (res)\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 * @experimental\n *\n * This is basically a (better typed) duplicate of Observable, which will replace Observable in the\n * next release.\n *\n * @template {{[key in keyof EVENTS]: function(...any):void}} EVENTS\n */\nexport class ObservableV2 {\n constructor () {\n /**\n * Some desc.\n * @type {Map<string, Set<any>>}\n */\n this._observers = map.create()\n }\n\n /**\n * @template {keyof EVENTS & string} NAME\n * @param {NAME} name\n * @param {EVENTS[NAME]} f\n */\n on (name, f) {\n map.setIfUndefined(this._observers, /** @type {string} */ (name), set.create).add(f)\n return f\n }\n\n /**\n * @template {keyof EVENTS & string} NAME\n * @param {NAME} name\n * @param {EVENTS[NAME]} f\n */\n once (name, f) {\n /**\n * @param {...any} args\n */\n const _f = (...args) => {\n this.off(name, /** @type {any} */ (_f))\n f(...args)\n }\n this.on(name, /** @type {any} */ (_f))\n }\n\n /**\n * @template {keyof EVENTS & string} NAME\n * @param {NAME} name\n * @param {EVENTS[NAME]} 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 * @template {keyof EVENTS & string} NAME\n * @param {NAME} name The event name.\n * @param {Parameters<EVENTS[NAME]>} 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\n/* c8 ignore start */\n/**\n * Handles named events.\n *\n * @deprecated\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/* c8 ignore end */\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 BaseMetadata,\n BaseUserMeta,\n Json,\n JsonObject,\n LsonObject,\n Room,\n User,\n} from \"@liveblocks/client\";\nimport { Observable } from \"lib0/observable\";\nimport type * as Y from \"yjs\";\n\nconst Y_PRESENCE_KEY = \"__yjs\";\nconst Y_PRESENCE_ID_KEY = \"__yjs_clientid\";\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<\n P extends JsonObject,\n S extends LsonObject,\n U extends BaseUserMeta,\n E extends Json,\n M extends BaseMetadata,\n> extends Observable<unknown> {\n private room: Room<P, S, U, E, M>;\n public doc: Y.Doc;\n public states: Map<number, unknown> = new Map();\n // used to map liveblock's ActorId to Yjs ClientID, both unique numbers representing a client\n public actorToClientMap: Map<number, number> = 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(doc: Y.Doc, room: Room<P, S, U, E, M>) {\n super();\n this.doc = doc;\n this.room = room;\n // Add the clientId to presence so we can map it to connectionId later\n this.room.updatePresence({\n [Y_PRESENCE_ID_KEY]: this.doc.clientID,\n } as unknown as Partial<P>);\n this.othersUnsub = this.room.events.others.subscribe((event) => {\n let updates:\n | { added: number[]; updated: number[]; removed: number[] }\n | undefined;\n\n // When others are changed, we emit an event that contains arrays added/updated/removed.\n if (event.type === \"leave\") {\n const targetClientId = this.actorToClientMap.get(\n event.user.connectionId\n );\n if (targetClientId !== undefined) {\n updates = { added: [], updated: [], removed: [targetClientId] };\n }\n // rebuild after the user leaves so we can get the ID of the user who left\n this.rebuildActorToClientMap(event.others);\n }\n if (event.type === \"enter\" || event.type === \"update\") {\n this.rebuildActorToClientMap(event.others);\n const targetClientId = this.actorToClientMap.get(\n event.user.connectionId\n );\n if (targetClientId !== undefined) {\n updates = {\n added: event.type === \"enter\" ? [targetClientId] : [],\n updated: event.type === \"update\" ? [targetClientId] : [],\n removed: [],\n };\n }\n }\n if (event.type === \"reset\") {\n this.rebuildActorToClientMap(event.others);\n }\n if (updates !== undefined) {\n this.emit(\"change\", [updates, \"presence\"]);\n this.emit(\"update\", [updates, \"presence\"]);\n }\n });\n }\n\n rebuildActorToClientMap(\n others: readonly User<JsonObject, BaseUserMeta>[]\n ): void {\n this.actorToClientMap.clear();\n others.forEach((user) => {\n if (user.presence[Y_PRESENCE_ID_KEY] !== undefined) {\n this.actorToClientMap.set(\n user.connectionId,\n user.presence[Y_PRESENCE_ID_KEY] as number\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;\n if (state === null) {\n if (presence === undefined) {\n // if presence is already undefined, we don't need to change anything here\n return;\n }\n this.room.updatePresence({ ...presence, [Y_PRESENCE_KEY]: null });\n this.emit(\"update\", [\n { added: [], updated: [], removed: [this.doc.clientID] },\n \"local\",\n ]);\n return;\n }\n // if presence was undefined, it's added, if not, it's updated\n const yPresence = presence?.[Y_PRESENCE_KEY];\n const added = yPresence === undefined ? [this.doc.clientID] : [];\n const updated = yPresence === undefined ? [] : [this.doc.clientID];\n this.room.updatePresence({\n [Y_PRESENCE_KEY]: {\n ...((yPresence as JsonObject) || {}),\n ...(state || {}),\n },\n } as unknown as Partial<P>);\n this.emit(\"update\", [{ added, updated, removed: [] }, \"local\"]);\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 [Y_PRESENCE_KEY]: { ...((presence as JsonObject) || {}), ...update },\n } as unknown as Partial<P>);\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>, otherUser) => {\n const otherPresence = otherUser.presence[Y_PRESENCE_KEY];\n const otherClientId = otherUser.presence[Y_PRESENCE_ID_KEY] as\n | number\n | undefined;\n if (otherPresence !== undefined && otherClientId !== undefined) {\n // set states of map clientId to yjs presence\n acc.set(otherClientId, otherPresence || {});\n }\n return acc;\n }, new Map());\n\n // add this client's yjs presence to states (local client not represented in others)\n const localPresence = this.room.getSelf()?.presence[Y_PRESENCE_KEY];\n if (localPresence !== undefined) {\n states.set(this.doc.clientID, localPresence);\n }\n return states;\n }\n}\n","import { Base64 } from \"js-base64\";\nimport { Observable } from \"lib0/observable\";\nimport { IndexeddbPersistence } from \"y-indexeddb\";\nimport * as Y from \"yjs\";\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: Uint8Array) => void;\n private fetchRoomDoc: (vector: string) => void;\n private useV2Updates: boolean;\n\n constructor({\n doc,\n isRoot,\n updateDoc,\n fetchDoc,\n useV2Updates,\n }: {\n doc: Y.Doc;\n isRoot: boolean;\n updateDoc: (update: Uint8Array, guid?: string) => void;\n fetchDoc: (vector: string, guid?: string) => void;\n useV2Updates: boolean;\n }) {\n super();\n this.doc = doc;\n this.useV2Updates = useV2Updates;\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: Uint8Array) => {\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 readOnly,\n v2,\n }: {\n update: Uint8Array;\n stateVector: string | null;\n readOnly: boolean;\n v2?: boolean;\n }): void => {\n // apply update from the server, updates from the server can be v1 or v2\n const applyUpdate = v2 ? Y.applyUpdateV2 : Y.applyUpdate;\n applyUpdate(this.doc, update, \"backend\");\n // if this update is the result of a fetch, the state vector is included\n if (stateVector) {\n if (!readOnly) {\n // Use server state to calculate a diff and send it\n try {\n // send v1 or v2update according to client option\n const encodeUpdate = this.useV2Updates\n ? Y.encodeStateAsUpdateV2\n : Y.encodeStateAsUpdate;\n const localUpdate = encodeUpdate(\n this.doc,\n Base64.toUint8Array(stateVector)\n );\n this.updateRoomDoc(localUpdate);\n } catch (e) {\n // something went wrong encoding local state to send to the server\n console.warn(e);\n }\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 = (\n update: Uint8Array,\n origin: string | IndexeddbPersistence\n ) => {\n // don't send updates from indexedb, those will get handled by sync\n const isFromLocal = origin instanceof IndexeddbPersistence;\n if (origin !== \"backend\" && !isFromLocal) {\n this.updateRoomDoc(update);\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
|
@@ -80,17 +80,20 @@ import * as Y2 from "yjs";
|
|
|
80
80
|
var Y_PRESENCE_KEY = "__yjs";
|
|
81
81
|
var Y_PRESENCE_ID_KEY = "__yjs_clientid";
|
|
82
82
|
var Awareness = class extends Observable {
|
|
83
|
+
room;
|
|
84
|
+
doc;
|
|
85
|
+
states = /* @__PURE__ */ new Map();
|
|
86
|
+
// used to map liveblock's ActorId to Yjs ClientID, both unique numbers representing a client
|
|
87
|
+
actorToClientMap = /* @__PURE__ */ new Map();
|
|
88
|
+
// Meta is used to keep track and timeout users who disconnect. Liveblocks provides this for us, so we don't need to
|
|
89
|
+
// manage it here. Unfortunately, it's expected to exist by various integrations, so it's an empty map.
|
|
90
|
+
meta = /* @__PURE__ */ new Map();
|
|
91
|
+
// _checkInterval this would hold a timer to remove users, but Liveblock's presence already handles this
|
|
92
|
+
// unfortunately it's typed by various integrations
|
|
93
|
+
_checkInterval = 0;
|
|
94
|
+
othersUnsub;
|
|
83
95
|
constructor(doc, room) {
|
|
84
96
|
super();
|
|
85
|
-
this.states = /* @__PURE__ */ new Map();
|
|
86
|
-
// used to map liveblock's ActorId to Yjs ClientID, both unique numbers representing a client
|
|
87
|
-
this.actorToClientMap = /* @__PURE__ */ new Map();
|
|
88
|
-
// Meta is used to keep track and timeout users who disconnect. Liveblocks provides this for us, so we don't need to
|
|
89
|
-
// manage it here. Unfortunately, it's expected to exist by various integrations, so it's an empty map.
|
|
90
|
-
this.meta = /* @__PURE__ */ new Map();
|
|
91
|
-
// _checkInterval this would hold a timer to remove users, but Liveblock's presence already handles this
|
|
92
|
-
// unfortunately it's typed by various integrations
|
|
93
|
-
this._checkInterval = 0;
|
|
94
97
|
this.doc = doc;
|
|
95
98
|
this.room = room;
|
|
96
99
|
this.room.updatePresence({
|
|
@@ -208,48 +211,22 @@ import { Base64 } from "js-base64";
|
|
|
208
211
|
import { IndexeddbPersistence } from "y-indexeddb";
|
|
209
212
|
import * as Y from "yjs";
|
|
210
213
|
var yDocHandler = class extends Observable {
|
|
214
|
+
unsubscribers = [];
|
|
215
|
+
_synced = false;
|
|
216
|
+
doc;
|
|
217
|
+
updateRoomDoc;
|
|
218
|
+
fetchRoomDoc;
|
|
219
|
+
useV2Updates;
|
|
211
220
|
constructor({
|
|
212
221
|
doc,
|
|
213
222
|
isRoot,
|
|
214
223
|
updateDoc,
|
|
215
|
-
fetchDoc
|
|
224
|
+
fetchDoc,
|
|
225
|
+
useV2Updates
|
|
216
226
|
}) {
|
|
217
227
|
super();
|
|
218
|
-
this.unsubscribers = [];
|
|
219
|
-
this._synced = false;
|
|
220
|
-
this.handleServerUpdate = ({
|
|
221
|
-
update,
|
|
222
|
-
stateVector,
|
|
223
|
-
readOnly
|
|
224
|
-
}) => {
|
|
225
|
-
Y.applyUpdate(this.doc, update, "backend");
|
|
226
|
-
if (stateVector) {
|
|
227
|
-
if (!readOnly) {
|
|
228
|
-
try {
|
|
229
|
-
const localUpdate = Y.encodeStateAsUpdate(
|
|
230
|
-
this.doc,
|
|
231
|
-
Base64.toUint8Array(stateVector)
|
|
232
|
-
);
|
|
233
|
-
this.updateRoomDoc(localUpdate);
|
|
234
|
-
} catch (e) {
|
|
235
|
-
console.warn(e);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
this.synced = true;
|
|
239
|
-
}
|
|
240
|
-
};
|
|
241
|
-
this.syncDoc = () => {
|
|
242
|
-
this.synced = false;
|
|
243
|
-
const encodedVector = Base64.fromUint8Array(Y.encodeStateVector(this.doc));
|
|
244
|
-
this.fetchRoomDoc(encodedVector);
|
|
245
|
-
};
|
|
246
|
-
this.updateHandler = (update, origin) => {
|
|
247
|
-
const isFromLocal = origin instanceof IndexeddbPersistence;
|
|
248
|
-
if (origin !== "backend" && !isFromLocal) {
|
|
249
|
-
this.updateRoomDoc(update);
|
|
250
|
-
}
|
|
251
|
-
};
|
|
252
228
|
this.doc = doc;
|
|
229
|
+
this.useV2Updates = useV2Updates;
|
|
253
230
|
this.doc.on("update", this.updateHandler);
|
|
254
231
|
this.updateRoomDoc = (update) => {
|
|
255
232
|
updateDoc(update, isRoot ? void 0 : this.doc.guid);
|
|
@@ -259,6 +236,35 @@ var yDocHandler = class extends Observable {
|
|
|
259
236
|
};
|
|
260
237
|
this.syncDoc();
|
|
261
238
|
}
|
|
239
|
+
handleServerUpdate = ({
|
|
240
|
+
update,
|
|
241
|
+
stateVector,
|
|
242
|
+
readOnly,
|
|
243
|
+
v2
|
|
244
|
+
}) => {
|
|
245
|
+
const applyUpdate2 = v2 ? Y.applyUpdateV2 : Y.applyUpdate;
|
|
246
|
+
applyUpdate2(this.doc, update, "backend");
|
|
247
|
+
if (stateVector) {
|
|
248
|
+
if (!readOnly) {
|
|
249
|
+
try {
|
|
250
|
+
const encodeUpdate = this.useV2Updates ? Y.encodeStateAsUpdateV2 : Y.encodeStateAsUpdate;
|
|
251
|
+
const localUpdate = encodeUpdate(
|
|
252
|
+
this.doc,
|
|
253
|
+
Base64.toUint8Array(stateVector)
|
|
254
|
+
);
|
|
255
|
+
this.updateRoomDoc(localUpdate);
|
|
256
|
+
} catch (e) {
|
|
257
|
+
console.warn(e);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
this.synced = true;
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
syncDoc = () => {
|
|
264
|
+
this.synced = false;
|
|
265
|
+
const encodedVector = Base64.fromUint8Array(Y.encodeStateVector(this.doc));
|
|
266
|
+
this.fetchRoomDoc(encodedVector);
|
|
267
|
+
};
|
|
262
268
|
// The sync'd property is required by some provider implementations
|
|
263
269
|
get synced() {
|
|
264
270
|
return this._synced;
|
|
@@ -270,6 +276,12 @@ var yDocHandler = class extends Observable {
|
|
|
270
276
|
this.emit("sync", [state]);
|
|
271
277
|
}
|
|
272
278
|
}
|
|
279
|
+
updateHandler = (update, origin) => {
|
|
280
|
+
const isFromLocal = origin instanceof IndexeddbPersistence;
|
|
281
|
+
if (origin !== "backend" && !isFromLocal) {
|
|
282
|
+
this.updateRoomDoc(update);
|
|
283
|
+
}
|
|
284
|
+
};
|
|
273
285
|
destroy() {
|
|
274
286
|
this.doc.off("update", this.updateHandler);
|
|
275
287
|
this.unsubscribers.forEach((unsub) => unsub());
|
|
@@ -280,83 +292,23 @@ var yDocHandler = class extends Observable {
|
|
|
280
292
|
|
|
281
293
|
// src/version.ts
|
|
282
294
|
var PKG_NAME = "@liveblocks/yjs";
|
|
283
|
-
var PKG_VERSION = "2.
|
|
295
|
+
var PKG_VERSION = "2.14.0-v2encoding";
|
|
284
296
|
var PKG_FORMAT = "esm";
|
|
285
297
|
|
|
286
298
|
// src/index.ts
|
|
287
299
|
detectDupes(PKG_NAME, PKG_VERSION, PKG_FORMAT);
|
|
288
300
|
var LiveblocksYjsProvider = class extends Observable {
|
|
301
|
+
room;
|
|
302
|
+
rootDoc;
|
|
303
|
+
options;
|
|
304
|
+
indexeddbProvider = null;
|
|
305
|
+
unsubscribers = [];
|
|
306
|
+
awareness;
|
|
307
|
+
rootDocHandler;
|
|
308
|
+
subdocHandlers = /* @__PURE__ */ new Map();
|
|
309
|
+
pending = [];
|
|
289
310
|
constructor(room, doc, options = {}) {
|
|
290
311
|
super();
|
|
291
|
-
this.indexeddbProvider = null;
|
|
292
|
-
this.unsubscribers = [];
|
|
293
|
-
this.subdocHandlers = /* @__PURE__ */ new Map();
|
|
294
|
-
this.pending = [];
|
|
295
|
-
this.handleSubdocs = ({
|
|
296
|
-
loaded,
|
|
297
|
-
removed,
|
|
298
|
-
added
|
|
299
|
-
}) => {
|
|
300
|
-
loaded.forEach(this.createSubdocHandler);
|
|
301
|
-
if (this.options.autoloadSubdocs) {
|
|
302
|
-
for (const subdoc of added) {
|
|
303
|
-
if (!this.subdocHandlers.has(subdoc.guid)) {
|
|
304
|
-
subdoc.load();
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
for (const subdoc of removed) {
|
|
309
|
-
if (this.subdocHandlers.has(subdoc.guid)) {
|
|
310
|
-
this.subdocHandlers.get(subdoc.guid)?.destroy();
|
|
311
|
-
this.subdocHandlers.delete(subdoc.guid);
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
};
|
|
315
|
-
this.getUniqueUpdateId = (update) => {
|
|
316
|
-
const clock = Y2.parseUpdateMeta(update).to.get(this.rootDoc.clientID) ?? "-1";
|
|
317
|
-
return this.rootDoc.clientID + ":" + clock;
|
|
318
|
-
};
|
|
319
|
-
this.updateDoc = (update, guid) => {
|
|
320
|
-
const canWrite = this.room.getSelf()?.canWrite ?? true;
|
|
321
|
-
if (canWrite) {
|
|
322
|
-
const updateId = this.getUniqueUpdateId(update);
|
|
323
|
-
this.pending.push(updateId);
|
|
324
|
-
this.room.updateYDoc(Base642.fromUint8Array(update), guid);
|
|
325
|
-
this.emit("status", [this.getStatus()]);
|
|
326
|
-
}
|
|
327
|
-
};
|
|
328
|
-
this.fetchDoc = (vector, guid) => {
|
|
329
|
-
this.room.fetchYDoc(vector, guid);
|
|
330
|
-
};
|
|
331
|
-
this.createSubdocHandler = (subdoc) => {
|
|
332
|
-
if (this.subdocHandlers.has(subdoc.guid)) {
|
|
333
|
-
this.subdocHandlers.get(subdoc.guid)?.syncDoc();
|
|
334
|
-
return;
|
|
335
|
-
}
|
|
336
|
-
const handler = new yDocHandler({
|
|
337
|
-
doc: subdoc,
|
|
338
|
-
isRoot: false,
|
|
339
|
-
updateDoc: this.updateDoc,
|
|
340
|
-
fetchDoc: this.fetchDoc
|
|
341
|
-
});
|
|
342
|
-
this.subdocHandlers.set(subdoc.guid, handler);
|
|
343
|
-
};
|
|
344
|
-
// attempt to load a subdoc of a given guid
|
|
345
|
-
this.loadSubdoc = (guid) => {
|
|
346
|
-
for (const subdoc of this.rootDoc.subdocs) {
|
|
347
|
-
if (subdoc.guid === guid) {
|
|
348
|
-
subdoc.load();
|
|
349
|
-
return true;
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
return false;
|
|
353
|
-
};
|
|
354
|
-
this.syncDoc = () => {
|
|
355
|
-
this.rootDocHandler.syncDoc();
|
|
356
|
-
for (const [_, handler] of this.subdocHandlers) {
|
|
357
|
-
handler.syncDoc();
|
|
358
|
-
}
|
|
359
|
-
};
|
|
360
312
|
this.rootDoc = doc;
|
|
361
313
|
this.room = room;
|
|
362
314
|
this.options = options;
|
|
@@ -364,7 +316,8 @@ var LiveblocksYjsProvider = class extends Observable {
|
|
|
364
316
|
doc,
|
|
365
317
|
isRoot: true,
|
|
366
318
|
updateDoc: this.updateDoc,
|
|
367
|
-
fetchDoc: this.fetchDoc
|
|
319
|
+
fetchDoc: this.fetchDoc,
|
|
320
|
+
useV2Updates: this.options.useV2Updates_experimental ?? false
|
|
368
321
|
});
|
|
369
322
|
room[kInternal].setYjsProvider(this);
|
|
370
323
|
this.awareness = new Awareness(this.rootDoc, this.room);
|
|
@@ -384,7 +337,7 @@ var LiveblocksYjsProvider = class extends Observable {
|
|
|
384
337
|
if (type === ClientMsgCode.UPDATE_YDOC) {
|
|
385
338
|
return;
|
|
386
339
|
}
|
|
387
|
-
const { stateVector, update: updateStr, guid } = message;
|
|
340
|
+
const { stateVector, update: updateStr, guid, v2 } = message;
|
|
388
341
|
const canWrite = this.room.getSelf()?.canWrite ?? true;
|
|
389
342
|
const update = Base642.toUint8Array(updateStr);
|
|
390
343
|
let foundPendingUpdate = false;
|
|
@@ -401,13 +354,15 @@ var LiveblocksYjsProvider = class extends Observable {
|
|
|
401
354
|
this.subdocHandlers.get(guid)?.handleServerUpdate({
|
|
402
355
|
update,
|
|
403
356
|
stateVector,
|
|
404
|
-
readOnly: !canWrite
|
|
357
|
+
readOnly: !canWrite,
|
|
358
|
+
v2
|
|
405
359
|
});
|
|
406
360
|
} else {
|
|
407
361
|
this.rootDocHandler.handleServerUpdate({
|
|
408
362
|
update,
|
|
409
363
|
stateVector,
|
|
410
|
-
readOnly: !canWrite
|
|
364
|
+
readOnly: !canWrite,
|
|
365
|
+
v2
|
|
411
366
|
});
|
|
412
367
|
}
|
|
413
368
|
}
|
|
@@ -436,6 +391,79 @@ var LiveblocksYjsProvider = class extends Observable {
|
|
|
436
391
|
this.rootDoc.on("subdocs", this.handleSubdocs);
|
|
437
392
|
this.syncDoc();
|
|
438
393
|
}
|
|
394
|
+
handleSubdocs = ({
|
|
395
|
+
loaded,
|
|
396
|
+
removed,
|
|
397
|
+
added
|
|
398
|
+
}) => {
|
|
399
|
+
loaded.forEach(this.createSubdocHandler);
|
|
400
|
+
if (this.options.autoloadSubdocs) {
|
|
401
|
+
for (const subdoc of added) {
|
|
402
|
+
if (!this.subdocHandlers.has(subdoc.guid)) {
|
|
403
|
+
subdoc.load();
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
for (const subdoc of removed) {
|
|
408
|
+
if (this.subdocHandlers.has(subdoc.guid)) {
|
|
409
|
+
this.subdocHandlers.get(subdoc.guid)?.destroy();
|
|
410
|
+
this.subdocHandlers.delete(subdoc.guid);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
getUniqueUpdateId = (update) => {
|
|
415
|
+
const clock = Y2.parseUpdateMeta(update).to.get(this.rootDoc.clientID) ?? "-1";
|
|
416
|
+
return this.rootDoc.clientID + ":" + clock;
|
|
417
|
+
};
|
|
418
|
+
updateDoc = (update, guid) => {
|
|
419
|
+
const canWrite = this.room.getSelf()?.canWrite ?? true;
|
|
420
|
+
if (canWrite) {
|
|
421
|
+
const updateId = this.getUniqueUpdateId(update);
|
|
422
|
+
this.pending.push(updateId);
|
|
423
|
+
this.room.updateYDoc(
|
|
424
|
+
Base642.fromUint8Array(update),
|
|
425
|
+
guid,
|
|
426
|
+
this.useV2Updates
|
|
427
|
+
);
|
|
428
|
+
this.emit("status", [this.getStatus()]);
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
fetchDoc = (vector, guid) => {
|
|
432
|
+
this.room.fetchYDoc(vector, guid, this.useV2Updates);
|
|
433
|
+
};
|
|
434
|
+
createSubdocHandler = (subdoc) => {
|
|
435
|
+
if (this.subdocHandlers.has(subdoc.guid)) {
|
|
436
|
+
this.subdocHandlers.get(subdoc.guid)?.syncDoc();
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
const handler = new yDocHandler({
|
|
440
|
+
doc: subdoc,
|
|
441
|
+
isRoot: false,
|
|
442
|
+
updateDoc: this.updateDoc,
|
|
443
|
+
fetchDoc: this.fetchDoc,
|
|
444
|
+
useV2Updates: this.options.useV2Updates_experimental ?? false
|
|
445
|
+
});
|
|
446
|
+
this.subdocHandlers.set(subdoc.guid, handler);
|
|
447
|
+
};
|
|
448
|
+
// attempt to load a subdoc of a given guid
|
|
449
|
+
loadSubdoc = (guid) => {
|
|
450
|
+
for (const subdoc of this.rootDoc.subdocs) {
|
|
451
|
+
if (subdoc.guid === guid) {
|
|
452
|
+
subdoc.load();
|
|
453
|
+
return true;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return false;
|
|
457
|
+
};
|
|
458
|
+
syncDoc = () => {
|
|
459
|
+
this.rootDocHandler.syncDoc();
|
|
460
|
+
for (const [_, handler] of this.subdocHandlers) {
|
|
461
|
+
handler.syncDoc();
|
|
462
|
+
}
|
|
463
|
+
};
|
|
464
|
+
get useV2Updates() {
|
|
465
|
+
return this.options.useV2Updates_experimental ?? false;
|
|
466
|
+
}
|
|
439
467
|
// The sync'd property is required by some provider implementations
|
|
440
468
|
get synced() {
|
|
441
469
|
return this.rootDocHandler.synced;
|
package/dist/index.mjs.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/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 type {\n BaseMetadata,\n DE,\n DM,\n DP,\n DS,\n DU,\n IYjsProvider,\n YjsSyncStatus,\n} from \"@liveblocks/core\";\nimport { ClientMsgCode, detectDupes, kInternal } from \"@liveblocks/core\";\nimport { Base64 } from \"js-base64\";\nimport { Observable } from \"lib0/observable\";\nimport { IndexeddbPersistence } from \"y-indexeddb\";\nimport * as Y from \"yjs\";\n\nimport { Awareness } from \"./awareness\";\nimport yDocHandler from \"./doc\";\nimport { PKG_FORMAT, PKG_NAME, PKG_VERSION } from \"./version\";\n\ndetectDupes(PKG_NAME, PKG_VERSION, PKG_FORMAT);\n\ntype ProviderOptions = {\n autoloadSubdocs?: boolean;\n offlineSupport_experimental?: boolean;\n};\n\nexport class LiveblocksYjsProvider<\n P extends JsonObject = DP,\n S extends LsonObject = DS,\n U extends BaseUserMeta = DU,\n E extends Json = DE,\n M extends BaseMetadata = DM,\n >\n extends Observable<unknown>\n implements IYjsProvider\n{\n private room: Room<P, S, U, E, M>;\n private rootDoc: Y.Doc;\n private options: ProviderOptions;\n private indexeddbProvider: IndexeddbPersistence | null = null;\n\n private unsubscribers: Array<() => void> = [];\n\n public awareness: Awareness<P, S, U, E, M>;\n\n public rootDocHandler: yDocHandler;\n public subdocHandlers: Map<string, yDocHandler> = new Map();\n\n private pending: string[] = [];\n\n constructor(\n room: Room<P, S, U, E, M>,\n doc: Y.Doc,\n options: ProviderOptions | undefined = {}\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\n // TODO: Display a warning if a YjsProvider is already attached to the room\n room[kInternal].setYjsProvider(this);\n\n // if we have a connectionId already during construction, use that\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 this.emit(\"status\", [this.getStatus()]);\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: updateStr, guid } = message;\n const canWrite = this.room.getSelf()?.canWrite ?? true;\n const update = Base64.toUint8Array(updateStr);\n let foundPendingUpdate = false;\n const updateId = this.getUniqueUpdateId(update);\n this.pending = this.pending.filter((pendingUpdate) => {\n if (pendingUpdate === updateId) {\n foundPendingUpdate = true;\n return false;\n }\n return true;\n });\n // if we found this update in our queue, we don't need to apply it\n if (!foundPendingUpdate) {\n // find the right doc and update\n if (guid !== undefined) {\n this.subdocHandlers.get(guid)?.handleServerUpdate({\n update,\n stateVector,\n readOnly: !canWrite,\n });\n } else {\n this.rootDocHandler.handleServerUpdate({\n update,\n stateVector,\n readOnly: !canWrite,\n });\n }\n }\n\n // notify any listeners that the status has changed\n this.emit(\"status\", [this.getStatus()]);\n })\n );\n\n if (options.offlineSupport_experimental) {\n this.indexeddbProvider = new IndexeddbPersistence(room.id, this.rootDoc);\n const onIndexedDbSync = () => {\n this.rootDocHandler.synced = true;\n };\n this.indexeddbProvider.on(\"synced\", onIndexedDbSync);\n\n this.unsubscribers.push(() => {\n this.indexeddbProvider?.off(\"synced\", onIndexedDbSync);\n });\n }\n\n // different consumers listen to sync and synced\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 this.emit(\"status\", [this.getStatus()]);\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: Set<Y.Doc>;\n removed: Set<Y.Doc>;\n added: Set<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 getUniqueUpdateId = (update: Uint8Array) => {\n const clock =\n Y.parseUpdateMeta(update).to.get(this.rootDoc.clientID) ?? \"-1\";\n return this.rootDoc.clientID + \":\" + clock;\n };\n\n private updateDoc = (update: Uint8Array, guid?: string) => {\n const canWrite = this.room.getSelf()?.canWrite ?? true;\n if (canWrite) {\n const updateId = this.getUniqueUpdateId(update);\n this.pending.push(updateId);\n this.room.updateYDoc(Base64.fromUint8Array(update), guid);\n this.emit(\"status\", [this.getStatus()]);\n }\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 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 public getStatus(): YjsSyncStatus {\n if (!this.synced) {\n return \"loading\";\n }\n return this.pending.length === 0 ? \"synchronized\" : \"synchronizing\";\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 async clearOfflineData(): Promise<void> {\n if (!this.indexeddbProvider) return;\n return this.indexeddbProvider.clearData();\n }\n\n getYDoc(): Y.Doc {\n return this.rootDoc;\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 K,V\n * @param {Map<K,V>} m\n * @return {Map<K,V>}\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 {Map<any, any>} MAP\n * @template {MAP extends Map<any,infer V> ? function():V : unknown} CF\n * @param {MAP} map\n * @param {MAP extends Map<infer K,any> ? K : unknown} key\n * @param {CF} createT\n * @return {ReturnType<CF>}\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/**\n * @template {ArrayLike<any>} ARR\n * @template {function(ARR extends ArrayLike<infer T> ? T : never, number, ARR):any} MAPPER\n * @param {ARR} arr\n * @param {MAPPER} mapper\n * @return {Array<MAPPER extends function(...any): infer M ? M : never>}\n */\nexport const map = (arr, mapper) => {\n /**\n * @type {Array<any>}\n */\n const res = Array(arr.length)\n for (let i = 0; i < arr.length; i++) {\n res[i] = mapper(/** @type {any} */ (arr[i]), i, /** @type {any} */ (arr))\n }\n return /** @type {any} */ (res)\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 * @experimental\n *\n * This is basically a (better typed) duplicate of Observable, which will replace Observable in the\n * next release.\n *\n * @template {{[key in keyof EVENTS]: function(...any):void}} EVENTS\n */\nexport class ObservableV2 {\n constructor () {\n /**\n * Some desc.\n * @type {Map<string, Set<any>>}\n */\n this._observers = map.create()\n }\n\n /**\n * @template {keyof EVENTS & string} NAME\n * @param {NAME} name\n * @param {EVENTS[NAME]} f\n */\n on (name, f) {\n map.setIfUndefined(this._observers, /** @type {string} */ (name), set.create).add(f)\n return f\n }\n\n /**\n * @template {keyof EVENTS & string} NAME\n * @param {NAME} name\n * @param {EVENTS[NAME]} f\n */\n once (name, f) {\n /**\n * @param {...any} args\n */\n const _f = (...args) => {\n this.off(name, /** @type {any} */ (_f))\n f(...args)\n }\n this.on(name, /** @type {any} */ (_f))\n }\n\n /**\n * @template {keyof EVENTS & string} NAME\n * @param {NAME} name\n * @param {EVENTS[NAME]} 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 * @template {keyof EVENTS & string} NAME\n * @param {NAME} name The event name.\n * @param {Parameters<EVENTS[NAME]>} 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\n/* c8 ignore start */\n/**\n * Handles named events.\n *\n * @deprecated\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/* c8 ignore end */\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 BaseMetadata,\n BaseUserMeta,\n Json,\n JsonObject,\n LsonObject,\n Room,\n User,\n} from \"@liveblocks/client\";\nimport { Observable } from \"lib0/observable\";\nimport type * as Y from \"yjs\";\n\nconst Y_PRESENCE_KEY = \"__yjs\";\nconst Y_PRESENCE_ID_KEY = \"__yjs_clientid\";\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<\n P extends JsonObject,\n S extends LsonObject,\n U extends BaseUserMeta,\n E extends Json,\n M extends BaseMetadata,\n> extends Observable<unknown> {\n private room: Room<P, S, U, E, M>;\n public doc: Y.Doc;\n public states: Map<number, unknown> = new Map();\n // used to map liveblock's ActorId to Yjs ClientID, both unique numbers representing a client\n public actorToClientMap: Map<number, number> = 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(doc: Y.Doc, room: Room<P, S, U, E, M>) {\n super();\n this.doc = doc;\n this.room = room;\n // Add the clientId to presence so we can map it to connectionId later\n this.room.updatePresence({\n [Y_PRESENCE_ID_KEY]: this.doc.clientID,\n } as unknown as Partial<P>);\n this.othersUnsub = this.room.events.others.subscribe((event) => {\n let updates:\n | { added: number[]; updated: number[]; removed: number[] }\n | undefined;\n\n // When others are changed, we emit an event that contains arrays added/updated/removed.\n if (event.type === \"leave\") {\n const targetClientId = this.actorToClientMap.get(\n event.user.connectionId\n );\n if (targetClientId !== undefined) {\n updates = { added: [], updated: [], removed: [targetClientId] };\n }\n // rebuild after the user leaves so we can get the ID of the user who left\n this.rebuildActorToClientMap(event.others);\n }\n if (event.type === \"enter\" || event.type === \"update\") {\n this.rebuildActorToClientMap(event.others);\n const targetClientId = this.actorToClientMap.get(\n event.user.connectionId\n );\n if (targetClientId !== undefined) {\n updates = {\n added: event.type === \"enter\" ? [targetClientId] : [],\n updated: event.type === \"update\" ? [targetClientId] : [],\n removed: [],\n };\n }\n }\n if (event.type === \"reset\") {\n this.rebuildActorToClientMap(event.others);\n }\n if (updates !== undefined) {\n this.emit(\"change\", [updates, \"presence\"]);\n this.emit(\"update\", [updates, \"presence\"]);\n }\n });\n }\n\n rebuildActorToClientMap(\n others: readonly User<JsonObject, BaseUserMeta>[]\n ): void {\n this.actorToClientMap.clear();\n others.forEach((user) => {\n if (user.presence[Y_PRESENCE_ID_KEY] !== undefined) {\n this.actorToClientMap.set(\n user.connectionId,\n user.presence[Y_PRESENCE_ID_KEY] as number\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;\n if (state === null) {\n if (presence === undefined) {\n // if presence is already undefined, we don't need to change anything here\n return;\n }\n this.room.updatePresence({ ...presence, [Y_PRESENCE_KEY]: null });\n this.emit(\"update\", [\n { added: [], updated: [], removed: [this.doc.clientID] },\n \"local\",\n ]);\n return;\n }\n // if presence was undefined, it's added, if not, it's updated\n const yPresence = presence?.[Y_PRESENCE_KEY];\n const added = yPresence === undefined ? [this.doc.clientID] : [];\n const updated = yPresence === undefined ? [] : [this.doc.clientID];\n this.room.updatePresence({\n [Y_PRESENCE_KEY]: {\n ...((yPresence as JsonObject) || {}),\n ...(state || {}),\n },\n } as unknown as Partial<P>);\n this.emit(\"update\", [{ added, updated, removed: [] }, \"local\"]);\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 [Y_PRESENCE_KEY]: { ...((presence as JsonObject) || {}), ...update },\n } as unknown as Partial<P>);\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>, otherUser) => {\n const otherPresence = otherUser.presence[Y_PRESENCE_KEY];\n const otherClientId = otherUser.presence[Y_PRESENCE_ID_KEY] as\n | number\n | undefined;\n if (otherPresence !== undefined && otherClientId !== undefined) {\n // set states of map clientId to yjs presence\n acc.set(otherClientId, otherPresence || {});\n }\n return acc;\n }, new Map());\n\n // add this client's yjs presence to states (local client not represented in others)\n const localPresence = this.room.getSelf()?.presence[Y_PRESENCE_KEY];\n if (localPresence !== undefined) {\n states.set(this.doc.clientID, localPresence);\n }\n return states;\n }\n}\n","import { Base64 } from \"js-base64\";\nimport { Observable } from \"lib0/observable\";\nimport { IndexeddbPersistence } from \"y-indexeddb\";\nimport * as Y from \"yjs\";\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: Uint8Array) => 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: Uint8Array, 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: Uint8Array) => {\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 readOnly,\n }: {\n update: Uint8Array;\n stateVector: string | null;\n readOnly: boolean;\n }): void => {\n // apply update from the server\n Y.applyUpdate(this.doc, update, \"backend\");\n // if this update is the result of a fetch, the state vector is included\n if (stateVector) {\n if (!readOnly) {\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(localUpdate);\n } catch (e) {\n // something went wrong encoding local state to send to the server\n console.warn(e);\n }\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 = (\n update: Uint8Array,\n origin: string | IndexeddbPersistence\n ) => {\n // don't send updates from indexedb, those will get handled by sync\n const isFromLocal = origin instanceof IndexeddbPersistence;\n if (origin !== \"backend\" && !isFromLocal) {\n this.updateRoomDoc(update);\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":";AAiBA,SAAS,eAAe,aAAa,iBAAiB;AACtD,SAAS,UAAAA,eAAc;;;ACJhB,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;;;ACnCtB,IAAM,aAAN,MAAiB;AAAA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AAQ,QAAQ;AAAA,MAClB;AACA,WAAK,KAAK,UAAU,CAAC,KAAK,CAAC;AAC3B,WAAK,KAAK,QAAQ,CAAC,KAAK,CAAC;AACzB,WAAK,KAAK,UAAU,CAAC,KAAK,UAAU,CAAC,CAAC;AAAA,IACxC,CAAC;AACD,SAAK,QAAQ,GAAG,WAAW,KAAK,aAAa;AAC7C,SAAK,QAAQ;AAAA,EACf;AAAA;AAAA,EAkFA,IAAI,SAAkB;AACpB,WAAO,KAAK,eAAe;AAAA,EAC7B;AAAA,EAEO,YAA2B;AAChC,QAAI,CAAC,KAAK,QAAQ;AAChB,aAAO;AAAA,IACT;AACA,WAAO,KAAK,QAAQ,WAAW,IAAI,iBAAiB;AAAA,EACtD;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,EAEA,MAAM,mBAAkC;AACtC,QAAI,CAAC,KAAK,kBAAmB;AAC7B,WAAO,KAAK,kBAAkB,UAAU;AAAA,EAC1C;AAAA,EAEA,UAAiB;AACf,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,aAAmB;AAAA,EAEnB;AAAA,EAEA,UAAgB;AAAA,EAEhB;AACF;","names":["Base64","create","create","IndexeddbPersistence","Y","Base64","IndexeddbPersistence"]}
|
|
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 type {\n BaseMetadata,\n DE,\n DM,\n DP,\n DS,\n DU,\n IYjsProvider,\n YjsSyncStatus,\n} from \"@liveblocks/core\";\nimport { ClientMsgCode, detectDupes, kInternal } from \"@liveblocks/core\";\nimport { Base64 } from \"js-base64\";\nimport { Observable } from \"lib0/observable\";\nimport { IndexeddbPersistence } from \"y-indexeddb\";\nimport * as Y from \"yjs\";\n\nimport { Awareness } from \"./awareness\";\nimport yDocHandler from \"./doc\";\nimport { PKG_FORMAT, PKG_NAME, PKG_VERSION } from \"./version\";\n\ndetectDupes(PKG_NAME, PKG_VERSION, PKG_FORMAT);\n\ntype ProviderOptions = {\n autoloadSubdocs?: boolean;\n offlineSupport_experimental?: boolean;\n useV2Updates_experimental?: boolean;\n};\n\nexport class LiveblocksYjsProvider<\n P extends JsonObject = DP,\n S extends LsonObject = DS,\n U extends BaseUserMeta = DU,\n E extends Json = DE,\n M extends BaseMetadata = DM,\n >\n extends Observable<unknown>\n implements IYjsProvider\n{\n private room: Room<P, S, U, E, M>;\n private rootDoc: Y.Doc;\n private options: ProviderOptions;\n private indexeddbProvider: IndexeddbPersistence | null = null;\n\n private unsubscribers: Array<() => void> = [];\n\n public awareness: Awareness<P, S, U, E, M>;\n\n public rootDocHandler: yDocHandler;\n public subdocHandlers: Map<string, yDocHandler> = new Map();\n\n private pending: string[] = [];\n\n constructor(\n room: Room<P, S, U, E, M>,\n doc: Y.Doc,\n options: ProviderOptions | undefined = {}\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 useV2Updates: this.options.useV2Updates_experimental ?? false,\n });\n\n // TODO: Display a warning if a YjsProvider is already attached to the room\n room[kInternal].setYjsProvider(this);\n\n // if we have a connectionId already during construction, use that\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 this.emit(\"status\", [this.getStatus()]);\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: updateStr, guid, v2 } = message;\n const canWrite = this.room.getSelf()?.canWrite ?? true;\n const update = Base64.toUint8Array(updateStr);\n let foundPendingUpdate = false;\n const updateId = this.getUniqueUpdateId(update);\n this.pending = this.pending.filter((pendingUpdate) => {\n if (pendingUpdate === updateId) {\n foundPendingUpdate = true;\n return false;\n }\n return true;\n });\n // if we found this update in our queue, we don't need to apply it\n if (!foundPendingUpdate) {\n // find the right doc and update\n if (guid !== undefined) {\n this.subdocHandlers.get(guid)?.handleServerUpdate({\n update,\n stateVector,\n readOnly: !canWrite,\n v2,\n });\n } else {\n this.rootDocHandler.handleServerUpdate({\n update,\n stateVector,\n readOnly: !canWrite,\n v2,\n });\n }\n }\n\n // notify any listeners that the status has changed\n this.emit(\"status\", [this.getStatus()]);\n })\n );\n\n if (options.offlineSupport_experimental) {\n this.indexeddbProvider = new IndexeddbPersistence(room.id, this.rootDoc);\n const onIndexedDbSync = () => {\n this.rootDocHandler.synced = true;\n };\n this.indexeddbProvider.on(\"synced\", onIndexedDbSync);\n\n this.unsubscribers.push(() => {\n this.indexeddbProvider?.off(\"synced\", onIndexedDbSync);\n });\n }\n\n // different consumers listen to sync and synced\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 this.emit(\"status\", [this.getStatus()]);\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: Set<Y.Doc>;\n removed: Set<Y.Doc>;\n added: Set<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 getUniqueUpdateId = (update: Uint8Array) => {\n const clock =\n Y.parseUpdateMeta(update).to.get(this.rootDoc.clientID) ?? \"-1\";\n return this.rootDoc.clientID + \":\" + clock;\n };\n\n private updateDoc = (update: Uint8Array, guid?: string) => {\n const canWrite = this.room.getSelf()?.canWrite ?? true;\n if (canWrite) {\n const updateId = this.getUniqueUpdateId(update);\n this.pending.push(updateId);\n this.room.updateYDoc(\n Base64.fromUint8Array(update),\n guid,\n this.useV2Updates\n );\n this.emit(\"status\", [this.getStatus()]);\n }\n };\n\n private fetchDoc = (vector: string, guid?: string) => {\n this.room.fetchYDoc(vector, guid, this.useV2Updates);\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 useV2Updates: this.options.useV2Updates_experimental ?? false,\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 this.rootDocHandler.syncDoc();\n for (const [_, handler] of this.subdocHandlers) {\n handler.syncDoc();\n }\n };\n\n get useV2Updates(): boolean {\n return this.options.useV2Updates_experimental ?? false;\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 public getStatus(): YjsSyncStatus {\n if (!this.synced) {\n return \"loading\";\n }\n return this.pending.length === 0 ? \"synchronized\" : \"synchronizing\";\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 async clearOfflineData(): Promise<void> {\n if (!this.indexeddbProvider) return;\n return this.indexeddbProvider.clearData();\n }\n\n getYDoc(): Y.Doc {\n return this.rootDoc;\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 K,V\n * @param {Map<K,V>} m\n * @return {Map<K,V>}\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 {Map<any, any>} MAP\n * @template {MAP extends Map<any,infer V> ? function():V : unknown} CF\n * @param {MAP} map\n * @param {MAP extends Map<infer K,any> ? K : unknown} key\n * @param {CF} createT\n * @return {ReturnType<CF>}\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/**\n * @template {ArrayLike<any>} ARR\n * @template {function(ARR extends ArrayLike<infer T> ? T : never, number, ARR):any} MAPPER\n * @param {ARR} arr\n * @param {MAPPER} mapper\n * @return {Array<MAPPER extends function(...any): infer M ? M : never>}\n */\nexport const map = (arr, mapper) => {\n /**\n * @type {Array<any>}\n */\n const res = Array(arr.length)\n for (let i = 0; i < arr.length; i++) {\n res[i] = mapper(/** @type {any} */ (arr[i]), i, /** @type {any} */ (arr))\n }\n return /** @type {any} */ (res)\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 * @experimental\n *\n * This is basically a (better typed) duplicate of Observable, which will replace Observable in the\n * next release.\n *\n * @template {{[key in keyof EVENTS]: function(...any):void}} EVENTS\n */\nexport class ObservableV2 {\n constructor () {\n /**\n * Some desc.\n * @type {Map<string, Set<any>>}\n */\n this._observers = map.create()\n }\n\n /**\n * @template {keyof EVENTS & string} NAME\n * @param {NAME} name\n * @param {EVENTS[NAME]} f\n */\n on (name, f) {\n map.setIfUndefined(this._observers, /** @type {string} */ (name), set.create).add(f)\n return f\n }\n\n /**\n * @template {keyof EVENTS & string} NAME\n * @param {NAME} name\n * @param {EVENTS[NAME]} f\n */\n once (name, f) {\n /**\n * @param {...any} args\n */\n const _f = (...args) => {\n this.off(name, /** @type {any} */ (_f))\n f(...args)\n }\n this.on(name, /** @type {any} */ (_f))\n }\n\n /**\n * @template {keyof EVENTS & string} NAME\n * @param {NAME} name\n * @param {EVENTS[NAME]} 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 * @template {keyof EVENTS & string} NAME\n * @param {NAME} name The event name.\n * @param {Parameters<EVENTS[NAME]>} 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\n/* c8 ignore start */\n/**\n * Handles named events.\n *\n * @deprecated\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/* c8 ignore end */\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 BaseMetadata,\n BaseUserMeta,\n Json,\n JsonObject,\n LsonObject,\n Room,\n User,\n} from \"@liveblocks/client\";\nimport { Observable } from \"lib0/observable\";\nimport type * as Y from \"yjs\";\n\nconst Y_PRESENCE_KEY = \"__yjs\";\nconst Y_PRESENCE_ID_KEY = \"__yjs_clientid\";\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<\n P extends JsonObject,\n S extends LsonObject,\n U extends BaseUserMeta,\n E extends Json,\n M extends BaseMetadata,\n> extends Observable<unknown> {\n private room: Room<P, S, U, E, M>;\n public doc: Y.Doc;\n public states: Map<number, unknown> = new Map();\n // used to map liveblock's ActorId to Yjs ClientID, both unique numbers representing a client\n public actorToClientMap: Map<number, number> = 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(doc: Y.Doc, room: Room<P, S, U, E, M>) {\n super();\n this.doc = doc;\n this.room = room;\n // Add the clientId to presence so we can map it to connectionId later\n this.room.updatePresence({\n [Y_PRESENCE_ID_KEY]: this.doc.clientID,\n } as unknown as Partial<P>);\n this.othersUnsub = this.room.events.others.subscribe((event) => {\n let updates:\n | { added: number[]; updated: number[]; removed: number[] }\n | undefined;\n\n // When others are changed, we emit an event that contains arrays added/updated/removed.\n if (event.type === \"leave\") {\n const targetClientId = this.actorToClientMap.get(\n event.user.connectionId\n );\n if (targetClientId !== undefined) {\n updates = { added: [], updated: [], removed: [targetClientId] };\n }\n // rebuild after the user leaves so we can get the ID of the user who left\n this.rebuildActorToClientMap(event.others);\n }\n if (event.type === \"enter\" || event.type === \"update\") {\n this.rebuildActorToClientMap(event.others);\n const targetClientId = this.actorToClientMap.get(\n event.user.connectionId\n );\n if (targetClientId !== undefined) {\n updates = {\n added: event.type === \"enter\" ? [targetClientId] : [],\n updated: event.type === \"update\" ? [targetClientId] : [],\n removed: [],\n };\n }\n }\n if (event.type === \"reset\") {\n this.rebuildActorToClientMap(event.others);\n }\n if (updates !== undefined) {\n this.emit(\"change\", [updates, \"presence\"]);\n this.emit(\"update\", [updates, \"presence\"]);\n }\n });\n }\n\n rebuildActorToClientMap(\n others: readonly User<JsonObject, BaseUserMeta>[]\n ): void {\n this.actorToClientMap.clear();\n others.forEach((user) => {\n if (user.presence[Y_PRESENCE_ID_KEY] !== undefined) {\n this.actorToClientMap.set(\n user.connectionId,\n user.presence[Y_PRESENCE_ID_KEY] as number\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;\n if (state === null) {\n if (presence === undefined) {\n // if presence is already undefined, we don't need to change anything here\n return;\n }\n this.room.updatePresence({ ...presence, [Y_PRESENCE_KEY]: null });\n this.emit(\"update\", [\n { added: [], updated: [], removed: [this.doc.clientID] },\n \"local\",\n ]);\n return;\n }\n // if presence was undefined, it's added, if not, it's updated\n const yPresence = presence?.[Y_PRESENCE_KEY];\n const added = yPresence === undefined ? [this.doc.clientID] : [];\n const updated = yPresence === undefined ? [] : [this.doc.clientID];\n this.room.updatePresence({\n [Y_PRESENCE_KEY]: {\n ...((yPresence as JsonObject) || {}),\n ...(state || {}),\n },\n } as unknown as Partial<P>);\n this.emit(\"update\", [{ added, updated, removed: [] }, \"local\"]);\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 [Y_PRESENCE_KEY]: { ...((presence as JsonObject) || {}), ...update },\n } as unknown as Partial<P>);\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>, otherUser) => {\n const otherPresence = otherUser.presence[Y_PRESENCE_KEY];\n const otherClientId = otherUser.presence[Y_PRESENCE_ID_KEY] as\n | number\n | undefined;\n if (otherPresence !== undefined && otherClientId !== undefined) {\n // set states of map clientId to yjs presence\n acc.set(otherClientId, otherPresence || {});\n }\n return acc;\n }, new Map());\n\n // add this client's yjs presence to states (local client not represented in others)\n const localPresence = this.room.getSelf()?.presence[Y_PRESENCE_KEY];\n if (localPresence !== undefined) {\n states.set(this.doc.clientID, localPresence);\n }\n return states;\n }\n}\n","import { Base64 } from \"js-base64\";\nimport { Observable } from \"lib0/observable\";\nimport { IndexeddbPersistence } from \"y-indexeddb\";\nimport * as Y from \"yjs\";\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: Uint8Array) => void;\n private fetchRoomDoc: (vector: string) => void;\n private useV2Updates: boolean;\n\n constructor({\n doc,\n isRoot,\n updateDoc,\n fetchDoc,\n useV2Updates,\n }: {\n doc: Y.Doc;\n isRoot: boolean;\n updateDoc: (update: Uint8Array, guid?: string) => void;\n fetchDoc: (vector: string, guid?: string) => void;\n useV2Updates: boolean;\n }) {\n super();\n this.doc = doc;\n this.useV2Updates = useV2Updates;\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: Uint8Array) => {\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 readOnly,\n v2,\n }: {\n update: Uint8Array;\n stateVector: string | null;\n readOnly: boolean;\n v2?: boolean;\n }): void => {\n // apply update from the server, updates from the server can be v1 or v2\n const applyUpdate = v2 ? Y.applyUpdateV2 : Y.applyUpdate;\n applyUpdate(this.doc, update, \"backend\");\n // if this update is the result of a fetch, the state vector is included\n if (stateVector) {\n if (!readOnly) {\n // Use server state to calculate a diff and send it\n try {\n // send v1 or v2update according to client option\n const encodeUpdate = this.useV2Updates\n ? Y.encodeStateAsUpdateV2\n : Y.encodeStateAsUpdate;\n const localUpdate = encodeUpdate(\n this.doc,\n Base64.toUint8Array(stateVector)\n );\n this.updateRoomDoc(localUpdate);\n } catch (e) {\n // something went wrong encoding local state to send to the server\n console.warn(e);\n }\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 = (\n update: Uint8Array,\n origin: string | IndexeddbPersistence\n ) => {\n // don't send updates from indexedb, those will get handled by sync\n const isFromLocal = origin instanceof IndexeddbPersistence;\n if (origin !== \"backend\" && !isFromLocal) {\n this.updateRoomDoc(update);\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":";AAiBA,SAAS,eAAe,aAAa,iBAAiB;AACtD,SAAS,UAAAA,eAAc;;;ACJhB,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;;;ACnCtB,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,cA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AAU,MAAM;AACtB,SAAK,eAAe,QAAQ;AAC5B,eAAW,CAAC,GAAG,OAAO,KAAK,KAAK,gBAAgB;AAC9C,cAAQ,QAAQ;AAAA,IAClB;AAAA,EACF;AAAA,EAEA,IAAI,eAAwB;AAC1B,WAAO,KAAK,QAAQ,6BAA6B;AAAA,EACnD;AAAA;AAAA,EAGA,IAAI,SAAkB;AACpB,WAAO,KAAK,eAAe;AAAA,EAC7B;AAAA,EAEO,YAA2B;AAChC,QAAI,CAAC,KAAK,QAAQ;AAChB,aAAO;AAAA,IACT;AACA,WAAO,KAAK,QAAQ,WAAW,IAAI,iBAAiB;AAAA,EACtD;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,EAEA,MAAM,mBAAkC;AACtC,QAAI,CAAC,KAAK,kBAAmB;AAC7B,WAAO,KAAK,kBAAkB,UAAU;AAAA,EAC1C;AAAA,EAEA,UAAiB;AACf,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,aAAmB;AAAA,EAEnB;AAAA,EAEA,UAAgB;AAAA,EAEhB;AACF;","names":["Base64","create","create","IndexeddbPersistence","Y","applyUpdate","Base64","IndexeddbPersistence"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@liveblocks/yjs",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.14.0-v2encoding",
|
|
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
|
"type": "commonjs",
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
"test:watch": "jest --silent --verbose --color=always --watch"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@liveblocks/client": "2.
|
|
37
|
-
"@liveblocks/core": "2.
|
|
36
|
+
"@liveblocks/client": "2.14.0-v2encoding",
|
|
37
|
+
"@liveblocks/core": "2.14.0-v2encoding",
|
|
38
38
|
"js-base64": "^3.7.7",
|
|
39
39
|
"y-indexeddb": "^9.0.12"
|
|
40
40
|
},
|