@pluv/client 0.17.3 → 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/dist/index.mjs CHANGED
@@ -57,10 +57,7 @@ import { noop } from "@pluv/crdt";
57
57
  var CrdtManager = class {
58
58
  constructor(options) {
59
59
  this.initialized = false;
60
- const {
61
- encodedState,
62
- initialStorage = noop.doc({})
63
- } = options;
60
+ const { encodedState, initialStorage = noop.doc({}) } = options;
64
61
  this._docFactory = initialStorage;
65
62
  this.doc = encodedState ? initialStorage.getFresh().applyEncodedState({ update: encodedState }) : initialStorage.getFresh();
66
63
  }
@@ -71,8 +68,7 @@ var CrdtManager = class {
71
68
  * @date June 13, 2023
72
69
  */
73
70
  applyUpdate(update, origin) {
74
- if (!this.initialized)
75
- return this;
71
+ if (!this.initialized) return this;
76
72
  const updates = typeof update === "string" ? [update] : update;
77
73
  this._applyDocUpdates(this.doc, updates, origin);
78
74
  return this;
@@ -86,24 +82,15 @@ var CrdtManager = class {
86
82
  initialize(params) {
87
83
  const { onInitialized, origin, update } = params;
88
84
  const updates = typeof update === "string" ? [update] : update;
89
- if (!updates.length)
90
- return this;
85
+ if (!updates.length) return this;
91
86
  if (this.initialized) {
92
87
  this.doc = this._applyDocUpdates(this.doc, updates, origin).track();
93
88
  return this;
94
89
  }
95
- const onEmpty = this._applyDocUpdates(
96
- this._docFactory.getEmpty(),
97
- updates,
98
- origin
99
- );
90
+ const onEmpty = this._applyDocUpdates(this._docFactory.getEmpty(), updates, origin);
100
91
  if (!onEmpty.isEmpty()) {
101
92
  onEmpty.destroy();
102
- this.doc = this._applyDocUpdates(
103
- this._docFactory.getFresh(),
104
- updates,
105
- origin
106
- ).track();
93
+ this.doc = this._applyDocUpdates(this._docFactory.getFresh(), updates, origin).track();
107
94
  this.initialized = true;
108
95
  onInitialized == null ? void 0 : onInitialized(this.doc.getEncodedState());
109
96
  return this;
@@ -116,9 +103,7 @@ var CrdtManager = class {
116
103
  }
117
104
  _applyDocUpdates(doc, updates, origin) {
118
105
  doc.transact(() => {
119
- doc.batchApplyEncodedState(
120
- updates.map((update) => ({ origin, update }))
121
- );
106
+ doc.batchApplyEncodedState(updates.map((update) => ({ origin, update })));
122
107
  }, origin);
123
108
  return doc;
124
109
  }
@@ -157,8 +142,7 @@ var EventNotifier = class {
157
142
  }
158
143
  subject(name) {
159
144
  const subject = this.subjects.get(name);
160
- if (subject)
161
- return subject;
145
+ if (subject) return subject;
162
146
  const newSubject = makeSubject2();
163
147
  this.subjects.set(name, newSubject);
164
148
  return newSubject;
@@ -177,8 +161,7 @@ var OtherNotifier = class {
177
161
  }
178
162
  subject(name) {
179
163
  const subject = this.subjects.get(name);
180
- if (subject)
181
- return subject;
164
+ if (subject) return subject;
182
165
  const newSubject = makeSubject3();
183
166
  this.subjects.set(name, newSubject);
184
167
  return newSubject;
@@ -204,8 +187,7 @@ var StateNotifier = class {
204
187
  subscribe(name, callback) {
205
188
  var _a;
206
189
  const source = (_a = this.subjects[name]) == null ? void 0 : _a.source;
207
- if (!source)
208
- throw new Error(`Could not subscribe to source: ${name}`);
190
+ if (!source) throw new Error(`Could not subscribe to source: ${name}`);
209
191
  return subscribe4(callback)(source).unsubscribe;
210
192
  }
211
193
  };
@@ -249,8 +231,7 @@ var UsersManager = class {
249
231
  return;
250
232
  }
251
233
  const other = this._others.get(connectionId);
252
- if (!other)
253
- return;
234
+ if (!other) return;
254
235
  const presence = __spreadValues(__spreadValues({}, other.presence), patch);
255
236
  (_b = this._presence) == null ? void 0 : _b.parse(presence);
256
237
  this._others.set(connectionId, __spreadProps(__spreadValues({}, other), { presence }));
@@ -277,17 +258,14 @@ var UsersManager = class {
277
258
  return;
278
259
  }
279
260
  const other = this._others.get(connectionId);
280
- if (!other)
281
- return;
261
+ if (!other) return;
282
262
  this._others.set(connectionId, __spreadProps(__spreadValues({}, other), { presence }));
283
263
  }
284
264
  setUser(connectionId, user, presence) {
285
265
  var _a;
286
- if (((_a = this._myself) == null ? void 0 : _a.connectionId) === connectionId)
287
- return;
266
+ if (((_a = this._myself) == null ? void 0 : _a.connectionId) === connectionId) return;
288
267
  const other = this._others.get(connectionId);
289
- if (other)
290
- return;
268
+ if (other) return;
291
269
  this._others.set(connectionId, {
292
270
  connectionId,
293
271
  presence: presence != null ? presence : this._initialPresence,
@@ -299,8 +277,7 @@ var UsersManager = class {
299
277
  */
300
278
  updateMyPresence(presence) {
301
279
  this._myPresence = __spreadValues(__spreadValues({}, this._myPresence), presence);
302
- if (!this._myself)
303
- return;
280
+ if (!this._myself) return;
304
281
  this._myself.presence = this._myPresence;
305
282
  }
306
283
  };
@@ -348,6 +325,9 @@ var MockedRoom = class extends AbstractRoom {
348
325
  this.event = (event, callback) => {
349
326
  return this._eventNotifier.subscribe(event, callback);
350
327
  };
328
+ this.getConnection = () => {
329
+ return Object.freeze(JSON.parse(JSON.stringify(this._state.connection)));
330
+ };
351
331
  this.getMyPresence = () => {
352
332
  return this._usersManager.myPresence;
353
333
  };
@@ -362,8 +342,7 @@ var MockedRoom = class extends AbstractRoom {
362
342
  };
363
343
  this.getStorage = (key) => {
364
344
  const sharedType = this._crdtManager.get(key);
365
- if (typeof sharedType === "undefined")
366
- return null;
345
+ if (typeof sharedType === "undefined") return null;
367
346
  return sharedType.value;
368
347
  };
369
348
  this.other = (connectionId, callback) => {
@@ -384,10 +363,8 @@ var MockedRoom = class extends AbstractRoom {
384
363
  this.transact = (fn, origin) => {
385
364
  const _origin = origin != null ? origin : this._state.connection.id;
386
365
  const crdtManager = this._crdtManager;
387
- if (typeof _origin !== "string")
388
- return;
389
- if (!crdtManager)
390
- return;
366
+ if (typeof _origin !== "string") return;
367
+ if (!crdtManager) return;
391
368
  crdtManager.doc.transact(() => {
392
369
  const storage = crdtManager.doc.get();
393
370
  fn(storage);
@@ -398,7 +375,8 @@ var MockedRoom = class extends AbstractRoom {
398
375
  };
399
376
  this.updateMyPresence = (presence) => {
400
377
  var _a;
401
- this._usersManager.updateMyPresence(presence);
378
+ const newPresence = typeof presence === "function" ? presence(this.getMyPresence()) : presence;
379
+ this._usersManager.updateMyPresence(newPresence);
402
380
  const myPresence = this._usersManager.myPresence;
403
381
  const myself = (_a = this._usersManager.myself) != null ? _a : null;
404
382
  this._stateNotifier.subjects["my-presence"].next(myPresence);
@@ -418,45 +396,33 @@ var MockedRoom = class extends AbstractRoom {
418
396
  return true;
419
397
  }
420
398
  broadcast(event, data) {
421
- if (!this._events)
422
- return;
399
+ if (!this._events) return;
423
400
  const resolver = this._events[event];
424
- if (!resolver)
425
- return;
401
+ if (!resolver) return;
426
402
  const result = resolver(data);
427
403
  Object.keys(result).forEach((type) => {
428
404
  const _type = type.toString();
429
405
  const data2 = result[_type];
430
- if (!data2)
431
- return;
406
+ if (!data2) return;
432
407
  this._eventNotifier.subject(_type).next(data2);
433
408
  });
434
409
  }
435
- getConnection() {
436
- return Object.freeze(
437
- JSON.parse(JSON.stringify(this._state.connection))
438
- );
439
- }
440
410
  getDoc() {
441
411
  return this._crdtManager.doc;
442
412
  }
443
413
  _observeCrdt() {
444
414
  var _a, _b;
445
415
  (_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
446
- if (!this._crdtManager)
447
- return;
416
+ if (!this._crdtManager) return;
448
417
  const unsubscribe = this._crdtManager.doc.subscribe((event) => {
449
418
  var _a2;
450
419
  const origin = (_a2 = event.origin) != null ? _a2 : null;
451
- if (!this._crdtManager)
452
- return;
453
- if (origin === "$STORAGE_UPDATED")
454
- return;
420
+ if (!this._crdtManager) return;
421
+ if (origin === "$STORAGE_UPDATED") return;
455
422
  const sharedTypes = this._crdtManager.doc.get();
456
423
  const storageRoot = Object.entries(sharedTypes).reduce(
457
424
  (acc, [prop, sharedType]) => {
458
- if (!this._crdtManager)
459
- return acc;
425
+ if (!this._crdtManager) return acc;
460
426
  const serialized = sharedType.toJson();
461
427
  this._crdtNotifier.subject(prop).next(serialized);
462
428
  return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
@@ -500,12 +466,10 @@ var debounce = (fn, options) => {
500
466
  let prevResult;
501
467
  let timeout = null;
502
468
  return (...args) => {
503
- if (typeof timeout === "number")
504
- clearTimeout(timeout);
469
+ if (typeof timeout === "number") clearTimeout(timeout);
505
470
  timeout = setTimeout(() => {
506
471
  timeout = null;
507
- if (!immediate)
508
- prevResult = fn(args);
472
+ if (!immediate) prevResult = fn(args);
509
473
  }, wait);
510
474
  if (immediate && !timeout) {
511
475
  fn(args);
@@ -566,14 +530,40 @@ var PluvRoom = class extends AbstractRoom {
566
530
  };
567
531
  this._windowListeners = null;
568
532
  this._wsListeners = null;
533
+ this.broadcast = new Proxy(
534
+ (event, data) => {
535
+ if (!this._state.webSocket) return;
536
+ if (this._state.connection.state !== "Open" /* Open */) return;
537
+ const type = event.toString();
538
+ this._sendMessage({ data, type });
539
+ },
540
+ {
541
+ get(fn, prop) {
542
+ return (data) => {
543
+ return fn(prop, data);
544
+ };
545
+ }
546
+ }
547
+ );
569
548
  this.canRedo = () => {
570
549
  return !!this._crdtManager.doc.canRedo();
571
550
  };
572
551
  this.canUndo = () => {
573
552
  return !!this._crdtManager.doc.canUndo();
574
553
  };
575
- this.event = (event, callback) => {
576
- return this._eventNotifier.subscribe(event, callback);
554
+ this.event = new Proxy(
555
+ (event, callback) => this._eventNotifier.subscribe(event, callback),
556
+ {
557
+ get(fn, prop) {
558
+ return (callback) => fn(prop, callback);
559
+ }
560
+ }
561
+ );
562
+ this.getConnection = () => {
563
+ return Object.freeze(JSON.parse(JSON.stringify(this._state.connection)));
564
+ };
565
+ this.getDoc = () => {
566
+ return this._crdtManager.doc;
577
567
  };
578
568
  this.getMyPresence = () => {
579
569
  return this._usersManager.myPresence;
@@ -589,8 +579,7 @@ var PluvRoom = class extends AbstractRoom {
589
579
  };
590
580
  this.getStorage = (key) => {
591
581
  const sharedType = this._crdtManager.get(key);
592
- if (typeof sharedType === "undefined")
593
- return null;
582
+ if (typeof sharedType === "undefined") return null;
594
583
  return sharedType;
595
584
  };
596
585
  this.other = (connectionId, callback) => {
@@ -610,8 +599,7 @@ var PluvRoom = class extends AbstractRoom {
610
599
  };
611
600
  this.transact = (fn, origin) => {
612
601
  const _origin = origin != null ? origin : this._state.connection.id;
613
- if (typeof _origin !== "string")
614
- return;
602
+ if (typeof _origin !== "string") return;
615
603
  this._crdtManager.doc.transact(() => {
616
604
  const storage = this._crdtManager.doc.get();
617
605
  fn(storage);
@@ -622,21 +610,18 @@ var PluvRoom = class extends AbstractRoom {
622
610
  };
623
611
  this.updateMyPresence = (presence) => {
624
612
  var _a;
625
- this._usersManager.updateMyPresence(presence);
613
+ const newPresence = typeof presence === "function" ? presence(this.getMyPresence()) : presence;
614
+ this._usersManager.updateMyPresence(newPresence);
626
615
  const myPresence = this._usersManager.myPresence;
627
616
  const myself = (_a = this._usersManager.myself) != null ? _a : null;
628
617
  this._stateNotifier.subjects["my-presence"].next(myPresence);
629
618
  !!myself && this._stateNotifier.subjects["myself"].next(myself);
630
- this.broadcast(
631
- "$UPDATE_PRESENCE",
632
- { presence }
633
- );
619
+ this.broadcast("$UPDATE_PRESENCE", { presence: newPresence });
634
620
  };
635
621
  this._flattenStorageStore = debounce(
636
622
  () => __async(this, null, function* () {
637
623
  const shouldFlatten = yield this._storageStore.getShouldFlatten();
638
- if (!shouldFlatten)
639
- return;
624
+ if (!shouldFlatten) return;
640
625
  const updates = yield this._storageStore.getUpdates();
641
626
  this._crdtManager.applyUpdate(updates, this);
642
627
  const encodedState = this._crdtManager.doc.getEncodedState();
@@ -658,10 +643,7 @@ var PluvRoom = class extends AbstractRoom {
658
643
  this._listeners = {
659
644
  onAuthorizationFail: (error) => {
660
645
  this._clearTimeout(this._timeouts.reconnect);
661
- this._timeouts.reconnect = setTimeout(
662
- this._reconnect.bind(this),
663
- RECONNECT_TIMEOUT_MS
664
- );
646
+ this._timeouts.reconnect = setTimeout(this._reconnect.bind(this), RECONNECT_TIMEOUT_MS);
665
647
  onAuthorizationFail == null ? void 0 : onAuthorizationFail(error);
666
648
  }
667
649
  };
@@ -679,25 +661,15 @@ var PluvRoom = class extends AbstractRoom {
679
661
  get webSocket() {
680
662
  return this._state.webSocket;
681
663
  }
682
- broadcast(event, data) {
683
- if (!this._state.webSocket)
684
- return;
685
- if (this._state.connection.state !== "Open" /* Open */)
686
- return;
687
- const type = event.toString();
688
- this._sendMessage({ data, type });
689
- }
690
664
  connect() {
691
665
  return __async(this, null, function* () {
692
666
  if (!this._endpoints.wsEndpoint) {
693
667
  throw new Error("Must provide an wsEndpoint.");
694
668
  }
695
- const canConnect = [
696
- "Closed" /* Closed */,
697
- "Untouched" /* Untouched */
698
- ].some((state) => this._state.connection.state === state);
699
- if (!canConnect)
700
- return;
669
+ const canConnect = ["Closed" /* Closed */, "Untouched" /* Untouched */].some(
670
+ (state) => this._state.connection.state === state
671
+ );
672
+ if (!canConnect) return;
701
673
  this._clearTimeout(this._timeouts.reconnect);
702
674
  this._updateState((oldState) => {
703
675
  oldState.connection.state = "Connecting" /* Connecting */;
@@ -731,14 +703,11 @@ var PluvRoom = class extends AbstractRoom {
731
703
  if (!this._endpoints.wsEndpoint) {
732
704
  throw new Error("Must provide an wsEndpoint.");
733
705
  }
734
- if (!this._state.webSocket)
735
- return;
736
- const canDisconnect = [
737
- "Connecting" /* Connecting */,
738
- "Open" /* Open */
739
- ].some((state) => this._state.connection.state === state);
740
- if (!canDisconnect)
741
- return;
706
+ if (!this._state.webSocket) return;
707
+ const canDisconnect = ["Connecting" /* Connecting */, "Open" /* Open */].some(
708
+ (state) => this._state.connection.state === state
709
+ );
710
+ if (!canDisconnect) return;
742
711
  this._clearInterval(this._intervals.heartbeat);
743
712
  this._closeWs();
744
713
  yield this._storageStore.destroy();
@@ -746,14 +715,6 @@ var PluvRoom = class extends AbstractRoom {
746
715
  this._stateNotifier.subjects["my-presence"].next(null);
747
716
  });
748
717
  }
749
- getConnection() {
750
- return Object.freeze(
751
- JSON.parse(JSON.stringify(this._state.connection))
752
- );
753
- }
754
- getDoc() {
755
- return this._crdtManager.doc;
756
- }
757
718
  _addToStorageStore(update) {
758
719
  return __async(this, null, function* () {
759
720
  yield this._storageStore.addUpdate(update);
@@ -775,63 +736,39 @@ var PluvRoom = class extends AbstractRoom {
775
736
  });
776
737
  }
777
738
  _attachWindowListeners() {
778
- if (typeof window === "undefined")
779
- return;
780
- if (typeof document === "undefined")
781
- return;
739
+ if (typeof window === "undefined") return;
740
+ if (typeof document === "undefined") return;
782
741
  this._windowListeners = {
783
742
  onNavigatorOnline: this._onNavigatorOnline.bind(this),
784
743
  onVisibilityChange: this._onVisibilityChange.bind(this)
785
744
  };
786
- window.addEventListener(
787
- "online",
788
- this._windowListeners.onNavigatorOnline
789
- );
790
- document.addEventListener(
791
- "visibilitychange",
792
- this._windowListeners.onVisibilityChange
793
- );
745
+ window.addEventListener("online", this._windowListeners.onNavigatorOnline);
746
+ document.addEventListener("visibilitychange", this._windowListeners.onVisibilityChange);
794
747
  }
795
748
  _attachWsListeners() {
796
- if (!this._state.webSocket)
797
- return;
749
+ if (!this._state.webSocket) return;
798
750
  this._wsListeners = {
799
751
  onClose: this._onClose.bind(this),
800
752
  onError: this._onError.bind(this),
801
753
  onMessage: this._onMessage.bind(this),
802
754
  onOpen: this._onOpen.bind(this)
803
755
  };
804
- this._state.webSocket.addEventListener(
805
- "close",
806
- this._wsListeners.onClose
807
- );
808
- this._state.webSocket.addEventListener(
809
- "error",
810
- this._wsListeners.onError
811
- );
812
- this._state.webSocket.addEventListener(
813
- "message",
814
- this._wsListeners.onMessage
815
- );
816
- this._state.webSocket.addEventListener(
817
- "open",
818
- this._wsListeners.onOpen
819
- );
756
+ this._state.webSocket.addEventListener("close", this._wsListeners.onClose);
757
+ this._state.webSocket.addEventListener("error", this._wsListeners.onError);
758
+ this._state.webSocket.addEventListener("message", this._wsListeners.onMessage);
759
+ this._state.webSocket.addEventListener("open", this._wsListeners.onOpen);
820
760
  }
821
761
  _clearInterval(interval) {
822
- if (Number.isNaN(interval))
823
- return;
762
+ if (Number.isNaN(interval)) return;
824
763
  clearInterval(interval);
825
764
  }
826
765
  _clearTimeout(timeout) {
827
- if (Number.isNaN(timeout))
828
- return;
766
+ if (Number.isNaN(timeout)) return;
829
767
  clearTimeout(timeout);
830
768
  }
831
769
  _closeWs() {
832
770
  var _a, _b;
833
- if (!this._state.webSocket)
834
- return;
771
+ if (!this._state.webSocket) return;
835
772
  (_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
836
773
  this._clearTimeout(this._timeouts.pong);
837
774
  this._clearTimeout(this._timeouts.reconnect);
@@ -843,46 +780,23 @@ var PluvRoom = class extends AbstractRoom {
843
780
  return readyState === ((_a2 = this._state.webSocket) == null ? void 0 : _a2.readyState);
844
781
  }
845
782
  );
846
- if (canClose)
847
- this._state.webSocket.close();
783
+ if (canClose) this._state.webSocket.close();
848
784
  this._detachWsListeners();
849
785
  }
850
786
  _detachWindowListeners() {
851
- if (!this._windowListeners)
852
- return;
853
- if (typeof window === "undefined")
854
- return;
855
- if (typeof document === "undefined")
856
- return;
857
- window.removeEventListener(
858
- "online",
859
- this._windowListeners.onNavigatorOnline
860
- );
861
- document.removeEventListener(
862
- "visibilitychange",
863
- this._windowListeners.onVisibilityChange
864
- );
787
+ if (!this._windowListeners) return;
788
+ if (typeof window === "undefined") return;
789
+ if (typeof document === "undefined") return;
790
+ window.removeEventListener("online", this._windowListeners.onNavigatorOnline);
791
+ document.removeEventListener("visibilitychange", this._windowListeners.onVisibilityChange);
865
792
  this._windowListeners = null;
866
793
  }
867
794
  _detachWsListeners() {
868
- if (!this._state.webSocket || !this._wsListeners)
869
- return;
870
- this._state.webSocket.removeEventListener(
871
- "close",
872
- this._wsListeners.onClose
873
- );
874
- this._state.webSocket.removeEventListener(
875
- "error",
876
- this._wsListeners.onError
877
- );
878
- this._state.webSocket.removeEventListener(
879
- "message",
880
- this._wsListeners.onMessage
881
- );
882
- this._state.webSocket.removeEventListener(
883
- "open",
884
- this._wsListeners.onOpen
885
- );
795
+ if (!this._state.webSocket || !this._wsListeners) return;
796
+ this._state.webSocket.removeEventListener("close", this._wsListeners.onClose);
797
+ this._state.webSocket.removeEventListener("error", this._wsListeners.onError);
798
+ this._state.webSocket.removeEventListener("message", this._wsListeners.onMessage);
799
+ this._state.webSocket.removeEventListener("open", this._wsListeners.onOpen);
886
800
  this._wsListeners = null;
887
801
  }
888
802
  _emitSharedTypes() {
@@ -898,8 +812,7 @@ var PluvRoom = class extends AbstractRoom {
898
812
  this._crdtNotifier.rootSubject.next(storageRoot);
899
813
  }
900
814
  _getAuthFetchOptions(room) {
901
- if (typeof this._endpoints.authEndpoint === "undefined")
902
- return null;
815
+ if (typeof this._endpoints.authEndpoint === "undefined") return null;
903
816
  if (this._endpoints.authEndpoint === true) {
904
817
  return {
905
818
  url: `/api/pluv/authorize?room=${room}`,
@@ -918,8 +831,7 @@ var PluvRoom = class extends AbstractRoom {
918
831
  _getAuthorization(room) {
919
832
  return __async(this, null, function* () {
920
833
  const fetchOptions = this._getAuthFetchOptions(room);
921
- if (!fetchOptions)
922
- return null;
834
+ if (!fetchOptions) return null;
923
835
  const { url, options } = fetchOptions;
924
836
  const res = yield fetch(url, options);
925
837
  if (!res.ok || res.status !== 200) {
@@ -928,9 +840,7 @@ var PluvRoom = class extends AbstractRoom {
928
840
  try {
929
841
  return yield res.text().then((text) => text.trim());
930
842
  } catch (err) {
931
- throw new Error(
932
- err instanceof Error ? err.message : "Room is unauthorized"
933
- );
843
+ throw new Error(err instanceof Error ? err.message : "Room is unauthorized");
934
844
  }
935
845
  });
936
846
  }
@@ -946,10 +856,8 @@ var PluvRoom = class extends AbstractRoom {
946
856
  }
947
857
  _handleExit(message) {
948
858
  const { connectionId } = message;
949
- if (!connectionId)
950
- return;
951
- if (!this._state.webSocket)
952
- throw new Error("Could not find WebSocket");
859
+ if (!connectionId) return;
860
+ if (!this._state.webSocket) throw new Error("Could not find WebSocket");
953
861
  this._usersManager.removeUser(connectionId);
954
862
  const others = this._usersManager.getOthers();
955
863
  this._stateNotifier.subjects.others.next(others);
@@ -959,15 +867,10 @@ var PluvRoom = class extends AbstractRoom {
959
867
  _handlePresenceUpdatedMessage(message) {
960
868
  var _a;
961
869
  const { connectionId } = message;
962
- if (!connectionId)
963
- return;
964
- if (!this._state.webSocket)
965
- throw new Error("Could not find WebSocket");
870
+ if (!connectionId) return;
871
+ if (!this._state.webSocket) throw new Error("Could not find WebSocket");
966
872
  const data = message.data;
967
- this._usersManager.patchPresence(
968
- connectionId,
969
- data.presence
970
- );
873
+ this._usersManager.patchPresence(connectionId, data.presence);
971
874
  const myPresence = this._usersManager.myPresence;
972
875
  const myself = (_a = this._usersManager.myself) != null ? _a : null;
973
876
  if ((myself == null ? void 0 : myself.connectionId) === connectionId) {
@@ -981,21 +884,14 @@ var PluvRoom = class extends AbstractRoom {
981
884
  this._stateNotifier.subjects["others"].next(others);
982
885
  }
983
886
  _handleReceiveOthers(message) {
984
- if (!message.connectionId)
985
- return;
986
- if (!this._state.webSocket)
987
- throw new Error("Could not find WebSocket");
887
+ if (!message.connectionId) return;
888
+ if (!this._state.webSocket) throw new Error("Could not find WebSocket");
988
889
  const data = message.data;
989
890
  Object.keys(data.others).forEach((connectionId) => {
990
891
  const { presence, user } = data.others[connectionId];
991
- if (!user)
992
- return;
993
892
  this._usersManager.setUser(connectionId, user);
994
893
  const other = this._usersManager.getOther(connectionId);
995
- !!presence && this._usersManager.patchPresence(
996
- connectionId,
997
- presence
998
- );
894
+ !!presence && this._usersManager.patchPresence(connectionId, presence);
999
895
  this._otherNotifier.subject(connectionId).next(other);
1000
896
  });
1001
897
  const others = this._usersManager.getOthers();
@@ -1004,20 +900,15 @@ var PluvRoom = class extends AbstractRoom {
1004
900
  _handleRegisteredMessage(message) {
1005
901
  var _a, _b;
1006
902
  const { connectionId, user } = message;
1007
- if (!connectionId)
1008
- return;
1009
- if (!this._state.webSocket)
1010
- throw new Error("Could not find WebSocket");
903
+ if (!connectionId) return;
904
+ if (!this._state.webSocket) throw new Error("Could not find WebSocket");
1011
905
  this._updateState((oldState) => {
1012
906
  oldState.connection.count += 1;
1013
907
  oldState.connection.id = connectionId;
1014
908
  oldState.authorization.user = user;
1015
909
  return oldState;
1016
910
  });
1017
- this._usersManager.setMyself(
1018
- connectionId,
1019
- user
1020
- );
911
+ this._usersManager.setMyself(connectionId, user);
1021
912
  const presence = this._usersManager.myPresence;
1022
913
  const myself = (_a = this._usersManager.myself) != null ? _a : null;
1023
914
  this._stateNotifier.subjects["my-presence"].next(presence);
@@ -1031,10 +922,8 @@ var PluvRoom = class extends AbstractRoom {
1031
922
  _handleStorageReceivedMessage(message) {
1032
923
  return __async(this, null, function* () {
1033
924
  const { connectionId } = message;
1034
- if (!connectionId)
1035
- return;
1036
- if (!this._state.webSocket)
1037
- throw new Error("Could not find WebSocket");
925
+ if (!connectionId) return;
926
+ if (!this._state.webSocket) throw new Error("Could not find WebSocket");
1038
927
  const data = message.data;
1039
928
  this._crdtManager.initialize({
1040
929
  onInitialized: (update2) => __async(this, null, function* () {
@@ -1058,13 +947,10 @@ var PluvRoom = class extends AbstractRoom {
1058
947
  }
1059
948
  _handleStorageUpdatedMessage(message) {
1060
949
  const { connectionId } = message;
1061
- if (!connectionId)
1062
- return;
1063
- if (!this._state.webSocket)
1064
- throw new Error("Could not find WebSocket");
950
+ if (!connectionId) return;
951
+ if (!this._state.webSocket) throw new Error("Could not find WebSocket");
1065
952
  const data = message.data;
1066
- if (!this._crdtManager)
1067
- return;
953
+ if (!this._crdtManager) return;
1068
954
  this._crdtManager.doc.applyEncodedState({
1069
955
  origin: ORIGIN_STORAGE_UPDATED,
1070
956
  update: data.state
@@ -1072,8 +958,7 @@ var PluvRoom = class extends AbstractRoom {
1072
958
  const sharedTypes = this._crdtManager.doc.get();
1073
959
  const storageRoot = Object.entries(sharedTypes).reduce(
1074
960
  (acc, [prop, sharedType]) => {
1075
- if (!this._crdtManager)
1076
- return acc;
961
+ if (!this._crdtManager) return acc;
1077
962
  const serialized = sharedType.toJson();
1078
963
  this._crdtNotifier.subject(prop).next(serialized);
1079
964
  return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
@@ -1084,22 +969,15 @@ var PluvRoom = class extends AbstractRoom {
1084
969
  }
1085
970
  _handleUserJoinedMessage(message) {
1086
971
  const { connectionId } = message;
1087
- if (!connectionId)
1088
- return;
1089
- if (!this._state.webSocket)
1090
- throw new Error("Could not find WebSocket");
1091
- if (!this._usersManager.myself)
1092
- return;
972
+ if (!connectionId) return;
973
+ if (!this._state.webSocket) throw new Error("Could not find WebSocket");
974
+ if (!this._usersManager.myself) return;
1093
975
  const data = message.data;
1094
976
  const myself = this._usersManager.myself;
1095
977
  if (myself.connectionId === connectionId) {
1096
978
  this._sendMessage({ type: "$GET_OTHERS", data: {} });
1097
979
  }
1098
- this._usersManager.setUser(
1099
- connectionId,
1100
- data.user,
1101
- data.presence
1102
- );
980
+ this._usersManager.setUser(connectionId, data.user, data.presence);
1103
981
  const other = this._usersManager.getOther(connectionId);
1104
982
  const others = this._usersManager.getOthers();
1105
983
  this._otherNotifier.subject(connectionId).next(other);
@@ -1107,23 +985,18 @@ var PluvRoom = class extends AbstractRoom {
1107
985
  }
1108
986
  _heartbeat() {
1109
987
  this._clearTimeout(this._timeouts.pong);
1110
- this._timeouts.pong = setTimeout(
1111
- this._reconnect.bind(this),
1112
- PONG_TIMEOUT_MS
1113
- );
988
+ this._timeouts.pong = setTimeout(this._reconnect.bind(this), PONG_TIMEOUT_MS);
1114
989
  this._sendMessage({ type: "$PING", data: {} });
1115
990
  }
1116
991
  _logDebug(...data) {
1117
992
  var _a;
1118
- if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production")
1119
- return;
993
+ if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production") return;
1120
994
  this._debug && console.log(...data);
1121
995
  }
1122
996
  _observeCrdt() {
1123
997
  var _a, _b;
1124
998
  (_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
1125
- if (!this._crdtManager)
1126
- return;
999
+ if (!this._crdtManager) return;
1127
1000
  const unsubscribe = this._crdtManager.doc.subscribe((event) => {
1128
1001
  var _a2;
1129
1002
  const origin = (_a2 = event.origin) != null ? _a2 : null;
@@ -1132,12 +1005,9 @@ var PluvRoom = class extends AbstractRoom {
1132
1005
  if (origin === ORIGIN_INITIALIZED || origin === ORIGIN_STORAGE_UPDATED) {
1133
1006
  return;
1134
1007
  }
1135
- if (!this._state.webSocket)
1136
- return;
1137
- if (!this._state.connection.id)
1138
- return;
1139
- if (this._state.webSocket.readyState !== WebSocket.OPEN)
1140
- return;
1008
+ if (!this._state.webSocket) return;
1009
+ if (!this._state.connection.id) return;
1010
+ if (this._state.webSocket.readyState !== WebSocket.OPEN) return;
1141
1011
  this._sendMessage({
1142
1012
  type: "$UPDATE_STORAGE",
1143
1013
  data: { origin, update: event.update }
@@ -1185,16 +1055,14 @@ var PluvRoom = class extends AbstractRoom {
1185
1055
  }
1186
1056
  _onMessage(event) {
1187
1057
  const message = this._parseMessage(event);
1188
- if (!message)
1189
- return;
1058
+ if (!message) return;
1190
1059
  const shouldLog = typeof this._debug === "boolean" ? this._debug : this._debug.output.find((value) => value === message.type);
1191
1060
  if (shouldLog) {
1192
1061
  this._logDebug("WebSocket event received: ", message.type, message);
1193
1062
  }
1194
1063
  this._eventNotifier.subject(message.type).next(message);
1195
1064
  const { connectionId } = message;
1196
- if (!connectionId)
1197
- return;
1065
+ if (!connectionId) return;
1198
1066
  switch (message.type) {
1199
1067
  case "$EXIT": {
1200
1068
  this._handleExit(message);
@@ -1232,18 +1100,14 @@ var PluvRoom = class extends AbstractRoom {
1232
1100
  }
1233
1101
  }
1234
1102
  _onOpen() {
1235
- if (this._state.connection.state !== "Connecting" /* Connecting */)
1236
- return;
1103
+ if (this._state.connection.state !== "Connecting" /* Connecting */) return;
1237
1104
  this._logDebug("WebSocket connected");
1238
1105
  this._updateState((oldState) => {
1239
1106
  oldState.connection.state = "Open" /* Open */;
1240
1107
  return oldState;
1241
1108
  });
1242
1109
  this._clearInterval(this._intervals.heartbeat);
1243
- this._intervals.heartbeat = setInterval(
1244
- this._heartbeat.bind(this),
1245
- HEARTBEAT_INTERVAL_MS
1246
- );
1110
+ this._intervals.heartbeat = setInterval(this._heartbeat.bind(this), HEARTBEAT_INTERVAL_MS);
1247
1111
  }
1248
1112
  _onNavigatorOnline() {
1249
1113
  return __async(this, null, function* () {
@@ -1255,10 +1119,8 @@ var PluvRoom = class extends AbstractRoom {
1255
1119
  }
1256
1120
  _onVisibilityChange() {
1257
1121
  return __async(this, null, function* () {
1258
- if (typeof document === "undefined")
1259
- return;
1260
- if (document.visibilityState !== "visible")
1261
- return;
1122
+ if (typeof document === "undefined") return;
1123
+ if (document.visibilityState !== "visible") return;
1262
1124
  switch (this._state.connection.state) {
1263
1125
  case "Open" /* Open */:
1264
1126
  this._clearInterval(this._intervals.heartbeat);
@@ -1272,16 +1134,12 @@ var PluvRoom = class extends AbstractRoom {
1272
1134
  });
1273
1135
  }
1274
1136
  _parseMessage(message) {
1275
- if (typeof message.data !== "string")
1276
- return null;
1137
+ if (typeof message.data !== "string") return null;
1277
1138
  try {
1278
1139
  const config = JSON.parse(message.data);
1279
- if (typeof config !== "object")
1280
- return null;
1281
- if (typeof config.type !== "string")
1282
- return null;
1283
- if (typeof config.data !== "object")
1284
- 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;
1285
1143
  return config;
1286
1144
  } catch (e) {
1287
1145
  return null;
@@ -1292,8 +1150,7 @@ var PluvRoom = class extends AbstractRoom {
1292
1150
  if (!this._endpoints.wsEndpoint) {
1293
1151
  throw new Error("Must provide an wsEndpoint.");
1294
1152
  }
1295
- if (!this._state.webSocket)
1296
- return;
1153
+ if (!this._state.webSocket) return;
1297
1154
  this._closeWs();
1298
1155
  this._updateState((oldState) => {
1299
1156
  oldState.authorization.token = null;
@@ -1306,8 +1163,7 @@ var PluvRoom = class extends AbstractRoom {
1306
1163
  }
1307
1164
  _sendMessage(data) {
1308
1165
  const webSocket = this._state.webSocket;
1309
- if ((webSocket == null ? void 0 : webSocket.readyState) !== WebSocket.OPEN)
1310
- return;
1166
+ if ((webSocket == null ? void 0 : webSocket.readyState) !== WebSocket.OPEN) return;
1311
1167
  const shouldLog = typeof this._debug === "boolean" ? this._debug : this._debug.input.find((value) => value === data.type);
1312
1168
  if (shouldLog) {
1313
1169
  this._logDebug("WebSocket event sent: ", data.type, data);
@@ -1317,9 +1173,7 @@ var PluvRoom = class extends AbstractRoom {
1317
1173
  _updateState(updater) {
1318
1174
  let authorization;
1319
1175
  try {
1320
- authorization = JSON.parse(
1321
- JSON.stringify(this._state.authorization)
1322
- );
1176
+ authorization = JSON.parse(JSON.stringify(this._state.authorization));
1323
1177
  } catch (e) {
1324
1178
  throw new Error("User is not JSON serializable");
1325
1179
  }
@@ -1342,8 +1196,7 @@ var PluvClient = class {
1342
1196
  this._rooms = /* @__PURE__ */ new Map();
1343
1197
  this.createRoom = (room, options) => {
1344
1198
  const oldRoom = this.getRoom(room);
1345
- if (oldRoom)
1346
- return oldRoom;
1199
+ if (oldRoom) return oldRoom;
1347
1200
  const newRoom = new PluvRoom(room, {
1348
1201
  addons: options.addons,
1349
1202
  authEndpoint: this._authEndpoint,
@@ -1376,8 +1229,7 @@ var PluvClient = class {
1376
1229
  };
1377
1230
  this.leave = (room) => __async(this, null, function* () {
1378
1231
  const toLeave = typeof room === "string" ? this.getRoom(room) : room;
1379
- if (!toLeave)
1380
- return;
1232
+ if (!toLeave) return;
1381
1233
  yield toLeave.disconnect();
1382
1234
  this._rooms.delete(toLeave.id);
1383
1235
  this._logDebug(`Left and deleted room: ${room}`);
@@ -1389,8 +1241,7 @@ var PluvClient = class {
1389
1241
  }
1390
1242
  _logDebug(...data) {
1391
1243
  var _a;
1392
- if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production")
1393
- return;
1244
+ if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production") return;
1394
1245
  this._debug && console.log(...data);
1395
1246
  }
1396
1247
  };