Skip to content

Commit

Permalink
Merge pull request #857 from orfins/feat/switch-to-next-instead-of-ra…
Browse files Browse the repository at this point in the history
…ndom

switchInstance returns next instance instead of random
  • Loading branch information
ManeraKai committed Dec 8, 2023
2 parents 51ffdeb + d9121aa commit 5a105da
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/assets/javascripts/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/assets/javascripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ function getRandomInstance(instances) {
return instances[~~(instances.length * Math.random())]
}

/**
* @param {string} currentInstanceUrl
* @param {Array.<T>} 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
*/
Expand Down Expand Up @@ -197,6 +214,7 @@ function ping(href) {

export default {
getRandomInstance,
getNextInstance,
protocolHost,
getList,
getBlacklist,
Expand Down

0 comments on commit 5a105da

Please sign in to comment.