diff --git a/js/modules/loki_message_api.js b/js/modules/loki_message_api.js index ee446e09c..77a1eb0ea 100644 --- a/js/modules/loki_message_api.js +++ b/js/modules/loki_message_api.js @@ -119,8 +119,15 @@ class LokiMessageAPI { data: data64, }; const promises = []; + let completedConnections = 0; for (let i = 0; i < numConnections; i += 1) { - promises.push(this.openSendConnection(params)); + const connectionPromise = this.openSendConnection(params).finally(() => { + completedConnections += 1; + if (completedConnections >= numConnections) { + delete this.sendingSwarmNodes[timestamp]; + } + }); + promises.push(connectionPromise); } // Taken from https://stackoverflow.com/questions/51160260/clean-way-to-wait-for-first-true-returned-by-promise @@ -142,7 +149,6 @@ class LokiMessageAPI { let success; try { // eslint-disable-next-line more/no-then - Promise.all(promises).then(delete this.sendingSwarmNodes[timestamp]); success = await firstTrue(promises); } catch (e) { if (e instanceof textsecure.WrongDifficultyError) { diff --git a/js/modules/loki_snode_api.js b/js/modules/loki_snode_api.js index 1eea467ed..4a682d26f 100644 --- a/js/modules/loki_snode_api.js +++ b/js/modules/loki_snode_api.js @@ -86,7 +86,7 @@ class LokiSnodeAPI { ]; } - async initialiseRandomPool() { + async initialiseRandomPool(attempts = 0) { const params = { limit: 20, fields: { @@ -113,7 +113,10 @@ class LokiSnodeAPI { port: snode.storage_port, })); } catch (e) { - throw new window.textsecure.SeedNodeError('Failed to contact seed node'); + if (attempts >= window.seedNodeList.length) { + throw new window.textsecure.SeedNodeError('Failed to contact seed node'); + } + this.initialiseRandomPool(attempts + 1); } }