This repository has been archived on 2023-11-29. You can view files and clone it, but cannot push or open issues or pull requests.
Uloz.to-rychle/urlpool.ts

49 lines
925 B
TypeScript
Raw Normal View History

2022-02-23 19:50:47 +01:00
export class UrlPool {
urls: string[];
used: boolean[] = [];
constructor(urls: string[]) {
this.urls = urls;
for(let i=0; i<urls.length; i++) {
this.used[i] = false;
}
}
get_raw(): [number, string] | undefined {
for(let i=0; i<this.urls.length; i++) {
if(this.used[i]) continue;
this.used[i] = true;
return [i, this.urls[i]];
}
}
async get(): Promise<[number, string] | undefined> {
let url = this.get_raw();
if(url) return url;
return new Promise((complete) => {
let check = setInterval(() => {
let url = this.get_raw();
if(url) {
clearInterval(check);
complete(url);
}
}, 500);
});
}
return(i: number) {
this.used[i] = false;
}
available(count?: number) {
let available: number[] = [];
for(let i=0; i<this.used.length; i++) {
if(!this.used[i]) {
available.push(i);
}
}
return count ? available.slice(0, count) : available;
}
}