1
0
mirror of https://git.sr.ht/~cadence/cloudtube synced 2024-11-25 17:07:30 +00:00
cloudtube/background/instances.js

46 lines
993 B
JavaScript
Raw Normal View History

const {request} = require("../utils/request")
2021-03-01 10:35:09 +00:00
class InstancesList {
constructor() {
this.list = []
this.inflight = null
this.update()
setInterval(() => {
this.update()
}, 60*60*1000)
}
2021-03-01 10:35:09 +00:00
/**
* Updates the list. Returns a promise of the new list. Called
* automatically.
*/
update() {
return this.inflight = request("https://api.invidious.io/instances.json?sort_by=health").then(res => res.json()).then(list => {
list = list.filter(i => i[1].type === "https").map(i => i[1].uri.replace(/\/+$/, ""))
this.list = list
this.inflight = null
return this.list
})
}
/**
* Return a promise of the list. If updates are in progress, wait
* for them.
*/
fetch() {
if (this.inflight) return this.inflight
else return Promise.resolve(this.list)
}
2021-03-01 10:35:09 +00:00
/**
* Return the current list, no matter whether it's been updated at all.
*/
get() {
return this.list
}
}
2021-03-01 10:35:09 +00:00
const instancesList = new InstancesList()
2021-03-01 10:35:09 +00:00
module.exports.instancesList = instancesList