@dxos/network-manager 0.1.56 → 0.1.57-main.281294d

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.
@@ -4,6 +4,16 @@ import { PublicKey } from '@dxos/keys';
4
4
  import { SignalMessage, SignalMessenger } from '../signal';
5
5
  import { Transport, TransportFactory } from '../transport';
6
6
  import { WireProtocol } from '../wire-protocol';
7
+ interface ConnectionCallbacks {
8
+ /**
9
+ * Connection opened.
10
+ */
11
+ onConnected?: () => void;
12
+ /**
13
+ * Connection closed.
14
+ */
15
+ onClosed?: (err?: Error) => void;
16
+ }
7
17
  /**
8
18
  * State machine for each connection.
9
19
  */
@@ -43,6 +53,7 @@ export declare class Connection {
43
53
  private readonly _signalMessaging;
44
54
  private readonly _protocol;
45
55
  private readonly _transportFactory;
56
+ private readonly _callbacks?;
46
57
  private readonly _ctx;
47
58
  private _state;
48
59
  private _transport;
@@ -55,7 +66,7 @@ export declare class Connection {
55
66
  private readonly _signalSendTask;
56
67
  private _signallingDelay;
57
68
  constructor(topic: PublicKey, ownId: PublicKey, // TODO(burdon): peerID?
58
- remoteId: PublicKey, sessionId: PublicKey, initiator: boolean, _signalMessaging: SignalMessenger, _protocol: WireProtocol, _transportFactory: TransportFactory);
69
+ remoteId: PublicKey, sessionId: PublicKey, initiator: boolean, _signalMessaging: SignalMessenger, _protocol: WireProtocol, _transportFactory: TransportFactory, _callbacks?: ConnectionCallbacks | undefined);
59
70
  get state(): ConnectionState;
60
71
  get transport(): Transport | undefined;
61
72
  get protocol(): WireProtocol;
@@ -72,4 +83,5 @@ export declare class Connection {
72
83
  signal(msg: SignalMessage): Promise<void>;
73
84
  private _changeState;
74
85
  }
86
+ export {};
75
87
  //# sourceMappingURL=connection.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../../../src/swarm/connection.ts"],"names":[],"mappings":"AAIA,OAAO,EAAgB,KAAK,EAAuB,MAAM,aAAa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
2
4
  import { Answer } from '@dxos/protocols/proto/dxos/mesh/swarm';
3
5
  import { OfferMessage, SignalMessage, SignalMessenger } from '../signal';
@@ -5,6 +7,9 @@ import { TransportFactory } from '../transport';
5
7
  import { WireProtocolProvider } from '../wire-protocol';
6
8
  import { Connection } from './connection';
7
9
  import { ConnectionLimiter } from './connection-limiter';
10
+ export declare class ConnectionDisplacedError extends SystemError {
11
+ constructor();
12
+ }
8
13
  interface PeerCallbacks {
9
14
  /**
10
15
  * Connection attempt initiated.
@@ -62,6 +67,7 @@ export declare class Peer {
62
67
  */
63
68
  advertizing: boolean;
64
69
  initiating: boolean;
70
+ readonly connectionDisplaced: Event<Connection>;
65
71
  constructor(id: PublicKey, topic: PublicKey, localPeerId: PublicKey, _signalMessaging: SignalMessenger, _protocolProvider: WireProtocolProvider, _transportFactory: TransportFactory, _connectionLimiter: ConnectionLimiter, _callbacks: PeerCallbacks);
66
72
  /**
67
73
  * Respond to remote offer.
@@ -1 +1 @@
1
- {"version":3,"file":"peer.d.ts","sourceRoot":"","sources":["../../../../src/swarm/peer.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAAE,MAAM,EAAE,MAAM,uCAAuC,CAAC;AAE/D,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AACzE,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAE,UAAU,EAAmB,MAAM,cAAc,CAAC;AAC3D,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAEzD,UAAU,aAAa;IACrB;;OAEG;IACH,WAAW,EAAE,CAAC,UAAU,EAAE,UAAU,KAAK,IAAI,CAAC;IAE9C;;OAEG;IACH,WAAW,EAAE,MAAM,IAAI,CAAC;IAExB;;OAEG;IACH,cAAc,EAA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,QAAQ,CAAC,gBAAgB;IACjC,OAAO,CAAC,QAAQ,CAAC,iBAAiB;IAClC,OAAO,CAAC,QAAQ,CAAC,iBAAiB;IAClC,OAAO,CAAC,QAAQ,CAAC,kBAAkB;IACnC,OAAO,CAAC,QAAQ,CAAC,UAAU;IA9B7B;;OAEG;IACH,OAAO,CAAC,eAAe,CAAK;IACrB,kBAAkB,UAAQ;IACjC,OAAO,CAAC,mBAAmB,CAAC,CAAS;IAErC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAiB;IACtC,OAAO,CAAC,cAAc,CAAC,CAAU;IAE1B,UAAU,CAAC,EAAE,UAAU,CAAC;IAE/B;;OAEG;IACI,WAAW,UAAS;IAEpB,UAAU,UAAS;IAE1B,SAAgB,mBAAmB,oBAA2B;gBAI5C,EAAE,EAAE,SAAS,EACb,KAAK,EAAE,SAAS,EAChB,WAAW,EAAE,SAAS,EACrB,gBAAgB,EAAE,eAAe,EACjC,iBAAiB,EAAE,oBAAoB,EACvC,iBAAiB,EAAE,gBAAgB,EACnC,kBAAkB,EAAE,iBAAiB,EACrC,UAAU,EAAE,aAAa;IAG5C;;OAEG;IACG,OAAO,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC;IAiDrD;;OAEG;IACG,kBAAkB;IAyCxB;;;OAGG;IACH,OAAO,CAAC,iBAAiB;IAsGnB,eAAe,CAAC,GAAG,CAAC,EAAE,KAAK;IAgB3B,QAAQ,CAAC,OAAO,EAAE,aAAa;IAU/B,OAAO;CAOd"}
@@ -24,6 +24,7 @@ export declare class Swarm {
24
24
  private readonly _transportFactory;
25
25
  private readonly _label;
26
26
  private readonly _connectionLimiter;
27
+ private readonly _initiationDelay;
27
28
  private readonly _swarmMessenger;
28
29
  private _ctx;
29
30
  private _listeningHandle?;
@@ -32,7 +33,7 @@ export declare class Swarm {
32
33
  */
33
34
  readonly _instanceId: string;
34
35
  readonly errors: ErrorStream;
35
- constructor(_topic: PublicKey, _ownPeerId: PublicKey, _topology: Topology, _protocolProvider: WireProtocolProvider, _messenger: Messenger, _transportFactory: TransportFactory, _label: string | undefined, _connectionLimiter: ConnectionLimiter);
36
+ constructor(_topic: PublicKey, _ownPeerId: PublicKey, _topology: Topology, _protocolProvider: WireProtocolProvider, _messenger: Messenger, _transportFactory: TransportFactory, _label: string | undefined, _connectionLimiter: ConnectionLimiter, _initiationDelay?: number);
36
37
  get connections(): Connection[];
37
38
  get ownPeerId(): PublicKey;
38
39
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"swarm.d.ts","sourceRoot":"","sources":["../../../../src/swarm/swarm.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAAmB,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE7D,OAAO,EAAE,UAAU,EAAE,MAAM,wCAAwC,CAAC;AACpE,OAAO,EAAE,MAAM,EAAE,MAAM,uCAAuC,CAAC;AAG/D,OAAO,EAAkB,YAAY,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AACxE,OAAO,EAAmB,QAAQ,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AACjC,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAE,UAAU,EAAmB,MAAM,cAAc,CAAC;AAC3D,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAQzD;;;;GAIG;AACH,qBAAa,KAAK;IA0Cd,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,UAAU;IAC3B,OAAO,CAAC,SAAS;IACjB,OAAO,CAAC,QAAQ,CAAC,iBAAiB;IAClC,OAAO,CAAC,QAAQ,CAAC,UAAU;IAC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,EAAE,SAAS,EACrB,iBAAiB,EAAE,gBAAgB,EACnC,MAAM,EAAE,MAAM,GAAG,SAAS,EAC1B,kBAAkB,EAAE,iBAAiB,EACrC,gBAAgB,SAAmB;IAkBtD,IAAI,WAAW,iBAId;IAED,IACI,SAAS,cAEZ;IAED;;OAEG;IACH,IAAI,KAAK,IAAI,MAAM,GAAG,SAAS,CAE9B;IAED,IACI,KAAK,IAAI,KAAK,CAEjB;IAEK,IAAI;IAaJ,OAAO;IAWP,WAAW,CAAC,QAAQ,EAAE,QAAQ;IAiBpC,YAAY,CAAC,UAAU,EAAE,UAAU;IA8B7B,OAAO,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC;IAwB/C,QAAQ,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAmB/C,SAAS;IAOT,QAAQ;IAId,OAAO,CAAC,gBAAgB;YAkDV,YAAY;IAM1B,OAAO,CAAC,mBAAmB;IAuC3B;;OAEG;YACW,mBAAmB;YA0BnB,gBAAgB;CAQ/B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/network-manager",
3
- "version": "0.1.56",
3
+ "version": "0.1.57-main.281294d",
4
4
  "description": "Network Manager",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -25,22 +25,22 @@
25
25
  "simple-peer": "9.11.1",
26
26
  "ws": "^7.4.4",
27
27
  "xor-distance": "^2.0.0",
28
- "@dxos/async": "0.1.56",
29
- "@dxos/codec-protobuf": "0.1.56",
30
- "@dxos/context": "0.1.56",
31
- "@dxos/credentials": "0.1.56",
32
- "@dxos/crypto": "0.1.56",
33
- "@dxos/debug": "0.1.56",
34
- "@dxos/errors": "0.1.56",
35
- "@dxos/invariant": "0.1.56",
36
- "@dxos/keys": "0.1.56",
37
- "@dxos/log": "0.1.56",
38
- "@dxos/messaging": "0.1.56",
39
- "@dxos/node-std": "0.1.56",
40
- "@dxos/protocols": "0.1.56",
41
- "@dxos/rpc": "0.1.56",
42
- "@dxos/teleport": "0.1.56",
43
- "@dxos/util": "0.1.56"
28
+ "@dxos/async": "0.1.57-main.281294d",
29
+ "@dxos/codec-protobuf": "0.1.57-main.281294d",
30
+ "@dxos/context": "0.1.57-main.281294d",
31
+ "@dxos/credentials": "0.1.57-main.281294d",
32
+ "@dxos/crypto": "0.1.57-main.281294d",
33
+ "@dxos/debug": "0.1.57-main.281294d",
34
+ "@dxos/errors": "0.1.57-main.281294d",
35
+ "@dxos/invariant": "0.1.57-main.281294d",
36
+ "@dxos/keys": "0.1.57-main.281294d",
37
+ "@dxos/log": "0.1.57-main.281294d",
38
+ "@dxos/messaging": "0.1.57-main.281294d",
39
+ "@dxos/node-std": "0.1.57-main.281294d",
40
+ "@dxos/protocols": "0.1.57-main.281294d",
41
+ "@dxos/rpc": "0.1.57-main.281294d",
42
+ "@dxos/teleport": "0.1.57-main.281294d",
43
+ "@dxos/util": "0.1.57-main.281294d"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@swc-node/register": "1.5.4",
@@ -50,7 +50,7 @@
50
50
  "earljs": "~0.1.10",
51
51
  "fast-check": "~3.3.0",
52
52
  "typescript": "^5.0.4",
53
- "@dxos/signal": "0.1.56"
53
+ "@dxos/signal": "0.1.57-main.281294d"
54
54
  },
55
55
  "publishConfig": {
56
56
  "access": "public"
@@ -27,6 +27,18 @@ const STARTING_SIGNALLING_DELAY = 10;
27
27
  */
28
28
  const MAX_SIGNALLING_DELAY = 300;
29
29
 
30
+ interface ConnectionCallbacks {
31
+ /**
32
+ * Connection opened.
33
+ */
34
+ onConnected?: () => void;
35
+
36
+ /**
37
+ * Connection closed.
38
+ */
39
+ onClosed?: (err?: Error) => void;
40
+ }
41
+
30
42
  /**
31
43
  * State machine for each connection.
32
44
  */
@@ -92,6 +104,7 @@ export class Connection {
92
104
  private readonly _signalMessaging: SignalMessenger,
93
105
  private readonly _protocol: WireProtocol,
94
106
  private readonly _transportFactory: TransportFactory,
107
+ private readonly _callbacks?: ConnectionCallbacks,
95
108
  ) {
96
109
  log.trace('dxos.mesh.connection.construct', {
97
110
  sessionId: this.sessionId,
@@ -150,6 +163,7 @@ export class Connection {
150
163
 
151
164
  this._transport.connected.once(() => {
152
165
  this._changeState(ConnectionState.CONNECTED);
166
+ this._callbacks?.onConnected?.();
153
167
  });
154
168
 
155
169
  this._transport.closed.once(() => {
@@ -206,6 +220,7 @@ export class Connection {
206
220
 
207
221
  log('closed', { peerId: this.ownId });
208
222
  this._changeState(ConnectionState.CLOSED);
223
+ this._callbacks?.onClosed?.(err);
209
224
  }
210
225
 
211
226
  private _sendSignal(signal: Signal) {
package/src/swarm/peer.ts CHANGED
@@ -2,9 +2,9 @@
2
2
  // Copyright 2022 DXOS.org
3
3
  //
4
4
 
5
- import { scheduleTask, synchronized } from '@dxos/async';
5
+ import { Event, scheduleTask, synchronized } from '@dxos/async';
6
6
  import { Context } from '@dxos/context';
7
- import { CancelledError } from '@dxos/errors';
7
+ import { CancelledError, SystemError } from '@dxos/errors';
8
8
  import { invariant } from '@dxos/invariant';
9
9
  import { PublicKey } from '@dxos/keys';
10
10
  import { log } from '@dxos/log';
@@ -16,6 +16,12 @@ import { WireProtocolProvider } from '../wire-protocol';
16
16
  import { Connection, ConnectionState } from './connection';
17
17
  import { ConnectionLimiter } from './connection-limiter';
18
18
 
19
+ export class ConnectionDisplacedError extends SystemError {
20
+ constructor() {
21
+ super('Connection displaced by remote initiator.');
22
+ }
23
+ }
24
+
19
25
  interface PeerCallbacks {
20
26
  /**
21
27
  * Connection attempt initiated.
@@ -82,6 +88,8 @@ export class Peer {
82
88
 
83
89
  public initiating = false;
84
90
 
91
+ public readonly connectionDisplaced = new Event<Connection>();
92
+
85
93
  // TODO(burdon): Convert to map?
86
94
  constructor(
87
95
  public readonly id: PublicKey,
@@ -114,7 +122,7 @@ export class Peer {
114
122
 
115
123
  if (this.connection) {
116
124
  // Close our connection and accept remote peer's connection.
117
- await this.closeConnection(new Error('Connection displaced by remote initiator.'));
125
+ await this.closeConnection(new ConnectionDisplacedError());
118
126
  }
119
127
  } else {
120
128
  // Continue with our origination attempt, the remote peer will close it's connection and accept ours.
@@ -214,15 +222,8 @@ export class Peer {
214
222
  // TODO(dmaretskyi): Init only when connection is established.
215
223
  this._protocolProvider({ initiator, localPeerId: this.localPeerId, remotePeerId: this.id, topic: this.topic }),
216
224
  this._transportFactory,
217
- );
218
- this._callbacks.onInitiated(connection);
219
-
220
- void this._connectionCtx?.dispose();
221
- this._connectionCtx = this._ctx.derive();
222
-
223
- connection.stateChanged.on((state) => {
224
- switch (state) {
225
- case ConnectionState.CONNECTED: {
225
+ {
226
+ onConnected: () => {
226
227
  this.availableToConnect = true;
227
228
  this._lastConnectionTime = Date.now();
228
229
  this._callbacks.onConnected();
@@ -235,11 +236,13 @@ export class Peer {
235
236
  sessionId,
236
237
  initiator,
237
238
  });
238
- break;
239
- }
240
-
241
- case ConnectionState.CLOSED: {
239
+ },
240
+ onClosed: (err) => {
242
241
  log('connection closed', { topic: this.topic, peerId: this.localPeerId, remoteId: this.id, initiator });
242
+
243
+ // Make sure none of the connections are stuck in the limiter.
244
+ this._connectionLimiter.doneConnecting(sessionId);
245
+
243
246
  invariant(this.connection === connection, 'Connection mismatch (race condition).');
244
247
 
245
248
  log.trace('dxos.mesh.connection.closed', {
@@ -250,31 +253,37 @@ export class Peer {
250
253
  initiator,
251
254
  });
252
255
 
253
- if (this._lastConnectionTime && this._lastConnectionTime + CONNECTION_COUNTS_STABLE_AFTER < Date.now()) {
254
- // If we're closing the connection, and it has been connected for a while, reset the backoff.
255
- this._availableAfter = 0;
256
+ if (err instanceof ConnectionDisplacedError) {
257
+ this.connectionDisplaced.emit(this.connection);
256
258
  } else {
257
- this.availableToConnect = false;
258
- this._availableAfter = increaseInterval(this._availableAfter);
259
+ if (this._lastConnectionTime && this._lastConnectionTime + CONNECTION_COUNTS_STABLE_AFTER < Date.now()) {
260
+ // If we're closing the connection, and it has been connected for a while, reset the backoff.
261
+ this._availableAfter = 0;
262
+ } else {
263
+ this.availableToConnect = false;
264
+ this._availableAfter = increaseInterval(this._availableAfter);
265
+ }
266
+ this._callbacks.onDisconnected();
267
+
268
+ scheduleTask(
269
+ this._connectionCtx!,
270
+ () => {
271
+ this.availableToConnect = true;
272
+ this._callbacks.onPeerAvailable();
273
+ },
274
+ this._availableAfter,
275
+ );
259
276
  }
260
277
 
261
278
  this.connection = undefined;
262
- this._callbacks.onDisconnected();
263
- this._connectionLimiter.doneConnecting(sessionId);
279
+ },
280
+ },
281
+ );
282
+ this._callbacks.onInitiated(connection);
264
283
 
265
- scheduleTask(
266
- this._connectionCtx!,
267
- () => {
268
- this.availableToConnect = true;
269
- this._callbacks.onPeerAvailable();
270
- },
271
- this._availableAfter,
272
- );
284
+ void this._connectionCtx?.dispose();
285
+ this._connectionCtx = this._ctx.derive();
273
286
 
274
- break;
275
- }
276
- }
277
- });
278
287
  connection.errors.handle((err) => {
279
288
  log.warn('connection error', { topic: this.topic, peerId: this.localPeerId, remoteId: this.id, initiator, err });
280
289
  log.trace('dxos.mesh.connection.error', {
@@ -299,7 +308,9 @@ export class Peer {
299
308
  if (!this.connection) {
300
309
  return;
301
310
  }
311
+
302
312
  const connection = this.connection;
313
+
303
314
  log('closing...', { peerId: this.id, sessionId: connection.sessionId });
304
315
 
305
316
  // Triggers `onStateChange` callback which will clean up the connection.
@@ -33,11 +33,13 @@ describe('Swarm', () => {
33
33
  peerId = PublicKey.random(),
34
34
  connectionLimiter = new ConnectionLimiter(),
35
35
  signalManager = new MemorySignalManager(context),
36
+ initiationDelay = 100,
36
37
  }: {
37
38
  topic?: PublicKey;
38
39
  peerId?: PublicKey;
39
40
  connectionLimiter?: ConnectionLimiter;
40
41
  signalManager?: SignalManager;
42
+ initiationDelay?: number;
41
43
  }): Promise<TestPeer> => {
42
44
  const protocol = new TestWireProtocol(peerId);
43
45
  const swarm = new Swarm(
@@ -49,6 +51,7 @@ describe('Swarm', () => {
49
51
  createWebRTCTransportFactory(),
50
52
  undefined,
51
53
  connectionLimiter,
54
+ initiationDelay,
52
55
  );
53
56
 
54
57
  afterTest(async () => {
@@ -85,6 +88,24 @@ describe('Swarm', () => {
85
88
  await connectSwarms(peer1, peer2);
86
89
  }).timeout(5_000);
87
90
 
91
+ test('with simultaneous connections one of the peers drops initiated connection', async () => {
92
+ const topic = PublicKey.random();
93
+
94
+ const peerId1 = PublicKey.fromHex('39ba0e42');
95
+ const peerId2 = PublicKey.fromHex('7d2bc6ab');
96
+
97
+ const peer1 = await setupSwarm({ peerId: peerId1, topic, initiationDelay: 0 });
98
+ const peer2 = await setupSwarm({ peerId: peerId2, topic, initiationDelay: 0 });
99
+
100
+ expect(peer1.swarm.connections.length).to.equal(0);
101
+ expect(peer2.swarm.connections.length).to.equal(0);
102
+
103
+ const connectionDisplaced = peer2.swarm._peers.get(peerId1)?.connectionDisplaced.waitForCount(1);
104
+
105
+ await connectSwarms(peer1, peer2);
106
+ await asyncTimeout(connectionDisplaced!, 1000);
107
+ }).timeout(5_000);
108
+
88
109
  test('second peer discovered after delay', async () => {
89
110
  const topic = PublicKey.random();
90
111
 
@@ -83,6 +83,7 @@ export class Swarm {
83
83
  private readonly _transportFactory: TransportFactory,
84
84
  private readonly _label: string | undefined,
85
85
  private readonly _connectionLimiter: ConnectionLimiter,
86
+ private readonly _initiationDelay = INITIATION_DELAY,
86
87
  ) {
87
88
  log.trace(
88
89
  'dxos.mesh.swarm.constructor',
@@ -353,7 +354,7 @@ export class Swarm {
353
354
  // If our peerId is higher, we will wait for a bit so that other peer has a chance to connect first.
354
355
  if (remoteId.toHex() < this._ownPeerId.toHex()) {
355
356
  log('initiation delay', { remoteId });
356
- await sleep(INITIATION_DELAY);
357
+ await sleep(this._initiationDelay);
357
358
  }
358
359
  if (ctx.disposed) {
359
360
  return;