@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.js CHANGED
@@ -86,10 +86,7 @@ var import_crdt = require("@pluv/crdt");
86
86
  var CrdtManager = class {
87
87
  constructor(options) {
88
88
  this.initialized = false;
89
- const {
90
- encodedState,
91
- initialStorage = import_crdt.noop.doc({})
92
- } = options;
89
+ const { encodedState, initialStorage = import_crdt.noop.doc({}) } = options;
93
90
  this._docFactory = initialStorage;
94
91
  this.doc = encodedState ? initialStorage.getFresh().applyEncodedState({ update: encodedState }) : initialStorage.getFresh();
95
92
  }
@@ -100,8 +97,7 @@ var CrdtManager = class {
100
97
  * @date June 13, 2023
101
98
  */
102
99
  applyUpdate(update, origin) {
103
- if (!this.initialized)
104
- return this;
100
+ if (!this.initialized) return this;
105
101
  const updates = typeof update === "string" ? [update] : update;
106
102
  this._applyDocUpdates(this.doc, updates, origin);
107
103
  return this;
@@ -115,24 +111,15 @@ var CrdtManager = class {
115
111
  initialize(params) {
116
112
  const { onInitialized, origin, update } = params;
117
113
  const updates = typeof update === "string" ? [update] : update;
118
- if (!updates.length)
119
- return this;
114
+ if (!updates.length) return this;
120
115
  if (this.initialized) {
121
116
  this.doc = this._applyDocUpdates(this.doc, updates, origin).track();
122
117
  return this;
123
118
  }
124
- const onEmpty = this._applyDocUpdates(
125
- this._docFactory.getEmpty(),
126
- updates,
127
- origin
128
- );
119
+ const onEmpty = this._applyDocUpdates(this._docFactory.getEmpty(), updates, origin);
129
120
  if (!onEmpty.isEmpty()) {
130
121
  onEmpty.destroy();
131
- this.doc = this._applyDocUpdates(
132
- this._docFactory.getFresh(),
133
- updates,
134
- origin
135
- ).track();
122
+ this.doc = this._applyDocUpdates(this._docFactory.getFresh(), updates, origin).track();
136
123
  this.initialized = true;
137
124
  onInitialized == null ? void 0 : onInitialized(this.doc.getEncodedState());
138
125
  return this;
@@ -145,9 +132,7 @@ var CrdtManager = class {
145
132
  }
146
133
  _applyDocUpdates(doc, updates, origin) {
147
134
  doc.transact(() => {
148
- doc.batchApplyEncodedState(
149
- updates.map((update) => ({ origin, update }))
150
- );
135
+ doc.batchApplyEncodedState(updates.map((update) => ({ origin, update })));
151
136
  }, origin);
152
137
  return doc;
153
138
  }
@@ -186,8 +171,7 @@ var EventNotifier = class {
186
171
  }
187
172
  subject(name) {
188
173
  const subject = this.subjects.get(name);
189
- if (subject)
190
- return subject;
174
+ if (subject) return subject;
191
175
  const newSubject = (0, import_wonka2.makeSubject)();
192
176
  this.subjects.set(name, newSubject);
193
177
  return newSubject;
@@ -206,8 +190,7 @@ var OtherNotifier = class {
206
190
  }
207
191
  subject(name) {
208
192
  const subject = this.subjects.get(name);
209
- if (subject)
210
- return subject;
193
+ if (subject) return subject;
211
194
  const newSubject = (0, import_wonka3.makeSubject)();
212
195
  this.subjects.set(name, newSubject);
213
196
  return newSubject;
@@ -233,8 +216,7 @@ var StateNotifier = class {
233
216
  subscribe(name, callback) {
234
217
  var _a;
235
218
  const source = (_a = this.subjects[name]) == null ? void 0 : _a.source;
236
- if (!source)
237
- throw new Error(`Could not subscribe to source: ${name}`);
219
+ if (!source) throw new Error(`Could not subscribe to source: ${name}`);
238
220
  return (0, import_wonka4.subscribe)(callback)(source).unsubscribe;
239
221
  }
240
222
  };
@@ -278,8 +260,7 @@ var UsersManager = class {
278
260
  return;
279
261
  }
280
262
  const other = this._others.get(connectionId);
281
- if (!other)
282
- return;
263
+ if (!other) return;
283
264
  const presence = __spreadValues(__spreadValues({}, other.presence), patch);
284
265
  (_b = this._presence) == null ? void 0 : _b.parse(presence);
285
266
  this._others.set(connectionId, __spreadProps(__spreadValues({}, other), { presence }));
@@ -306,17 +287,14 @@ var UsersManager = class {
306
287
  return;
307
288
  }
308
289
  const other = this._others.get(connectionId);
309
- if (!other)
310
- return;
290
+ if (!other) return;
311
291
  this._others.set(connectionId, __spreadProps(__spreadValues({}, other), { presence }));
312
292
  }
313
293
  setUser(connectionId, user, presence) {
314
294
  var _a;
315
- if (((_a = this._myself) == null ? void 0 : _a.connectionId) === connectionId)
316
- return;
295
+ if (((_a = this._myself) == null ? void 0 : _a.connectionId) === connectionId) return;
317
296
  const other = this._others.get(connectionId);
318
- if (other)
319
- return;
297
+ if (other) return;
320
298
  this._others.set(connectionId, {
321
299
  connectionId,
322
300
  presence: presence != null ? presence : this._initialPresence,
@@ -328,8 +306,7 @@ var UsersManager = class {
328
306
  */
329
307
  updateMyPresence(presence) {
330
308
  this._myPresence = __spreadValues(__spreadValues({}, this._myPresence), presence);
331
- if (!this._myself)
332
- return;
309
+ if (!this._myself) return;
333
310
  this._myself.presence = this._myPresence;
334
311
  }
335
312
  };
@@ -377,6 +354,9 @@ var MockedRoom = class extends AbstractRoom {
377
354
  this.event = (event, callback) => {
378
355
  return this._eventNotifier.subscribe(event, callback);
379
356
  };
357
+ this.getConnection = () => {
358
+ return Object.freeze(JSON.parse(JSON.stringify(this._state.connection)));
359
+ };
380
360
  this.getMyPresence = () => {
381
361
  return this._usersManager.myPresence;
382
362
  };
@@ -391,8 +371,7 @@ var MockedRoom = class extends AbstractRoom {
391
371
  };
392
372
  this.getStorage = (key) => {
393
373
  const sharedType = this._crdtManager.get(key);
394
- if (typeof sharedType === "undefined")
395
- return null;
374
+ if (typeof sharedType === "undefined") return null;
396
375
  return sharedType.value;
397
376
  };
398
377
  this.other = (connectionId, callback) => {
@@ -413,10 +392,8 @@ var MockedRoom = class extends AbstractRoom {
413
392
  this.transact = (fn, origin) => {
414
393
  const _origin = origin != null ? origin : this._state.connection.id;
415
394
  const crdtManager = this._crdtManager;
416
- if (typeof _origin !== "string")
417
- return;
418
- if (!crdtManager)
419
- return;
395
+ if (typeof _origin !== "string") return;
396
+ if (!crdtManager) return;
420
397
  crdtManager.doc.transact(() => {
421
398
  const storage = crdtManager.doc.get();
422
399
  fn(storage);
@@ -427,7 +404,8 @@ var MockedRoom = class extends AbstractRoom {
427
404
  };
428
405
  this.updateMyPresence = (presence) => {
429
406
  var _a;
430
- this._usersManager.updateMyPresence(presence);
407
+ const newPresence = typeof presence === "function" ? presence(this.getMyPresence()) : presence;
408
+ this._usersManager.updateMyPresence(newPresence);
431
409
  const myPresence = this._usersManager.myPresence;
432
410
  const myself = (_a = this._usersManager.myself) != null ? _a : null;
433
411
  this._stateNotifier.subjects["my-presence"].next(myPresence);
@@ -447,45 +425,33 @@ var MockedRoom = class extends AbstractRoom {
447
425
  return true;
448
426
  }
449
427
  broadcast(event, data) {
450
- if (!this._events)
451
- return;
428
+ if (!this._events) return;
452
429
  const resolver = this._events[event];
453
- if (!resolver)
454
- return;
430
+ if (!resolver) return;
455
431
  const result = resolver(data);
456
432
  Object.keys(result).forEach((type) => {
457
433
  const _type = type.toString();
458
434
  const data2 = result[_type];
459
- if (!data2)
460
- return;
435
+ if (!data2) return;
461
436
  this._eventNotifier.subject(_type).next(data2);
462
437
  });
463
438
  }
464
- getConnection() {
465
- return Object.freeze(
466
- JSON.parse(JSON.stringify(this._state.connection))
467
- );
468
- }
469
439
  getDoc() {
470
440
  return this._crdtManager.doc;
471
441
  }
472
442
  _observeCrdt() {
473
443
  var _a, _b;
474
444
  (_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
475
- if (!this._crdtManager)
476
- return;
445
+ if (!this._crdtManager) return;
477
446
  const unsubscribe = this._crdtManager.doc.subscribe((event) => {
478
447
  var _a2;
479
448
  const origin = (_a2 = event.origin) != null ? _a2 : null;
480
- if (!this._crdtManager)
481
- return;
482
- if (origin === "$STORAGE_UPDATED")
483
- return;
449
+ if (!this._crdtManager) return;
450
+ if (origin === "$STORAGE_UPDATED") return;
484
451
  const sharedTypes = this._crdtManager.doc.get();
485
452
  const storageRoot = Object.entries(sharedTypes).reduce(
486
453
  (acc, [prop, sharedType]) => {
487
- if (!this._crdtManager)
488
- return acc;
454
+ if (!this._crdtManager) return acc;
489
455
  const serialized = sharedType.toJson();
490
456
  this._crdtNotifier.subject(prop).next(serialized);
491
457
  return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
@@ -529,12 +495,10 @@ var debounce = (fn, options) => {
529
495
  let prevResult;
530
496
  let timeout = null;
531
497
  return (...args) => {
532
- if (typeof timeout === "number")
533
- clearTimeout(timeout);
498
+ if (typeof timeout === "number") clearTimeout(timeout);
534
499
  timeout = setTimeout(() => {
535
500
  timeout = null;
536
- if (!immediate)
537
- prevResult = fn(args);
501
+ if (!immediate) prevResult = fn(args);
538
502
  }, wait);
539
503
  if (immediate && !timeout) {
540
504
  fn(args);
@@ -595,14 +559,40 @@ var PluvRoom = class extends AbstractRoom {
595
559
  };
596
560
  this._windowListeners = null;
597
561
  this._wsListeners = null;
562
+ this.broadcast = new Proxy(
563
+ (event, data) => {
564
+ if (!this._state.webSocket) return;
565
+ if (this._state.connection.state !== "Open" /* Open */) return;
566
+ const type = event.toString();
567
+ this._sendMessage({ data, type });
568
+ },
569
+ {
570
+ get(fn, prop) {
571
+ return (data) => {
572
+ return fn(prop, data);
573
+ };
574
+ }
575
+ }
576
+ );
598
577
  this.canRedo = () => {
599
578
  return !!this._crdtManager.doc.canRedo();
600
579
  };
601
580
  this.canUndo = () => {
602
581
  return !!this._crdtManager.doc.canUndo();
603
582
  };
604
- this.event = (event, callback) => {
605
- return this._eventNotifier.subscribe(event, callback);
583
+ this.event = new Proxy(
584
+ (event, callback) => this._eventNotifier.subscribe(event, callback),
585
+ {
586
+ get(fn, prop) {
587
+ return (callback) => fn(prop, callback);
588
+ }
589
+ }
590
+ );
591
+ this.getConnection = () => {
592
+ return Object.freeze(JSON.parse(JSON.stringify(this._state.connection)));
593
+ };
594
+ this.getDoc = () => {
595
+ return this._crdtManager.doc;
606
596
  };
607
597
  this.getMyPresence = () => {
608
598
  return this._usersManager.myPresence;
@@ -618,8 +608,7 @@ var PluvRoom = class extends AbstractRoom {
618
608
  };
619
609
  this.getStorage = (key) => {
620
610
  const sharedType = this._crdtManager.get(key);
621
- if (typeof sharedType === "undefined")
622
- return null;
611
+ if (typeof sharedType === "undefined") return null;
623
612
  return sharedType;
624
613
  };
625
614
  this.other = (connectionId, callback) => {
@@ -639,8 +628,7 @@ var PluvRoom = class extends AbstractRoom {
639
628
  };
640
629
  this.transact = (fn, origin) => {
641
630
  const _origin = origin != null ? origin : this._state.connection.id;
642
- if (typeof _origin !== "string")
643
- return;
631
+ if (typeof _origin !== "string") return;
644
632
  this._crdtManager.doc.transact(() => {
645
633
  const storage = this._crdtManager.doc.get();
646
634
  fn(storage);
@@ -651,21 +639,18 @@ var PluvRoom = class extends AbstractRoom {
651
639
  };
652
640
  this.updateMyPresence = (presence) => {
653
641
  var _a;
654
- this._usersManager.updateMyPresence(presence);
642
+ const newPresence = typeof presence === "function" ? presence(this.getMyPresence()) : presence;
643
+ this._usersManager.updateMyPresence(newPresence);
655
644
  const myPresence = this._usersManager.myPresence;
656
645
  const myself = (_a = this._usersManager.myself) != null ? _a : null;
657
646
  this._stateNotifier.subjects["my-presence"].next(myPresence);
658
647
  !!myself && this._stateNotifier.subjects["myself"].next(myself);
659
- this.broadcast(
660
- "$UPDATE_PRESENCE",
661
- { presence }
662
- );
648
+ this.broadcast("$UPDATE_PRESENCE", { presence: newPresence });
663
649
  };
664
650
  this._flattenStorageStore = debounce(
665
651
  () => __async(this, null, function* () {
666
652
  const shouldFlatten = yield this._storageStore.getShouldFlatten();
667
- if (!shouldFlatten)
668
- return;
653
+ if (!shouldFlatten) return;
669
654
  const updates = yield this._storageStore.getUpdates();
670
655
  this._crdtManager.applyUpdate(updates, this);
671
656
  const encodedState = this._crdtManager.doc.getEncodedState();
@@ -687,10 +672,7 @@ var PluvRoom = class extends AbstractRoom {
687
672
  this._listeners = {
688
673
  onAuthorizationFail: (error) => {
689
674
  this._clearTimeout(this._timeouts.reconnect);
690
- this._timeouts.reconnect = setTimeout(
691
- this._reconnect.bind(this),
692
- RECONNECT_TIMEOUT_MS
693
- );
675
+ this._timeouts.reconnect = setTimeout(this._reconnect.bind(this), RECONNECT_TIMEOUT_MS);
694
676
  onAuthorizationFail == null ? void 0 : onAuthorizationFail(error);
695
677
  }
696
678
  };
@@ -708,25 +690,15 @@ var PluvRoom = class extends AbstractRoom {
708
690
  get webSocket() {
709
691
  return this._state.webSocket;
710
692
  }
711
- broadcast(event, data) {
712
- if (!this._state.webSocket)
713
- return;
714
- if (this._state.connection.state !== "Open" /* Open */)
715
- return;
716
- const type = event.toString();
717
- this._sendMessage({ data, type });
718
- }
719
693
  connect() {
720
694
  return __async(this, null, function* () {
721
695
  if (!this._endpoints.wsEndpoint) {
722
696
  throw new Error("Must provide an wsEndpoint.");
723
697
  }
724
- const canConnect = [
725
- "Closed" /* Closed */,
726
- "Untouched" /* Untouched */
727
- ].some((state) => this._state.connection.state === state);
728
- if (!canConnect)
729
- return;
698
+ const canConnect = ["Closed" /* Closed */, "Untouched" /* Untouched */].some(
699
+ (state) => this._state.connection.state === state
700
+ );
701
+ if (!canConnect) return;
730
702
  this._clearTimeout(this._timeouts.reconnect);
731
703
  this._updateState((oldState) => {
732
704
  oldState.connection.state = "Connecting" /* Connecting */;
@@ -760,14 +732,11 @@ var PluvRoom = class extends AbstractRoom {
760
732
  if (!this._endpoints.wsEndpoint) {
761
733
  throw new Error("Must provide an wsEndpoint.");
762
734
  }
763
- if (!this._state.webSocket)
764
- return;
765
- const canDisconnect = [
766
- "Connecting" /* Connecting */,
767
- "Open" /* Open */
768
- ].some((state) => this._state.connection.state === state);
769
- if (!canDisconnect)
770
- return;
735
+ if (!this._state.webSocket) return;
736
+ const canDisconnect = ["Connecting" /* Connecting */, "Open" /* Open */].some(
737
+ (state) => this._state.connection.state === state
738
+ );
739
+ if (!canDisconnect) return;
771
740
  this._clearInterval(this._intervals.heartbeat);
772
741
  this._closeWs();
773
742
  yield this._storageStore.destroy();
@@ -775,14 +744,6 @@ var PluvRoom = class extends AbstractRoom {
775
744
  this._stateNotifier.subjects["my-presence"].next(null);
776
745
  });
777
746
  }
778
- getConnection() {
779
- return Object.freeze(
780
- JSON.parse(JSON.stringify(this._state.connection))
781
- );
782
- }
783
- getDoc() {
784
- return this._crdtManager.doc;
785
- }
786
747
  _addToStorageStore(update) {
787
748
  return __async(this, null, function* () {
788
749
  yield this._storageStore.addUpdate(update);
@@ -804,63 +765,39 @@ var PluvRoom = class extends AbstractRoom {
804
765
  });
805
766
  }
806
767
  _attachWindowListeners() {
807
- if (typeof window === "undefined")
808
- return;
809
- if (typeof document === "undefined")
810
- return;
768
+ if (typeof window === "undefined") return;
769
+ if (typeof document === "undefined") return;
811
770
  this._windowListeners = {
812
771
  onNavigatorOnline: this._onNavigatorOnline.bind(this),
813
772
  onVisibilityChange: this._onVisibilityChange.bind(this)
814
773
  };
815
- window.addEventListener(
816
- "online",
817
- this._windowListeners.onNavigatorOnline
818
- );
819
- document.addEventListener(
820
- "visibilitychange",
821
- this._windowListeners.onVisibilityChange
822
- );
774
+ window.addEventListener("online", this._windowListeners.onNavigatorOnline);
775
+ document.addEventListener("visibilitychange", this._windowListeners.onVisibilityChange);
823
776
  }
824
777
  _attachWsListeners() {
825
- if (!this._state.webSocket)
826
- return;
778
+ if (!this._state.webSocket) return;
827
779
  this._wsListeners = {
828
780
  onClose: this._onClose.bind(this),
829
781
  onError: this._onError.bind(this),
830
782
  onMessage: this._onMessage.bind(this),
831
783
  onOpen: this._onOpen.bind(this)
832
784
  };
833
- this._state.webSocket.addEventListener(
834
- "close",
835
- this._wsListeners.onClose
836
- );
837
- this._state.webSocket.addEventListener(
838
- "error",
839
- this._wsListeners.onError
840
- );
841
- this._state.webSocket.addEventListener(
842
- "message",
843
- this._wsListeners.onMessage
844
- );
845
- this._state.webSocket.addEventListener(
846
- "open",
847
- this._wsListeners.onOpen
848
- );
785
+ this._state.webSocket.addEventListener("close", this._wsListeners.onClose);
786
+ this._state.webSocket.addEventListener("error", this._wsListeners.onError);
787
+ this._state.webSocket.addEventListener("message", this._wsListeners.onMessage);
788
+ this._state.webSocket.addEventListener("open", this._wsListeners.onOpen);
849
789
  }
850
790
  _clearInterval(interval) {
851
- if (Number.isNaN(interval))
852
- return;
791
+ if (Number.isNaN(interval)) return;
853
792
  clearInterval(interval);
854
793
  }
855
794
  _clearTimeout(timeout) {
856
- if (Number.isNaN(timeout))
857
- return;
795
+ if (Number.isNaN(timeout)) return;
858
796
  clearTimeout(timeout);
859
797
  }
860
798
  _closeWs() {
861
799
  var _a, _b;
862
- if (!this._state.webSocket)
863
- return;
800
+ if (!this._state.webSocket) return;
864
801
  (_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
865
802
  this._clearTimeout(this._timeouts.pong);
866
803
  this._clearTimeout(this._timeouts.reconnect);
@@ -872,46 +809,23 @@ var PluvRoom = class extends AbstractRoom {
872
809
  return readyState === ((_a2 = this._state.webSocket) == null ? void 0 : _a2.readyState);
873
810
  }
874
811
  );
875
- if (canClose)
876
- this._state.webSocket.close();
812
+ if (canClose) this._state.webSocket.close();
877
813
  this._detachWsListeners();
878
814
  }
879
815
  _detachWindowListeners() {
880
- if (!this._windowListeners)
881
- return;
882
- if (typeof window === "undefined")
883
- return;
884
- if (typeof document === "undefined")
885
- return;
886
- window.removeEventListener(
887
- "online",
888
- this._windowListeners.onNavigatorOnline
889
- );
890
- document.removeEventListener(
891
- "visibilitychange",
892
- this._windowListeners.onVisibilityChange
893
- );
816
+ if (!this._windowListeners) return;
817
+ if (typeof window === "undefined") return;
818
+ if (typeof document === "undefined") return;
819
+ window.removeEventListener("online", this._windowListeners.onNavigatorOnline);
820
+ document.removeEventListener("visibilitychange", this._windowListeners.onVisibilityChange);
894
821
  this._windowListeners = null;
895
822
  }
896
823
  _detachWsListeners() {
897
- if (!this._state.webSocket || !this._wsListeners)
898
- return;
899
- this._state.webSocket.removeEventListener(
900
- "close",
901
- this._wsListeners.onClose
902
- );
903
- this._state.webSocket.removeEventListener(
904
- "error",
905
- this._wsListeners.onError
906
- );
907
- this._state.webSocket.removeEventListener(
908
- "message",
909
- this._wsListeners.onMessage
910
- );
911
- this._state.webSocket.removeEventListener(
912
- "open",
913
- this._wsListeners.onOpen
914
- );
824
+ if (!this._state.webSocket || !this._wsListeners) return;
825
+ this._state.webSocket.removeEventListener("close", this._wsListeners.onClose);
826
+ this._state.webSocket.removeEventListener("error", this._wsListeners.onError);
827
+ this._state.webSocket.removeEventListener("message", this._wsListeners.onMessage);
828
+ this._state.webSocket.removeEventListener("open", this._wsListeners.onOpen);
915
829
  this._wsListeners = null;
916
830
  }
917
831
  _emitSharedTypes() {
@@ -927,8 +841,7 @@ var PluvRoom = class extends AbstractRoom {
927
841
  this._crdtNotifier.rootSubject.next(storageRoot);
928
842
  }
929
843
  _getAuthFetchOptions(room) {
930
- if (typeof this._endpoints.authEndpoint === "undefined")
931
- return null;
844
+ if (typeof this._endpoints.authEndpoint === "undefined") return null;
932
845
  if (this._endpoints.authEndpoint === true) {
933
846
  return {
934
847
  url: `/api/pluv/authorize?room=${room}`,
@@ -947,8 +860,7 @@ var PluvRoom = class extends AbstractRoom {
947
860
  _getAuthorization(room) {
948
861
  return __async(this, null, function* () {
949
862
  const fetchOptions = this._getAuthFetchOptions(room);
950
- if (!fetchOptions)
951
- return null;
863
+ if (!fetchOptions) return null;
952
864
  const { url, options } = fetchOptions;
953
865
  const res = yield fetch(url, options);
954
866
  if (!res.ok || res.status !== 200) {
@@ -957,9 +869,7 @@ var PluvRoom = class extends AbstractRoom {
957
869
  try {
958
870
  return yield res.text().then((text) => text.trim());
959
871
  } catch (err) {
960
- throw new Error(
961
- err instanceof Error ? err.message : "Room is unauthorized"
962
- );
872
+ throw new Error(err instanceof Error ? err.message : "Room is unauthorized");
963
873
  }
964
874
  });
965
875
  }
@@ -975,10 +885,8 @@ var PluvRoom = class extends AbstractRoom {
975
885
  }
976
886
  _handleExit(message) {
977
887
  const { connectionId } = message;
978
- if (!connectionId)
979
- return;
980
- if (!this._state.webSocket)
981
- throw new Error("Could not find WebSocket");
888
+ if (!connectionId) return;
889
+ if (!this._state.webSocket) throw new Error("Could not find WebSocket");
982
890
  this._usersManager.removeUser(connectionId);
983
891
  const others = this._usersManager.getOthers();
984
892
  this._stateNotifier.subjects.others.next(others);
@@ -988,15 +896,10 @@ var PluvRoom = class extends AbstractRoom {
988
896
  _handlePresenceUpdatedMessage(message) {
989
897
  var _a;
990
898
  const { connectionId } = message;
991
- if (!connectionId)
992
- return;
993
- if (!this._state.webSocket)
994
- throw new Error("Could not find WebSocket");
899
+ if (!connectionId) return;
900
+ if (!this._state.webSocket) throw new Error("Could not find WebSocket");
995
901
  const data = message.data;
996
- this._usersManager.patchPresence(
997
- connectionId,
998
- data.presence
999
- );
902
+ this._usersManager.patchPresence(connectionId, data.presence);
1000
903
  const myPresence = this._usersManager.myPresence;
1001
904
  const myself = (_a = this._usersManager.myself) != null ? _a : null;
1002
905
  if ((myself == null ? void 0 : myself.connectionId) === connectionId) {
@@ -1010,21 +913,14 @@ var PluvRoom = class extends AbstractRoom {
1010
913
  this._stateNotifier.subjects["others"].next(others);
1011
914
  }
1012
915
  _handleReceiveOthers(message) {
1013
- if (!message.connectionId)
1014
- return;
1015
- if (!this._state.webSocket)
1016
- throw new Error("Could not find WebSocket");
916
+ if (!message.connectionId) return;
917
+ if (!this._state.webSocket) throw new Error("Could not find WebSocket");
1017
918
  const data = message.data;
1018
919
  Object.keys(data.others).forEach((connectionId) => {
1019
920
  const { presence, user } = data.others[connectionId];
1020
- if (!user)
1021
- return;
1022
921
  this._usersManager.setUser(connectionId, user);
1023
922
  const other = this._usersManager.getOther(connectionId);
1024
- !!presence && this._usersManager.patchPresence(
1025
- connectionId,
1026
- presence
1027
- );
923
+ !!presence && this._usersManager.patchPresence(connectionId, presence);
1028
924
  this._otherNotifier.subject(connectionId).next(other);
1029
925
  });
1030
926
  const others = this._usersManager.getOthers();
@@ -1033,20 +929,15 @@ var PluvRoom = class extends AbstractRoom {
1033
929
  _handleRegisteredMessage(message) {
1034
930
  var _a, _b;
1035
931
  const { connectionId, user } = message;
1036
- if (!connectionId)
1037
- return;
1038
- if (!this._state.webSocket)
1039
- throw new Error("Could not find WebSocket");
932
+ if (!connectionId) return;
933
+ if (!this._state.webSocket) throw new Error("Could not find WebSocket");
1040
934
  this._updateState((oldState) => {
1041
935
  oldState.connection.count += 1;
1042
936
  oldState.connection.id = connectionId;
1043
937
  oldState.authorization.user = user;
1044
938
  return oldState;
1045
939
  });
1046
- this._usersManager.setMyself(
1047
- connectionId,
1048
- user
1049
- );
940
+ this._usersManager.setMyself(connectionId, user);
1050
941
  const presence = this._usersManager.myPresence;
1051
942
  const myself = (_a = this._usersManager.myself) != null ? _a : null;
1052
943
  this._stateNotifier.subjects["my-presence"].next(presence);
@@ -1060,10 +951,8 @@ var PluvRoom = class extends AbstractRoom {
1060
951
  _handleStorageReceivedMessage(message) {
1061
952
  return __async(this, null, function* () {
1062
953
  const { connectionId } = message;
1063
- if (!connectionId)
1064
- return;
1065
- if (!this._state.webSocket)
1066
- throw new Error("Could not find WebSocket");
954
+ if (!connectionId) return;
955
+ if (!this._state.webSocket) throw new Error("Could not find WebSocket");
1067
956
  const data = message.data;
1068
957
  this._crdtManager.initialize({
1069
958
  onInitialized: (update2) => __async(this, null, function* () {
@@ -1087,13 +976,10 @@ var PluvRoom = class extends AbstractRoom {
1087
976
  }
1088
977
  _handleStorageUpdatedMessage(message) {
1089
978
  const { connectionId } = message;
1090
- if (!connectionId)
1091
- return;
1092
- if (!this._state.webSocket)
1093
- throw new Error("Could not find WebSocket");
979
+ if (!connectionId) return;
980
+ if (!this._state.webSocket) throw new Error("Could not find WebSocket");
1094
981
  const data = message.data;
1095
- if (!this._crdtManager)
1096
- return;
982
+ if (!this._crdtManager) return;
1097
983
  this._crdtManager.doc.applyEncodedState({
1098
984
  origin: ORIGIN_STORAGE_UPDATED,
1099
985
  update: data.state
@@ -1101,8 +987,7 @@ var PluvRoom = class extends AbstractRoom {
1101
987
  const sharedTypes = this._crdtManager.doc.get();
1102
988
  const storageRoot = Object.entries(sharedTypes).reduce(
1103
989
  (acc, [prop, sharedType]) => {
1104
- if (!this._crdtManager)
1105
- return acc;
990
+ if (!this._crdtManager) return acc;
1106
991
  const serialized = sharedType.toJson();
1107
992
  this._crdtNotifier.subject(prop).next(serialized);
1108
993
  return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
@@ -1113,22 +998,15 @@ var PluvRoom = class extends AbstractRoom {
1113
998
  }
1114
999
  _handleUserJoinedMessage(message) {
1115
1000
  const { connectionId } = message;
1116
- if (!connectionId)
1117
- return;
1118
- if (!this._state.webSocket)
1119
- throw new Error("Could not find WebSocket");
1120
- if (!this._usersManager.myself)
1121
- return;
1001
+ if (!connectionId) return;
1002
+ if (!this._state.webSocket) throw new Error("Could not find WebSocket");
1003
+ if (!this._usersManager.myself) return;
1122
1004
  const data = message.data;
1123
1005
  const myself = this._usersManager.myself;
1124
1006
  if (myself.connectionId === connectionId) {
1125
1007
  this._sendMessage({ type: "$GET_OTHERS", data: {} });
1126
1008
  }
1127
- this._usersManager.setUser(
1128
- connectionId,
1129
- data.user,
1130
- data.presence
1131
- );
1009
+ this._usersManager.setUser(connectionId, data.user, data.presence);
1132
1010
  const other = this._usersManager.getOther(connectionId);
1133
1011
  const others = this._usersManager.getOthers();
1134
1012
  this._otherNotifier.subject(connectionId).next(other);
@@ -1136,23 +1014,18 @@ var PluvRoom = class extends AbstractRoom {
1136
1014
  }
1137
1015
  _heartbeat() {
1138
1016
  this._clearTimeout(this._timeouts.pong);
1139
- this._timeouts.pong = setTimeout(
1140
- this._reconnect.bind(this),
1141
- PONG_TIMEOUT_MS
1142
- );
1017
+ this._timeouts.pong = setTimeout(this._reconnect.bind(this), PONG_TIMEOUT_MS);
1143
1018
  this._sendMessage({ type: "$PING", data: {} });
1144
1019
  }
1145
1020
  _logDebug(...data) {
1146
1021
  var _a;
1147
- if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production")
1148
- return;
1022
+ if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production") return;
1149
1023
  this._debug && console.log(...data);
1150
1024
  }
1151
1025
  _observeCrdt() {
1152
1026
  var _a, _b;
1153
1027
  (_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
1154
- if (!this._crdtManager)
1155
- return;
1028
+ if (!this._crdtManager) return;
1156
1029
  const unsubscribe = this._crdtManager.doc.subscribe((event) => {
1157
1030
  var _a2;
1158
1031
  const origin = (_a2 = event.origin) != null ? _a2 : null;
@@ -1161,12 +1034,9 @@ var PluvRoom = class extends AbstractRoom {
1161
1034
  if (origin === ORIGIN_INITIALIZED || origin === ORIGIN_STORAGE_UPDATED) {
1162
1035
  return;
1163
1036
  }
1164
- if (!this._state.webSocket)
1165
- return;
1166
- if (!this._state.connection.id)
1167
- return;
1168
- if (this._state.webSocket.readyState !== WebSocket.OPEN)
1169
- return;
1037
+ if (!this._state.webSocket) return;
1038
+ if (!this._state.connection.id) return;
1039
+ if (this._state.webSocket.readyState !== WebSocket.OPEN) return;
1170
1040
  this._sendMessage({
1171
1041
  type: "$UPDATE_STORAGE",
1172
1042
  data: { origin, update: event.update }
@@ -1214,16 +1084,14 @@ var PluvRoom = class extends AbstractRoom {
1214
1084
  }
1215
1085
  _onMessage(event) {
1216
1086
  const message = this._parseMessage(event);
1217
- if (!message)
1218
- return;
1087
+ if (!message) return;
1219
1088
  const shouldLog = typeof this._debug === "boolean" ? this._debug : this._debug.output.find((value) => value === message.type);
1220
1089
  if (shouldLog) {
1221
1090
  this._logDebug("WebSocket event received: ", message.type, message);
1222
1091
  }
1223
1092
  this._eventNotifier.subject(message.type).next(message);
1224
1093
  const { connectionId } = message;
1225
- if (!connectionId)
1226
- return;
1094
+ if (!connectionId) return;
1227
1095
  switch (message.type) {
1228
1096
  case "$EXIT": {
1229
1097
  this._handleExit(message);
@@ -1261,18 +1129,14 @@ var PluvRoom = class extends AbstractRoom {
1261
1129
  }
1262
1130
  }
1263
1131
  _onOpen() {
1264
- if (this._state.connection.state !== "Connecting" /* Connecting */)
1265
- return;
1132
+ if (this._state.connection.state !== "Connecting" /* Connecting */) return;
1266
1133
  this._logDebug("WebSocket connected");
1267
1134
  this._updateState((oldState) => {
1268
1135
  oldState.connection.state = "Open" /* Open */;
1269
1136
  return oldState;
1270
1137
  });
1271
1138
  this._clearInterval(this._intervals.heartbeat);
1272
- this._intervals.heartbeat = setInterval(
1273
- this._heartbeat.bind(this),
1274
- HEARTBEAT_INTERVAL_MS
1275
- );
1139
+ this._intervals.heartbeat = setInterval(this._heartbeat.bind(this), HEARTBEAT_INTERVAL_MS);
1276
1140
  }
1277
1141
  _onNavigatorOnline() {
1278
1142
  return __async(this, null, function* () {
@@ -1284,10 +1148,8 @@ var PluvRoom = class extends AbstractRoom {
1284
1148
  }
1285
1149
  _onVisibilityChange() {
1286
1150
  return __async(this, null, function* () {
1287
- if (typeof document === "undefined")
1288
- return;
1289
- if (document.visibilityState !== "visible")
1290
- return;
1151
+ if (typeof document === "undefined") return;
1152
+ if (document.visibilityState !== "visible") return;
1291
1153
  switch (this._state.connection.state) {
1292
1154
  case "Open" /* Open */:
1293
1155
  this._clearInterval(this._intervals.heartbeat);
@@ -1301,16 +1163,12 @@ var PluvRoom = class extends AbstractRoom {
1301
1163
  });
1302
1164
  }
1303
1165
  _parseMessage(message) {
1304
- if (typeof message.data !== "string")
1305
- return null;
1166
+ if (typeof message.data !== "string") return null;
1306
1167
  try {
1307
1168
  const config = JSON.parse(message.data);
1308
- if (typeof config !== "object")
1309
- return null;
1310
- if (typeof config.type !== "string")
1311
- return null;
1312
- if (typeof config.data !== "object")
1313
- return null;
1169
+ if (typeof config !== "object") return null;
1170
+ if (typeof config.type !== "string") return null;
1171
+ if (typeof config.data !== "object") return null;
1314
1172
  return config;
1315
1173
  } catch (e) {
1316
1174
  return null;
@@ -1321,8 +1179,7 @@ var PluvRoom = class extends AbstractRoom {
1321
1179
  if (!this._endpoints.wsEndpoint) {
1322
1180
  throw new Error("Must provide an wsEndpoint.");
1323
1181
  }
1324
- if (!this._state.webSocket)
1325
- return;
1182
+ if (!this._state.webSocket) return;
1326
1183
  this._closeWs();
1327
1184
  this._updateState((oldState) => {
1328
1185
  oldState.authorization.token = null;
@@ -1335,8 +1192,7 @@ var PluvRoom = class extends AbstractRoom {
1335
1192
  }
1336
1193
  _sendMessage(data) {
1337
1194
  const webSocket = this._state.webSocket;
1338
- if ((webSocket == null ? void 0 : webSocket.readyState) !== WebSocket.OPEN)
1339
- return;
1195
+ if ((webSocket == null ? void 0 : webSocket.readyState) !== WebSocket.OPEN) return;
1340
1196
  const shouldLog = typeof this._debug === "boolean" ? this._debug : this._debug.input.find((value) => value === data.type);
1341
1197
  if (shouldLog) {
1342
1198
  this._logDebug("WebSocket event sent: ", data.type, data);
@@ -1346,9 +1202,7 @@ var PluvRoom = class extends AbstractRoom {
1346
1202
  _updateState(updater) {
1347
1203
  let authorization;
1348
1204
  try {
1349
- authorization = JSON.parse(
1350
- JSON.stringify(this._state.authorization)
1351
- );
1205
+ authorization = JSON.parse(JSON.stringify(this._state.authorization));
1352
1206
  } catch (e) {
1353
1207
  throw new Error("User is not JSON serializable");
1354
1208
  }
@@ -1371,8 +1225,7 @@ var PluvClient = class {
1371
1225
  this._rooms = /* @__PURE__ */ new Map();
1372
1226
  this.createRoom = (room, options) => {
1373
1227
  const oldRoom = this.getRoom(room);
1374
- if (oldRoom)
1375
- return oldRoom;
1228
+ if (oldRoom) return oldRoom;
1376
1229
  const newRoom = new PluvRoom(room, {
1377
1230
  addons: options.addons,
1378
1231
  authEndpoint: this._authEndpoint,
@@ -1405,8 +1258,7 @@ var PluvClient = class {
1405
1258
  };
1406
1259
  this.leave = (room) => __async(this, null, function* () {
1407
1260
  const toLeave = typeof room === "string" ? this.getRoom(room) : room;
1408
- if (!toLeave)
1409
- return;
1261
+ if (!toLeave) return;
1410
1262
  yield toLeave.disconnect();
1411
1263
  this._rooms.delete(toLeave.id);
1412
1264
  this._logDebug(`Left and deleted room: ${room}`);
@@ -1418,8 +1270,7 @@ var PluvClient = class {
1418
1270
  }
1419
1271
  _logDebug(...data) {
1420
1272
  var _a;
1421
- if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production")
1422
- return;
1273
+ if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production") return;
1423
1274
  this._debug && console.log(...data);
1424
1275
  }
1425
1276
  };