@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.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
|
};
|
|
@@ -376,11 +369,10 @@ var MockedRoom = class extends AbstractRoom {
|
|
|
376
369
|
this.getOthers = () => {
|
|
377
370
|
return this._usersManager.getOthers();
|
|
378
371
|
};
|
|
379
|
-
this.getStorage = (
|
|
380
|
-
const sharedType = this._crdtManager.get(
|
|
381
|
-
if (typeof sharedType === "undefined")
|
|
382
|
-
|
|
383
|
-
return sharedType.value;
|
|
372
|
+
this.getStorage = (type) => {
|
|
373
|
+
const sharedType = this._crdtManager.get(type);
|
|
374
|
+
if (typeof sharedType === "undefined") return null;
|
|
375
|
+
return sharedType;
|
|
384
376
|
};
|
|
385
377
|
this.other = (connectionId, callback) => {
|
|
386
378
|
return this._otherNotifier.subscribe(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,44 +425,39 @@ 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
|
}
|
|
442
|
+
getStorageJson(type) {
|
|
443
|
+
if (this._state.connection.id === null) return null;
|
|
444
|
+
if (typeof type === "undefined") return this._crdtManager.doc.toJson();
|
|
445
|
+
return this._crdtManager.doc.toJson(type);
|
|
446
|
+
}
|
|
457
447
|
_observeCrdt() {
|
|
458
448
|
var _a, _b;
|
|
459
449
|
(_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
|
|
460
|
-
if (!this._crdtManager)
|
|
461
|
-
return;
|
|
450
|
+
if (!this._crdtManager) return;
|
|
462
451
|
const unsubscribe = this._crdtManager.doc.subscribe((event) => {
|
|
463
452
|
var _a2;
|
|
464
453
|
const origin = (_a2 = event.origin) != null ? _a2 : null;
|
|
465
|
-
if (!this._crdtManager)
|
|
466
|
-
|
|
467
|
-
if (origin === "$STORAGE_UPDATED")
|
|
468
|
-
return;
|
|
454
|
+
if (!this._crdtManager) return;
|
|
455
|
+
if (origin === "$STORAGE_UPDATED") return;
|
|
469
456
|
const sharedTypes = this._crdtManager.doc.get();
|
|
470
|
-
const storageRoot = Object.
|
|
471
|
-
(acc,
|
|
472
|
-
if (!this._crdtManager)
|
|
473
|
-
|
|
474
|
-
const serialized = sharedType.toJson();
|
|
457
|
+
const storageRoot = Object.keys(sharedTypes).reduce(
|
|
458
|
+
(acc, prop) => {
|
|
459
|
+
if (!this._crdtManager) return acc;
|
|
460
|
+
const serialized = this._crdtManager.doc.toJson(prop);
|
|
475
461
|
this._crdtNotifier.subject(prop).next(serialized);
|
|
476
462
|
return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
|
|
477
463
|
},
|
|
@@ -514,12 +500,10 @@ var debounce = (fn, options) => {
|
|
|
514
500
|
let prevResult;
|
|
515
501
|
let timeout = null;
|
|
516
502
|
return (...args) => {
|
|
517
|
-
if (typeof timeout === "number")
|
|
518
|
-
clearTimeout(timeout);
|
|
503
|
+
if (typeof timeout === "number") clearTimeout(timeout);
|
|
519
504
|
timeout = setTimeout(() => {
|
|
520
505
|
timeout = null;
|
|
521
|
-
if (!immediate)
|
|
522
|
-
prevResult = fn(args);
|
|
506
|
+
if (!immediate) prevResult = fn(args);
|
|
523
507
|
}, wait);
|
|
524
508
|
if (immediate && !timeout) {
|
|
525
509
|
fn(args);
|
|
@@ -582,10 +566,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
582
566
|
this._wsListeners = null;
|
|
583
567
|
this.broadcast = new Proxy(
|
|
584
568
|
(event, data) => {
|
|
585
|
-
if (!this._state.webSocket)
|
|
586
|
-
|
|
587
|
-
if (this._state.connection.state !== "Open" /* Open */)
|
|
588
|
-
return;
|
|
569
|
+
if (!this._state.webSocket) return;
|
|
570
|
+
if (this._state.connection.state !== "Open" /* Open */) return;
|
|
589
571
|
const type = event.toString();
|
|
590
572
|
this._sendMessage({ data, type });
|
|
591
573
|
},
|
|
@@ -611,6 +593,12 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
611
593
|
}
|
|
612
594
|
}
|
|
613
595
|
);
|
|
596
|
+
this.getConnection = () => {
|
|
597
|
+
return Object.freeze(JSON.parse(JSON.stringify(this._state.connection)));
|
|
598
|
+
};
|
|
599
|
+
this.getDoc = () => {
|
|
600
|
+
return this._crdtManager.doc;
|
|
601
|
+
};
|
|
614
602
|
this.getMyPresence = () => {
|
|
615
603
|
return this._usersManager.myPresence;
|
|
616
604
|
};
|
|
@@ -623,10 +611,9 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
623
611
|
this.getOthers = () => {
|
|
624
612
|
return this._usersManager.getOthers();
|
|
625
613
|
};
|
|
626
|
-
this.getStorage = (
|
|
627
|
-
const sharedType = this._crdtManager.get(
|
|
628
|
-
if (typeof sharedType === "undefined")
|
|
629
|
-
return null;
|
|
614
|
+
this.getStorage = (type) => {
|
|
615
|
+
const sharedType = this._crdtManager.get(type);
|
|
616
|
+
if (typeof sharedType === "undefined") return null;
|
|
630
617
|
return sharedType;
|
|
631
618
|
};
|
|
632
619
|
this.other = (connectionId, callback) => {
|
|
@@ -646,8 +633,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
646
633
|
};
|
|
647
634
|
this.transact = (fn, origin) => {
|
|
648
635
|
const _origin = origin != null ? origin : this._state.connection.id;
|
|
649
|
-
if (typeof _origin !== "string")
|
|
650
|
-
return;
|
|
636
|
+
if (typeof _origin !== "string") return;
|
|
651
637
|
this._crdtManager.doc.transact(() => {
|
|
652
638
|
const storage = this._crdtManager.doc.get();
|
|
653
639
|
fn(storage);
|
|
@@ -658,18 +644,18 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
658
644
|
};
|
|
659
645
|
this.updateMyPresence = (presence) => {
|
|
660
646
|
var _a;
|
|
661
|
-
this.
|
|
647
|
+
const newPresence = typeof presence === "function" ? presence(this.getMyPresence()) : presence;
|
|
648
|
+
this._usersManager.updateMyPresence(newPresence);
|
|
662
649
|
const myPresence = this._usersManager.myPresence;
|
|
663
650
|
const myself = (_a = this._usersManager.myself) != null ? _a : null;
|
|
664
651
|
this._stateNotifier.subjects["my-presence"].next(myPresence);
|
|
665
652
|
!!myself && this._stateNotifier.subjects["myself"].next(myself);
|
|
666
|
-
this.broadcast("$UPDATE_PRESENCE", { presence });
|
|
653
|
+
this.broadcast("$UPDATE_PRESENCE", { presence: newPresence });
|
|
667
654
|
};
|
|
668
655
|
this._flattenStorageStore = debounce(
|
|
669
656
|
() => __async(this, null, function* () {
|
|
670
657
|
const shouldFlatten = yield this._storageStore.getShouldFlatten();
|
|
671
|
-
if (!shouldFlatten)
|
|
672
|
-
return;
|
|
658
|
+
if (!shouldFlatten) return;
|
|
673
659
|
const updates = yield this._storageStore.getUpdates();
|
|
674
660
|
this._crdtManager.applyUpdate(updates, this);
|
|
675
661
|
const encodedState = this._crdtManager.doc.getEncodedState();
|
|
@@ -717,8 +703,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
717
703
|
const canConnect = ["Closed" /* Closed */, "Untouched" /* Untouched */].some(
|
|
718
704
|
(state) => this._state.connection.state === state
|
|
719
705
|
);
|
|
720
|
-
if (!canConnect)
|
|
721
|
-
return;
|
|
706
|
+
if (!canConnect) return;
|
|
722
707
|
this._clearTimeout(this._timeouts.reconnect);
|
|
723
708
|
this._updateState((oldState) => {
|
|
724
709
|
oldState.connection.state = "Connecting" /* Connecting */;
|
|
@@ -752,13 +737,11 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
752
737
|
if (!this._endpoints.wsEndpoint) {
|
|
753
738
|
throw new Error("Must provide an wsEndpoint.");
|
|
754
739
|
}
|
|
755
|
-
if (!this._state.webSocket)
|
|
756
|
-
return;
|
|
740
|
+
if (!this._state.webSocket) return;
|
|
757
741
|
const canDisconnect = ["Connecting" /* Connecting */, "Open" /* Open */].some(
|
|
758
742
|
(state) => this._state.connection.state === state
|
|
759
743
|
);
|
|
760
|
-
if (!canDisconnect)
|
|
761
|
-
return;
|
|
744
|
+
if (!canDisconnect) return;
|
|
762
745
|
this._clearInterval(this._intervals.heartbeat);
|
|
763
746
|
this._closeWs();
|
|
764
747
|
yield this._storageStore.destroy();
|
|
@@ -766,11 +749,12 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
766
749
|
this._stateNotifier.subjects["my-presence"].next(null);
|
|
767
750
|
});
|
|
768
751
|
}
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
return
|
|
752
|
+
getStorageJson(type) {
|
|
753
|
+
if (this._state.connection.id === null) return null;
|
|
754
|
+
if (typeof type === "undefined") return this._crdtManager.doc.toJson();
|
|
755
|
+
const sharedType = this._crdtManager.get(type);
|
|
756
|
+
if (typeof sharedType === "undefined") return null;
|
|
757
|
+
return this._crdtManager.doc.toJson(type);
|
|
774
758
|
}
|
|
775
759
|
_addToStorageStore(update) {
|
|
776
760
|
return __async(this, null, function* () {
|
|
@@ -793,10 +777,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
793
777
|
});
|
|
794
778
|
}
|
|
795
779
|
_attachWindowListeners() {
|
|
796
|
-
if (typeof window === "undefined")
|
|
797
|
-
|
|
798
|
-
if (typeof document === "undefined")
|
|
799
|
-
return;
|
|
780
|
+
if (typeof window === "undefined") return;
|
|
781
|
+
if (typeof document === "undefined") return;
|
|
800
782
|
this._windowListeners = {
|
|
801
783
|
onNavigatorOnline: this._onNavigatorOnline.bind(this),
|
|
802
784
|
onVisibilityChange: this._onVisibilityChange.bind(this)
|
|
@@ -805,8 +787,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
805
787
|
document.addEventListener("visibilitychange", this._windowListeners.onVisibilityChange);
|
|
806
788
|
}
|
|
807
789
|
_attachWsListeners() {
|
|
808
|
-
if (!this._state.webSocket)
|
|
809
|
-
return;
|
|
790
|
+
if (!this._state.webSocket) return;
|
|
810
791
|
this._wsListeners = {
|
|
811
792
|
onClose: this._onClose.bind(this),
|
|
812
793
|
onError: this._onError.bind(this),
|
|
@@ -819,19 +800,16 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
819
800
|
this._state.webSocket.addEventListener("open", this._wsListeners.onOpen);
|
|
820
801
|
}
|
|
821
802
|
_clearInterval(interval) {
|
|
822
|
-
if (Number.isNaN(interval))
|
|
823
|
-
return;
|
|
803
|
+
if (Number.isNaN(interval)) return;
|
|
824
804
|
clearInterval(interval);
|
|
825
805
|
}
|
|
826
806
|
_clearTimeout(timeout) {
|
|
827
|
-
if (Number.isNaN(timeout))
|
|
828
|
-
return;
|
|
807
|
+
if (Number.isNaN(timeout)) return;
|
|
829
808
|
clearTimeout(timeout);
|
|
830
809
|
}
|
|
831
810
|
_closeWs() {
|
|
832
811
|
var _a, _b;
|
|
833
|
-
if (!this._state.webSocket)
|
|
834
|
-
return;
|
|
812
|
+
if (!this._state.webSocket) return;
|
|
835
813
|
(_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
|
|
836
814
|
this._clearTimeout(this._timeouts.pong);
|
|
837
815
|
this._clearTimeout(this._timeouts.reconnect);
|
|
@@ -843,24 +821,19 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
843
821
|
return readyState === ((_a2 = this._state.webSocket) == null ? void 0 : _a2.readyState);
|
|
844
822
|
}
|
|
845
823
|
);
|
|
846
|
-
if (canClose)
|
|
847
|
-
this._state.webSocket.close();
|
|
824
|
+
if (canClose) this._state.webSocket.close();
|
|
848
825
|
this._detachWsListeners();
|
|
849
826
|
}
|
|
850
827
|
_detachWindowListeners() {
|
|
851
|
-
if (!this._windowListeners)
|
|
852
|
-
|
|
853
|
-
if (typeof
|
|
854
|
-
return;
|
|
855
|
-
if (typeof document === "undefined")
|
|
856
|
-
return;
|
|
828
|
+
if (!this._windowListeners) return;
|
|
829
|
+
if (typeof window === "undefined") return;
|
|
830
|
+
if (typeof document === "undefined") return;
|
|
857
831
|
window.removeEventListener("online", this._windowListeners.onNavigatorOnline);
|
|
858
832
|
document.removeEventListener("visibilitychange", this._windowListeners.onVisibilityChange);
|
|
859
833
|
this._windowListeners = null;
|
|
860
834
|
}
|
|
861
835
|
_detachWsListeners() {
|
|
862
|
-
if (!this._state.webSocket || !this._wsListeners)
|
|
863
|
-
return;
|
|
836
|
+
if (!this._state.webSocket || !this._wsListeners) return;
|
|
864
837
|
this._state.webSocket.removeEventListener("close", this._wsListeners.onClose);
|
|
865
838
|
this._state.webSocket.removeEventListener("error", this._wsListeners.onError);
|
|
866
839
|
this._state.webSocket.removeEventListener("message", this._wsListeners.onMessage);
|
|
@@ -869,9 +842,9 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
869
842
|
}
|
|
870
843
|
_emitSharedTypes() {
|
|
871
844
|
const sharedTypes = this._crdtManager.doc.get();
|
|
872
|
-
const storageRoot = Object.
|
|
873
|
-
(acc,
|
|
874
|
-
const serialized =
|
|
845
|
+
const storageRoot = Object.keys(sharedTypes).reduce(
|
|
846
|
+
(acc, prop) => {
|
|
847
|
+
const serialized = this._crdtManager.doc.toJson(prop);
|
|
875
848
|
this._crdtNotifier.subject(prop).next(serialized);
|
|
876
849
|
return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
|
|
877
850
|
},
|
|
@@ -880,8 +853,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
880
853
|
this._crdtNotifier.rootSubject.next(storageRoot);
|
|
881
854
|
}
|
|
882
855
|
_getAuthFetchOptions(room) {
|
|
883
|
-
if (typeof this._endpoints.authEndpoint === "undefined")
|
|
884
|
-
return null;
|
|
856
|
+
if (typeof this._endpoints.authEndpoint === "undefined") return null;
|
|
885
857
|
if (this._endpoints.authEndpoint === true) {
|
|
886
858
|
return {
|
|
887
859
|
url: `/api/pluv/authorize?room=${room}`,
|
|
@@ -900,8 +872,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
900
872
|
_getAuthorization(room) {
|
|
901
873
|
return __async(this, null, function* () {
|
|
902
874
|
const fetchOptions = this._getAuthFetchOptions(room);
|
|
903
|
-
if (!fetchOptions)
|
|
904
|
-
return null;
|
|
875
|
+
if (!fetchOptions) return null;
|
|
905
876
|
const { url, options } = fetchOptions;
|
|
906
877
|
const res = yield fetch(url, options);
|
|
907
878
|
if (!res.ok || res.status !== 200) {
|
|
@@ -926,10 +897,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
926
897
|
}
|
|
927
898
|
_handleExit(message) {
|
|
928
899
|
const { connectionId } = message;
|
|
929
|
-
if (!connectionId)
|
|
930
|
-
|
|
931
|
-
if (!this._state.webSocket)
|
|
932
|
-
throw new Error("Could not find WebSocket");
|
|
900
|
+
if (!connectionId) return;
|
|
901
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
933
902
|
this._usersManager.removeUser(connectionId);
|
|
934
903
|
const others = this._usersManager.getOthers();
|
|
935
904
|
this._stateNotifier.subjects.others.next(others);
|
|
@@ -939,10 +908,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
939
908
|
_handlePresenceUpdatedMessage(message) {
|
|
940
909
|
var _a;
|
|
941
910
|
const { connectionId } = message;
|
|
942
|
-
if (!connectionId)
|
|
943
|
-
|
|
944
|
-
if (!this._state.webSocket)
|
|
945
|
-
throw new Error("Could not find WebSocket");
|
|
911
|
+
if (!connectionId) return;
|
|
912
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
946
913
|
const data = message.data;
|
|
947
914
|
this._usersManager.patchPresence(connectionId, data.presence);
|
|
948
915
|
const myPresence = this._usersManager.myPresence;
|
|
@@ -958,15 +925,11 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
958
925
|
this._stateNotifier.subjects["others"].next(others);
|
|
959
926
|
}
|
|
960
927
|
_handleReceiveOthers(message) {
|
|
961
|
-
if (!message.connectionId)
|
|
962
|
-
|
|
963
|
-
if (!this._state.webSocket)
|
|
964
|
-
throw new Error("Could not find WebSocket");
|
|
928
|
+
if (!message.connectionId) return;
|
|
929
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
965
930
|
const data = message.data;
|
|
966
931
|
Object.keys(data.others).forEach((connectionId) => {
|
|
967
932
|
const { presence, user } = data.others[connectionId];
|
|
968
|
-
if (!user)
|
|
969
|
-
return;
|
|
970
933
|
this._usersManager.setUser(connectionId, user);
|
|
971
934
|
const other = this._usersManager.getOther(connectionId);
|
|
972
935
|
!!presence && this._usersManager.patchPresence(connectionId, presence);
|
|
@@ -978,10 +941,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
978
941
|
_handleRegisteredMessage(message) {
|
|
979
942
|
var _a, _b;
|
|
980
943
|
const { connectionId, user } = message;
|
|
981
|
-
if (!connectionId)
|
|
982
|
-
|
|
983
|
-
if (!this._state.webSocket)
|
|
984
|
-
throw new Error("Could not find WebSocket");
|
|
944
|
+
if (!connectionId) return;
|
|
945
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
985
946
|
this._updateState((oldState) => {
|
|
986
947
|
oldState.connection.count += 1;
|
|
987
948
|
oldState.connection.id = connectionId;
|
|
@@ -1002,10 +963,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1002
963
|
_handleStorageReceivedMessage(message) {
|
|
1003
964
|
return __async(this, null, function* () {
|
|
1004
965
|
const { connectionId } = message;
|
|
1005
|
-
if (!connectionId)
|
|
1006
|
-
|
|
1007
|
-
if (!this._state.webSocket)
|
|
1008
|
-
throw new Error("Could not find WebSocket");
|
|
966
|
+
if (!connectionId) return;
|
|
967
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
1009
968
|
const data = message.data;
|
|
1010
969
|
this._crdtManager.initialize({
|
|
1011
970
|
onInitialized: (update2) => __async(this, null, function* () {
|
|
@@ -1029,23 +988,19 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1029
988
|
}
|
|
1030
989
|
_handleStorageUpdatedMessage(message) {
|
|
1031
990
|
const { connectionId } = message;
|
|
1032
|
-
if (!connectionId)
|
|
1033
|
-
|
|
1034
|
-
if (!this._state.webSocket)
|
|
1035
|
-
throw new Error("Could not find WebSocket");
|
|
991
|
+
if (!connectionId) return;
|
|
992
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
1036
993
|
const data = message.data;
|
|
1037
|
-
if (!this._crdtManager)
|
|
1038
|
-
return;
|
|
994
|
+
if (!this._crdtManager) return;
|
|
1039
995
|
this._crdtManager.doc.applyEncodedState({
|
|
1040
996
|
origin: ORIGIN_STORAGE_UPDATED,
|
|
1041
997
|
update: data.state
|
|
1042
998
|
});
|
|
1043
999
|
const sharedTypes = this._crdtManager.doc.get();
|
|
1044
|
-
const storageRoot = Object.
|
|
1045
|
-
(acc,
|
|
1046
|
-
if (!this._crdtManager)
|
|
1047
|
-
|
|
1048
|
-
const serialized = sharedType.toJson();
|
|
1000
|
+
const storageRoot = Object.keys(sharedTypes).reduce(
|
|
1001
|
+
(acc, prop) => {
|
|
1002
|
+
if (!this._crdtManager) return acc;
|
|
1003
|
+
const serialized = this._crdtManager.doc.toJson(prop);
|
|
1049
1004
|
this._crdtNotifier.subject(prop).next(serialized);
|
|
1050
1005
|
return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
|
|
1051
1006
|
},
|
|
@@ -1055,12 +1010,9 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1055
1010
|
}
|
|
1056
1011
|
_handleUserJoinedMessage(message) {
|
|
1057
1012
|
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;
|
|
1013
|
+
if (!connectionId) return;
|
|
1014
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
1015
|
+
if (!this._usersManager.myself) return;
|
|
1064
1016
|
const data = message.data;
|
|
1065
1017
|
const myself = this._usersManager.myself;
|
|
1066
1018
|
if (myself.connectionId === connectionId) {
|
|
@@ -1079,15 +1031,13 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1079
1031
|
}
|
|
1080
1032
|
_logDebug(...data) {
|
|
1081
1033
|
var _a;
|
|
1082
|
-
if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production")
|
|
1083
|
-
return;
|
|
1034
|
+
if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production") return;
|
|
1084
1035
|
this._debug && console.log(...data);
|
|
1085
1036
|
}
|
|
1086
1037
|
_observeCrdt() {
|
|
1087
1038
|
var _a, _b;
|
|
1088
1039
|
(_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
|
|
1089
|
-
if (!this._crdtManager)
|
|
1090
|
-
return;
|
|
1040
|
+
if (!this._crdtManager) return;
|
|
1091
1041
|
const unsubscribe = this._crdtManager.doc.subscribe((event) => {
|
|
1092
1042
|
var _a2;
|
|
1093
1043
|
const origin = (_a2 = event.origin) != null ? _a2 : null;
|
|
@@ -1096,12 +1046,9 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1096
1046
|
if (origin === ORIGIN_INITIALIZED || origin === ORIGIN_STORAGE_UPDATED) {
|
|
1097
1047
|
return;
|
|
1098
1048
|
}
|
|
1099
|
-
if (!this._state.webSocket)
|
|
1100
|
-
|
|
1101
|
-
if (
|
|
1102
|
-
return;
|
|
1103
|
-
if (this._state.webSocket.readyState !== WebSocket.OPEN)
|
|
1104
|
-
return;
|
|
1049
|
+
if (!this._state.webSocket) return;
|
|
1050
|
+
if (!this._state.connection.id) return;
|
|
1051
|
+
if (this._state.webSocket.readyState !== WebSocket.OPEN) return;
|
|
1105
1052
|
this._sendMessage({
|
|
1106
1053
|
type: "$UPDATE_STORAGE",
|
|
1107
1054
|
data: { origin, update: event.update }
|
|
@@ -1149,16 +1096,14 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1149
1096
|
}
|
|
1150
1097
|
_onMessage(event) {
|
|
1151
1098
|
const message = this._parseMessage(event);
|
|
1152
|
-
if (!message)
|
|
1153
|
-
return;
|
|
1099
|
+
if (!message) return;
|
|
1154
1100
|
const shouldLog = typeof this._debug === "boolean" ? this._debug : this._debug.output.find((value) => value === message.type);
|
|
1155
1101
|
if (shouldLog) {
|
|
1156
1102
|
this._logDebug("WebSocket event received: ", message.type, message);
|
|
1157
1103
|
}
|
|
1158
1104
|
this._eventNotifier.subject(message.type).next(message);
|
|
1159
1105
|
const { connectionId } = message;
|
|
1160
|
-
if (!connectionId)
|
|
1161
|
-
return;
|
|
1106
|
+
if (!connectionId) return;
|
|
1162
1107
|
switch (message.type) {
|
|
1163
1108
|
case "$EXIT": {
|
|
1164
1109
|
this._handleExit(message);
|
|
@@ -1196,8 +1141,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1196
1141
|
}
|
|
1197
1142
|
}
|
|
1198
1143
|
_onOpen() {
|
|
1199
|
-
if (this._state.connection.state !== "Connecting" /* Connecting */)
|
|
1200
|
-
return;
|
|
1144
|
+
if (this._state.connection.state !== "Connecting" /* Connecting */) return;
|
|
1201
1145
|
this._logDebug("WebSocket connected");
|
|
1202
1146
|
this._updateState((oldState) => {
|
|
1203
1147
|
oldState.connection.state = "Open" /* Open */;
|
|
@@ -1216,10 +1160,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1216
1160
|
}
|
|
1217
1161
|
_onVisibilityChange() {
|
|
1218
1162
|
return __async(this, null, function* () {
|
|
1219
|
-
if (typeof document === "undefined")
|
|
1220
|
-
|
|
1221
|
-
if (document.visibilityState !== "visible")
|
|
1222
|
-
return;
|
|
1163
|
+
if (typeof document === "undefined") return;
|
|
1164
|
+
if (document.visibilityState !== "visible") return;
|
|
1223
1165
|
switch (this._state.connection.state) {
|
|
1224
1166
|
case "Open" /* Open */:
|
|
1225
1167
|
this._clearInterval(this._intervals.heartbeat);
|
|
@@ -1233,16 +1175,12 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1233
1175
|
});
|
|
1234
1176
|
}
|
|
1235
1177
|
_parseMessage(message) {
|
|
1236
|
-
if (typeof message.data !== "string")
|
|
1237
|
-
return null;
|
|
1178
|
+
if (typeof message.data !== "string") return null;
|
|
1238
1179
|
try {
|
|
1239
1180
|
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;
|
|
1181
|
+
if (typeof config !== "object") return null;
|
|
1182
|
+
if (typeof config.type !== "string") return null;
|
|
1183
|
+
if (typeof config.data !== "object") return null;
|
|
1246
1184
|
return config;
|
|
1247
1185
|
} catch (e) {
|
|
1248
1186
|
return null;
|
|
@@ -1253,8 +1191,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1253
1191
|
if (!this._endpoints.wsEndpoint) {
|
|
1254
1192
|
throw new Error("Must provide an wsEndpoint.");
|
|
1255
1193
|
}
|
|
1256
|
-
if (!this._state.webSocket)
|
|
1257
|
-
return;
|
|
1194
|
+
if (!this._state.webSocket) return;
|
|
1258
1195
|
this._closeWs();
|
|
1259
1196
|
this._updateState((oldState) => {
|
|
1260
1197
|
oldState.authorization.token = null;
|
|
@@ -1267,8 +1204,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1267
1204
|
}
|
|
1268
1205
|
_sendMessage(data) {
|
|
1269
1206
|
const webSocket = this._state.webSocket;
|
|
1270
|
-
if ((webSocket == null ? void 0 : webSocket.readyState) !== WebSocket.OPEN)
|
|
1271
|
-
return;
|
|
1207
|
+
if ((webSocket == null ? void 0 : webSocket.readyState) !== WebSocket.OPEN) return;
|
|
1272
1208
|
const shouldLog = typeof this._debug === "boolean" ? this._debug : this._debug.input.find((value) => value === data.type);
|
|
1273
1209
|
if (shouldLog) {
|
|
1274
1210
|
this._logDebug("WebSocket event sent: ", data.type, data);
|
|
@@ -1301,8 +1237,7 @@ var PluvClient = class {
|
|
|
1301
1237
|
this._rooms = /* @__PURE__ */ new Map();
|
|
1302
1238
|
this.createRoom = (room, options) => {
|
|
1303
1239
|
const oldRoom = this.getRoom(room);
|
|
1304
|
-
if (oldRoom)
|
|
1305
|
-
return oldRoom;
|
|
1240
|
+
if (oldRoom) return oldRoom;
|
|
1306
1241
|
const newRoom = new PluvRoom(room, {
|
|
1307
1242
|
addons: options.addons,
|
|
1308
1243
|
authEndpoint: this._authEndpoint,
|
|
@@ -1335,8 +1270,7 @@ var PluvClient = class {
|
|
|
1335
1270
|
};
|
|
1336
1271
|
this.leave = (room) => __async(this, null, function* () {
|
|
1337
1272
|
const toLeave = typeof room === "string" ? this.getRoom(room) : room;
|
|
1338
|
-
if (!toLeave)
|
|
1339
|
-
return;
|
|
1273
|
+
if (!toLeave) return;
|
|
1340
1274
|
yield toLeave.disconnect();
|
|
1341
1275
|
this._rooms.delete(toLeave.id);
|
|
1342
1276
|
this._logDebug(`Left and deleted room: ${room}`);
|
|
@@ -1348,8 +1282,7 @@ var PluvClient = class {
|
|
|
1348
1282
|
}
|
|
1349
1283
|
_logDebug(...data) {
|
|
1350
1284
|
var _a;
|
|
1351
|
-
if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production")
|
|
1352
|
-
return;
|
|
1285
|
+
if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production") return;
|
|
1353
1286
|
this._debug && console.log(...data);
|
|
1354
1287
|
}
|
|
1355
1288
|
};
|