@dxos/messaging 0.8.4-main.5ad4a44 → 0.8.4-main.66e292d

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.
@@ -30,24 +30,31 @@ var MessengerMonitor = class {
30
30
  var MESSAGE_TIMEOUT = 1e4;
31
31
 
32
32
  // src/messenger.ts
33
- function _define_property(obj, key, value) {
34
- if (key in obj) {
35
- Object.defineProperty(obj, key, {
36
- value,
37
- enumerable: true,
38
- configurable: true,
39
- writable: true
40
- });
41
- } else {
42
- obj[key] = value;
43
- }
44
- return obj;
45
- }
46
33
  var __dxlog_file = "/__w/dxos/dxos/packages/core/mesh/messaging/src/messenger.ts";
47
34
  var ReliablePayload = schema.getCodecForType("dxos.mesh.messaging.ReliablePayload");
48
35
  var Acknowledgement = schema.getCodecForType("dxos.mesh.messaging.Acknowledgement");
49
36
  var RECEIVED_MESSAGES_GC_INTERVAL = 12e4;
50
37
  var Messenger = class {
38
+ _monitor = new MessengerMonitor();
39
+ _signalManager;
40
+ // { peerId, payloadType } => listeners set
41
+ _listeners = new ComplexMap(({ peerId, payloadType }) => peerId + payloadType);
42
+ // peerId => listeners set
43
+ _defaultListeners = /* @__PURE__ */ new Map();
44
+ _onAckCallbacks = new ComplexMap(PublicKey.hash);
45
+ _receivedMessages = new ComplexSet(PublicKey.hash);
46
+ /**
47
+ * Keys scheduled to be cleared from _receivedMessages on the next iteration.
48
+ */
49
+ _toClear = new ComplexSet(PublicKey.hash);
50
+ _ctx;
51
+ _closed = true;
52
+ _retryDelay;
53
+ constructor({ signalManager, retryDelay = 1e3 }) {
54
+ this._signalManager = signalManager;
55
+ this._retryDelay = retryDelay;
56
+ this.open();
57
+ }
51
58
  open() {
52
59
  if (!this._closed) {
53
60
  return;
@@ -177,7 +184,10 @@ var Messenger = class {
177
184
  C: (f, a) => f(...a)
178
185
  });
179
186
  this._onAckCallbacks.delete(reliablePayload.messageId);
180
- timeoutHit(new ProtocolTimeoutError("signaling message not delivered", new TimeoutError(MESSAGE_TIMEOUT, "Message not delivered")));
187
+ timeoutHit(new ProtocolTimeoutError({
188
+ message: "signaling message not delivered",
189
+ cause: new TimeoutError(MESSAGE_TIMEOUT, "Message not delivered")
190
+ }));
181
191
  void messageContext.dispose();
182
192
  this._monitor.recordReliableMessage({
183
193
  sendAttempts,
@@ -403,21 +413,6 @@ var Messenger = class {
403
413
  });
404
414
  }
405
415
  }
406
- constructor({ signalManager, retryDelay = 1e3 }) {
407
- _define_property(this, "_monitor", new MessengerMonitor());
408
- _define_property(this, "_signalManager", void 0);
409
- _define_property(this, "_listeners", new ComplexMap(({ peerId, payloadType }) => peerId + payloadType));
410
- _define_property(this, "_defaultListeners", /* @__PURE__ */ new Map());
411
- _define_property(this, "_onAckCallbacks", new ComplexMap(PublicKey.hash));
412
- _define_property(this, "_receivedMessages", new ComplexSet(PublicKey.hash));
413
- _define_property(this, "_toClear", new ComplexSet(PublicKey.hash));
414
- _define_property(this, "_ctx", void 0);
415
- _define_property(this, "_closed", true);
416
- _define_property(this, "_retryDelay", void 0);
417
- this._signalManager = signalManager;
418
- this._retryDelay = retryDelay;
419
- this.open();
420
- }
421
416
  };
422
417
 
423
418
  // src/signal-client/signal-client.ts
@@ -431,20 +426,22 @@ import { SignalState } from "@dxos/protocols/proto/dxos/mesh/signal";
431
426
 
432
427
  // src/signal-client/signal-client-monitor.ts
433
428
  import { trace as trace3 } from "@dxos/tracing";
434
- function _define_property2(obj, key, value) {
435
- if (key in obj) {
436
- Object.defineProperty(obj, key, {
437
- value,
438
- enumerable: true,
439
- configurable: true,
440
- writable: true
441
- });
442
- } else {
443
- obj[key] = value;
444
- }
445
- return obj;
446
- }
447
429
  var SignalClientMonitor = class {
430
+ _performance = {
431
+ sentMessages: 0,
432
+ receivedMessages: 0,
433
+ reconnectCounter: 0,
434
+ joinCounter: 0,
435
+ leaveCounter: 0
436
+ };
437
+ /**
438
+ * Timestamp of when the connection attempt was began.
439
+ */
440
+ _connectionStarted = /* @__PURE__ */ new Date();
441
+ /**
442
+ * Timestamp of last state change.
443
+ */
444
+ _lastStateChange = /* @__PURE__ */ new Date();
448
445
  getRecordedTimestamps() {
449
446
  return {
450
447
  connectionStarted: this._connectionStarted,
@@ -514,17 +511,6 @@ var SignalClientMonitor = class {
514
511
  }
515
512
  });
516
513
  }
517
- constructor() {
518
- _define_property2(this, "_performance", {
519
- sentMessages: 0,
520
- receivedMessages: 0,
521
- reconnectCounter: 0,
522
- joinCounter: 0,
523
- leaveCounter: 0
524
- });
525
- _define_property2(this, "_connectionStarted", /* @__PURE__ */ new Date());
526
- _define_property2(this, "_lastStateChange", /* @__PURE__ */ new Date());
527
- }
528
514
  };
529
515
  var getByteCount = (message) => {
530
516
  return message.author.peerKey.length + message.recipient.peerKey.length + message.payload.type_url.length + message.payload.value.length;
@@ -541,21 +527,36 @@ import { cancelWithContext } from "@dxos/context";
541
527
  import { PublicKey as PublicKey2 } from "@dxos/keys";
542
528
  import { log as log2 } from "@dxos/log";
543
529
  import { ComplexMap as ComplexMap2, ComplexSet as ComplexSet2, safeAwaitAll } from "@dxos/util";
544
- function _define_property3(obj, key, value) {
545
- if (key in obj) {
546
- Object.defineProperty(obj, key, {
547
- value,
548
- enumerable: true,
549
- configurable: true,
550
- writable: true
551
- });
552
- } else {
553
- obj[key] = value;
554
- }
555
- return obj;
556
- }
557
530
  var __dxlog_file2 = "/__w/dxos/dxos/packages/core/mesh/messaging/src/signal-client/signal-local-state.ts";
558
531
  var SignalLocalState = class {
532
+ _onMessage;
533
+ _onSwarmEvent;
534
+ /**
535
+ * Swarm events streams. Keys represent actually joined topic and peerId.
536
+ */
537
+ _swarmStreams = new ComplexMap2(({ topic, peerId }) => topic.toHex() + peerId.toHex());
538
+ /**
539
+ * Represent desired joined topic and peerId.
540
+ */
541
+ _joinedTopics = new ComplexSet2(({ topic, peerId }) => topic.toHex() + peerId.toHex());
542
+ /**
543
+ * Represent desired message subscriptions.
544
+ */
545
+ _subscribedMessages = new ComplexSet2(({ peerId }) => peerId.toHex());
546
+ /**
547
+ * Message streams. Keys represents actually subscribed peers.
548
+ * @internal
549
+ */
550
+ messageStreams = new ComplexMap2((key) => key.toHex());
551
+ /**
552
+ * Event to use in tests to wait till subscription is successfully established.
553
+ * @internal
554
+ */
555
+ reconciled = new Event();
556
+ constructor(_onMessage, _onSwarmEvent) {
557
+ this._onMessage = _onMessage;
558
+ this._onSwarmEvent = _onSwarmEvent;
559
+ }
559
560
  async safeCloseStreams() {
560
561
  const streams = [
561
562
  ...this._swarmStreams.values()
@@ -715,22 +716,6 @@ var SignalLocalState = class {
715
716
  this.messageStreams.set(peerId, messageStream);
716
717
  }
717
718
  }
718
- constructor(_onMessage, _onSwarmEvent) {
719
- _define_property3(this, "_onMessage", void 0);
720
- _define_property3(this, "_onSwarmEvent", void 0);
721
- _define_property3(this, "_swarmStreams", void 0);
722
- _define_property3(this, "_joinedTopics", void 0);
723
- _define_property3(this, "_subscribedMessages", void 0);
724
- _define_property3(this, "messageStreams", void 0);
725
- _define_property3(this, "reconciled", void 0);
726
- this._onMessage = _onMessage;
727
- this._onSwarmEvent = _onSwarmEvent;
728
- this._swarmStreams = new ComplexMap2(({ topic, peerId }) => topic.toHex() + peerId.toHex());
729
- this._joinedTopics = new ComplexSet2(({ topic, peerId }) => topic.toHex() + peerId.toHex());
730
- this._subscribedMessages = new ComplexSet2(({ peerId }) => peerId.toHex());
731
- this.messageStreams = new ComplexMap2((key) => key.toHex());
732
- this.reconciled = new Event();
733
- }
734
719
  };
735
720
 
736
721
  // src/signal-client/signal-rpc-client.ts
@@ -757,22 +742,132 @@ var SignalRpcClientMonitor = class {
757
742
  };
758
743
 
759
744
  // src/signal-client/signal-rpc-client.ts
760
- function _define_property4(obj, key, value) {
761
- if (key in obj) {
762
- Object.defineProperty(obj, key, {
763
- value,
764
- enumerable: true,
765
- configurable: true,
766
- writable: true
767
- });
768
- } else {
769
- obj[key] = value;
770
- }
771
- return obj;
772
- }
773
745
  var __dxlog_file3 = "/__w/dxos/dxos/packages/core/mesh/messaging/src/signal-client/signal-rpc-client.ts";
774
746
  var SIGNAL_KEEPALIVE_INTERVAL = 1e4;
775
747
  var SignalRPCClient = class {
748
+ _socket;
749
+ _rpc;
750
+ _connectTrigger = new Trigger();
751
+ _keepaliveCtx;
752
+ _closed = false;
753
+ _url;
754
+ _callbacks;
755
+ _closeComplete = new Trigger();
756
+ _monitor = new SignalRpcClientMonitor();
757
+ constructor({ url, callbacks = {} }) {
758
+ const traceId = PublicKey3.random().toHex();
759
+ log3.trace("dxos.mesh.signal-rpc-client.constructor", trace5.begin({
760
+ id: traceId
761
+ }), {
762
+ F: __dxlog_file3,
763
+ L: 66,
764
+ S: this,
765
+ C: (f, a) => f(...a)
766
+ });
767
+ this._url = url;
768
+ this._callbacks = callbacks;
769
+ this._socket = new WebSocket(this._url);
770
+ this._rpc = createProtoRpcPeer({
771
+ requested: {
772
+ Signal: schema2.getService("dxos.mesh.signal.Signal")
773
+ },
774
+ noHandshake: true,
775
+ port: {
776
+ send: (msg) => {
777
+ if (this._closed) {
778
+ return;
779
+ }
780
+ try {
781
+ this._socket.send(msg);
782
+ } catch (err) {
783
+ log3.warn("send error", err, {
784
+ F: __dxlog_file3,
785
+ L: 85,
786
+ S: this,
787
+ C: (f, a) => f(...a)
788
+ });
789
+ }
790
+ },
791
+ subscribe: (cb) => {
792
+ this._socket.onmessage = async (msg) => {
793
+ if (typeof Blob !== "undefined" && msg.data instanceof Blob) {
794
+ cb(Buffer.from(await msg.data.arrayBuffer()));
795
+ } else {
796
+ cb(msg.data);
797
+ }
798
+ };
799
+ }
800
+ },
801
+ encodingOptions: {
802
+ preserveAny: true
803
+ }
804
+ });
805
+ this._socket.onopen = async () => {
806
+ try {
807
+ await this._rpc.open();
808
+ if (this._closed) {
809
+ await this._safeCloseRpc();
810
+ return;
811
+ }
812
+ log3(`RPC open ${this._url}`, void 0, {
813
+ F: __dxlog_file3,
814
+ L: 110,
815
+ S: this,
816
+ C: (f, a) => f(...a)
817
+ });
818
+ this._callbacks.onConnected?.();
819
+ this._connectTrigger.wake();
820
+ this._keepaliveCtx = new Context2(void 0, {
821
+ F: __dxlog_file3,
822
+ L: 113
823
+ });
824
+ scheduleTaskInterval2(this._keepaliveCtx, async () => {
825
+ this._socket?.send("__ping__");
826
+ }, SIGNAL_KEEPALIVE_INTERVAL);
827
+ } catch (err) {
828
+ this._callbacks.onError?.(err);
829
+ this._socket.close();
830
+ this._closed = true;
831
+ }
832
+ };
833
+ this._socket.onclose = async () => {
834
+ log3(`Disconnected ${this._url}`, void 0, {
835
+ F: __dxlog_file3,
836
+ L: 133,
837
+ S: this,
838
+ C: (f, a) => f(...a)
839
+ });
840
+ this._callbacks.onDisconnected?.();
841
+ this._closeComplete.wake();
842
+ await this.close();
843
+ };
844
+ this._socket.onerror = async (event) => {
845
+ if (this._closed) {
846
+ this._socket.close();
847
+ return;
848
+ }
849
+ this._closed = true;
850
+ this._callbacks.onError?.(event.error ?? new Error(event.message));
851
+ await this._safeCloseRpc();
852
+ log3.warn(`Socket ${event.type ?? "unknown"} error`, {
853
+ message: event.message,
854
+ url: this._url
855
+ }, {
856
+ F: __dxlog_file3,
857
+ L: 149,
858
+ S: this,
859
+ C: (f, a) => f(...a)
860
+ });
861
+ };
862
+ log3.trace("dxos.mesh.signal-rpc-client.constructor", trace5.end({
863
+ id: traceId
864
+ }), {
865
+ F: __dxlog_file3,
866
+ L: 152,
867
+ S: this,
868
+ C: (f, a) => f(...a)
869
+ });
870
+ }
776
871
  async close() {
777
872
  if (this._closed) {
778
873
  return;
@@ -890,151 +985,54 @@ var SignalRPCClient = class {
890
985
  });
891
986
  }
892
987
  }
893
- constructor({ url, callbacks = {} }) {
894
- _define_property4(this, "_socket", void 0);
895
- _define_property4(this, "_rpc", void 0);
896
- _define_property4(this, "_connectTrigger", new Trigger());
897
- _define_property4(this, "_keepaliveCtx", void 0);
898
- _define_property4(this, "_closed", false);
899
- _define_property4(this, "_url", void 0);
900
- _define_property4(this, "_callbacks", void 0);
901
- _define_property4(this, "_closeComplete", new Trigger());
902
- _define_property4(this, "_monitor", new SignalRpcClientMonitor());
903
- const traceId = PublicKey3.random().toHex();
904
- log3.trace("dxos.mesh.signal-rpc-client.constructor", trace5.begin({
905
- id: traceId
906
- }), {
907
- F: __dxlog_file3,
908
- L: 66,
909
- S: this,
910
- C: (f, a) => f(...a)
911
- });
912
- this._url = url;
913
- this._callbacks = callbacks;
914
- this._socket = new WebSocket(this._url);
915
- this._rpc = createProtoRpcPeer({
916
- requested: {
917
- Signal: schema2.getService("dxos.mesh.signal.Signal")
918
- },
919
- noHandshake: true,
920
- port: {
921
- send: (msg) => {
922
- if (this._closed) {
923
- return;
924
- }
925
- try {
926
- this._socket.send(msg);
927
- } catch (err) {
928
- log3.warn("send error", err, {
929
- F: __dxlog_file3,
930
- L: 85,
931
- S: this,
932
- C: (f, a) => f(...a)
933
- });
934
- }
935
- },
936
- subscribe: (cb) => {
937
- this._socket.onmessage = async (msg) => {
938
- if (typeof Blob !== "undefined" && msg.data instanceof Blob) {
939
- cb(Buffer.from(await msg.data.arrayBuffer()));
940
- } else {
941
- cb(msg.data);
942
- }
943
- };
944
- }
945
- },
946
- encodingOptions: {
947
- preserveAny: true
948
- }
949
- });
950
- this._socket.onopen = async () => {
951
- try {
952
- await this._rpc.open();
953
- if (this._closed) {
954
- await this._safeCloseRpc();
955
- return;
956
- }
957
- log3(`RPC open ${this._url}`, void 0, {
958
- F: __dxlog_file3,
959
- L: 110,
960
- S: this,
961
- C: (f, a) => f(...a)
962
- });
963
- this._callbacks.onConnected?.();
964
- this._connectTrigger.wake();
965
- this._keepaliveCtx = new Context2(void 0, {
966
- F: __dxlog_file3,
967
- L: 113
968
- });
969
- scheduleTaskInterval2(this._keepaliveCtx, async () => {
970
- this._socket?.send("__ping__");
971
- }, SIGNAL_KEEPALIVE_INTERVAL);
972
- } catch (err) {
973
- this._callbacks.onError?.(err);
974
- this._socket.close();
975
- this._closed = true;
976
- }
977
- };
978
- this._socket.onclose = async () => {
979
- log3(`Disconnected ${this._url}`, void 0, {
980
- F: __dxlog_file3,
981
- L: 133,
982
- S: this,
983
- C: (f, a) => f(...a)
984
- });
985
- this._callbacks.onDisconnected?.();
986
- this._closeComplete.wake();
987
- await this.close();
988
- };
989
- this._socket.onerror = async (event) => {
990
- if (this._closed) {
991
- this._socket.close();
992
- return;
993
- }
994
- this._closed = true;
995
- this._callbacks.onError?.(event.error ?? new Error(event.message));
996
- await this._safeCloseRpc();
997
- log3.warn(`Socket ${event.type ?? "unknown"} error`, {
998
- message: event.message,
999
- url: this._url
1000
- }, {
1001
- F: __dxlog_file3,
1002
- L: 149,
1003
- S: this,
1004
- C: (f, a) => f(...a)
1005
- });
1006
- };
1007
- log3.trace("dxos.mesh.signal-rpc-client.constructor", trace5.end({
1008
- id: traceId
1009
- }), {
1010
- F: __dxlog_file3,
1011
- L: 152,
1012
- S: this,
1013
- C: (f, a) => f(...a)
1014
- });
1015
- }
1016
988
  };
1017
989
 
1018
990
  // src/signal-client/signal-client.ts
1019
- function _define_property5(obj, key, value) {
1020
- if (key in obj) {
1021
- Object.defineProperty(obj, key, {
1022
- value,
1023
- enumerable: true,
1024
- configurable: true,
1025
- writable: true
1026
- });
1027
- } else {
1028
- obj[key] = value;
1029
- }
1030
- return obj;
1031
- }
1032
991
  var __dxlog_file4 = "/__w/dxos/dxos/packages/core/mesh/messaging/src/signal-client/signal-client.ts";
1033
992
  var DEFAULT_RECONNECT_TIMEOUT = 100;
1034
993
  var MAX_RECONNECT_TIMEOUT = 5e3;
1035
994
  var ERROR_RECONCILE_DELAY = 1e3;
1036
995
  var RECONCILE_INTERVAL = 5e3;
1037
996
  var SignalClient = class extends Resource {
997
+ _host;
998
+ _getMetadata;
999
+ _monitor = new SignalClientMonitor();
1000
+ _state = SignalState.CLOSED;
1001
+ _lastError;
1002
+ _lastReconciliationFailed = false;
1003
+ _clientReady = new Trigger2();
1004
+ _connectionCtx;
1005
+ _client;
1006
+ _reconcileTask;
1007
+ _reconnectTask;
1008
+ /**
1009
+ * Number of milliseconds after which the connection will be attempted again in case of error.
1010
+ */
1011
+ _reconnectAfter = DEFAULT_RECONNECT_TIMEOUT;
1012
+ _instanceId = PublicKey4.random().toHex();
1013
+ /**
1014
+ * @internal
1015
+ */
1016
+ localState;
1017
+ statusChanged = new Event2();
1018
+ onMessage = new Event2();
1019
+ swarmEvent = new Event2();
1020
+ /**
1021
+ * @param _host Signal server websocket URL.
1022
+ * @param onMessage called when a new message is received.
1023
+ * @param onSwarmEvent called when a new swarm event is received.
1024
+ * @param _getMetadata signal-message metadata provider, called for every message.
1025
+ */
1026
+ constructor(_host, _getMetadata) {
1027
+ super(), this._host = _host, this._getMetadata = _getMetadata;
1028
+ if (!this._host.startsWith("wss://") && !this._host.startsWith("ws://")) {
1029
+ throw new Error(`Signal server requires a websocket URL. Provided: ${this._host}`);
1030
+ }
1031
+ this.localState = new SignalLocalState(async (message) => {
1032
+ this._monitor.recordMessageReceived(message);
1033
+ this.onMessage.emit(message);
1034
+ }, async (event) => this.swarmEvent.emit(event));
1035
+ }
1038
1036
  async _open() {
1039
1037
  log4.trace("dxos.mesh.signal-client.open", trace6.begin({
1040
1038
  id: this._instanceId
@@ -1432,28 +1430,6 @@ var SignalClient = class extends Resource {
1432
1430
  });
1433
1431
  this._client = void 0;
1434
1432
  }
1435
- /**
1436
- * @param _host Signal server websocket URL.
1437
- * @param onMessage called when a new message is received.
1438
- * @param onSwarmEvent called when a new swarm event is received.
1439
- * @param _getMetadata signal-message metadata provider, called for every message.
1440
- */
1441
- constructor(_host, _getMetadata) {
1442
- super(), _define_property5(this, "_host", void 0), _define_property5(this, "_getMetadata", void 0), _define_property5(this, "_monitor", void 0), _define_property5(this, "_state", void 0), _define_property5(this, "_lastError", void 0), _define_property5(this, "_lastReconciliationFailed", void 0), _define_property5(this, "_clientReady", void 0), _define_property5(this, "_connectionCtx", void 0), _define_property5(this, "_client", void 0), _define_property5(this, "_reconcileTask", void 0), _define_property5(this, "_reconnectTask", void 0), /**
1443
- * Number of milliseconds after which the connection will be attempted again in case of error.
1444
- */
1445
- _define_property5(this, "_reconnectAfter", void 0), _define_property5(this, "_instanceId", void 0), /**
1446
- * @internal
1447
- */
1448
- _define_property5(this, "localState", void 0), _define_property5(this, "statusChanged", void 0), _define_property5(this, "onMessage", void 0), _define_property5(this, "swarmEvent", void 0), this._host = _host, this._getMetadata = _getMetadata, this._monitor = new SignalClientMonitor(), this._state = SignalState.CLOSED, this._lastReconciliationFailed = false, this._clientReady = new Trigger2(), this._reconnectAfter = DEFAULT_RECONNECT_TIMEOUT, this._instanceId = PublicKey4.random().toHex(), this.statusChanged = new Event2(), this.onMessage = new Event2(), this.swarmEvent = new Event2();
1449
- if (!this._host.startsWith("wss://") && !this._host.startsWith("ws://")) {
1450
- throw new Error(`Signal server requires a websocket URL. Provided: ${this._host}`);
1451
- }
1452
- this.localState = new SignalLocalState(async (message) => {
1453
- this._monitor.recordMessageReceived(message);
1454
- this.onMessage.emit(message);
1455
- }, async (event) => this.swarmEvent.emit(event));
1456
- }
1457
1433
  };
1458
1434
 
1459
1435
  // src/signal-methods.ts
@@ -1467,28 +1443,33 @@ import { PublicKey as PublicKey5 } from "@dxos/keys";
1467
1443
  import { log as log5 } from "@dxos/log";
1468
1444
  import { schema as schema3 } from "@dxos/protocols/proto";
1469
1445
  import { ComplexMap as ComplexMap3, ComplexSet as ComplexSet3 } from "@dxos/util";
1470
- function _define_property6(obj, key, value) {
1471
- if (key in obj) {
1472
- Object.defineProperty(obj, key, {
1473
- value,
1474
- enumerable: true,
1475
- configurable: true,
1476
- writable: true
1477
- });
1478
- } else {
1479
- obj[key] = value;
1480
- }
1481
- return obj;
1482
- }
1483
1446
  var __dxlog_file5 = "/__w/dxos/dxos/packages/core/mesh/messaging/src/signal-manager/memory-signal-manager.ts";
1484
1447
  var MemorySignalManagerContext = class {
1485
- constructor() {
1486
- _define_property6(this, "swarmEvent", new Event3());
1487
- _define_property6(this, "swarms", new ComplexMap3(PublicKey5.hash));
1488
- _define_property6(this, "connections", new ComplexMap3(PeerInfoHash));
1489
- }
1448
+ // Swarm messages.
1449
+ swarmEvent = new Event3();
1450
+ // Mapping from topic to set of peers.
1451
+ swarms = new ComplexMap3(PublicKey5.hash);
1452
+ // Map of connections for each peer for signaling.
1453
+ connections = new ComplexMap3(PeerInfoHash);
1490
1454
  };
1491
1455
  var MemorySignalManager = class {
1456
+ _context;
1457
+ statusChanged = new Event3();
1458
+ swarmEvent = new Event3();
1459
+ onMessage = new Event3();
1460
+ /** Will be used to emit SwarmEvents on .open() and .close() */
1461
+ _joinedSwarms = new ComplexSet3(({ topic, peer }) => topic.toHex() + peer.peerKey);
1462
+ _ctx;
1463
+ // TODO(dmaretskyi): Replace with callback.
1464
+ _freezeTrigger = new Trigger3().wake();
1465
+ constructor(_context) {
1466
+ this._context = _context;
1467
+ this._ctx = new Context3(void 0, {
1468
+ F: __dxlog_file5,
1469
+ L: 54
1470
+ });
1471
+ this._ctx.onDispose(this._context.swarmEvent.on((data) => this.swarmEvent.emit(data)));
1472
+ }
1492
1473
  async open() {
1493
1474
  if (!this._ctx.disposed) {
1494
1475
  return;
@@ -1706,26 +1687,6 @@ var MemorySignalManager = class {
1706
1687
  unfreeze() {
1707
1688
  this._freezeTrigger.wake();
1708
1689
  }
1709
- constructor(_context) {
1710
- _define_property6(this, "_context", void 0);
1711
- _define_property6(this, "statusChanged", void 0);
1712
- _define_property6(this, "swarmEvent", void 0);
1713
- _define_property6(this, "onMessage", void 0);
1714
- _define_property6(this, "_joinedSwarms", void 0);
1715
- _define_property6(this, "_ctx", void 0);
1716
- _define_property6(this, "_freezeTrigger", void 0);
1717
- this._context = _context;
1718
- this.statusChanged = new Event3();
1719
- this.swarmEvent = new Event3();
1720
- this.onMessage = new Event3();
1721
- this._joinedSwarms = new ComplexSet3(({ topic, peer }) => topic.toHex() + peer.peerKey);
1722
- this._freezeTrigger = new Trigger3().wake();
1723
- this._ctx = new Context3(void 0, {
1724
- F: __dxlog_file5,
1725
- L: 54
1726
- });
1727
- this._ctx.onDispose(this._context.swarmEvent.on((data) => this.swarmEvent.emit(data)));
1728
- }
1729
1690
  };
1730
1691
  var dec = (payload) => {
1731
1692
  if (!payload.type_url.endsWith("ReliablePayload")) {
@@ -1767,19 +1728,6 @@ var WebsocketSignalManagerMonitor = class {
1767
1728
  };
1768
1729
 
1769
1730
  // src/signal-manager/websocket-signal-manager.ts
1770
- function _define_property7(obj, key, value) {
1771
- if (key in obj) {
1772
- Object.defineProperty(obj, key, {
1773
- value,
1774
- enumerable: true,
1775
- configurable: true,
1776
- writable: true
1777
- });
1778
- } else {
1779
- obj[key] = value;
1780
- }
1781
- return obj;
1782
- }
1783
1731
  function _ts_decorate(decorators, target, key, desc) {
1784
1732
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1785
1733
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -1790,6 +1738,42 @@ var __dxlog_file6 = "/__w/dxos/dxos/packages/core/mesh/messaging/src/signal-mana
1790
1738
  var MAX_SERVER_FAILURES = 5;
1791
1739
  var WSS_SIGNAL_SERVER_REBOOT_DELAY = 3e3;
1792
1740
  var WebsocketSignalManager = class extends Resource2 {
1741
+ _hosts;
1742
+ _getMetadata;
1743
+ _servers = /* @__PURE__ */ new Map();
1744
+ _monitor = new WebsocketSignalManagerMonitor();
1745
+ /**
1746
+ * Used to avoid logging failed server restarts more than once until the server actually recovers.
1747
+ */
1748
+ _failedServersBitfield;
1749
+ failureCount = /* @__PURE__ */ new Map();
1750
+ statusChanged = new Event4();
1751
+ swarmEvent = new Event4();
1752
+ onMessage = new Event4();
1753
+ _instanceId = PublicKey6.random().toHex();
1754
+ constructor(_hosts, _getMetadata) {
1755
+ super(), this._hosts = _hosts, this._getMetadata = _getMetadata;
1756
+ log6("Created WebsocketSignalManager", {
1757
+ hosts: this._hosts
1758
+ }, {
1759
+ F: __dxlog_file6,
1760
+ L: 59,
1761
+ S: this,
1762
+ C: (f, a) => f(...a)
1763
+ });
1764
+ for (const host of this._hosts) {
1765
+ if (this._servers.has(host.server)) {
1766
+ continue;
1767
+ }
1768
+ const server = new SignalClient(host.server, this._getMetadata);
1769
+ server.swarmEvent.on((data) => this.swarmEvent.emit(data));
1770
+ server.onMessage.on((data) => this.onMessage.emit(data));
1771
+ server.statusChanged.on(() => this.statusChanged.emit(this.getStatus()));
1772
+ this._servers.set(host.server, server);
1773
+ this.failureCount.set(host.server, 0);
1774
+ }
1775
+ this._failedServersBitfield = BitField.zeros(this._hosts.length);
1776
+ }
1793
1777
  async _open() {
1794
1778
  log6("open signal manager", {
1795
1779
  hosts: this._hosts
@@ -2047,32 +2031,6 @@ var WebsocketSignalManager = class extends Resource2 {
2047
2031
  async _forEachServer(fn) {
2048
2032
  return Promise.all(Array.from(this._servers.entries()).map(([serverName, server], idx) => fn(server, serverName, idx)));
2049
2033
  }
2050
- constructor(_hosts, _getMetadata) {
2051
- super(), _define_property7(this, "_hosts", void 0), _define_property7(this, "_getMetadata", void 0), _define_property7(this, "_servers", void 0), _define_property7(this, "_monitor", void 0), /**
2052
- * Used to avoid logging failed server restarts more than once until the server actually recovers.
2053
- */
2054
- _define_property7(this, "_failedServersBitfield", void 0), _define_property7(this, "failureCount", void 0), _define_property7(this, "statusChanged", void 0), _define_property7(this, "swarmEvent", void 0), _define_property7(this, "onMessage", void 0), _define_property7(this, "_instanceId", void 0), this._hosts = _hosts, this._getMetadata = _getMetadata, this._servers = /* @__PURE__ */ new Map(), this._monitor = new WebsocketSignalManagerMonitor(), this.failureCount = /* @__PURE__ */ new Map(), this.statusChanged = new Event4(), this.swarmEvent = new Event4(), this.onMessage = new Event4(), this._instanceId = PublicKey6.random().toHex();
2055
- log6("Created WebsocketSignalManager", {
2056
- hosts: this._hosts
2057
- }, {
2058
- F: __dxlog_file6,
2059
- L: 59,
2060
- S: this,
2061
- C: (f, a) => f(...a)
2062
- });
2063
- for (const host of this._hosts) {
2064
- if (this._servers.has(host.server)) {
2065
- continue;
2066
- }
2067
- const server = new SignalClient(host.server, this._getMetadata);
2068
- server.swarmEvent.on((data) => this.swarmEvent.emit(data));
2069
- server.onMessage.on((data) => this.onMessage.emit(data));
2070
- server.statusChanged.on(() => this.statusChanged.emit(this.getStatus()));
2071
- this._servers.set(host.server, server);
2072
- this.failureCount.set(host.server, 0);
2073
- }
2074
- this._failedServersBitfield = BitField.zeros(this._hosts.length);
2075
- }
2076
2034
  };
2077
2035
  _ts_decorate([
2078
2036
  synchronized
@@ -2095,21 +2053,24 @@ import { EdgeService } from "@dxos/protocols";
2095
2053
  import { bufWkt } from "@dxos/protocols/buf";
2096
2054
  import { SwarmRequest_Action as SwarmRequestAction, SwarmRequestSchema, SwarmResponseSchema } from "@dxos/protocols/buf/dxos/edge/messenger_pb";
2097
2055
  import { ComplexMap as ComplexMap4, ComplexSet as ComplexSet4 } from "@dxos/util";
2098
- function _define_property8(obj, key, value) {
2099
- if (key in obj) {
2100
- Object.defineProperty(obj, key, {
2101
- value,
2102
- enumerable: true,
2103
- configurable: true,
2104
- writable: true
2105
- });
2106
- } else {
2107
- obj[key] = value;
2108
- }
2109
- return obj;
2110
- }
2111
2056
  var __dxlog_file7 = "/__w/dxos/dxos/packages/core/mesh/messaging/src/signal-manager/edge-signal-manager.ts";
2112
2057
  var EdgeSignalManager = class extends Resource3 {
2058
+ /**
2059
+ * @deprecated
2060
+ */
2061
+ swarmEvent = new Event5();
2062
+ swarmState = new Event5();
2063
+ onMessage = new Event5();
2064
+ /**
2065
+ * Swarm key -> { peer: <own state payload>, joinedPeers: <state of swarm> }.
2066
+ */
2067
+ // TODO(mykola): This class should not contain swarm state joinedPeers. Temporary before network-manager API changes to accept list of peers.
2068
+ _swarmPeers = new ComplexMap4(PublicKey7.hash);
2069
+ _edgeConnection;
2070
+ constructor({ edgeConnection }) {
2071
+ super();
2072
+ this._edgeConnection = edgeConnection;
2073
+ }
2113
2074
  async _open() {
2114
2075
  this._ctx.onDispose(this._edgeConnection.onMessage((message) => this._onMessage(message)));
2115
2076
  this._ctx.onDispose(this._edgeConnection.onReconnected(() => {
@@ -2344,17 +2305,6 @@ var EdgeSignalManager = class extends Resource3 {
2344
2305
  });
2345
2306
  }
2346
2307
  }
2347
- constructor({ edgeConnection }) {
2348
- super(), /**
2349
- * @deprecated
2350
- */
2351
- _define_property8(this, "swarmEvent", new Event5()), _define_property8(this, "swarmState", new Event5()), _define_property8(this, "onMessage", new Event5()), /**
2352
- * Swarm key -> { peer: <own state payload>, joinedPeers: <state of swarm> }.
2353
- */
2354
- // TODO(mykola): This class should not contain swarm state joinedPeers. Temporary before network-manager API changes to accept list of peers.
2355
- _define_property8(this, "_swarmPeers", new ComplexMap4(PublicKey7.hash)), _define_property8(this, "_edgeConnection", void 0);
2356
- this._edgeConnection = edgeConnection;
2357
- }
2358
2308
  };
2359
2309
  var createMessageSource = (topic, peerInfo) => {
2360
2310
  return {
@@ -2430,4 +2380,4 @@ export {
2430
2380
  EdgeSignalManager,
2431
2381
  setIdentityTags
2432
2382
  };
2433
- //# sourceMappingURL=chunk-CD3GXEDZ.mjs.map
2383
+ //# sourceMappingURL=chunk-4EMAT5U7.mjs.map