1
0
mirror of https://git.sr.ht/~cadence/cloudtube synced 2024-11-10 02:27:29 +00:00
cloudtube/background/instances.js
2021-03-01 23:35:09 +13:00

46 lines
993 B
JavaScript

const {request} = require("../utils/request")
class InstancesList {
constructor() {
this.list = []
this.inflight = null
this.update()
setInterval(() => {
this.update()
}, 60*60*1000)
}
/**
* 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)
}
/**
* Return the current list, no matter whether it's been updated at all.
*/
get() {
return this.list
}
}
const instancesList = new InstancesList()
module.exports.instancesList = instancesList