@dxos/messaging 0.8.4-main.28f8d3d → 0.8.4-main.2c6827d

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.
@@ -531,33 +531,28 @@ var SignalLocalState = class {
531
531
  /**
532
532
  * Swarm events streams. Keys represent actually joined topic and peerId.
533
533
  */
534
- _swarmStreams;
534
+ _swarmStreams = new ComplexMap2(({ topic, peerId }) => topic.toHex() + peerId.toHex());
535
535
  /**
536
536
  * Represent desired joined topic and peerId.
537
537
  */
538
- _joinedTopics;
538
+ _joinedTopics = new ComplexSet2(({ topic, peerId }) => topic.toHex() + peerId.toHex());
539
539
  /**
540
540
  * Represent desired message subscriptions.
541
541
  */
542
- _subscribedMessages;
542
+ _subscribedMessages = new ComplexSet2(({ peerId }) => peerId.toHex());
543
543
  /**
544
544
  * Message streams. Keys represents actually subscribed peers.
545
545
  * @internal
546
546
  */
547
- messageStreams;
547
+ messageStreams = new ComplexMap2((key) => key.toHex());
548
548
  /**
549
549
  * Event to use in tests to wait till subscription is successfully established.
550
550
  * @internal
551
551
  */
552
- reconciled;
552
+ reconciled = new Event();
553
553
  constructor(_onMessage, _onSwarmEvent) {
554
554
  this._onMessage = _onMessage;
555
555
  this._onSwarmEvent = _onSwarmEvent;
556
- this._swarmStreams = new ComplexMap2(({ topic, peerId }) => topic.toHex() + peerId.toHex());
557
- this._joinedTopics = new ComplexSet2(({ topic, peerId }) => topic.toHex() + peerId.toHex());
558
- this._subscribedMessages = new ComplexSet2(({ peerId }) => peerId.toHex());
559
- this.messageStreams = new ComplexMap2((key) => key.toHex());
560
- this.reconciled = new Event();
561
556
  }
562
557
  async safeCloseStreams() {
563
558
  const streams = [
@@ -998,11 +993,11 @@ var RECONCILE_INTERVAL = 5e3;
998
993
  var SignalClient = class extends Resource {
999
994
  _host;
1000
995
  _getMetadata;
1001
- _monitor;
1002
- _state;
996
+ _monitor = new SignalClientMonitor();
997
+ _state = SignalState.CLOSED;
1003
998
  _lastError;
1004
- _lastReconciliationFailed;
1005
- _clientReady;
999
+ _lastReconciliationFailed = false;
1000
+ _clientReady = new Trigger2();
1006
1001
  _connectionCtx;
1007
1002
  _client;
1008
1003
  _reconcileTask;
@@ -1010,15 +1005,15 @@ var SignalClient = class extends Resource {
1010
1005
  /**
1011
1006
  * Number of milliseconds after which the connection will be attempted again in case of error.
1012
1007
  */
1013
- _reconnectAfter;
1014
- _instanceId;
1008
+ _reconnectAfter = DEFAULT_RECONNECT_TIMEOUT;
1009
+ _instanceId = PublicKey4.random().toHex();
1015
1010
  /**
1016
1011
  * @internal
1017
1012
  */
1018
1013
  localState;
1019
- statusChanged;
1020
- onMessage;
1021
- swarmEvent;
1014
+ statusChanged = new Event2();
1015
+ onMessage = new Event2();
1016
+ swarmEvent = new Event2();
1022
1017
  /**
1023
1018
  * @param _host Signal server websocket URL.
1024
1019
  * @param onMessage called when a new message is received.
@@ -1026,7 +1021,7 @@ var SignalClient = class extends Resource {
1026
1021
  * @param _getMetadata signal-message metadata provider, called for every message.
1027
1022
  */
1028
1023
  constructor(_host, _getMetadata) {
1029
- super(), 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();
1024
+ super(), this._host = _host, this._getMetadata = _getMetadata;
1030
1025
  if (!this._host.startsWith("wss://") && !this._host.startsWith("ws://")) {
1031
1026
  throw new Error(`Signal server requires a websocket URL. Provided: ${this._host}`);
1032
1027
  }
@@ -1456,21 +1451,16 @@ var MemorySignalManagerContext = class {
1456
1451
  };
1457
1452
  var MemorySignalManager = class {
1458
1453
  _context;
1459
- statusChanged;
1460
- swarmEvent;
1461
- onMessage;
1454
+ statusChanged = new Event3();
1455
+ swarmEvent = new Event3();
1456
+ onMessage = new Event3();
1462
1457
  /** Will be used to emit SwarmEvents on .open() and .close() */
1463
- _joinedSwarms;
1458
+ _joinedSwarms = new ComplexSet3(({ topic, peer }) => topic.toHex() + peer.peerKey);
1464
1459
  _ctx;
1465
1460
  // TODO(dmaretskyi): Replace with callback.
1466
- _freezeTrigger;
1461
+ _freezeTrigger = new Trigger3().wake();
1467
1462
  constructor(_context) {
1468
1463
  this._context = _context;
1469
- this.statusChanged = new Event3();
1470
- this.swarmEvent = new Event3();
1471
- this.onMessage = new Event3();
1472
- this._joinedSwarms = new ComplexSet3(({ topic, peer }) => topic.toHex() + peer.peerKey);
1473
- this._freezeTrigger = new Trigger3().wake();
1474
1464
  this._ctx = new Context3(void 0, {
1475
1465
  F: __dxlog_file5,
1476
1466
  L: 54
@@ -1747,19 +1737,19 @@ var WSS_SIGNAL_SERVER_REBOOT_DELAY = 3e3;
1747
1737
  var WebsocketSignalManager = class extends Resource2 {
1748
1738
  _hosts;
1749
1739
  _getMetadata;
1750
- _servers;
1751
- _monitor;
1740
+ _servers = /* @__PURE__ */ new Map();
1741
+ _monitor = new WebsocketSignalManagerMonitor();
1752
1742
  /**
1753
1743
  * Used to avoid logging failed server restarts more than once until the server actually recovers.
1754
1744
  */
1755
1745
  _failedServersBitfield;
1756
- failureCount;
1757
- statusChanged;
1758
- swarmEvent;
1759
- onMessage;
1760
- _instanceId;
1746
+ failureCount = /* @__PURE__ */ new Map();
1747
+ statusChanged = new Event4();
1748
+ swarmEvent = new Event4();
1749
+ onMessage = new Event4();
1750
+ _instanceId = PublicKey6.random().toHex();
1761
1751
  constructor(_hosts, _getMetadata) {
1762
- super(), 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();
1752
+ super(), this._hosts = _hosts, this._getMetadata = _getMetadata;
1763
1753
  log6("Created WebsocketSignalManager", {
1764
1754
  hosts: this._hosts
1765
1755
  }, {
@@ -2052,7 +2042,7 @@ _ts_decorate([
2052
2042
  // src/signal-manager/edge-signal-manager.ts
2053
2043
  import { Event as Event5, scheduleMicroTask } from "@dxos/async";
2054
2044
  import { Resource as Resource3, cancelWithContext as cancelWithContext3 } from "@dxos/context";
2055
- import { protocol } from "@dxos/edge-client";
2045
+ import { EdgeIdentityChangedError, protocol } from "@dxos/edge-client";
2056
2046
  import { invariant as invariant6 } from "@dxos/invariant";
2057
2047
  import { PublicKey as PublicKey7 } from "@dxos/keys";
2058
2048
  import { log as log7 } from "@dxos/log";
@@ -2121,16 +2111,23 @@ var EdgeSignalManager = class extends Resource3 {
2121
2111
  }
2122
2112
  async leave({ topic, peer }) {
2123
2113
  this._swarmPeers.delete(topic);
2124
- await this._edgeConnection.send(protocol.createMessage(SwarmRequestSchema, {
2125
- serviceId: EdgeService.SWARM,
2126
- source: createMessageSource(topic, peer),
2127
- payload: {
2128
- action: SwarmRequestAction.LEAVE,
2129
- swarmKeys: [
2130
- topic.toHex()
2131
- ]
2114
+ try {
2115
+ await this._edgeConnection.send(protocol.createMessage(SwarmRequestSchema, {
2116
+ serviceId: EdgeService.SWARM,
2117
+ source: createMessageSource(topic, peer),
2118
+ payload: {
2119
+ action: SwarmRequestAction.LEAVE,
2120
+ swarmKeys: [
2121
+ topic.toHex()
2122
+ ]
2123
+ }
2124
+ }));
2125
+ } catch (err) {
2126
+ if (err instanceof EdgeIdentityChangedError) {
2127
+ return;
2132
2128
  }
2133
- }));
2129
+ throw err;
2130
+ }
2134
2131
  }
2135
2132
  async query({ topic }) {
2136
2133
  const response = cancelWithContext3(this._ctx, this.swarmState.waitFor((state) => state.swarmKey === topic.toHex()));
@@ -2159,7 +2156,7 @@ var EdgeSignalManager = class extends Resource3 {
2159
2156
  }
2160
2157
  }, {
2161
2158
  F: __dxlog_file7,
2162
- L: 122,
2159
+ L: 131,
2163
2160
  S: this,
2164
2161
  C: (f, a) => f(...a)
2165
2162
  });
@@ -2194,7 +2191,7 @@ var EdgeSignalManager = class extends Resource3 {
2194
2191
  _processSwarmResponse(message) {
2195
2192
  invariant6(protocol.getPayloadType(message) === SwarmResponseSchema.typeName, "Wrong payload type", {
2196
2193
  F: __dxlog_file7,
2197
- L: 159,
2194
+ L: 168,
2198
2195
  S: this,
2199
2196
  A: [
2200
2197
  "protocol.getPayloadType(message) === SwarmResponseSchema.typeName",
@@ -2238,7 +2235,7 @@ var EdgeSignalManager = class extends Resource3 {
2238
2235
  _processMessage(message) {
2239
2236
  invariant6(protocol.getPayloadType(message) === bufWkt.AnySchema.typeName, "Wrong payload type", {
2240
2237
  F: __dxlog_file7,
2241
- L: 197,
2238
+ L: 206,
2242
2239
  S: this,
2243
2240
  A: [
2244
2241
  "protocol.getPayloadType(message) === bufWkt.AnySchema.typeName",
@@ -2248,7 +2245,7 @@ var EdgeSignalManager = class extends Resource3 {
2248
2245
  const payload = protocol.getPayload(message, bufWkt.AnySchema);
2249
2246
  invariant6(message.source, "source is missing", {
2250
2247
  F: __dxlog_file7,
2251
- L: 199,
2248
+ L: 208,
2252
2249
  S: this,
2253
2250
  A: [
2254
2251
  "message.source",
@@ -2257,7 +2254,7 @@ var EdgeSignalManager = class extends Resource3 {
2257
2254
  });
2258
2255
  invariant6(message.target, "target is missing", {
2259
2256
  F: __dxlog_file7,
2260
- L: 200,
2257
+ L: 209,
2261
2258
  S: this,
2262
2259
  A: [
2263
2260
  "message.target",
@@ -2266,7 +2263,7 @@ var EdgeSignalManager = class extends Resource3 {
2266
2263
  });
2267
2264
  invariant6(message.target.length === 1, "target should have exactly one item", {
2268
2265
  F: __dxlog_file7,
2269
- L: 201,
2266
+ L: 210,
2270
2267
  S: this,
2271
2268
  A: [
2272
2269
  "message.target.length === 1",
@@ -2290,7 +2287,7 @@ var EdgeSignalManager = class extends Resource3 {
2290
2287
  swarms: Array.from(this._swarmPeers.keys())
2291
2288
  }, {
2292
2289
  F: __dxlog_file7,
2293
- L: 220,
2290
+ L: 229,
2294
2291
  S: this,
2295
2292
  C: (f, a) => f(...a)
2296
2293
  });
@@ -2380,4 +2377,4 @@ export {
2380
2377
  EdgeSignalManager,
2381
2378
  setIdentityTags
2382
2379
  };
2383
- //# sourceMappingURL=chunk-TPBTNKLU.mjs.map
2380
+ //# sourceMappingURL=chunk-L7NDSF6K.mjs.map