diff --git a/src/assets/javascripts/services.js b/src/assets/javascripts/services.js index 13219ab..d431369 100644 --- a/src/assets/javascripts/services.js +++ b/src/assets/javascripts/services.js @@ -618,7 +618,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) { @@ -631,7 +631,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 4026e18..fe08e57 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 */ @@ -197,6 +214,7 @@ function ping(href) { export default { getRandomInstance, + getNextInstance, protocolHost, getList, getBlacklist,