Skip to content

Commit

Permalink
Refactor the subtitle downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
dannvix committed Nov 13, 2021
1 parent ddc22a5 commit 44e7f74
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const console = {
log: (...args) => window.console.log(prefix, ...args),
warn: (...args) => window.console.warn(prefix, ...args),
error: (...args) => window.console.error(prefix, ...args),
debug: (...args) => window.console.debug(prefix, ...args),
};

module.exports = console;
19 changes: 7 additions & 12 deletions src/nflxmultisubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,15 @@ class SubtitleBase {
_download() {
if (!this.urls) return Promise.resolve();

console.log('Selecting fastest server, candidates: ',
console.debug('Selecting fastest server, candidates: ',
this.urls.map(u => u.substr(0, 24)));

let download_started = false;
return new Promise((resolve, reject) => {
this.urls.forEach(url => {
fetch(new Request(url), {method: 'HEAD'}).then(r => {
if (download_started) return;

download_started = true;
console.log('Fastest: ', url.substr(0, 24));
this._extract(fetch(url)).then(() => resolve());
});
});
return Promise.race(
this.urls.map(url => fetch(new Request(url), {method: 'HEAD'}))
).then(r => {
const url = r.url;
console.debug(`Fastest: ${url.substr(0, 24)}`);
return this._extract(fetch(url));
});
}

Expand Down

0 comments on commit 44e7f74

Please sign in to comment.