From d9121aa5d8fc3df647ca5fe40cbd168b5a62dfdb Mon Sep 17 00:00:00 2001 From: Or <> Date: Sat, 18 Nov 2023 17:40:35 +0200 Subject: [PATCH] switchInstance returns next instance instead of random --- src/assets/javascripts/services.js | 4 ++-- src/assets/javascripts/utils.js | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/assets/javascripts/services.js b/src/assets/javascripts/services.js index 7f00b7c..6fa6767 100644 --- a/src/assets/javascripts/services.js +++ b/src/assets/javascripts/services.js @@ -605,7 +605,7 @@ function switchInstance(url, customService) { if (customService) { const instancesList = options[options[customService].frontend] if (instancesList !== undefined) { - resolve(`${utils.getRandomInstance(instancesList)}${url.pathname}${url.search}`) + resolve(`${utils.getNextInstance(url.origin, instancesList)}${url.pathname}${url.search}`) } } else { for (const service in config.services) { @@ -618,7 +618,7 @@ function switchInstance(url, customService) { resolve() return } - resolve(`${utils.getRandomInstance(instancesList)}${url.pathname}${url.search}`) + resolve(`${utils.getNextInstance(url.origin, instancesList)}${url.pathname}${url.search}`) return } } diff --git a/src/assets/javascripts/utils.js b/src/assets/javascripts/utils.js index f7b9fd7..0cc5331 100644 --- a/src/assets/javascripts/utils.js +++ b/src/assets/javascripts/utils.js @@ -8,6 +8,23 @@ function getRandomInstance(instances) { return instances[~~(instances.length * Math.random())] } +/** + * @param {string} currentInstanceUrl + * @param {Array.} instances + * @returns {T} + */ +function getNextInstance(currentInstanceUrl, instances) { + const currentInstanceIndex = instances.indexOf(currentInstanceUrl); + + if (currentInstanceIndex === -1){ + return getRandomInstance(instances); + } + + const nextInstanceIndex = (currentInstanceIndex + 1) % instances.length; + + return instances[nextInstanceIndex]; +} + /** * @param {string} str */ @@ -195,6 +212,7 @@ function ping(href) { export default { getRandomInstance, + getNextInstance, protocolHost, getList, getBlacklist,