diff --git a/src/lib/waku.ts b/src/lib/waku.ts index 7f3b473db8..bdd477190c 100644 --- a/src/lib/waku.ts +++ b/src/lib/waku.ts @@ -135,8 +135,18 @@ export class Waku { this.startKeepAlive(connection.remotePeer, pingKeepAlive, relayKeepAlive); }); + /** + * NOTE: Event is not being emitted on closing nor loosing a connection. + * @see https://github.com/libp2p/js-libp2p/issues/939 + * + * >This event will be triggered anytime we are disconnected from another peer, + * >regardless of the circumstances of that disconnection. + * >If we happen to have multiple connections to a peer, + * >this event will **only** be triggered when the last connection is closed. + * @see https://github.com/libp2p/js-libp2p/blob/bad9e8c0ff58d60a78314077720c82ae331cc55b/doc/API.md?plain=1#L2100 + */ libp2p.connectionManager.on("peer:disconnect", (connection: Connection) => { - // TODO: reconnect or stop; this event will only be triggered when the last connection is closed. + // TODO: (retry) reconnect or stop completely and emit own event this.stopKeepAlive(connection.remotePeer); }); @@ -284,15 +294,7 @@ export class Waku { } async stop(): Promise { - for (const timer of [ - ...Object.values(this.pingKeepAliveTimers), - ...Object.values(this.relayKeepAliveTimers), - ]) { - clearInterval(timer); - } - this.pingKeepAliveTimers = {}; - this.relayKeepAliveTimers = {}; - + this.stopAllKeepAlives(); await this.libp2p.stop(); } @@ -446,6 +448,18 @@ export class Waku { delete this.relayKeepAliveTimers[peerIdStr]; } } + + private stopAllKeepAlives(): void { + for (const timer of [ + ...Object.values(this.pingKeepAliveTimers), + ...Object.values(this.relayKeepAliveTimers), + ]) { + clearInterval(timer); + } + + this.pingKeepAliveTimers = {}; + this.relayKeepAliveTimers = {}; + } } const awaitTimeout = (ms: number, rejectReason: string): Promise =>