From 44e7f74e7a20898edaa8e2ba5c8c6e4ca7afed36 Mon Sep 17 00:00:00 2001 From: Dan Chen Date: Sat, 13 Nov 2021 16:51:21 +0800 Subject: [PATCH] Refactor the subtitle downloader --- src/console.js | 1 + src/nflxmultisubs.js | 19 +++++++------------ 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/console.js b/src/console.js index c1aa7dc..3209f4e 100644 --- a/src/console.js +++ b/src/console.js @@ -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; diff --git a/src/nflxmultisubs.js b/src/nflxmultisubs.js index 8893e4a..b22bb19 100644 --- a/src/nflxmultisubs.js +++ b/src/nflxmultisubs.js @@ -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)); }); }