@pluv/client 0.18.0 → 0.20.0
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/.turbo/turbo-build.log +15 -15
- package/CHANGELOG.md +101 -0
- package/README.md +0 -92
- package/dist/index.d.mts +40 -33
- package/dist/index.d.ts +40 -33
- package/dist/index.js +118 -185
- package/dist/index.mjs +118 -185
- package/package.json +6 -6
- package/src/AbstractRoom.ts +10 -7
- package/src/CrdtManager.ts +3 -3
- package/src/CrdtNotifier.ts +10 -10
- package/src/MockedRoom.ts +35 -17
- package/src/OtherNotifier.ts +2 -6
- package/src/PluvClient.ts +5 -10
- package/src/PluvRoom.ts +50 -31
- package/src/index.ts +1 -1
- package/src/types.ts +10 -6
package/dist/index.mjs
CHANGED
|
@@ -68,8 +68,7 @@ var CrdtManager = class {
|
|
|
68
68
|
* @date June 13, 2023
|
|
69
69
|
*/
|
|
70
70
|
applyUpdate(update, origin) {
|
|
71
|
-
if (!this.initialized)
|
|
72
|
-
return this;
|
|
71
|
+
if (!this.initialized) return this;
|
|
73
72
|
const updates = typeof update === "string" ? [update] : update;
|
|
74
73
|
this._applyDocUpdates(this.doc, updates, origin);
|
|
75
74
|
return this;
|
|
@@ -83,8 +82,7 @@ var CrdtManager = class {
|
|
|
83
82
|
initialize(params) {
|
|
84
83
|
const { onInitialized, origin, update } = params;
|
|
85
84
|
const updates = typeof update === "string" ? [update] : update;
|
|
86
|
-
if (!updates.length)
|
|
87
|
-
return this;
|
|
85
|
+
if (!updates.length) return this;
|
|
88
86
|
if (this.initialized) {
|
|
89
87
|
this.doc = this._applyDocUpdates(this.doc, updates, origin).track();
|
|
90
88
|
return this;
|
|
@@ -144,8 +142,7 @@ var EventNotifier = class {
|
|
|
144
142
|
}
|
|
145
143
|
subject(name) {
|
|
146
144
|
const subject = this.subjects.get(name);
|
|
147
|
-
if (subject)
|
|
148
|
-
return subject;
|
|
145
|
+
if (subject) return subject;
|
|
149
146
|
const newSubject = makeSubject2();
|
|
150
147
|
this.subjects.set(name, newSubject);
|
|
151
148
|
return newSubject;
|
|
@@ -164,8 +161,7 @@ var OtherNotifier = class {
|
|
|
164
161
|
}
|
|
165
162
|
subject(name) {
|
|
166
163
|
const subject = this.subjects.get(name);
|
|
167
|
-
if (subject)
|
|
168
|
-
return subject;
|
|
164
|
+
if (subject) return subject;
|
|
169
165
|
const newSubject = makeSubject3();
|
|
170
166
|
this.subjects.set(name, newSubject);
|
|
171
167
|
return newSubject;
|
|
@@ -191,8 +187,7 @@ var StateNotifier = class {
|
|
|
191
187
|
subscribe(name, callback) {
|
|
192
188
|
var _a;
|
|
193
189
|
const source = (_a = this.subjects[name]) == null ? void 0 : _a.source;
|
|
194
|
-
if (!source)
|
|
195
|
-
throw new Error(`Could not subscribe to source: ${name}`);
|
|
190
|
+
if (!source) throw new Error(`Could not subscribe to source: ${name}`);
|
|
196
191
|
return subscribe4(callback)(source).unsubscribe;
|
|
197
192
|
}
|
|
198
193
|
};
|
|
@@ -236,8 +231,7 @@ var UsersManager = class {
|
|
|
236
231
|
return;
|
|
237
232
|
}
|
|
238
233
|
const other = this._others.get(connectionId);
|
|
239
|
-
if (!other)
|
|
240
|
-
return;
|
|
234
|
+
if (!other) return;
|
|
241
235
|
const presence = __spreadValues(__spreadValues({}, other.presence), patch);
|
|
242
236
|
(_b = this._presence) == null ? void 0 : _b.parse(presence);
|
|
243
237
|
this._others.set(connectionId, __spreadProps(__spreadValues({}, other), { presence }));
|
|
@@ -264,17 +258,14 @@ var UsersManager = class {
|
|
|
264
258
|
return;
|
|
265
259
|
}
|
|
266
260
|
const other = this._others.get(connectionId);
|
|
267
|
-
if (!other)
|
|
268
|
-
return;
|
|
261
|
+
if (!other) return;
|
|
269
262
|
this._others.set(connectionId, __spreadProps(__spreadValues({}, other), { presence }));
|
|
270
263
|
}
|
|
271
264
|
setUser(connectionId, user, presence) {
|
|
272
265
|
var _a;
|
|
273
|
-
if (((_a = this._myself) == null ? void 0 : _a.connectionId) === connectionId)
|
|
274
|
-
return;
|
|
266
|
+
if (((_a = this._myself) == null ? void 0 : _a.connectionId) === connectionId) return;
|
|
275
267
|
const other = this._others.get(connectionId);
|
|
276
|
-
if (other)
|
|
277
|
-
return;
|
|
268
|
+
if (other) return;
|
|
278
269
|
this._others.set(connectionId, {
|
|
279
270
|
connectionId,
|
|
280
271
|
presence: presence != null ? presence : this._initialPresence,
|
|
@@ -286,8 +277,7 @@ var UsersManager = class {
|
|
|
286
277
|
*/
|
|
287
278
|
updateMyPresence(presence) {
|
|
288
279
|
this._myPresence = __spreadValues(__spreadValues({}, this._myPresence), presence);
|
|
289
|
-
if (!this._myself)
|
|
290
|
-
return;
|
|
280
|
+
if (!this._myself) return;
|
|
291
281
|
this._myself.presence = this._myPresence;
|
|
292
282
|
}
|
|
293
283
|
};
|
|
@@ -335,6 +325,9 @@ var MockedRoom = class extends AbstractRoom {
|
|
|
335
325
|
this.event = (event, callback) => {
|
|
336
326
|
return this._eventNotifier.subscribe(event, callback);
|
|
337
327
|
};
|
|
328
|
+
this.getConnection = () => {
|
|
329
|
+
return Object.freeze(JSON.parse(JSON.stringify(this._state.connection)));
|
|
330
|
+
};
|
|
338
331
|
this.getMyPresence = () => {
|
|
339
332
|
return this._usersManager.myPresence;
|
|
340
333
|
};
|
|
@@ -347,11 +340,10 @@ var MockedRoom = class extends AbstractRoom {
|
|
|
347
340
|
this.getOthers = () => {
|
|
348
341
|
return this._usersManager.getOthers();
|
|
349
342
|
};
|
|
350
|
-
this.getStorage = (
|
|
351
|
-
const sharedType = this._crdtManager.get(
|
|
352
|
-
if (typeof sharedType === "undefined")
|
|
353
|
-
|
|
354
|
-
return sharedType.value;
|
|
343
|
+
this.getStorage = (type) => {
|
|
344
|
+
const sharedType = this._crdtManager.get(type);
|
|
345
|
+
if (typeof sharedType === "undefined") return null;
|
|
346
|
+
return sharedType;
|
|
355
347
|
};
|
|
356
348
|
this.other = (connectionId, callback) => {
|
|
357
349
|
return this._otherNotifier.subscribe(connectionId, callback);
|
|
@@ -371,10 +363,8 @@ var MockedRoom = class extends AbstractRoom {
|
|
|
371
363
|
this.transact = (fn, origin) => {
|
|
372
364
|
const _origin = origin != null ? origin : this._state.connection.id;
|
|
373
365
|
const crdtManager = this._crdtManager;
|
|
374
|
-
if (typeof _origin !== "string")
|
|
375
|
-
|
|
376
|
-
if (!crdtManager)
|
|
377
|
-
return;
|
|
366
|
+
if (typeof _origin !== "string") return;
|
|
367
|
+
if (!crdtManager) return;
|
|
378
368
|
crdtManager.doc.transact(() => {
|
|
379
369
|
const storage = crdtManager.doc.get();
|
|
380
370
|
fn(storage);
|
|
@@ -385,7 +375,8 @@ var MockedRoom = class extends AbstractRoom {
|
|
|
385
375
|
};
|
|
386
376
|
this.updateMyPresence = (presence) => {
|
|
387
377
|
var _a;
|
|
388
|
-
this.
|
|
378
|
+
const newPresence = typeof presence === "function" ? presence(this.getMyPresence()) : presence;
|
|
379
|
+
this._usersManager.updateMyPresence(newPresence);
|
|
389
380
|
const myPresence = this._usersManager.myPresence;
|
|
390
381
|
const myself = (_a = this._usersManager.myself) != null ? _a : null;
|
|
391
382
|
this._stateNotifier.subjects["my-presence"].next(myPresence);
|
|
@@ -405,44 +396,39 @@ var MockedRoom = class extends AbstractRoom {
|
|
|
405
396
|
return true;
|
|
406
397
|
}
|
|
407
398
|
broadcast(event, data) {
|
|
408
|
-
if (!this._events)
|
|
409
|
-
return;
|
|
399
|
+
if (!this._events) return;
|
|
410
400
|
const resolver = this._events[event];
|
|
411
|
-
if (!resolver)
|
|
412
|
-
return;
|
|
401
|
+
if (!resolver) return;
|
|
413
402
|
const result = resolver(data);
|
|
414
403
|
Object.keys(result).forEach((type) => {
|
|
415
404
|
const _type = type.toString();
|
|
416
405
|
const data2 = result[_type];
|
|
417
|
-
if (!data2)
|
|
418
|
-
return;
|
|
406
|
+
if (!data2) return;
|
|
419
407
|
this._eventNotifier.subject(_type).next(data2);
|
|
420
408
|
});
|
|
421
409
|
}
|
|
422
|
-
getConnection() {
|
|
423
|
-
return Object.freeze(JSON.parse(JSON.stringify(this._state.connection)));
|
|
424
|
-
}
|
|
425
410
|
getDoc() {
|
|
426
411
|
return this._crdtManager.doc;
|
|
427
412
|
}
|
|
413
|
+
getStorageJson(type) {
|
|
414
|
+
if (this._state.connection.id === null) return null;
|
|
415
|
+
if (typeof type === "undefined") return this._crdtManager.doc.toJson();
|
|
416
|
+
return this._crdtManager.doc.toJson(type);
|
|
417
|
+
}
|
|
428
418
|
_observeCrdt() {
|
|
429
419
|
var _a, _b;
|
|
430
420
|
(_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
|
|
431
|
-
if (!this._crdtManager)
|
|
432
|
-
return;
|
|
421
|
+
if (!this._crdtManager) return;
|
|
433
422
|
const unsubscribe = this._crdtManager.doc.subscribe((event) => {
|
|
434
423
|
var _a2;
|
|
435
424
|
const origin = (_a2 = event.origin) != null ? _a2 : null;
|
|
436
|
-
if (!this._crdtManager)
|
|
437
|
-
|
|
438
|
-
if (origin === "$STORAGE_UPDATED")
|
|
439
|
-
return;
|
|
425
|
+
if (!this._crdtManager) return;
|
|
426
|
+
if (origin === "$STORAGE_UPDATED") return;
|
|
440
427
|
const sharedTypes = this._crdtManager.doc.get();
|
|
441
|
-
const storageRoot = Object.
|
|
442
|
-
(acc,
|
|
443
|
-
if (!this._crdtManager)
|
|
444
|
-
|
|
445
|
-
const serialized = sharedType.toJson();
|
|
428
|
+
const storageRoot = Object.keys(sharedTypes).reduce(
|
|
429
|
+
(acc, prop) => {
|
|
430
|
+
if (!this._crdtManager) return acc;
|
|
431
|
+
const serialized = this._crdtManager.doc.toJson(prop);
|
|
446
432
|
this._crdtNotifier.subject(prop).next(serialized);
|
|
447
433
|
return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
|
|
448
434
|
},
|
|
@@ -485,12 +471,10 @@ var debounce = (fn, options) => {
|
|
|
485
471
|
let prevResult;
|
|
486
472
|
let timeout = null;
|
|
487
473
|
return (...args) => {
|
|
488
|
-
if (typeof timeout === "number")
|
|
489
|
-
clearTimeout(timeout);
|
|
474
|
+
if (typeof timeout === "number") clearTimeout(timeout);
|
|
490
475
|
timeout = setTimeout(() => {
|
|
491
476
|
timeout = null;
|
|
492
|
-
if (!immediate)
|
|
493
|
-
prevResult = fn(args);
|
|
477
|
+
if (!immediate) prevResult = fn(args);
|
|
494
478
|
}, wait);
|
|
495
479
|
if (immediate && !timeout) {
|
|
496
480
|
fn(args);
|
|
@@ -553,10 +537,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
553
537
|
this._wsListeners = null;
|
|
554
538
|
this.broadcast = new Proxy(
|
|
555
539
|
(event, data) => {
|
|
556
|
-
if (!this._state.webSocket)
|
|
557
|
-
|
|
558
|
-
if (this._state.connection.state !== "Open" /* Open */)
|
|
559
|
-
return;
|
|
540
|
+
if (!this._state.webSocket) return;
|
|
541
|
+
if (this._state.connection.state !== "Open" /* Open */) return;
|
|
560
542
|
const type = event.toString();
|
|
561
543
|
this._sendMessage({ data, type });
|
|
562
544
|
},
|
|
@@ -582,6 +564,12 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
582
564
|
}
|
|
583
565
|
}
|
|
584
566
|
);
|
|
567
|
+
this.getConnection = () => {
|
|
568
|
+
return Object.freeze(JSON.parse(JSON.stringify(this._state.connection)));
|
|
569
|
+
};
|
|
570
|
+
this.getDoc = () => {
|
|
571
|
+
return this._crdtManager.doc;
|
|
572
|
+
};
|
|
585
573
|
this.getMyPresence = () => {
|
|
586
574
|
return this._usersManager.myPresence;
|
|
587
575
|
};
|
|
@@ -594,10 +582,9 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
594
582
|
this.getOthers = () => {
|
|
595
583
|
return this._usersManager.getOthers();
|
|
596
584
|
};
|
|
597
|
-
this.getStorage = (
|
|
598
|
-
const sharedType = this._crdtManager.get(
|
|
599
|
-
if (typeof sharedType === "undefined")
|
|
600
|
-
return null;
|
|
585
|
+
this.getStorage = (type) => {
|
|
586
|
+
const sharedType = this._crdtManager.get(type);
|
|
587
|
+
if (typeof sharedType === "undefined") return null;
|
|
601
588
|
return sharedType;
|
|
602
589
|
};
|
|
603
590
|
this.other = (connectionId, callback) => {
|
|
@@ -617,8 +604,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
617
604
|
};
|
|
618
605
|
this.transact = (fn, origin) => {
|
|
619
606
|
const _origin = origin != null ? origin : this._state.connection.id;
|
|
620
|
-
if (typeof _origin !== "string")
|
|
621
|
-
return;
|
|
607
|
+
if (typeof _origin !== "string") return;
|
|
622
608
|
this._crdtManager.doc.transact(() => {
|
|
623
609
|
const storage = this._crdtManager.doc.get();
|
|
624
610
|
fn(storage);
|
|
@@ -629,18 +615,18 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
629
615
|
};
|
|
630
616
|
this.updateMyPresence = (presence) => {
|
|
631
617
|
var _a;
|
|
632
|
-
this.
|
|
618
|
+
const newPresence = typeof presence === "function" ? presence(this.getMyPresence()) : presence;
|
|
619
|
+
this._usersManager.updateMyPresence(newPresence);
|
|
633
620
|
const myPresence = this._usersManager.myPresence;
|
|
634
621
|
const myself = (_a = this._usersManager.myself) != null ? _a : null;
|
|
635
622
|
this._stateNotifier.subjects["my-presence"].next(myPresence);
|
|
636
623
|
!!myself && this._stateNotifier.subjects["myself"].next(myself);
|
|
637
|
-
this.broadcast("$UPDATE_PRESENCE", { presence });
|
|
624
|
+
this.broadcast("$UPDATE_PRESENCE", { presence: newPresence });
|
|
638
625
|
};
|
|
639
626
|
this._flattenStorageStore = debounce(
|
|
640
627
|
() => __async(this, null, function* () {
|
|
641
628
|
const shouldFlatten = yield this._storageStore.getShouldFlatten();
|
|
642
|
-
if (!shouldFlatten)
|
|
643
|
-
return;
|
|
629
|
+
if (!shouldFlatten) return;
|
|
644
630
|
const updates = yield this._storageStore.getUpdates();
|
|
645
631
|
this._crdtManager.applyUpdate(updates, this);
|
|
646
632
|
const encodedState = this._crdtManager.doc.getEncodedState();
|
|
@@ -688,8 +674,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
688
674
|
const canConnect = ["Closed" /* Closed */, "Untouched" /* Untouched */].some(
|
|
689
675
|
(state) => this._state.connection.state === state
|
|
690
676
|
);
|
|
691
|
-
if (!canConnect)
|
|
692
|
-
return;
|
|
677
|
+
if (!canConnect) return;
|
|
693
678
|
this._clearTimeout(this._timeouts.reconnect);
|
|
694
679
|
this._updateState((oldState) => {
|
|
695
680
|
oldState.connection.state = "Connecting" /* Connecting */;
|
|
@@ -723,13 +708,11 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
723
708
|
if (!this._endpoints.wsEndpoint) {
|
|
724
709
|
throw new Error("Must provide an wsEndpoint.");
|
|
725
710
|
}
|
|
726
|
-
if (!this._state.webSocket)
|
|
727
|
-
return;
|
|
711
|
+
if (!this._state.webSocket) return;
|
|
728
712
|
const canDisconnect = ["Connecting" /* Connecting */, "Open" /* Open */].some(
|
|
729
713
|
(state) => this._state.connection.state === state
|
|
730
714
|
);
|
|
731
|
-
if (!canDisconnect)
|
|
732
|
-
return;
|
|
715
|
+
if (!canDisconnect) return;
|
|
733
716
|
this._clearInterval(this._intervals.heartbeat);
|
|
734
717
|
this._closeWs();
|
|
735
718
|
yield this._storageStore.destroy();
|
|
@@ -737,11 +720,12 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
737
720
|
this._stateNotifier.subjects["my-presence"].next(null);
|
|
738
721
|
});
|
|
739
722
|
}
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
return
|
|
723
|
+
getStorageJson(type) {
|
|
724
|
+
if (this._state.connection.id === null) return null;
|
|
725
|
+
if (typeof type === "undefined") return this._crdtManager.doc.toJson();
|
|
726
|
+
const sharedType = this._crdtManager.get(type);
|
|
727
|
+
if (typeof sharedType === "undefined") return null;
|
|
728
|
+
return this._crdtManager.doc.toJson(type);
|
|
745
729
|
}
|
|
746
730
|
_addToStorageStore(update) {
|
|
747
731
|
return __async(this, null, function* () {
|
|
@@ -764,10 +748,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
764
748
|
});
|
|
765
749
|
}
|
|
766
750
|
_attachWindowListeners() {
|
|
767
|
-
if (typeof window === "undefined")
|
|
768
|
-
|
|
769
|
-
if (typeof document === "undefined")
|
|
770
|
-
return;
|
|
751
|
+
if (typeof window === "undefined") return;
|
|
752
|
+
if (typeof document === "undefined") return;
|
|
771
753
|
this._windowListeners = {
|
|
772
754
|
onNavigatorOnline: this._onNavigatorOnline.bind(this),
|
|
773
755
|
onVisibilityChange: this._onVisibilityChange.bind(this)
|
|
@@ -776,8 +758,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
776
758
|
document.addEventListener("visibilitychange", this._windowListeners.onVisibilityChange);
|
|
777
759
|
}
|
|
778
760
|
_attachWsListeners() {
|
|
779
|
-
if (!this._state.webSocket)
|
|
780
|
-
return;
|
|
761
|
+
if (!this._state.webSocket) return;
|
|
781
762
|
this._wsListeners = {
|
|
782
763
|
onClose: this._onClose.bind(this),
|
|
783
764
|
onError: this._onError.bind(this),
|
|
@@ -790,19 +771,16 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
790
771
|
this._state.webSocket.addEventListener("open", this._wsListeners.onOpen);
|
|
791
772
|
}
|
|
792
773
|
_clearInterval(interval) {
|
|
793
|
-
if (Number.isNaN(interval))
|
|
794
|
-
return;
|
|
774
|
+
if (Number.isNaN(interval)) return;
|
|
795
775
|
clearInterval(interval);
|
|
796
776
|
}
|
|
797
777
|
_clearTimeout(timeout) {
|
|
798
|
-
if (Number.isNaN(timeout))
|
|
799
|
-
return;
|
|
778
|
+
if (Number.isNaN(timeout)) return;
|
|
800
779
|
clearTimeout(timeout);
|
|
801
780
|
}
|
|
802
781
|
_closeWs() {
|
|
803
782
|
var _a, _b;
|
|
804
|
-
if (!this._state.webSocket)
|
|
805
|
-
return;
|
|
783
|
+
if (!this._state.webSocket) return;
|
|
806
784
|
(_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
|
|
807
785
|
this._clearTimeout(this._timeouts.pong);
|
|
808
786
|
this._clearTimeout(this._timeouts.reconnect);
|
|
@@ -814,24 +792,19 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
814
792
|
return readyState === ((_a2 = this._state.webSocket) == null ? void 0 : _a2.readyState);
|
|
815
793
|
}
|
|
816
794
|
);
|
|
817
|
-
if (canClose)
|
|
818
|
-
this._state.webSocket.close();
|
|
795
|
+
if (canClose) this._state.webSocket.close();
|
|
819
796
|
this._detachWsListeners();
|
|
820
797
|
}
|
|
821
798
|
_detachWindowListeners() {
|
|
822
|
-
if (!this._windowListeners)
|
|
823
|
-
|
|
824
|
-
if (typeof
|
|
825
|
-
return;
|
|
826
|
-
if (typeof document === "undefined")
|
|
827
|
-
return;
|
|
799
|
+
if (!this._windowListeners) return;
|
|
800
|
+
if (typeof window === "undefined") return;
|
|
801
|
+
if (typeof document === "undefined") return;
|
|
828
802
|
window.removeEventListener("online", this._windowListeners.onNavigatorOnline);
|
|
829
803
|
document.removeEventListener("visibilitychange", this._windowListeners.onVisibilityChange);
|
|
830
804
|
this._windowListeners = null;
|
|
831
805
|
}
|
|
832
806
|
_detachWsListeners() {
|
|
833
|
-
if (!this._state.webSocket || !this._wsListeners)
|
|
834
|
-
return;
|
|
807
|
+
if (!this._state.webSocket || !this._wsListeners) return;
|
|
835
808
|
this._state.webSocket.removeEventListener("close", this._wsListeners.onClose);
|
|
836
809
|
this._state.webSocket.removeEventListener("error", this._wsListeners.onError);
|
|
837
810
|
this._state.webSocket.removeEventListener("message", this._wsListeners.onMessage);
|
|
@@ -840,9 +813,9 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
840
813
|
}
|
|
841
814
|
_emitSharedTypes() {
|
|
842
815
|
const sharedTypes = this._crdtManager.doc.get();
|
|
843
|
-
const storageRoot = Object.
|
|
844
|
-
(acc,
|
|
845
|
-
const serialized =
|
|
816
|
+
const storageRoot = Object.keys(sharedTypes).reduce(
|
|
817
|
+
(acc, prop) => {
|
|
818
|
+
const serialized = this._crdtManager.doc.toJson(prop);
|
|
846
819
|
this._crdtNotifier.subject(prop).next(serialized);
|
|
847
820
|
return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
|
|
848
821
|
},
|
|
@@ -851,8 +824,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
851
824
|
this._crdtNotifier.rootSubject.next(storageRoot);
|
|
852
825
|
}
|
|
853
826
|
_getAuthFetchOptions(room) {
|
|
854
|
-
if (typeof this._endpoints.authEndpoint === "undefined")
|
|
855
|
-
return null;
|
|
827
|
+
if (typeof this._endpoints.authEndpoint === "undefined") return null;
|
|
856
828
|
if (this._endpoints.authEndpoint === true) {
|
|
857
829
|
return {
|
|
858
830
|
url: `/api/pluv/authorize?room=${room}`,
|
|
@@ -871,8 +843,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
871
843
|
_getAuthorization(room) {
|
|
872
844
|
return __async(this, null, function* () {
|
|
873
845
|
const fetchOptions = this._getAuthFetchOptions(room);
|
|
874
|
-
if (!fetchOptions)
|
|
875
|
-
return null;
|
|
846
|
+
if (!fetchOptions) return null;
|
|
876
847
|
const { url, options } = fetchOptions;
|
|
877
848
|
const res = yield fetch(url, options);
|
|
878
849
|
if (!res.ok || res.status !== 200) {
|
|
@@ -897,10 +868,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
897
868
|
}
|
|
898
869
|
_handleExit(message) {
|
|
899
870
|
const { connectionId } = message;
|
|
900
|
-
if (!connectionId)
|
|
901
|
-
|
|
902
|
-
if (!this._state.webSocket)
|
|
903
|
-
throw new Error("Could not find WebSocket");
|
|
871
|
+
if (!connectionId) return;
|
|
872
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
904
873
|
this._usersManager.removeUser(connectionId);
|
|
905
874
|
const others = this._usersManager.getOthers();
|
|
906
875
|
this._stateNotifier.subjects.others.next(others);
|
|
@@ -910,10 +879,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
910
879
|
_handlePresenceUpdatedMessage(message) {
|
|
911
880
|
var _a;
|
|
912
881
|
const { connectionId } = message;
|
|
913
|
-
if (!connectionId)
|
|
914
|
-
|
|
915
|
-
if (!this._state.webSocket)
|
|
916
|
-
throw new Error("Could not find WebSocket");
|
|
882
|
+
if (!connectionId) return;
|
|
883
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
917
884
|
const data = message.data;
|
|
918
885
|
this._usersManager.patchPresence(connectionId, data.presence);
|
|
919
886
|
const myPresence = this._usersManager.myPresence;
|
|
@@ -929,15 +896,11 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
929
896
|
this._stateNotifier.subjects["others"].next(others);
|
|
930
897
|
}
|
|
931
898
|
_handleReceiveOthers(message) {
|
|
932
|
-
if (!message.connectionId)
|
|
933
|
-
|
|
934
|
-
if (!this._state.webSocket)
|
|
935
|
-
throw new Error("Could not find WebSocket");
|
|
899
|
+
if (!message.connectionId) return;
|
|
900
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
936
901
|
const data = message.data;
|
|
937
902
|
Object.keys(data.others).forEach((connectionId) => {
|
|
938
903
|
const { presence, user } = data.others[connectionId];
|
|
939
|
-
if (!user)
|
|
940
|
-
return;
|
|
941
904
|
this._usersManager.setUser(connectionId, user);
|
|
942
905
|
const other = this._usersManager.getOther(connectionId);
|
|
943
906
|
!!presence && this._usersManager.patchPresence(connectionId, presence);
|
|
@@ -949,10 +912,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
949
912
|
_handleRegisteredMessage(message) {
|
|
950
913
|
var _a, _b;
|
|
951
914
|
const { connectionId, user } = message;
|
|
952
|
-
if (!connectionId)
|
|
953
|
-
|
|
954
|
-
if (!this._state.webSocket)
|
|
955
|
-
throw new Error("Could not find WebSocket");
|
|
915
|
+
if (!connectionId) return;
|
|
916
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
956
917
|
this._updateState((oldState) => {
|
|
957
918
|
oldState.connection.count += 1;
|
|
958
919
|
oldState.connection.id = connectionId;
|
|
@@ -973,10 +934,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
973
934
|
_handleStorageReceivedMessage(message) {
|
|
974
935
|
return __async(this, null, function* () {
|
|
975
936
|
const { connectionId } = message;
|
|
976
|
-
if (!connectionId)
|
|
977
|
-
|
|
978
|
-
if (!this._state.webSocket)
|
|
979
|
-
throw new Error("Could not find WebSocket");
|
|
937
|
+
if (!connectionId) return;
|
|
938
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
980
939
|
const data = message.data;
|
|
981
940
|
this._crdtManager.initialize({
|
|
982
941
|
onInitialized: (update2) => __async(this, null, function* () {
|
|
@@ -1000,23 +959,19 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1000
959
|
}
|
|
1001
960
|
_handleStorageUpdatedMessage(message) {
|
|
1002
961
|
const { connectionId } = message;
|
|
1003
|
-
if (!connectionId)
|
|
1004
|
-
|
|
1005
|
-
if (!this._state.webSocket)
|
|
1006
|
-
throw new Error("Could not find WebSocket");
|
|
962
|
+
if (!connectionId) return;
|
|
963
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
1007
964
|
const data = message.data;
|
|
1008
|
-
if (!this._crdtManager)
|
|
1009
|
-
return;
|
|
965
|
+
if (!this._crdtManager) return;
|
|
1010
966
|
this._crdtManager.doc.applyEncodedState({
|
|
1011
967
|
origin: ORIGIN_STORAGE_UPDATED,
|
|
1012
968
|
update: data.state
|
|
1013
969
|
});
|
|
1014
970
|
const sharedTypes = this._crdtManager.doc.get();
|
|
1015
|
-
const storageRoot = Object.
|
|
1016
|
-
(acc,
|
|
1017
|
-
if (!this._crdtManager)
|
|
1018
|
-
|
|
1019
|
-
const serialized = sharedType.toJson();
|
|
971
|
+
const storageRoot = Object.keys(sharedTypes).reduce(
|
|
972
|
+
(acc, prop) => {
|
|
973
|
+
if (!this._crdtManager) return acc;
|
|
974
|
+
const serialized = this._crdtManager.doc.toJson(prop);
|
|
1020
975
|
this._crdtNotifier.subject(prop).next(serialized);
|
|
1021
976
|
return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
|
|
1022
977
|
},
|
|
@@ -1026,12 +981,9 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1026
981
|
}
|
|
1027
982
|
_handleUserJoinedMessage(message) {
|
|
1028
983
|
const { connectionId } = message;
|
|
1029
|
-
if (!connectionId)
|
|
1030
|
-
|
|
1031
|
-
if (!this.
|
|
1032
|
-
throw new Error("Could not find WebSocket");
|
|
1033
|
-
if (!this._usersManager.myself)
|
|
1034
|
-
return;
|
|
984
|
+
if (!connectionId) return;
|
|
985
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
986
|
+
if (!this._usersManager.myself) return;
|
|
1035
987
|
const data = message.data;
|
|
1036
988
|
const myself = this._usersManager.myself;
|
|
1037
989
|
if (myself.connectionId === connectionId) {
|
|
@@ -1050,15 +1002,13 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1050
1002
|
}
|
|
1051
1003
|
_logDebug(...data) {
|
|
1052
1004
|
var _a;
|
|
1053
|
-
if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production")
|
|
1054
|
-
return;
|
|
1005
|
+
if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production") return;
|
|
1055
1006
|
this._debug && console.log(...data);
|
|
1056
1007
|
}
|
|
1057
1008
|
_observeCrdt() {
|
|
1058
1009
|
var _a, _b;
|
|
1059
1010
|
(_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
|
|
1060
|
-
if (!this._crdtManager)
|
|
1061
|
-
return;
|
|
1011
|
+
if (!this._crdtManager) return;
|
|
1062
1012
|
const unsubscribe = this._crdtManager.doc.subscribe((event) => {
|
|
1063
1013
|
var _a2;
|
|
1064
1014
|
const origin = (_a2 = event.origin) != null ? _a2 : null;
|
|
@@ -1067,12 +1017,9 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1067
1017
|
if (origin === ORIGIN_INITIALIZED || origin === ORIGIN_STORAGE_UPDATED) {
|
|
1068
1018
|
return;
|
|
1069
1019
|
}
|
|
1070
|
-
if (!this._state.webSocket)
|
|
1071
|
-
|
|
1072
|
-
if (
|
|
1073
|
-
return;
|
|
1074
|
-
if (this._state.webSocket.readyState !== WebSocket.OPEN)
|
|
1075
|
-
return;
|
|
1020
|
+
if (!this._state.webSocket) return;
|
|
1021
|
+
if (!this._state.connection.id) return;
|
|
1022
|
+
if (this._state.webSocket.readyState !== WebSocket.OPEN) return;
|
|
1076
1023
|
this._sendMessage({
|
|
1077
1024
|
type: "$UPDATE_STORAGE",
|
|
1078
1025
|
data: { origin, update: event.update }
|
|
@@ -1120,16 +1067,14 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1120
1067
|
}
|
|
1121
1068
|
_onMessage(event) {
|
|
1122
1069
|
const message = this._parseMessage(event);
|
|
1123
|
-
if (!message)
|
|
1124
|
-
return;
|
|
1070
|
+
if (!message) return;
|
|
1125
1071
|
const shouldLog = typeof this._debug === "boolean" ? this._debug : this._debug.output.find((value) => value === message.type);
|
|
1126
1072
|
if (shouldLog) {
|
|
1127
1073
|
this._logDebug("WebSocket event received: ", message.type, message);
|
|
1128
1074
|
}
|
|
1129
1075
|
this._eventNotifier.subject(message.type).next(message);
|
|
1130
1076
|
const { connectionId } = message;
|
|
1131
|
-
if (!connectionId)
|
|
1132
|
-
return;
|
|
1077
|
+
if (!connectionId) return;
|
|
1133
1078
|
switch (message.type) {
|
|
1134
1079
|
case "$EXIT": {
|
|
1135
1080
|
this._handleExit(message);
|
|
@@ -1167,8 +1112,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1167
1112
|
}
|
|
1168
1113
|
}
|
|
1169
1114
|
_onOpen() {
|
|
1170
|
-
if (this._state.connection.state !== "Connecting" /* Connecting */)
|
|
1171
|
-
return;
|
|
1115
|
+
if (this._state.connection.state !== "Connecting" /* Connecting */) return;
|
|
1172
1116
|
this._logDebug("WebSocket connected");
|
|
1173
1117
|
this._updateState((oldState) => {
|
|
1174
1118
|
oldState.connection.state = "Open" /* Open */;
|
|
@@ -1187,10 +1131,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1187
1131
|
}
|
|
1188
1132
|
_onVisibilityChange() {
|
|
1189
1133
|
return __async(this, null, function* () {
|
|
1190
|
-
if (typeof document === "undefined")
|
|
1191
|
-
|
|
1192
|
-
if (document.visibilityState !== "visible")
|
|
1193
|
-
return;
|
|
1134
|
+
if (typeof document === "undefined") return;
|
|
1135
|
+
if (document.visibilityState !== "visible") return;
|
|
1194
1136
|
switch (this._state.connection.state) {
|
|
1195
1137
|
case "Open" /* Open */:
|
|
1196
1138
|
this._clearInterval(this._intervals.heartbeat);
|
|
@@ -1204,16 +1146,12 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1204
1146
|
});
|
|
1205
1147
|
}
|
|
1206
1148
|
_parseMessage(message) {
|
|
1207
|
-
if (typeof message.data !== "string")
|
|
1208
|
-
return null;
|
|
1149
|
+
if (typeof message.data !== "string") return null;
|
|
1209
1150
|
try {
|
|
1210
1151
|
const config = JSON.parse(message.data);
|
|
1211
|
-
if (typeof config !== "object")
|
|
1212
|
-
|
|
1213
|
-
if (typeof config.
|
|
1214
|
-
return null;
|
|
1215
|
-
if (typeof config.data !== "object")
|
|
1216
|
-
return null;
|
|
1152
|
+
if (typeof config !== "object") return null;
|
|
1153
|
+
if (typeof config.type !== "string") return null;
|
|
1154
|
+
if (typeof config.data !== "object") return null;
|
|
1217
1155
|
return config;
|
|
1218
1156
|
} catch (e) {
|
|
1219
1157
|
return null;
|
|
@@ -1224,8 +1162,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1224
1162
|
if (!this._endpoints.wsEndpoint) {
|
|
1225
1163
|
throw new Error("Must provide an wsEndpoint.");
|
|
1226
1164
|
}
|
|
1227
|
-
if (!this._state.webSocket)
|
|
1228
|
-
return;
|
|
1165
|
+
if (!this._state.webSocket) return;
|
|
1229
1166
|
this._closeWs();
|
|
1230
1167
|
this._updateState((oldState) => {
|
|
1231
1168
|
oldState.authorization.token = null;
|
|
@@ -1238,8 +1175,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1238
1175
|
}
|
|
1239
1176
|
_sendMessage(data) {
|
|
1240
1177
|
const webSocket = this._state.webSocket;
|
|
1241
|
-
if ((webSocket == null ? void 0 : webSocket.readyState) !== WebSocket.OPEN)
|
|
1242
|
-
return;
|
|
1178
|
+
if ((webSocket == null ? void 0 : webSocket.readyState) !== WebSocket.OPEN) return;
|
|
1243
1179
|
const shouldLog = typeof this._debug === "boolean" ? this._debug : this._debug.input.find((value) => value === data.type);
|
|
1244
1180
|
if (shouldLog) {
|
|
1245
1181
|
this._logDebug("WebSocket event sent: ", data.type, data);
|
|
@@ -1272,8 +1208,7 @@ var PluvClient = class {
|
|
|
1272
1208
|
this._rooms = /* @__PURE__ */ new Map();
|
|
1273
1209
|
this.createRoom = (room, options) => {
|
|
1274
1210
|
const oldRoom = this.getRoom(room);
|
|
1275
|
-
if (oldRoom)
|
|
1276
|
-
return oldRoom;
|
|
1211
|
+
if (oldRoom) return oldRoom;
|
|
1277
1212
|
const newRoom = new PluvRoom(room, {
|
|
1278
1213
|
addons: options.addons,
|
|
1279
1214
|
authEndpoint: this._authEndpoint,
|
|
@@ -1306,8 +1241,7 @@ var PluvClient = class {
|
|
|
1306
1241
|
};
|
|
1307
1242
|
this.leave = (room) => __async(this, null, function* () {
|
|
1308
1243
|
const toLeave = typeof room === "string" ? this.getRoom(room) : room;
|
|
1309
|
-
if (!toLeave)
|
|
1310
|
-
return;
|
|
1244
|
+
if (!toLeave) return;
|
|
1311
1245
|
yield toLeave.disconnect();
|
|
1312
1246
|
this._rooms.delete(toLeave.id);
|
|
1313
1247
|
this._logDebug(`Left and deleted room: ${room}`);
|
|
@@ -1319,8 +1253,7 @@ var PluvClient = class {
|
|
|
1319
1253
|
}
|
|
1320
1254
|
_logDebug(...data) {
|
|
1321
1255
|
var _a;
|
|
1322
|
-
if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production")
|
|
1323
|
-
return;
|
|
1256
|
+
if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production") return;
|
|
1324
1257
|
this._debug && console.log(...data);
|
|
1325
1258
|
}
|
|
1326
1259
|
};
|