From 9aa6c406c6e99d5f39b4b2b1ad768ed27cf06ad3 Mon Sep 17 00:00:00 2001 From: Xmader Date: Fri, 13 Nov 2020 00:54:20 -0500 Subject: [PATCH 1/3] feat: btns fallback to load from MSCZ file (`Individual Parts`) --- src/btn.ts | 19 +++++++++++++------ src/main.ts | 8 +++++--- src/utils.ts | 13 +++++++++++++ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/btn.ts b/src/btn.ts index 63b30c6..9571fee 100644 --- a/src/btn.ts +++ b/src/btn.ts @@ -1,5 +1,6 @@ import { loadMscore, WebMscore } from './mscore' +import { useTimeout } from './utils' import i18n from './i18n' // @ts-ignore import btnListCss from './btn.css' @@ -159,12 +160,18 @@ export namespace BtnAction { }) } - export const download = (url: UrlInput): BtnAction => { - return process(async (): Promise => { - const _url = await normalizeUrlInput(url) - const a = document.createElement('a') - a.href = _url - a.dispatchEvent(new MouseEvent('click')) + export const download = (url: UrlInput, fallback?: () => Promisable, timeout = 30 * 1000): BtnAction => { + return process(async (): Promise => { + try { + const _url = await useTimeout(normalizeUrlInput(url), timeout) + const a = document.createElement('a') + a.href = _url + a.dispatchEvent(new MouseEvent('click')) + } catch (err) { + // use fallback + console.error(err) + return fallback?.() + } }) } diff --git a/src/main.ts b/src/main.ts index 9cbce7c..d68aed8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -13,6 +13,8 @@ const main = (): void => { const btnList = new BtnList() const filename = scoreinfo.fileName + let indvPartBtn: HTMLButtonElement | null = null + btnList.add({ name: i18n('DOWNLOAD')('MSCZ'), action: BtnAction.process(downloadMscz), @@ -35,15 +37,15 @@ const main = (): void => { btnList.add({ name: i18n('DOWNLOAD')('MIDI'), - action: BtnAction.download(() => getFileUrl('midi')), + action: BtnAction.download(() => getFileUrl('midi'), () => indvPartBtn?.click()), }) btnList.add({ name: i18n('DOWNLOAD')('MP3'), - action: BtnAction.download(() => getFileUrl('mp3')), + action: BtnAction.download(() => getFileUrl('mp3'), () => indvPartBtn?.click()), }) - btnList.add({ + indvPartBtn = btnList.add({ name: i18n('IND_PARTS')(), tooltip: i18n('IND_PARTS_TOOLTIP')(), action: BtnAction.mscoreWindow(async (w, score, txt) => { diff --git a/src/utils.ts b/src/utils.ts index ee5b88e..2665295 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -19,6 +19,19 @@ export const fetchData = async (url: string, init?: RequestInit): Promise (promise: T | Promise, ms: number): Promise => { + if (!(promise instanceof Promise)) { + return promise + } + + return new Promise((resolve, reject) => { + const i = setTimeout(() => { + reject(new Error('timeout')) + }, ms) + promise.then(resolve, reject).finally(() => clearTimeout(i)) + }) +} + export const waitForDocumentLoaded = (): Promise => { if (document.readyState !== 'complete') { return new Promise(resolve => { From 837b960e884f59398d9d4c21da0eec9766d23598 Mon Sep 17 00:00:00 2001 From: Xmader Date: Fri, 13 Nov 2020 01:00:45 -0500 Subject: [PATCH 2/3] refactor: btns fallback --- src/btn.ts | 2 +- src/main.ts | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/btn.ts b/src/btn.ts index 9571fee..18d3336 100644 --- a/src/btn.ts +++ b/src/btn.ts @@ -160,7 +160,7 @@ export namespace BtnAction { }) } - export const download = (url: UrlInput, fallback?: () => Promisable, timeout = 30 * 1000): BtnAction => { + export const download = (url: UrlInput, fallback?: () => Promisable, timeout = Infinity): BtnAction => { return process(async (): Promise => { try { const _url = await useTimeout(normalizeUrlInput(url), timeout) diff --git a/src/main.ts b/src/main.ts index d68aed8..2aacb23 100644 --- a/src/main.ts +++ b/src/main.ts @@ -14,6 +14,11 @@ const main = (): void => { const filename = scoreinfo.fileName let indvPartBtn: HTMLButtonElement | null = null + const timeout = 30 * 1000 // 30s + const fallback = () => { + // btns fallback to load from MSCZ file (`Individual Parts`) + return indvPartBtn?.click() + } btnList.add({ name: i18n('DOWNLOAD')('MSCZ'), @@ -37,12 +42,12 @@ const main = (): void => { btnList.add({ name: i18n('DOWNLOAD')('MIDI'), - action: BtnAction.download(() => getFileUrl('midi'), () => indvPartBtn?.click()), + action: BtnAction.download(() => getFileUrl('midi'), fallback, timeout), }) btnList.add({ name: i18n('DOWNLOAD')('MP3'), - action: BtnAction.download(() => getFileUrl('mp3'), () => indvPartBtn?.click()), + action: BtnAction.download(() => getFileUrl('mp3'), fallback, timeout), }) indvPartBtn = btnList.add({ From f5e308b964105e1c42e9a6b41243d1fb3a6003a5 Mon Sep 17 00:00:00 2001 From: Xmader Date: Fri, 13 Nov 2020 01:15:01 -0500 Subject: [PATCH 3/3] refactor: btns fallback --- src/btn.ts | 30 +++++++++++++++--------------- src/main.ts | 7 +++---- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/btn.ts b/src/btn.ts index 18d3336..4d3e162 100644 --- a/src/btn.ts +++ b/src/btn.ts @@ -160,19 +160,13 @@ export namespace BtnAction { }) } - export const download = (url: UrlInput, fallback?: () => Promisable, timeout = Infinity): BtnAction => { + export const download = (url: UrlInput, fallback?: () => Promisable, timeout?: number): BtnAction => { return process(async (): Promise => { - try { - const _url = await useTimeout(normalizeUrlInput(url), timeout) - const a = document.createElement('a') - a.href = _url - a.dispatchEvent(new MouseEvent('click')) - } catch (err) { - // use fallback - console.error(err) - return fallback?.() - } - }) + const _url = await normalizeUrlInput(url) + const a = document.createElement('a') + a.href = _url + a.dispatchEvent(new MouseEvent('click')) + }, fallback, timeout) } export const mscoreWindow = (fn: (w: Window, score: WebMscore, processingTextEl: ChildNode) => any): BtnAction => { @@ -206,7 +200,7 @@ export namespace BtnAction { } } - export const process = (fn: () => any): BtnAction => { + export const process = (fn: () => any, fallback?: () => Promisable, timeout = Infinity): BtnAction => { return async (name, btn, setText): Promise => { const _onclick = btn.onclick @@ -214,11 +208,17 @@ export namespace BtnAction { setText(i18n('PROCESSING')()) try { - await fn() + await useTimeout(fn(), timeout) setText(name) } catch (err) { - setText(i18n('BTN_ERROR')()) console.error(err) + if (fallback) { + // use fallback + await fallback() + setText(name) + } else { + setText(i18n('BTN_ERROR')()) + } } btn.onclick = _onclick diff --git a/src/main.ts b/src/main.ts index 2aacb23..060e062 100644 --- a/src/main.ts +++ b/src/main.ts @@ -14,7 +14,6 @@ const main = (): void => { const filename = scoreinfo.fileName let indvPartBtn: HTMLButtonElement | null = null - const timeout = 30 * 1000 // 30s const fallback = () => { // btns fallback to load from MSCZ file (`Individual Parts`) return indvPartBtn?.click() @@ -27,7 +26,7 @@ const main = (): void => { btnList.add({ name: i18n('DOWNLOAD')('PDF'), - action: BtnAction.process(downloadPDF), + action: BtnAction.process(downloadPDF, fallback, 3 * 60 * 1000 /* 3min */), }) btnList.add({ @@ -42,12 +41,12 @@ const main = (): void => { btnList.add({ name: i18n('DOWNLOAD')('MIDI'), - action: BtnAction.download(() => getFileUrl('midi'), fallback, timeout), + action: BtnAction.download(() => getFileUrl('midi'), fallback, 30 * 1000 /* 30s */), }) btnList.add({ name: i18n('DOWNLOAD')('MP3'), - action: BtnAction.download(() => getFileUrl('mp3'), fallback, timeout), + action: BtnAction.download(() => getFileUrl('mp3'), fallback, 30 * 1000 /* 30s */), }) indvPartBtn = btnList.add({