@dxos/network-manager 0.1.35 → 0.1.36

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.
@@ -433,8 +433,8 @@ var MessageRouter = class {
433
433
 
434
434
  // packages/core/mesh/network-manager/src/swarm/swarm.ts
435
435
  import assert4 from "@dxos/node-std/assert";
436
- import { Event as Event2, scheduleTask, sleep, synchronized as synchronized3 } from "@dxos/async";
437
- import { Context } from "@dxos/context";
436
+ import { Event as Event2, scheduleTask as scheduleTask2, sleep, synchronized as synchronized3 } from "@dxos/async";
437
+ import { Context as Context2 } from "@dxos/context";
438
438
  import { ErrorStream as ErrorStream2 } from "@dxos/debug";
439
439
  import { PublicKey as PublicKey4 } from "@dxos/keys";
440
440
  import { log as log4, logInfo } from "@dxos/log";
@@ -443,7 +443,8 @@ import { ComplexMap as ComplexMap2, isNotNullOrUndefined } from "@dxos/util";
443
443
 
444
444
  // packages/core/mesh/network-manager/src/swarm/peer.ts
445
445
  import assert3 from "@dxos/node-std/assert";
446
- import { synchronized as synchronized2 } from "@dxos/async";
446
+ import { scheduleTask, synchronized as synchronized2 } from "@dxos/async";
447
+ import { Context } from "@dxos/context";
447
448
  import { PublicKey as PublicKey3 } from "@dxos/keys";
448
449
  import { log as log3 } from "@dxos/log";
449
450
  var __decorate2 = function(decorators, target, key, desc) {
@@ -456,6 +457,7 @@ var __decorate2 = function(decorators, target, key, desc) {
456
457
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
457
458
  return c > 3 && r && Object.defineProperty(target, key, r), r;
458
459
  };
460
+ var CONNECTION_COUNTS_STABLE_AFTER = 5e3;
459
461
  var Peer = class {
460
462
  constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _callbacks) {
461
463
  this.id = id;
@@ -465,6 +467,9 @@ var Peer = class {
465
467
  this._protocolProvider = _protocolProvider;
466
468
  this._transportFactory = _transportFactory;
467
469
  this._callbacks = _callbacks;
470
+ this._availableAfter = 0;
471
+ this.availableToConnect = true;
472
+ this._ctx = new Context();
468
473
  this.advertizing = false;
469
474
  this.initiating = false;
470
475
  }
@@ -481,7 +486,7 @@ var Peer = class {
481
486
  peerId: this.localPeerId
482
487
  }, {
483
488
  file: "peer.ts",
484
- line: 86,
489
+ line: 107,
485
490
  scope: this,
486
491
  callSite: (f, a) => f(...a)
487
492
  });
@@ -508,7 +513,7 @@ var Peer = class {
508
513
  err
509
514
  }, {
510
515
  file: "peer.ts",
511
- line: 110,
516
+ line: 131,
512
517
  scope: this,
513
518
  callSite: (f, a) => f(...a)
514
519
  });
@@ -537,7 +542,7 @@ var Peer = class {
537
542
  sessionId
538
543
  }, {
539
544
  file: "peer.ts",
540
- line: 128,
545
+ line: 149,
541
546
  scope: this,
542
547
  callSite: (f, a) => f(...a)
543
548
  });
@@ -560,14 +565,14 @@ var Peer = class {
560
565
  remoteId: this.id
561
566
  }, {
562
567
  file: "peer.ts",
563
- line: 140,
568
+ line: 161,
564
569
  scope: this,
565
570
  callSite: (f, a) => f(...a)
566
571
  });
567
572
  if (connection.state !== ConnectionState.INITIAL) {
568
573
  log3("ignoring response", {}, {
569
574
  file: "peer.ts",
570
- line: 142,
575
+ line: 163,
571
576
  scope: this,
572
577
  callSite: (f, a) => f(...a)
573
578
  });
@@ -587,7 +592,7 @@ var Peer = class {
587
592
  err
588
593
  }, {
589
594
  file: "peer.ts",
590
- line: 153,
595
+ line: 174,
591
596
  scope: this,
592
597
  callSite: (f, a) => f(...a)
593
598
  });
@@ -602,6 +607,7 @@ var Peer = class {
602
607
  * Either we're initiating a connection or creating one in response to an offer from the other peer.
603
608
  */
604
609
  _createConnection(initiator, sessionId) {
610
+ var _a;
605
611
  log3("creating connection", {
606
612
  topic: this.topic,
607
613
  peerId: this.localPeerId,
@@ -610,7 +616,7 @@ var Peer = class {
610
616
  sessionId
611
617
  }, {
612
618
  file: "peer.ts",
613
- line: 167,
619
+ line: 188,
614
620
  scope: this,
615
621
  callSite: (f, a) => f(...a)
616
622
  });
@@ -633,9 +639,13 @@ var Peer = class {
633
639
  );
634
640
  connection._traceParent = this._traceParent;
635
641
  this._callbacks.onInitiated(connection);
642
+ void ((_a = this._connectionCtx) == null ? void 0 : _a.dispose());
643
+ this._connectionCtx = this._ctx.derive();
636
644
  connection.stateChanged.on((state) => {
637
645
  switch (state) {
638
646
  case ConnectionState.CONNECTED: {
647
+ this.availableToConnect = true;
648
+ this._lastConnectionTime = Date.now();
639
649
  this._callbacks.onConnected();
640
650
  break;
641
651
  }
@@ -647,13 +657,23 @@ var Peer = class {
647
657
  initiator
648
658
  }, {
649
659
  file: "peer.ts",
650
- line: 197,
660
+ line: 224,
651
661
  scope: this,
652
662
  callSite: (f, a) => f(...a)
653
663
  });
654
664
  assert3(this.connection === connection, "Connection mismatch (race condition).");
665
+ if (this._lastConnectionTime && this._lastConnectionTime + CONNECTION_COUNTS_STABLE_AFTER < Date.now()) {
666
+ this._availableAfter = 0;
667
+ } else {
668
+ this.availableToConnect = false;
669
+ this._availableAfter = increaseInterval(this._availableAfter);
670
+ }
655
671
  this.connection = void 0;
656
672
  this._callbacks.onDisconnected();
673
+ scheduleTask(this._connectionCtx, () => {
674
+ this.availableToConnect = true;
675
+ this._callbacks.onPeerAvailable();
676
+ }, this._availableAfter);
657
677
  break;
658
678
  }
659
679
  }
@@ -667,7 +687,7 @@ var Peer = class {
667
687
  err
668
688
  }, {
669
689
  file: "peer.ts",
670
- line: 207,
690
+ line: 252,
671
691
  scope: this,
672
692
  callSite: (f, a) => f(...a)
673
693
  });
@@ -686,7 +706,7 @@ var Peer = class {
686
706
  sessionId: connection.sessionId
687
707
  }, {
688
708
  file: "peer.ts",
689
- line: 223,
709
+ line: 268,
690
710
  scope: this,
691
711
  callSite: (f, a) => f(...a)
692
712
  });
@@ -696,7 +716,7 @@ var Peer = class {
696
716
  sessionId: connection.sessionId
697
717
  }, {
698
718
  file: "peer.ts",
699
- line: 229,
719
+ line: 274,
700
720
  scope: this,
701
721
  callSite: (f, a) => f(...a)
702
722
  });
@@ -707,7 +727,7 @@ var Peer = class {
707
727
  message
708
728
  }, {
709
729
  file: "peer.ts",
710
- line: 234,
730
+ line: 279,
711
731
  scope: this,
712
732
  callSite: (f, a) => f(...a)
713
733
  });
@@ -717,12 +737,13 @@ var Peer = class {
717
737
  }
718
738
  async destroy() {
719
739
  var _a;
740
+ await this._ctx.dispose();
720
741
  log3("Destroying peer", {
721
742
  peerId: this.id,
722
743
  topic: this.topic
723
744
  }, {
724
745
  file: "peer.ts",
725
- line: 242,
746
+ line: 288,
726
747
  scope: this,
727
748
  callSite: (f, a) => f(...a)
728
749
  });
@@ -732,6 +753,18 @@ var Peer = class {
732
753
  __decorate2([
733
754
  synchronized2
734
755
  ], Peer.prototype, "destroy", null);
756
+ var increaseInterval = (interval) => {
757
+ if (interval === 0) {
758
+ return 50;
759
+ } else if (interval < 500) {
760
+ return 500;
761
+ } else if (interval < 1e3) {
762
+ return 1e3;
763
+ } else if (interval < 5e3) {
764
+ return 5e3;
765
+ }
766
+ return 1e4;
767
+ };
735
768
 
736
769
  // packages/core/mesh/network-manager/src/swarm/swarm.ts
737
770
  var __decorate3 = function(decorators, target, key, desc) {
@@ -758,7 +791,7 @@ var Swarm = class {
758
791
  this._transportFactory = _transportFactory;
759
792
  this._label = _label;
760
793
  this._peers = new ComplexMap2(PublicKey4.hash);
761
- this._ctx = new Context();
794
+ this._ctx = new Context2();
762
795
  this.connectionAdded = new Event2();
763
796
  this.disconnected = new Event2();
764
797
  this.connected = new Event2();
@@ -998,7 +1031,7 @@ var Swarm = class {
998
1031
  }
999
1032
  // For debug purposes
1000
1033
  async goOnline() {
1001
- this._ctx = new Context();
1034
+ this._ctx = new Context2();
1002
1035
  }
1003
1036
  _getOrCreatePeer(peerId) {
1004
1037
  let peer = this._peers.get(peerId);
@@ -1027,6 +1060,9 @@ var Swarm = class {
1027
1060
  },
1028
1061
  onOffer: (remoteId) => {
1029
1062
  return this._topology.onOffer(remoteId);
1063
+ },
1064
+ onPeerAvailable: () => {
1065
+ this._topology.update();
1030
1066
  }
1031
1067
  });
1032
1068
  peer._traceParent = this._instanceId;
@@ -1044,19 +1080,19 @@ var Swarm = class {
1044
1080
  getState: () => ({
1045
1081
  ownPeerId: this._ownPeerId,
1046
1082
  connected: Array.from(this._peers.values()).filter((peer) => peer.connection).map((peer) => peer.id),
1047
- candidates: Array.from(this._peers.values()).filter((peer) => !peer.connection && peer.advertizing).map((peer) => peer.id)
1083
+ candidates: Array.from(this._peers.values()).filter((peer) => !peer.connection && peer.advertizing && peer.availableToConnect).map((peer) => peer.id)
1048
1084
  }),
1049
1085
  connect: (peer) => {
1050
1086
  if (this._ctx.disposed) {
1051
1087
  return;
1052
1088
  }
1053
- scheduleTask(this._ctx, async () => {
1089
+ scheduleTask2(this._ctx, async () => {
1054
1090
  try {
1055
1091
  await this._initiateConnection(peer);
1056
1092
  } catch (err) {
1057
1093
  log4.warn("initiation error", err, {
1058
1094
  file: "swarm.ts",
1059
- line: 317,
1095
+ line: 320,
1060
1096
  scope: this,
1061
1097
  callSite: (f, a) => f(...a)
1062
1098
  });
@@ -1067,7 +1103,7 @@ var Swarm = class {
1067
1103
  if (this._ctx.disposed) {
1068
1104
  return;
1069
1105
  }
1070
- scheduleTask(this._ctx, async () => {
1106
+ scheduleTask2(this._ctx, async () => {
1071
1107
  await this._closeConnection(peer);
1072
1108
  this._topology.update();
1073
1109
  });
@@ -1084,7 +1120,7 @@ var Swarm = class {
1084
1120
  remoteId
1085
1121
  }, {
1086
1122
  file: "swarm.ts",
1087
- line: 344,
1123
+ line: 347,
1088
1124
  scope: this,
1089
1125
  callSite: (f, a) => f(...a)
1090
1126
  });
@@ -1101,7 +1137,7 @@ var Swarm = class {
1101
1137
  remoteId
1102
1138
  }, {
1103
1139
  file: "swarm.ts",
1104
- line: 358,
1140
+ line: 361,
1105
1141
  scope: this,
1106
1142
  callSite: (f, a) => f(...a)
1107
1143
  });
@@ -1111,7 +1147,7 @@ var Swarm = class {
1111
1147
  remoteId
1112
1148
  }, {
1113
1149
  file: "swarm.ts",
1114
- line: 361,
1150
+ line: 364,
1115
1151
  scope: this,
1116
1152
  callSite: (f, a) => f(...a)
1117
1153
  });
@@ -1301,6 +1337,7 @@ var NetworkManager = class {
1301
1337
  this.connectionStateChanged = new Event5();
1302
1338
  this.topicsUpdated = new Event5();
1303
1339
  this._instanceId = PublicKey7.random().toHex();
1340
+ signalManager._traceParent = this._instanceId;
1304
1341
  this._transportFactory = transportFactory;
1305
1342
  this._signalManager = signalManager;
1306
1343
  this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => {
@@ -1342,10 +1379,11 @@ var NetworkManager = class {
1342
1379
  }
1343
1380
  async open() {
1344
1381
  log6.trace("dxos.mesh.network-manager", trace3.begin({
1345
- id: this._instanceId
1382
+ id: this._instanceId,
1383
+ parentId: this._traceParent
1346
1384
  }), {
1347
1385
  file: "network-manager.ts",
1348
- line: 133,
1386
+ line: 135,
1349
1387
  scope: this,
1350
1388
  callSite: (f, a) => f(...a)
1351
1389
  });
@@ -1357,7 +1395,7 @@ var NetworkManager = class {
1357
1395
  await this.leaveSwarm(topic).catch((err) => {
1358
1396
  log6(err, {}, {
1359
1397
  file: "network-manager.ts",
1360
- line: 141,
1398
+ line: 143,
1361
1399
  scope: this,
1362
1400
  callSite: (f, a) => f(...a)
1363
1401
  });
@@ -1369,7 +1407,7 @@ var NetworkManager = class {
1369
1407
  id: this._instanceId
1370
1408
  }), {
1371
1409
  file: "network-manager.ts",
1372
- line: 147,
1410
+ line: 149,
1373
1411
  scope: this,
1374
1412
  callSite: (f, a) => f(...a)
1375
1413
  });
@@ -1392,7 +1430,7 @@ var NetworkManager = class {
1392
1430
  topology: topology.toString()
1393
1431
  }, {
1394
1432
  file: "network-manager.ts",
1395
- line: 168,
1433
+ line: 170,
1396
1434
  scope: this,
1397
1435
  callSite: (f, a) => f(...a)
1398
1436
  });
@@ -1402,7 +1440,7 @@ var NetworkManager = class {
1402
1440
  error
1403
1441
  }, {
1404
1442
  file: "network-manager.ts",
1405
- line: 171,
1443
+ line: 173,
1406
1444
  scope: this,
1407
1445
  callSite: (f, a) => f(...a)
1408
1446
  });
@@ -1413,7 +1451,7 @@ var NetworkManager = class {
1413
1451
  peerId
1414
1452
  }).catch((error) => log6.catch(error, {}, {
1415
1453
  file: "network-manager.ts",
1416
- line: 175,
1454
+ line: 177,
1417
1455
  scope: this,
1418
1456
  callSite: (f, a) => f(...a)
1419
1457
  }));
@@ -1425,7 +1463,7 @@ var NetworkManager = class {
1425
1463
  count: this._swarms.size
1426
1464
  }, {
1427
1465
  file: "network-manager.ts",
1428
- line: 180,
1466
+ line: 182,
1429
1467
  scope: this,
1430
1468
  callSite: (f, a) => f(...a)
1431
1469
  });
@@ -1443,7 +1481,7 @@ var NetworkManager = class {
1443
1481
  topic: PublicKey7.from(topic).truncate()
1444
1482
  }, {
1445
1483
  file: "network-manager.ts",
1446
- line: 192,
1484
+ line: 194,
1447
1485
  scope: this,
1448
1486
  callSite: (f, a) => f(...a)
1449
1487
  });
@@ -1453,7 +1491,7 @@ var NetworkManager = class {
1453
1491
  topic: PublicKey7.from(topic)
1454
1492
  }, {
1455
1493
  file: "network-manager.ts",
1456
- line: 196,
1494
+ line: 198,
1457
1495
  scope: this,
1458
1496
  callSite: (f, a) => f(...a)
1459
1497
  });
@@ -1474,7 +1512,7 @@ var NetworkManager = class {
1474
1512
  count: this._swarms.size
1475
1513
  }, {
1476
1514
  file: "network-manager.ts",
1477
- line: 210,
1515
+ line: 212,
1478
1516
  scope: this,
1479
1517
  callSite: (f, a) => f(...a)
1480
1518
  });
@@ -2098,7 +2136,7 @@ var WebRTCTransportService = class {
2098
2136
  // packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts
2099
2137
  import assert12 from "@dxos/node-std/assert";
2100
2138
  import { Event as Event8 } from "@dxos/async";
2101
- import { Context as Context2 } from "@dxos/context";
2139
+ import { Context as Context3 } from "@dxos/context";
2102
2140
  import { ErrorStream as ErrorStream5 } from "@dxos/debug";
2103
2141
  import { PublicKey as PublicKey11 } from "@dxos/keys";
2104
2142
  import { log as log12 } from "@dxos/log";
@@ -2108,7 +2146,7 @@ var WebRTCTransportProxy = class {
2108
2146
  constructor(_params) {
2109
2147
  this._params = _params;
2110
2148
  this._proxyId = PublicKey11.random();
2111
- this._ctx = new Context2();
2149
+ this._ctx = new Context3();
2112
2150
  this.closed = new Event8();
2113
2151
  this._closed = false;
2114
2152
  this.connected = new Event8();
@@ -2284,4 +2322,4 @@ export {
2284
2322
  WebRTCTransportProxyFactory,
2285
2323
  createTeleportProtocolFactory
2286
2324
  };
2287
- //# sourceMappingURL=chunk-4W77MFHZ.mjs.map
2325
+ //# sourceMappingURL=chunk-A2UJX3UX.mjs.map