767: Clear all timers on `Waku.stop` r=D4nte a=felicio

Resolves https://github.com/status-im/js-waku/issues/766

Co-authored-by: Felicio Mununga <felicio@users.noreply.github.com>
This commit is contained in:
status-bors-ng[bot] 2022-05-30 07:25:17 +00:00 committed by GitHub
commit 6df972694c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 2 deletions

View File

@ -100,7 +100,8 @@
"webfonts", "webfonts",
"websockets", "websockets",
"wifi", "wifi",
"xsalsa20" "xsalsa20",
"Alives"
], ],
"flagWords": [], "flagWords": [],
"ignorePaths": [ "ignorePaths": [

View File

@ -138,6 +138,17 @@ export class Waku {
this.startKeepAlive(connection.remotePeer, pingKeepAlive, relayKeepAlive); this.startKeepAlive(connection.remotePeer, pingKeepAlive, relayKeepAlive);
}); });
/**
* NOTE: Event is not being emitted on closing nor losing a connection.
* @see https://github.com/libp2p/js-libp2p/issues/939
* @see https://github.com/status-im/js-waku/issues/252
*
* >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) => { libp2p.connectionManager.on("peer:disconnect", (connection: Connection) => {
this.stopKeepAlive(connection.remotePeer); this.stopKeepAlive(connection.remotePeer);
}); });
@ -296,7 +307,8 @@ export class Waku {
} }
async stop(): Promise<void> { async stop(): Promise<void> {
return this.libp2p.stop(); this.stopAllKeepAlives();
await this.libp2p.stop();
} }
/** /**
@ -460,6 +472,18 @@ export class Waku {
delete this.relayKeepAliveTimers[peerIdStr]; 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<void> => const awaitTimeout = (ms: number, rejectReason: string): Promise<void> =>