@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.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
|
};
|
|
@@ -349,8 +342,7 @@ var MockedRoom = class extends AbstractRoom {
|
|
|
349
342
|
};
|
|
350
343
|
this.getStorage = (key) => {
|
|
351
344
|
const sharedType = this._crdtManager.get(key);
|
|
352
|
-
if (typeof sharedType === "undefined")
|
|
353
|
-
return null;
|
|
345
|
+
if (typeof sharedType === "undefined") return null;
|
|
354
346
|
return sharedType.value;
|
|
355
347
|
};
|
|
356
348
|
this.other = (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,43 +396,33 @@ 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
|
}
|
|
428
413
|
_observeCrdt() {
|
|
429
414
|
var _a, _b;
|
|
430
415
|
(_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
|
|
431
|
-
if (!this._crdtManager)
|
|
432
|
-
return;
|
|
416
|
+
if (!this._crdtManager) return;
|
|
433
417
|
const unsubscribe = this._crdtManager.doc.subscribe((event) => {
|
|
434
418
|
var _a2;
|
|
435
419
|
const origin = (_a2 = event.origin) != null ? _a2 : null;
|
|
436
|
-
if (!this._crdtManager)
|
|
437
|
-
|
|
438
|
-
if (origin === "$STORAGE_UPDATED")
|
|
439
|
-
return;
|
|
420
|
+
if (!this._crdtManager) return;
|
|
421
|
+
if (origin === "$STORAGE_UPDATED") return;
|
|
440
422
|
const sharedTypes = this._crdtManager.doc.get();
|
|
441
423
|
const storageRoot = Object.entries(sharedTypes).reduce(
|
|
442
424
|
(acc, [prop, sharedType]) => {
|
|
443
|
-
if (!this._crdtManager)
|
|
444
|
-
return acc;
|
|
425
|
+
if (!this._crdtManager) return acc;
|
|
445
426
|
const serialized = sharedType.toJson();
|
|
446
427
|
this._crdtNotifier.subject(prop).next(serialized);
|
|
447
428
|
return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
|
|
@@ -485,12 +466,10 @@ var debounce = (fn, options) => {
|
|
|
485
466
|
let prevResult;
|
|
486
467
|
let timeout = null;
|
|
487
468
|
return (...args) => {
|
|
488
|
-
if (typeof timeout === "number")
|
|
489
|
-
clearTimeout(timeout);
|
|
469
|
+
if (typeof timeout === "number") clearTimeout(timeout);
|
|
490
470
|
timeout = setTimeout(() => {
|
|
491
471
|
timeout = null;
|
|
492
|
-
if (!immediate)
|
|
493
|
-
prevResult = fn(args);
|
|
472
|
+
if (!immediate) prevResult = fn(args);
|
|
494
473
|
}, wait);
|
|
495
474
|
if (immediate && !timeout) {
|
|
496
475
|
fn(args);
|
|
@@ -553,10 +532,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
553
532
|
this._wsListeners = null;
|
|
554
533
|
this.broadcast = new Proxy(
|
|
555
534
|
(event, data) => {
|
|
556
|
-
if (!this._state.webSocket)
|
|
557
|
-
|
|
558
|
-
if (this._state.connection.state !== "Open" /* Open */)
|
|
559
|
-
return;
|
|
535
|
+
if (!this._state.webSocket) return;
|
|
536
|
+
if (this._state.connection.state !== "Open" /* Open */) return;
|
|
560
537
|
const type = event.toString();
|
|
561
538
|
this._sendMessage({ data, type });
|
|
562
539
|
},
|
|
@@ -582,6 +559,12 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
582
559
|
}
|
|
583
560
|
}
|
|
584
561
|
);
|
|
562
|
+
this.getConnection = () => {
|
|
563
|
+
return Object.freeze(JSON.parse(JSON.stringify(this._state.connection)));
|
|
564
|
+
};
|
|
565
|
+
this.getDoc = () => {
|
|
566
|
+
return this._crdtManager.doc;
|
|
567
|
+
};
|
|
585
568
|
this.getMyPresence = () => {
|
|
586
569
|
return this._usersManager.myPresence;
|
|
587
570
|
};
|
|
@@ -596,8 +579,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
596
579
|
};
|
|
597
580
|
this.getStorage = (key) => {
|
|
598
581
|
const sharedType = this._crdtManager.get(key);
|
|
599
|
-
if (typeof sharedType === "undefined")
|
|
600
|
-
return null;
|
|
582
|
+
if (typeof sharedType === "undefined") return null;
|
|
601
583
|
return sharedType;
|
|
602
584
|
};
|
|
603
585
|
this.other = (connectionId, callback) => {
|
|
@@ -617,8 +599,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
617
599
|
};
|
|
618
600
|
this.transact = (fn, origin) => {
|
|
619
601
|
const _origin = origin != null ? origin : this._state.connection.id;
|
|
620
|
-
if (typeof _origin !== "string")
|
|
621
|
-
return;
|
|
602
|
+
if (typeof _origin !== "string") return;
|
|
622
603
|
this._crdtManager.doc.transact(() => {
|
|
623
604
|
const storage = this._crdtManager.doc.get();
|
|
624
605
|
fn(storage);
|
|
@@ -629,18 +610,18 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
629
610
|
};
|
|
630
611
|
this.updateMyPresence = (presence) => {
|
|
631
612
|
var _a;
|
|
632
|
-
this.
|
|
613
|
+
const newPresence = typeof presence === "function" ? presence(this.getMyPresence()) : presence;
|
|
614
|
+
this._usersManager.updateMyPresence(newPresence);
|
|
633
615
|
const myPresence = this._usersManager.myPresence;
|
|
634
616
|
const myself = (_a = this._usersManager.myself) != null ? _a : null;
|
|
635
617
|
this._stateNotifier.subjects["my-presence"].next(myPresence);
|
|
636
618
|
!!myself && this._stateNotifier.subjects["myself"].next(myself);
|
|
637
|
-
this.broadcast("$UPDATE_PRESENCE", { presence });
|
|
619
|
+
this.broadcast("$UPDATE_PRESENCE", { presence: newPresence });
|
|
638
620
|
};
|
|
639
621
|
this._flattenStorageStore = debounce(
|
|
640
622
|
() => __async(this, null, function* () {
|
|
641
623
|
const shouldFlatten = yield this._storageStore.getShouldFlatten();
|
|
642
|
-
if (!shouldFlatten)
|
|
643
|
-
return;
|
|
624
|
+
if (!shouldFlatten) return;
|
|
644
625
|
const updates = yield this._storageStore.getUpdates();
|
|
645
626
|
this._crdtManager.applyUpdate(updates, this);
|
|
646
627
|
const encodedState = this._crdtManager.doc.getEncodedState();
|
|
@@ -688,8 +669,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
688
669
|
const canConnect = ["Closed" /* Closed */, "Untouched" /* Untouched */].some(
|
|
689
670
|
(state) => this._state.connection.state === state
|
|
690
671
|
);
|
|
691
|
-
if (!canConnect)
|
|
692
|
-
return;
|
|
672
|
+
if (!canConnect) return;
|
|
693
673
|
this._clearTimeout(this._timeouts.reconnect);
|
|
694
674
|
this._updateState((oldState) => {
|
|
695
675
|
oldState.connection.state = "Connecting" /* Connecting */;
|
|
@@ -723,13 +703,11 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
723
703
|
if (!this._endpoints.wsEndpoint) {
|
|
724
704
|
throw new Error("Must provide an wsEndpoint.");
|
|
725
705
|
}
|
|
726
|
-
if (!this._state.webSocket)
|
|
727
|
-
return;
|
|
706
|
+
if (!this._state.webSocket) return;
|
|
728
707
|
const canDisconnect = ["Connecting" /* Connecting */, "Open" /* Open */].some(
|
|
729
708
|
(state) => this._state.connection.state === state
|
|
730
709
|
);
|
|
731
|
-
if (!canDisconnect)
|
|
732
|
-
return;
|
|
710
|
+
if (!canDisconnect) return;
|
|
733
711
|
this._clearInterval(this._intervals.heartbeat);
|
|
734
712
|
this._closeWs();
|
|
735
713
|
yield this._storageStore.destroy();
|
|
@@ -737,12 +715,6 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
737
715
|
this._stateNotifier.subjects["my-presence"].next(null);
|
|
738
716
|
});
|
|
739
717
|
}
|
|
740
|
-
getConnection() {
|
|
741
|
-
return Object.freeze(JSON.parse(JSON.stringify(this._state.connection)));
|
|
742
|
-
}
|
|
743
|
-
getDoc() {
|
|
744
|
-
return this._crdtManager.doc;
|
|
745
|
-
}
|
|
746
718
|
_addToStorageStore(update) {
|
|
747
719
|
return __async(this, null, function* () {
|
|
748
720
|
yield this._storageStore.addUpdate(update);
|
|
@@ -764,10 +736,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
764
736
|
});
|
|
765
737
|
}
|
|
766
738
|
_attachWindowListeners() {
|
|
767
|
-
if (typeof window === "undefined")
|
|
768
|
-
|
|
769
|
-
if (typeof document === "undefined")
|
|
770
|
-
return;
|
|
739
|
+
if (typeof window === "undefined") return;
|
|
740
|
+
if (typeof document === "undefined") return;
|
|
771
741
|
this._windowListeners = {
|
|
772
742
|
onNavigatorOnline: this._onNavigatorOnline.bind(this),
|
|
773
743
|
onVisibilityChange: this._onVisibilityChange.bind(this)
|
|
@@ -776,8 +746,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
776
746
|
document.addEventListener("visibilitychange", this._windowListeners.onVisibilityChange);
|
|
777
747
|
}
|
|
778
748
|
_attachWsListeners() {
|
|
779
|
-
if (!this._state.webSocket)
|
|
780
|
-
return;
|
|
749
|
+
if (!this._state.webSocket) return;
|
|
781
750
|
this._wsListeners = {
|
|
782
751
|
onClose: this._onClose.bind(this),
|
|
783
752
|
onError: this._onError.bind(this),
|
|
@@ -790,19 +759,16 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
790
759
|
this._state.webSocket.addEventListener("open", this._wsListeners.onOpen);
|
|
791
760
|
}
|
|
792
761
|
_clearInterval(interval) {
|
|
793
|
-
if (Number.isNaN(interval))
|
|
794
|
-
return;
|
|
762
|
+
if (Number.isNaN(interval)) return;
|
|
795
763
|
clearInterval(interval);
|
|
796
764
|
}
|
|
797
765
|
_clearTimeout(timeout) {
|
|
798
|
-
if (Number.isNaN(timeout))
|
|
799
|
-
return;
|
|
766
|
+
if (Number.isNaN(timeout)) return;
|
|
800
767
|
clearTimeout(timeout);
|
|
801
768
|
}
|
|
802
769
|
_closeWs() {
|
|
803
770
|
var _a, _b;
|
|
804
|
-
if (!this._state.webSocket)
|
|
805
|
-
return;
|
|
771
|
+
if (!this._state.webSocket) return;
|
|
806
772
|
(_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
|
|
807
773
|
this._clearTimeout(this._timeouts.pong);
|
|
808
774
|
this._clearTimeout(this._timeouts.reconnect);
|
|
@@ -814,24 +780,19 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
814
780
|
return readyState === ((_a2 = this._state.webSocket) == null ? void 0 : _a2.readyState);
|
|
815
781
|
}
|
|
816
782
|
);
|
|
817
|
-
if (canClose)
|
|
818
|
-
this._state.webSocket.close();
|
|
783
|
+
if (canClose) this._state.webSocket.close();
|
|
819
784
|
this._detachWsListeners();
|
|
820
785
|
}
|
|
821
786
|
_detachWindowListeners() {
|
|
822
|
-
if (!this._windowListeners)
|
|
823
|
-
|
|
824
|
-
if (typeof
|
|
825
|
-
return;
|
|
826
|
-
if (typeof document === "undefined")
|
|
827
|
-
return;
|
|
787
|
+
if (!this._windowListeners) return;
|
|
788
|
+
if (typeof window === "undefined") return;
|
|
789
|
+
if (typeof document === "undefined") return;
|
|
828
790
|
window.removeEventListener("online", this._windowListeners.onNavigatorOnline);
|
|
829
791
|
document.removeEventListener("visibilitychange", this._windowListeners.onVisibilityChange);
|
|
830
792
|
this._windowListeners = null;
|
|
831
793
|
}
|
|
832
794
|
_detachWsListeners() {
|
|
833
|
-
if (!this._state.webSocket || !this._wsListeners)
|
|
834
|
-
return;
|
|
795
|
+
if (!this._state.webSocket || !this._wsListeners) return;
|
|
835
796
|
this._state.webSocket.removeEventListener("close", this._wsListeners.onClose);
|
|
836
797
|
this._state.webSocket.removeEventListener("error", this._wsListeners.onError);
|
|
837
798
|
this._state.webSocket.removeEventListener("message", this._wsListeners.onMessage);
|
|
@@ -851,8 +812,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
851
812
|
this._crdtNotifier.rootSubject.next(storageRoot);
|
|
852
813
|
}
|
|
853
814
|
_getAuthFetchOptions(room) {
|
|
854
|
-
if (typeof this._endpoints.authEndpoint === "undefined")
|
|
855
|
-
return null;
|
|
815
|
+
if (typeof this._endpoints.authEndpoint === "undefined") return null;
|
|
856
816
|
if (this._endpoints.authEndpoint === true) {
|
|
857
817
|
return {
|
|
858
818
|
url: `/api/pluv/authorize?room=${room}`,
|
|
@@ -871,8 +831,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
871
831
|
_getAuthorization(room) {
|
|
872
832
|
return __async(this, null, function* () {
|
|
873
833
|
const fetchOptions = this._getAuthFetchOptions(room);
|
|
874
|
-
if (!fetchOptions)
|
|
875
|
-
return null;
|
|
834
|
+
if (!fetchOptions) return null;
|
|
876
835
|
const { url, options } = fetchOptions;
|
|
877
836
|
const res = yield fetch(url, options);
|
|
878
837
|
if (!res.ok || res.status !== 200) {
|
|
@@ -897,10 +856,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
897
856
|
}
|
|
898
857
|
_handleExit(message) {
|
|
899
858
|
const { connectionId } = message;
|
|
900
|
-
if (!connectionId)
|
|
901
|
-
|
|
902
|
-
if (!this._state.webSocket)
|
|
903
|
-
throw new Error("Could not find WebSocket");
|
|
859
|
+
if (!connectionId) return;
|
|
860
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
904
861
|
this._usersManager.removeUser(connectionId);
|
|
905
862
|
const others = this._usersManager.getOthers();
|
|
906
863
|
this._stateNotifier.subjects.others.next(others);
|
|
@@ -910,10 +867,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
910
867
|
_handlePresenceUpdatedMessage(message) {
|
|
911
868
|
var _a;
|
|
912
869
|
const { connectionId } = message;
|
|
913
|
-
if (!connectionId)
|
|
914
|
-
|
|
915
|
-
if (!this._state.webSocket)
|
|
916
|
-
throw new Error("Could not find WebSocket");
|
|
870
|
+
if (!connectionId) return;
|
|
871
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
917
872
|
const data = message.data;
|
|
918
873
|
this._usersManager.patchPresence(connectionId, data.presence);
|
|
919
874
|
const myPresence = this._usersManager.myPresence;
|
|
@@ -929,15 +884,11 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
929
884
|
this._stateNotifier.subjects["others"].next(others);
|
|
930
885
|
}
|
|
931
886
|
_handleReceiveOthers(message) {
|
|
932
|
-
if (!message.connectionId)
|
|
933
|
-
|
|
934
|
-
if (!this._state.webSocket)
|
|
935
|
-
throw new Error("Could not find WebSocket");
|
|
887
|
+
if (!message.connectionId) return;
|
|
888
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
936
889
|
const data = message.data;
|
|
937
890
|
Object.keys(data.others).forEach((connectionId) => {
|
|
938
891
|
const { presence, user } = data.others[connectionId];
|
|
939
|
-
if (!user)
|
|
940
|
-
return;
|
|
941
892
|
this._usersManager.setUser(connectionId, user);
|
|
942
893
|
const other = this._usersManager.getOther(connectionId);
|
|
943
894
|
!!presence && this._usersManager.patchPresence(connectionId, presence);
|
|
@@ -949,10 +900,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
949
900
|
_handleRegisteredMessage(message) {
|
|
950
901
|
var _a, _b;
|
|
951
902
|
const { connectionId, user } = message;
|
|
952
|
-
if (!connectionId)
|
|
953
|
-
|
|
954
|
-
if (!this._state.webSocket)
|
|
955
|
-
throw new Error("Could not find WebSocket");
|
|
903
|
+
if (!connectionId) return;
|
|
904
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
956
905
|
this._updateState((oldState) => {
|
|
957
906
|
oldState.connection.count += 1;
|
|
958
907
|
oldState.connection.id = connectionId;
|
|
@@ -973,10 +922,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
973
922
|
_handleStorageReceivedMessage(message) {
|
|
974
923
|
return __async(this, null, function* () {
|
|
975
924
|
const { connectionId } = message;
|
|
976
|
-
if (!connectionId)
|
|
977
|
-
|
|
978
|
-
if (!this._state.webSocket)
|
|
979
|
-
throw new Error("Could not find WebSocket");
|
|
925
|
+
if (!connectionId) return;
|
|
926
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
980
927
|
const data = message.data;
|
|
981
928
|
this._crdtManager.initialize({
|
|
982
929
|
onInitialized: (update2) => __async(this, null, function* () {
|
|
@@ -1000,13 +947,10 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1000
947
|
}
|
|
1001
948
|
_handleStorageUpdatedMessage(message) {
|
|
1002
949
|
const { connectionId } = message;
|
|
1003
|
-
if (!connectionId)
|
|
1004
|
-
|
|
1005
|
-
if (!this._state.webSocket)
|
|
1006
|
-
throw new Error("Could not find WebSocket");
|
|
950
|
+
if (!connectionId) return;
|
|
951
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
1007
952
|
const data = message.data;
|
|
1008
|
-
if (!this._crdtManager)
|
|
1009
|
-
return;
|
|
953
|
+
if (!this._crdtManager) return;
|
|
1010
954
|
this._crdtManager.doc.applyEncodedState({
|
|
1011
955
|
origin: ORIGIN_STORAGE_UPDATED,
|
|
1012
956
|
update: data.state
|
|
@@ -1014,8 +958,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1014
958
|
const sharedTypes = this._crdtManager.doc.get();
|
|
1015
959
|
const storageRoot = Object.entries(sharedTypes).reduce(
|
|
1016
960
|
(acc, [prop, sharedType]) => {
|
|
1017
|
-
if (!this._crdtManager)
|
|
1018
|
-
return acc;
|
|
961
|
+
if (!this._crdtManager) return acc;
|
|
1019
962
|
const serialized = sharedType.toJson();
|
|
1020
963
|
this._crdtNotifier.subject(prop).next(serialized);
|
|
1021
964
|
return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
|
|
@@ -1026,12 +969,9 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1026
969
|
}
|
|
1027
970
|
_handleUserJoinedMessage(message) {
|
|
1028
971
|
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;
|
|
972
|
+
if (!connectionId) return;
|
|
973
|
+
if (!this._state.webSocket) throw new Error("Could not find WebSocket");
|
|
974
|
+
if (!this._usersManager.myself) return;
|
|
1035
975
|
const data = message.data;
|
|
1036
976
|
const myself = this._usersManager.myself;
|
|
1037
977
|
if (myself.connectionId === connectionId) {
|
|
@@ -1050,15 +990,13 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1050
990
|
}
|
|
1051
991
|
_logDebug(...data) {
|
|
1052
992
|
var _a;
|
|
1053
|
-
if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production")
|
|
1054
|
-
return;
|
|
993
|
+
if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production") return;
|
|
1055
994
|
this._debug && console.log(...data);
|
|
1056
995
|
}
|
|
1057
996
|
_observeCrdt() {
|
|
1058
997
|
var _a, _b;
|
|
1059
998
|
(_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
|
|
1060
|
-
if (!this._crdtManager)
|
|
1061
|
-
return;
|
|
999
|
+
if (!this._crdtManager) return;
|
|
1062
1000
|
const unsubscribe = this._crdtManager.doc.subscribe((event) => {
|
|
1063
1001
|
var _a2;
|
|
1064
1002
|
const origin = (_a2 = event.origin) != null ? _a2 : null;
|
|
@@ -1067,12 +1005,9 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1067
1005
|
if (origin === ORIGIN_INITIALIZED || origin === ORIGIN_STORAGE_UPDATED) {
|
|
1068
1006
|
return;
|
|
1069
1007
|
}
|
|
1070
|
-
if (!this._state.webSocket)
|
|
1071
|
-
|
|
1072
|
-
if (
|
|
1073
|
-
return;
|
|
1074
|
-
if (this._state.webSocket.readyState !== WebSocket.OPEN)
|
|
1075
|
-
return;
|
|
1008
|
+
if (!this._state.webSocket) return;
|
|
1009
|
+
if (!this._state.connection.id) return;
|
|
1010
|
+
if (this._state.webSocket.readyState !== WebSocket.OPEN) return;
|
|
1076
1011
|
this._sendMessage({
|
|
1077
1012
|
type: "$UPDATE_STORAGE",
|
|
1078
1013
|
data: { origin, update: event.update }
|
|
@@ -1120,16 +1055,14 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1120
1055
|
}
|
|
1121
1056
|
_onMessage(event) {
|
|
1122
1057
|
const message = this._parseMessage(event);
|
|
1123
|
-
if (!message)
|
|
1124
|
-
return;
|
|
1058
|
+
if (!message) return;
|
|
1125
1059
|
const shouldLog = typeof this._debug === "boolean" ? this._debug : this._debug.output.find((value) => value === message.type);
|
|
1126
1060
|
if (shouldLog) {
|
|
1127
1061
|
this._logDebug("WebSocket event received: ", message.type, message);
|
|
1128
1062
|
}
|
|
1129
1063
|
this._eventNotifier.subject(message.type).next(message);
|
|
1130
1064
|
const { connectionId } = message;
|
|
1131
|
-
if (!connectionId)
|
|
1132
|
-
return;
|
|
1065
|
+
if (!connectionId) return;
|
|
1133
1066
|
switch (message.type) {
|
|
1134
1067
|
case "$EXIT": {
|
|
1135
1068
|
this._handleExit(message);
|
|
@@ -1167,8 +1100,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1167
1100
|
}
|
|
1168
1101
|
}
|
|
1169
1102
|
_onOpen() {
|
|
1170
|
-
if (this._state.connection.state !== "Connecting" /* Connecting */)
|
|
1171
|
-
return;
|
|
1103
|
+
if (this._state.connection.state !== "Connecting" /* Connecting */) return;
|
|
1172
1104
|
this._logDebug("WebSocket connected");
|
|
1173
1105
|
this._updateState((oldState) => {
|
|
1174
1106
|
oldState.connection.state = "Open" /* Open */;
|
|
@@ -1187,10 +1119,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1187
1119
|
}
|
|
1188
1120
|
_onVisibilityChange() {
|
|
1189
1121
|
return __async(this, null, function* () {
|
|
1190
|
-
if (typeof document === "undefined")
|
|
1191
|
-
|
|
1192
|
-
if (document.visibilityState !== "visible")
|
|
1193
|
-
return;
|
|
1122
|
+
if (typeof document === "undefined") return;
|
|
1123
|
+
if (document.visibilityState !== "visible") return;
|
|
1194
1124
|
switch (this._state.connection.state) {
|
|
1195
1125
|
case "Open" /* Open */:
|
|
1196
1126
|
this._clearInterval(this._intervals.heartbeat);
|
|
@@ -1204,16 +1134,12 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1204
1134
|
});
|
|
1205
1135
|
}
|
|
1206
1136
|
_parseMessage(message) {
|
|
1207
|
-
if (typeof message.data !== "string")
|
|
1208
|
-
return null;
|
|
1137
|
+
if (typeof message.data !== "string") return null;
|
|
1209
1138
|
try {
|
|
1210
1139
|
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;
|
|
1140
|
+
if (typeof config !== "object") return null;
|
|
1141
|
+
if (typeof config.type !== "string") return null;
|
|
1142
|
+
if (typeof config.data !== "object") return null;
|
|
1217
1143
|
return config;
|
|
1218
1144
|
} catch (e) {
|
|
1219
1145
|
return null;
|
|
@@ -1224,8 +1150,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1224
1150
|
if (!this._endpoints.wsEndpoint) {
|
|
1225
1151
|
throw new Error("Must provide an wsEndpoint.");
|
|
1226
1152
|
}
|
|
1227
|
-
if (!this._state.webSocket)
|
|
1228
|
-
return;
|
|
1153
|
+
if (!this._state.webSocket) return;
|
|
1229
1154
|
this._closeWs();
|
|
1230
1155
|
this._updateState((oldState) => {
|
|
1231
1156
|
oldState.authorization.token = null;
|
|
@@ -1238,8 +1163,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1238
1163
|
}
|
|
1239
1164
|
_sendMessage(data) {
|
|
1240
1165
|
const webSocket = this._state.webSocket;
|
|
1241
|
-
if ((webSocket == null ? void 0 : webSocket.readyState) !== WebSocket.OPEN)
|
|
1242
|
-
return;
|
|
1166
|
+
if ((webSocket == null ? void 0 : webSocket.readyState) !== WebSocket.OPEN) return;
|
|
1243
1167
|
const shouldLog = typeof this._debug === "boolean" ? this._debug : this._debug.input.find((value) => value === data.type);
|
|
1244
1168
|
if (shouldLog) {
|
|
1245
1169
|
this._logDebug("WebSocket event sent: ", data.type, data);
|
|
@@ -1272,8 +1196,7 @@ var PluvClient = class {
|
|
|
1272
1196
|
this._rooms = /* @__PURE__ */ new Map();
|
|
1273
1197
|
this.createRoom = (room, options) => {
|
|
1274
1198
|
const oldRoom = this.getRoom(room);
|
|
1275
|
-
if (oldRoom)
|
|
1276
|
-
return oldRoom;
|
|
1199
|
+
if (oldRoom) return oldRoom;
|
|
1277
1200
|
const newRoom = new PluvRoom(room, {
|
|
1278
1201
|
addons: options.addons,
|
|
1279
1202
|
authEndpoint: this._authEndpoint,
|
|
@@ -1306,8 +1229,7 @@ var PluvClient = class {
|
|
|
1306
1229
|
};
|
|
1307
1230
|
this.leave = (room) => __async(this, null, function* () {
|
|
1308
1231
|
const toLeave = typeof room === "string" ? this.getRoom(room) : room;
|
|
1309
|
-
if (!toLeave)
|
|
1310
|
-
return;
|
|
1232
|
+
if (!toLeave) return;
|
|
1311
1233
|
yield toLeave.disconnect();
|
|
1312
1234
|
this._rooms.delete(toLeave.id);
|
|
1313
1235
|
this._logDebug(`Left and deleted room: ${room}`);
|
|
@@ -1319,8 +1241,7 @@ var PluvClient = class {
|
|
|
1319
1241
|
}
|
|
1320
1242
|
_logDebug(...data) {
|
|
1321
1243
|
var _a;
|
|
1322
|
-
if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production")
|
|
1323
|
-
return;
|
|
1244
|
+
if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production") return;
|
|
1324
1245
|
this._debug && console.log(...data);
|
|
1325
1246
|
}
|
|
1326
1247
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pluv/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.0",
|
|
4
4
|
"description": "@pluv/io frontend client",
|
|
5
5
|
"author": "leedavidcs",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,15 +18,15 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"wonka": "^6.3.4",
|
|
21
|
-
"@pluv/crdt": "^0.
|
|
22
|
-
"@pluv/types": "^0.
|
|
21
|
+
"@pluv/crdt": "^0.19.0",
|
|
22
|
+
"@pluv/types": "^0.19.0"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"eslint": "^8.57.0",
|
|
26
|
-
"tsup": "^8.0
|
|
26
|
+
"tsup": "^8.1.0",
|
|
27
27
|
"typescript": "^5.4.5",
|
|
28
|
-
"@pluv/tsconfig": "^0.
|
|
29
|
-
"eslint-config-pluv": "^0.
|
|
28
|
+
"@pluv/tsconfig": "^0.19.0",
|
|
29
|
+
"eslint-config-pluv": "^0.19.0"
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
32
|
"build": "tsup src/index.ts --format esm,cjs --dts",
|