diff --git a/src/index.ts b/src/index.ts index 56084ac..255fcf4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -123,16 +123,22 @@ export default class DHT { } private async fillConnections(): Promise { - let available = [...this._relays.keys()].filter( - (x) => ![...this._activeRelays.keys()].includes(x) - ); + let available: string[] = []; + + const updateAvailable = () => { + available = [...this._relays.keys()].filter( + (x) => ![...this._activeRelays.keys()].includes(x) + ); + }; + updateAvailable(); + let relayPromises = []; if (0 === available.length) { return; } while ( this._activeRelays.size <= - Math.min(this._maxConnections, available.length + this._activeRelays.size) + Math.min(this._maxConnections, available.length) ) { const relayIndex = await randomNumber(0, available.length - 1);