@pluv/client 0.18.0 → 0.19.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 +24 -0
- package/dist/index.d.mts +12 -11
- package/dist/index.d.ts +12 -11
- package/dist/index.js +93 -172
- package/dist/index.mjs +93 -172
- package/package.json +6 -6
- package/src/AbstractRoom.ts +2 -2
- package/src/MockedRoom.ts +13 -5
- package/src/OtherNotifier.ts +2 -6
- package/src/PluvRoom.ts +17 -10
- package/src/types.ts +10 -6
package/dist/index.js
CHANGED
|
@@ -97,8 +97,7 @@ var CrdtManager = class {
|
|
|
97
97
|
* @date June 13, 2023
|
|
98
98
|
*/
|
|
99
99
|
applyUpdate(update, origin) {
|
|
100
|
-
if (!this.initialized)
|
|
101
|
-
return this;
|
|
100
|
+
if (!this.initialized) return this;
|
|
102
101
|
const updates = typeof update === "string" ? [update] : update;
|
|
103
102
|
this._applyDocUpdates(this.doc, updates, origin);
|
|
104
103
|
return this;
|
|
@@ -112,8 +111,7 @@ var CrdtManager = class {
|
|
|
112
111
|
initialize(params) {
|
|
113
112
|
const { onInitialized, origin, update } = params;
|
|
114
113
|
const updates = typeof update === "string" ? [update] : update;
|
|
115
|
-
if (!updates.length)
|
|
116
|
-
return this;
|
|
114
|
+
if (!updates.length) return this;
|
|
117
115
|
if (this.initialized) {
|
|
118
116
|
this.doc = this._applyDocUpdates(this.doc, updates, origin).track();
|
|
119
117
|
return this;
|
|
@@ -173,8 +171,7 @@ var EventNotifier = class {
|
|
|
173
171
|
}
|
|
174
172
|
subject(name) {
|
|
175
173
|
const subject = this.subjects.get(name);
|
|
176
|
-
if (subject)
|
|
177
|
-
return subject;
|
|
174
|
+
if (subject) return subject;
|
|
178
175
|
const newSubject = (0, import_wonka2.makeSubject)();
|
|
179
176
|
this.subjects.set(name, newSubject);
|
|
180
177
|
return newSubject;
|
|
@@ -193,8 +190,7 @@ var OtherNotifier = class {
|
|
|
193
190
|
}
|
|
194
191
|
subject(name) {
|
|
195
192
|
const subject = this.subjects.get(name);
|
|
196
|
-
if (subject)
|
|
197
|
-
return subject;
|
|
193
|
+
if (subject) return subject;
|
|
198
194
|
const newSubject = (0, import_wonka3.makeSubject)();
|
|
199
195
|
this.subjects.set(name, newSubject);
|
|
200
196
|
return newSubject;
|
|
@@ -220,8 +216,7 @@ var StateNotifier = class {
|
|
|
220
216
|
subscribe(name, callback) {
|
|
221
217
|
var _a;
|
|
222
218
|
const source = (_a = this.subjects[name]) == null ? void 0 : _a.source;
|
|
223
|
-
if (!source)
|
|
224
|
-
throw new Error(`Could not subscribe to source: ${name}`);
|
|
219
|
+
if (!source) throw new Error(`Could not subscribe to source: ${name}`);
|
|
225
220
|
return (0, import_wonka4.subscribe)(callback)(source).unsubscribe;
|
|
226
221
|
}
|
|
227
222
|
};
|
|
@@ -265,8 +260,7 @@ var UsersManager = class {
|
|
|
265
260
|
return;
|
|
266
261
|
}
|
|
267
262
|
const other = this._others.get(connectionId);
|
|
268
|
-
if (!other)
|
|
269
|
-
return;
|
|
263
|
+
if (!other) return;
|
|
270
264
|
const presence = __spreadValues(__spreadValues({}, other.presence), patch);
|
|
271
265
|
(_b = this._presence) == null ? void 0 : _b.parse(presence);
|
|
272
266
|
this._others.set(connectionId, __spreadProps(__spreadValues({}, other), { presence }));
|
|
@@ -293,17 +287,14 @@ var UsersManager = class {
|
|
|
293
287
|
return;
|
|
294
288
|
}
|
|
295
289
|
const other = this._others.get(connectionId);
|
|
296
|
-
if (!other)
|
|
297
|
-
return;
|
|
290
|
+
if (!other) return;
|
|
298
291
|
this._others.set(connectionId, __spreadProps(__spreadValues({}, other), { presence }));
|
|
299
292
|
}
|
|
300
293
|
setUser(connectionId, user, presence) {
|
|
301
294
|
var _a;
|
|
302
|
-
if (((_a = this._myself) == null ? void 0 : _a.connectionId) === connectionId)
|
|
303
|
-
return;
|
|
295
|
+
if (((_a = this._myself) == null ? void 0 : _a.connectionId) === connectionId) return;
|
|
304
296
|
const other = this._others.get(connectionId);
|
|
305
|
-
if (other)
|
|
306
|
-
return;
|
|
297
|
+
if (other) return;
|
|
307
298
|
this._others.set(connectionId, {
|
|
308
299
|
connectionId,
|
|
309
300
|
presence: presence != null ? presence : this._initialPresence,
|
|
@@ -315,8 +306,7 @@ var UsersManager = class {
|
|
|
315
306
|
*/
|
|
316
307
|
updateMyPresence(presence) {
|
|
317
308
|
this._myPresence = __spreadValues(__spreadValues({}, this._myPresence), presence);
|
|
318
|
-
if (!this._myself)
|
|
319
|
-
return;
|
|
309
|
+
if (!this._myself) return;
|
|
320
310
|
this._myself.presence = this._myPresence;
|
|
321
311
|
}
|
|
322
312
|
};
|
|
@@ -364,6 +354,9 @@ var MockedRoom = class extends AbstractRoom {
|
|
|
364
354
|
this.event = (event, callback) => {
|
|
365
355
|
return this._eventNotifier.subscribe(event, callback);
|
|
366
356
|
};
|
|
357
|
+
this.getConnection = () => {
|
|
358
|
+
return Object.freeze(JSON.parse(JSON.stringify(this._state.connection)));
|
|
359
|
+
};
|
|
367
360
|
this.getMyPresence = () => {
|
|
368
361
|
return this._usersManager.myPresence;
|
|
369
362
|
};
|
|
@@ -378,8 +371,7 @@ var MockedRoom = class extends AbstractRoom {
|
|
|
378
371
|
};
|
|
379
372
|
this.getStorage = (key) => {
|
|
380
373
|
const sharedType = this._crdtManager.get(key);
|
|
381
|
-
if (typeof sharedType === "undefined")
|
|
382
|
-
return null;
|
|
374
|
+
if (typeof sharedType === "undefined") return null;
|
|
383
375
|
return sharedType.value;
|
|
384
376
|
};
|
|
385
377
|
this.other = (connectionId, callback) => {
|
|
@@ -400,10 +392,8 @@ var MockedRoom = class extends AbstractRoom {
|
|
|
400
392
|
this.transact = (fn, origin) => {
|
|
401
393
|
const _origin = origin != null ? origin : this._state.connection.id;
|
|
402
394
|
const crdtManager = this._crdtManager;
|
|
403
|
-
if (typeof _origin !== "string")
|
|
404
|
-
|
|
405
|
-
if (!crdtManager)
|
|
406
|
-
return;
|
|
395
|
+
if (typeof _origin !== "string") return;
|
|
396
|
+
if (!crdtManager) return;
|
|
407
397
|
crdtManager.doc.transact(() => {
|
|
408
398
|
const storage = crdtManager.doc.get();
|
|
409
399
|
fn(storage);
|
|
@@ -414,7 +404,8 @@ var MockedRoom = class extends AbstractRoom {
|
|
|
414
404
|
};
|
|
415
405
|
this.updateMyPresence = (presence) => {
|
|
416
406
|
var _a;
|
|
417
|
-
this.
|
|
407
|
+
const newPresence = typeof presence === "function" ? presence(this.getMyPresence()) : presence;
|
|
408
|
+
this._usersManager.updateMyPresence(newPresence);
|
|
418
409
|
const myPresence = this._usersManager.myPresence;
|
|
419
410
|
const myself = (_a = this._usersManager.myself) != null ? _a : null;
|
|
420
411
|
this._stateNotifier.subjects["my-presence"].next(myPresence);
|
|
@@ -434,43 +425,33 @@ var MockedRoom = class extends AbstractRoom {
|
|
|
434
425
|
return true;
|
|
435
426
|
}
|
|
436
427
|
broadcast(event, data) {
|
|
437
|
-
if (!this._events)
|
|
438
|
-
return;
|
|
428
|
+
if (!this._events) return;
|
|
439
429
|
const resolver = this._events[event];
|
|
440
|
-
if (!resolver)
|
|
441
|
-
return;
|
|
430
|
+
if (!resolver) return;
|
|
442
431
|
const result = resolver(data);
|
|
443
432
|
Object.keys(result).forEach((type) => {
|
|
444
433
|
const _type = type.toString();
|
|
445
434
|
const data2 = result[_type];
|
|
446
|
-
if (!data2)
|
|
447
|
-
return;
|
|
435
|
+
if (!data2) return;
|
|
448
436
|
this._eventNotifier.subject(_type).next(data2);
|
|
449
437
|
});
|
|
450
438
|
}
|
|
451
|
-
getConnection() {
|
|
452
|
-
return Object.freeze(JSON.parse(JSON.stringify(this._state.connection)));
|
|
453
|
-
}
|
|
454
439
|
getDoc() {
|
|
455
440
|
return this._crdtManager.doc;
|
|
456
441
|
}
|
|
457
442
|
_observeCrdt() {
|
|
458
443
|
var _a, _b;
|
|
459
444
|
(_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
|
|
460
|
-
if (!this._crdtManager)
|
|
461
|
-
return;
|
|
445
|
+
if (!this._crdtManager) return;
|
|
462
446
|
const unsubscribe = this._crdtManager.doc.subscribe((event) => {
|
|
463
447
|
var _a2;
|
|
464
448
|
const origin = (_a2 = event.origin) != null ? _a2 : null;
|
|
465
|
-
if (!this._crdtManager)
|
|
466
|
-
|
|
467
|
-
if (origin === "$STORAGE_UPDATED")
|
|
468
|
-
return;
|
|
449
|
+
if (!this._crdtManager) return;
|
|
450
|
+
if (origin === "$STORAGE_UPDATED") return;
|
|
469
451
|
const sharedTypes = this._crdtManager.doc.get();
|
|
470
452
|
const storageRoot = Object.entries(sharedTypes).reduce(
|
|
471
453
|
(acc, [prop, sharedType]) => {
|
|
472
|
-
if (!this._crdtManager)
|
|
473
|
-
return acc;
|
|
454
|
+
if (!this._crdtManager) return acc;
|
|
474
455
|
const serialized = sharedType.toJson();
|
|
475
456
|
this._crdtNotifier.subject(prop).next(serialized);
|
|
476
457
|
return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
|
|
@@ -514,12 +495,10 @@ var debounce = (fn, options) => {
|
|
|
514
495
|
let prevResult;
|
|
515
496
|
let timeout = null;
|
|
516
497
|
return (...args) => {
|
|
517
|
-
if (typeof timeout === "number")
|
|
518
|
-
clearTimeout(timeout);
|
|
498
|
+
if (typeof timeout === "number") clearTimeout(timeout);
|
|
519
499
|
timeout = setTimeout(() => {
|
|
520
500
|
timeout = null;
|
|
521
|
-
if (!immediate)
|
|
522
|
-
prevResult = fn(args);
|
|
501
|
+
if (!immediate) prevResult = fn(args);
|
|
523
502
|
}, wait);
|
|
524
503
|
if (immediate && !timeout) {
|
|
525
504
|
fn(args);
|
|
@@ -582,10 +561,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
582
561
|
this._wsListeners = null;
|
|
583
562
|
this.broadcast = new Proxy(
|
|
584
563
|
(event, data) => {
|
|
585
|
-
if (!this._state.webSocket)
|
|
586
|
-
|
|
587
|
-
if (this._state.connection.state !== "Open" /* Open */)
|
|
588
|
-
return;
|
|
564
|
+
if (!this._state.webSocket) return;
|
|
565
|
+
if (this._state.connection.state !== "Open" /* Open */) return;
|
|
589
566
|
const type = event.toString();
|
|
590
567
|
this._sendMessage({ data, type });
|
|
591
568
|
},
|
|
@@ -611,6 +588,12 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
611
588
|
}
|
|
612
589
|
}
|
|
613
590
|
);
|
|
591
|
+
this.getConnection = () => {
|
|
592
|
+
return Object.freeze(JSON.parse(JSON.stringify(this._state.connection)));
|
|
593
|
+
};
|
|
594
|
+
this.getDoc = () => {
|
|
595
|
+
return this._crdtManager.doc;
|
|
596
|
+
};
|
|
614
597
|
this.getMyPresence = () => {
|
|
615
598
|
return this._usersManager.myPresence;
|
|
616
599
|
};
|
|
@@ -625,8 +608,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
625
608
|
};
|
|
626
609
|
this.getStorage = (key) => {
|
|
627
610
|
const sharedType = this._crdtManager.get(key);
|
|
628
|
-
if (typeof sharedType === "undefined")
|
|
629
|
-
return null;
|
|
611
|
+
if (typeof sharedType === "undefined") return null;
|
|
630
612
|
return sharedType;
|
|
631
613
|
};
|
|
632
614
|
this.other = (connectionId, callback) => {
|
|
@@ -646,8 +628,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
646
628
|
};
|
|
647
629
|
this.transact = (fn, origin) => {
|
|
648
630
|
const _origin = origin != null ? origin : this._state.connection.id;
|
|
649
|
-
if (typeof _origin !== "string")
|
|
650
|
-
return;
|
|
631
|
+
if (typeof _origin !== "string") return;
|
|
651
632
|
this._crdtManager.doc.transact(() => {
|
|
652
633
|
const storage = this._crdtManager.doc.get();
|
|
653
634
|
fn(storage);
|
|
@@ -658,18 +639,18 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
658
639
|
};
|
|
659
640
|
this.updateMyPresence = (presence) => {
|
|
660
641
|
var _a;
|
|
661
|
-
this.
|
|
642
|
+
const newPresence = typeof presence === "function" ? presence(this.getMyPresence()) : presence;
|
|
643
|
+
this._usersManager.updateMyPresence(newPresence);
|
|
662
644
|
const myPresence = this._usersManager.myPresence;
|
|
663
645
|
const myself = (_a = this._usersManager.myself) != null ? _a : null;
|
|
664
646
|
this._stateNotifier.subjects["my-presence"].next(myPresence);
|
|
665
647
|
!!myself && this._stateNotifier.subjects["myself"].next(myself);
|
|
666
|
-
this.broadcast("$UPDATE_PRESENCE", { presence });
|
|
648
|
+
this.broadcast("$UPDATE_PRESENCE", { presence: newPresence });
|
|
667
649
|
};
|
|
668
650
|
this._flattenStorageStore = debounce(
|
|
669
651
|
() => __async(this, null, function* () {
|
|
670
652
|
const shouldFlatten = yield this._storageStore.getShouldFlatten();
|
|
671
|
-
if (!shouldFlatten)
|
|
672
|
-
return;
|
|
653
|
+
if (!shouldFlatten) return;
|
|
673
654
|
const updates = yield this._storageStore.getUpdates();
|
|
674
655
|
this._crdtManager.applyUpdate(updates, this);
|
|
675
656
|
const encodedState = this._crdtManager.doc.getEncodedState();
|
|
@@ -717,8 +698,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
717
698
|
const canConnect = ["Closed" /* Closed */, "Untouched" /* Untouched */].some(
|
|
718
699
|
(state) => this._state.connection.state === state
|
|
719
700
|
);
|
|
720
|
-
if (!canConnect)
|
|
721
|
-
return;
|
|
701
|
+
if (!canConnect) return;
|
|
722
702
|
this._clearTimeout(this._timeouts.reconnect);
|
|
723
703
|
this._updateState((oldState) => {
|
|
724
704
|
oldState.connection.state = "Connecting" /* Connecting */;
|
|
@@ -752,13 +732,11 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
752
732
|
if (!this._endpoints.wsEndpoint) {
|
|
753
733
|
throw new Error("Must provide an wsEndpoint.");
|
|
754
734
|
}
|
|
755
|
-
if (!this._state.webSocket)
|
|
756
|
-
return;
|
|
735
|
+
if (!this._state.webSocket) return;
|
|
757
736
|
const canDisconnect = ["Connecting" /* Connecting */, "Open" /* Open */].some(
|
|
758
737
|
(state) => this._state.connection.state === state
|
|
759
738
|
);
|
|
760
|
-
if (!canDisconnect)
|
|
761
|
-
return;
|
|
739
|
+
if (!canDisconnect) return;
|
|
762
740
|
this._clearInterval(this._intervals.heartbeat);
|
|
763
741
|
this._closeWs();
|
|
764
742
|
yield this._storageStore.destroy();
|
|
@@ -766,12 +744,6 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
766
744
|
this._stateNotifier.subjects["my-presence"].next(null);
|
|
767
745
|
});
|
|
768
746
|
}
|
|
769
|
-
getConnection() {
|
|
770
|
-
return Object.freeze(JSON.parse(JSON.stringify(this._state.connection)));
|
|
771
|
-
}
|
|
772
|
-
getDoc() {
|
|
773
|
-
return this._crdtManager.doc;
|
|
774
|
-
}
|
|
775
747
|
_addToStorageStore(update) {
|
|
776
748
|
return __async(this, null, function* () {
|
|
777
749
|
yield this._storageStore.addUpdate(update);
|
|
@@ -793,10 +765,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
793
765
|
});
|
|
794
766
|
}
|
|
795
767
|
_attachWindowListeners() {
|
|
796
|
-
if (typeof window === "undefined")
|
|
797
|
-
|
|
798
|
-
if (typeof document === "undefined")
|
|
799
|
-
return;
|
|
768
|
+
if (typeof window === "undefined") return;
|
|
769
|
+
if (typeof document === "undefined") return;
|
|
800
770
|
this._windowListeners = {
|
|
801
771
|
onNavigatorOnline: this._onNavigatorOnline.bind(this),
|
|
802
772
|
onVisibilityChange: this._onVisibilityChange.bind(this)
|
|
@@ -805,8 +775,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
805
775
|
document.addEventListener("visibilitychange", this._windowListeners.onVisibilityChange);
|
|
806
776
|
}
|
|
807
777
|
_attachWsListeners() {
|
|
808
|
-
if (!this._state.webSocket)
|
|
809
|
-
return;
|
|
778
|
+
if (!this._state.webSocket) return;
|
|
810
779
|
this._wsListeners = {
|
|
811
780
|
onClose: this._onClose.bind(this),
|
|
812
781
|
onError: this._onError.bind(this),
|
|
@@ -819,19 +788,16 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
819
788
|
this._state.webSocket.addEventListener("open", this._wsListeners.onOpen);
|
|
820
789
|
}
|
|
821
790
|
_clearInterval(interval) {
|
|
822
|
-
if (Number.isNaN(interval))
|
|
823
|
-
return;
|
|
791
|
+
if (Number.isNaN(interval)) return;
|
|
824
792
|
clearInterval(interval);
|
|
825
793
|
}
|
|
826
794
|
_clearTimeout(timeout) {
|
|
827
|
-
if (Number.isNaN(timeout))
|
|
828
|
-
return;
|
|
795
|
+
if (Number.isNaN(timeout)) return;
|
|
829
796
|
clearTimeout(timeout);
|
|
830
797
|
}
|
|
831
798
|
_closeWs() {
|
|
832
799
|
var _a, _b;
|
|
833
|
-
if (!this._state.webSocket)
|
|
834
|
-
return;
|
|
800
|
+
if (!this._state.webSocket) return;
|
|
835
801
|
(_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
|
|
836
802
|
this._clearTimeout(this._timeouts.pong);
|
|
837
803
|
this._clearTimeout(this._timeouts.reconnect);
|
|
@@ -843,24 +809,19 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
843
809
|
return readyState === ((_a2 = this._state.webSocket) == null ? void 0 : _a2.readyState);
|
|
844
810
|
}
|
|
845
811
|
);
|
|
846
|
-
if (canClose)
|
|
847
|
-
this._state.webSocket.close();
|
|
812
|
+
if (canClose) this._state.webSocket.close();
|
|
848
813
|
this._detachWsListeners();
|
|
849
814
|
}
|
|
850
815
|
_detachWindowListeners() {
|
|
851
|
-
if (!this._windowListeners)
|
|
852
|
-
|
|
853
|
-
if (typeof
|
|
854
|
-
return;
|
|
855
|
-
if (typeof document === "undefined")
|
|
856
|
-
return;
|
|
816
|
+
if (!this._windowListeners) return;
|
|
817
|
+
if (typeof window === "undefined") return;
|
|
818
|
+
if (typeof document === "undefined") return;
|
|
857
819
|
window.removeEventListener("online", this._windowListeners.onNavigatorOnline);
|
|
858
820
|
document.removeEventListener("visibilitychange", this._windowListeners.onVisibilityChange);
|
|
859
821
|
this._windowListeners = null;
|
|
860
822
|
}
|
|
861
823
|
_detachWsListeners() {
|
|
862
|
-
if (!this._state.webSocket || !this._wsListeners)
|
|
863
|
-
return;
|
|
824
|
+
if (!this._state.webSocket || !this._wsListeners) return;
|
|
864
825
|
this._state.webSocket.removeEventListener("close", this._wsListeners.onClose);
|
|
865
826
|
this._state.webSocket.removeEventListener("error", this._wsListeners.onError);
|
|
866
827
|
this._state.webSocket.removeEventListener("message", this._wsListeners.onMessage);
|
|
@@ -880,8 +841,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
880
841
|
this._crdtNotifier.rootSubject.next(storageRoot);
|
|
881
842
|
}
|
|
882
843
|
_getAuthFetchOptions(room) {
|
|
883
|
-
if (typeof this._endpoints.authEndpoint === "undefined")
|
|
884
|
-
return null;
|
|
844
|
+
if (typeof this._endpoints.authEndpoint === "undefined") return null;
|
|
885
845
|
if (this._endpoints.authEndpoint === true) {
|
|
886
846
|
return {
|
|
887
847
|
url: `/api/pluv/authorize?room=${room}`,
|
|
@@ -900,8 +860,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
900
860
|
_getAuthorization(room) {
|
|
901
861
|
return __async(this, null, function* () {
|
|
902
862
|
const fetchOptions = this._getAuthFetchOptions(room);
|
|
903
|
-
if (!fetchOptions)
|
|
904
|
-
return null;
|
|
863
|
+
if (!fetchOptions) return null;
|
|
905
864
|
const { url, options } = fetchOptions;
|
|
906
865
|
const res = yield fetch(url, options);
|
|
907
866
|
if (!res.ok || res.status !== 200) {
|
|
@@ -926,10 +885,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
926
885
|
}
|
|
927
886
|
_handleExit(message) {
|
|
928
887
|
const { connectionId } = message;
|
|
929
|
-
if (!connectionId)
|
|
930
|
-
|
|
931
|
-
if (!this._state.webSocket)
|
|
932
|
-
throw new Error("Could not find WebSocket");
|
|
888
|
+
if (!connectionId) return;
|
|
889
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
933
890
|
this._usersManager.removeUser(connectionId);
|
|
934
891
|
const others = this._usersManager.getOthers();
|
|
935
892
|
this._stateNotifier.subjects.others.next(others);
|
|
@@ -939,10 +896,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
939
896
|
_handlePresenceUpdatedMessage(message) {
|
|
940
897
|
var _a;
|
|
941
898
|
const { connectionId } = message;
|
|
942
|
-
if (!connectionId)
|
|
943
|
-
|
|
944
|
-
if (!this._state.webSocket)
|
|
945
|
-
throw new Error("Could not find WebSocket");
|
|
899
|
+
if (!connectionId) return;
|
|
900
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
946
901
|
const data = message.data;
|
|
947
902
|
this._usersManager.patchPresence(connectionId, data.presence);
|
|
948
903
|
const myPresence = this._usersManager.myPresence;
|
|
@@ -958,15 +913,11 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
958
913
|
this._stateNotifier.subjects["others"].next(others);
|
|
959
914
|
}
|
|
960
915
|
_handleReceiveOthers(message) {
|
|
961
|
-
if (!message.connectionId)
|
|
962
|
-
|
|
963
|
-
if (!this._state.webSocket)
|
|
964
|
-
throw new Error("Could not find WebSocket");
|
|
916
|
+
if (!message.connectionId) return;
|
|
917
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
965
918
|
const data = message.data;
|
|
966
919
|
Object.keys(data.others).forEach((connectionId) => {
|
|
967
920
|
const { presence, user } = data.others[connectionId];
|
|
968
|
-
if (!user)
|
|
969
|
-
return;
|
|
970
921
|
this._usersManager.setUser(connectionId, user);
|
|
971
922
|
const other = this._usersManager.getOther(connectionId);
|
|
972
923
|
!!presence && this._usersManager.patchPresence(connectionId, presence);
|
|
@@ -978,10 +929,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
978
929
|
_handleRegisteredMessage(message) {
|
|
979
930
|
var _a, _b;
|
|
980
931
|
const { connectionId, user } = message;
|
|
981
|
-
if (!connectionId)
|
|
982
|
-
|
|
983
|
-
if (!this._state.webSocket)
|
|
984
|
-
throw new Error("Could not find WebSocket");
|
|
932
|
+
if (!connectionId) return;
|
|
933
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
985
934
|
this._updateState((oldState) => {
|
|
986
935
|
oldState.connection.count += 1;
|
|
987
936
|
oldState.connection.id = connectionId;
|
|
@@ -1002,10 +951,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1002
951
|
_handleStorageReceivedMessage(message) {
|
|
1003
952
|
return __async(this, null, function* () {
|
|
1004
953
|
const { connectionId } = message;
|
|
1005
|
-
if (!connectionId)
|
|
1006
|
-
|
|
1007
|
-
if (!this._state.webSocket)
|
|
1008
|
-
throw new Error("Could not find WebSocket");
|
|
954
|
+
if (!connectionId) return;
|
|
955
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
1009
956
|
const data = message.data;
|
|
1010
957
|
this._crdtManager.initialize({
|
|
1011
958
|
onInitialized: (update2) => __async(this, null, function* () {
|
|
@@ -1029,13 +976,10 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1029
976
|
}
|
|
1030
977
|
_handleStorageUpdatedMessage(message) {
|
|
1031
978
|
const { connectionId } = message;
|
|
1032
|
-
if (!connectionId)
|
|
1033
|
-
|
|
1034
|
-
if (!this._state.webSocket)
|
|
1035
|
-
throw new Error("Could not find WebSocket");
|
|
979
|
+
if (!connectionId) return;
|
|
980
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
1036
981
|
const data = message.data;
|
|
1037
|
-
if (!this._crdtManager)
|
|
1038
|
-
return;
|
|
982
|
+
if (!this._crdtManager) return;
|
|
1039
983
|
this._crdtManager.doc.applyEncodedState({
|
|
1040
984
|
origin: ORIGIN_STORAGE_UPDATED,
|
|
1041
985
|
update: data.state
|
|
@@ -1043,8 +987,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1043
987
|
const sharedTypes = this._crdtManager.doc.get();
|
|
1044
988
|
const storageRoot = Object.entries(sharedTypes).reduce(
|
|
1045
989
|
(acc, [prop, sharedType]) => {
|
|
1046
|
-
if (!this._crdtManager)
|
|
1047
|
-
return acc;
|
|
990
|
+
if (!this._crdtManager) return acc;
|
|
1048
991
|
const serialized = sharedType.toJson();
|
|
1049
992
|
this._crdtNotifier.subject(prop).next(serialized);
|
|
1050
993
|
return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
|
|
@@ -1055,12 +998,9 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1055
998
|
}
|
|
1056
999
|
_handleUserJoinedMessage(message) {
|
|
1057
1000
|
const { connectionId } = message;
|
|
1058
|
-
if (!connectionId)
|
|
1059
|
-
|
|
1060
|
-
if (!this.
|
|
1061
|
-
throw new Error("Could not find WebSocket");
|
|
1062
|
-
if (!this._usersManager.myself)
|
|
1063
|
-
return;
|
|
1001
|
+
if (!connectionId) return;
|
|
1002
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
1003
|
+
if (!this._usersManager.myself) return;
|
|
1064
1004
|
const data = message.data;
|
|
1065
1005
|
const myself = this._usersManager.myself;
|
|
1066
1006
|
if (myself.connectionId === connectionId) {
|
|
@@ -1079,15 +1019,13 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1079
1019
|
}
|
|
1080
1020
|
_logDebug(...data) {
|
|
1081
1021
|
var _a;
|
|
1082
|
-
if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production")
|
|
1083
|
-
return;
|
|
1022
|
+
if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production") return;
|
|
1084
1023
|
this._debug && console.log(...data);
|
|
1085
1024
|
}
|
|
1086
1025
|
_observeCrdt() {
|
|
1087
1026
|
var _a, _b;
|
|
1088
1027
|
(_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
|
|
1089
|
-
if (!this._crdtManager)
|
|
1090
|
-
return;
|
|
1028
|
+
if (!this._crdtManager) return;
|
|
1091
1029
|
const unsubscribe = this._crdtManager.doc.subscribe((event) => {
|
|
1092
1030
|
var _a2;
|
|
1093
1031
|
const origin = (_a2 = event.origin) != null ? _a2 : null;
|
|
@@ -1096,12 +1034,9 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1096
1034
|
if (origin === ORIGIN_INITIALIZED || origin === ORIGIN_STORAGE_UPDATED) {
|
|
1097
1035
|
return;
|
|
1098
1036
|
}
|
|
1099
|
-
if (!this._state.webSocket)
|
|
1100
|
-
|
|
1101
|
-
if (
|
|
1102
|
-
return;
|
|
1103
|
-
if (this._state.webSocket.readyState !== WebSocket.OPEN)
|
|
1104
|
-
return;
|
|
1037
|
+
if (!this._state.webSocket) return;
|
|
1038
|
+
if (!this._state.connection.id) return;
|
|
1039
|
+
if (this._state.webSocket.readyState !== WebSocket.OPEN) return;
|
|
1105
1040
|
this._sendMessage({
|
|
1106
1041
|
type: "$UPDATE_STORAGE",
|
|
1107
1042
|
data: { origin, update: event.update }
|
|
@@ -1149,16 +1084,14 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1149
1084
|
}
|
|
1150
1085
|
_onMessage(event) {
|
|
1151
1086
|
const message = this._parseMessage(event);
|
|
1152
|
-
if (!message)
|
|
1153
|
-
return;
|
|
1087
|
+
if (!message) return;
|
|
1154
1088
|
const shouldLog = typeof this._debug === "boolean" ? this._debug : this._debug.output.find((value) => value === message.type);
|
|
1155
1089
|
if (shouldLog) {
|
|
1156
1090
|
this._logDebug("WebSocket event received: ", message.type, message);
|
|
1157
1091
|
}
|
|
1158
1092
|
this._eventNotifier.subject(message.type).next(message);
|
|
1159
1093
|
const { connectionId } = message;
|
|
1160
|
-
if (!connectionId)
|
|
1161
|
-
return;
|
|
1094
|
+
if (!connectionId) return;
|
|
1162
1095
|
switch (message.type) {
|
|
1163
1096
|
case "$EXIT": {
|
|
1164
1097
|
this._handleExit(message);
|
|
@@ -1196,8 +1129,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1196
1129
|
}
|
|
1197
1130
|
}
|
|
1198
1131
|
_onOpen() {
|
|
1199
|
-
if (this._state.connection.state !== "Connecting" /* Connecting */)
|
|
1200
|
-
return;
|
|
1132
|
+
if (this._state.connection.state !== "Connecting" /* Connecting */) return;
|
|
1201
1133
|
this._logDebug("WebSocket connected");
|
|
1202
1134
|
this._updateState((oldState) => {
|
|
1203
1135
|
oldState.connection.state = "Open" /* Open */;
|
|
@@ -1216,10 +1148,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1216
1148
|
}
|
|
1217
1149
|
_onVisibilityChange() {
|
|
1218
1150
|
return __async(this, null, function* () {
|
|
1219
|
-
if (typeof document === "undefined")
|
|
1220
|
-
|
|
1221
|
-
if (document.visibilityState !== "visible")
|
|
1222
|
-
return;
|
|
1151
|
+
if (typeof document === "undefined") return;
|
|
1152
|
+
if (document.visibilityState !== "visible") return;
|
|
1223
1153
|
switch (this._state.connection.state) {
|
|
1224
1154
|
case "Open" /* Open */:
|
|
1225
1155
|
this._clearInterval(this._intervals.heartbeat);
|
|
@@ -1233,16 +1163,12 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1233
1163
|
});
|
|
1234
1164
|
}
|
|
1235
1165
|
_parseMessage(message) {
|
|
1236
|
-
if (typeof message.data !== "string")
|
|
1237
|
-
return null;
|
|
1166
|
+
if (typeof message.data !== "string") return null;
|
|
1238
1167
|
try {
|
|
1239
1168
|
const config = JSON.parse(message.data);
|
|
1240
|
-
if (typeof config !== "object")
|
|
1241
|
-
|
|
1242
|
-
if (typeof config.
|
|
1243
|
-
return null;
|
|
1244
|
-
if (typeof config.data !== "object")
|
|
1245
|
-
return null;
|
|
1169
|
+
if (typeof config !== "object") return null;
|
|
1170
|
+
if (typeof config.type !== "string") return null;
|
|
1171
|
+
if (typeof config.data !== "object") return null;
|
|
1246
1172
|
return config;
|
|
1247
1173
|
} catch (e) {
|
|
1248
1174
|
return null;
|
|
@@ -1253,8 +1179,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1253
1179
|
if (!this._endpoints.wsEndpoint) {
|
|
1254
1180
|
throw new Error("Must provide an wsEndpoint.");
|
|
1255
1181
|
}
|
|
1256
|
-
if (!this._state.webSocket)
|
|
1257
|
-
return;
|
|
1182
|
+
if (!this._state.webSocket) return;
|
|
1258
1183
|
this._closeWs();
|
|
1259
1184
|
this._updateState((oldState) => {
|
|
1260
1185
|
oldState.authorization.token = null;
|
|
@@ -1267,8 +1192,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1267
1192
|
}
|
|
1268
1193
|
_sendMessage(data) {
|
|
1269
1194
|
const webSocket = this._state.webSocket;
|
|
1270
|
-
if ((webSocket == null ? void 0 : webSocket.readyState) !== WebSocket.OPEN)
|
|
1271
|
-
return;
|
|
1195
|
+
if ((webSocket == null ? void 0 : webSocket.readyState) !== WebSocket.OPEN) return;
|
|
1272
1196
|
const shouldLog = typeof this._debug === "boolean" ? this._debug : this._debug.input.find((value) => value === data.type);
|
|
1273
1197
|
if (shouldLog) {
|
|
1274
1198
|
this._logDebug("WebSocket event sent: ", data.type, data);
|
|
@@ -1301,8 +1225,7 @@ var PluvClient = class {
|
|
|
1301
1225
|
this._rooms = /* @__PURE__ */ new Map();
|
|
1302
1226
|
this.createRoom = (room, options) => {
|
|
1303
1227
|
const oldRoom = this.getRoom(room);
|
|
1304
|
-
if (oldRoom)
|
|
1305
|
-
return oldRoom;
|
|
1228
|
+
if (oldRoom) return oldRoom;
|
|
1306
1229
|
const newRoom = new PluvRoom(room, {
|
|
1307
1230
|
addons: options.addons,
|
|
1308
1231
|
authEndpoint: this._authEndpoint,
|
|
@@ -1335,8 +1258,7 @@ var PluvClient = class {
|
|
|
1335
1258
|
};
|
|
1336
1259
|
this.leave = (room) => __async(this, null, function* () {
|
|
1337
1260
|
const toLeave = typeof room === "string" ? this.getRoom(room) : room;
|
|
1338
|
-
if (!toLeave)
|
|
1339
|
-
return;
|
|
1261
|
+
if (!toLeave) return;
|
|
1340
1262
|
yield toLeave.disconnect();
|
|
1341
1263
|
this._rooms.delete(toLeave.id);
|
|
1342
1264
|
this._logDebug(`Left and deleted room: ${room}`);
|
|
@@ -1348,8 +1270,7 @@ var PluvClient = class {
|
|
|
1348
1270
|
}
|
|
1349
1271
|
_logDebug(...data) {
|
|
1350
1272
|
var _a;
|
|
1351
|
-
if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production")
|
|
1352
|
-
return;
|
|
1273
|
+
if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production") return;
|
|
1353
1274
|
this._debug && console.log(...data);
|
|
1354
1275
|
}
|
|
1355
1276
|
};
|