From 665bca00ea7967d6642876631b66e1af1ceea4fd Mon Sep 17 00:00:00 2001 From: nyagami Date: Thu, 18 Jan 2024 23:16:09 +0700 Subject: [PATCH] fix: regex freeze app --- src/hooks/persisted/useDownload.ts | 69 +++++++++---------- .../SettingsGeneralScreen.tsx | 2 - src/utils/fetch/fetch.ts | 9 ++- 3 files changed, 38 insertions(+), 42 deletions(-) diff --git a/src/hooks/persisted/useDownload.ts b/src/hooks/persisted/useDownload.ts index de18418a..f885a771 100644 --- a/src/hooks/persisted/useDownload.ts +++ b/src/hooks/persisted/useDownload.ts @@ -23,50 +23,43 @@ interface TaskData { const downloadChapterAction = async (taskData?: TaskData) => { try { MMKVStorage.set(BACKGROUND_ACTION, BackgoundAction.DOWNLOAD_CHAPTER); - while (true) { - let queue = getMMKVObject(DOWNLOAD_QUEUE) || []; - if (queue.length === 0) { - await Notifications.scheduleNotificationAsync({ + let queue = getMMKVObject(DOWNLOAD_QUEUE) || []; + while (queue.length > 0) { + const { novel, chapter } = queue[0]; + await BackgroundService.updateNotification({ + taskTitle: `Downloading: ${novel.name}`, + taskDesc: `Chapter: ${chapter.name}`, + }); + await downloadChapter( + novel.pluginId, + novel.id, + chapter.id, + chapter.url, + ).catch((error: Error) => + Notifications.scheduleNotificationAsync({ content: { - title: 'Downloader', - body: 'Download completed', + title: chapter.name, + body: `Download failed: ${error.message}`, }, trigger: null, - }); - break; - } else { - const { novel, chapter } = queue[0]; - await BackgroundService.updateNotification({ - taskTitle: `Downloading: ${novel.name}`, - taskDesc: `Chapter: ${chapter.name}`, - }); - try { - await downloadChapter( - novel.pluginId, - novel.id, - chapter.id, - chapter.url, - ); - } catch (error: any) { - Notifications.scheduleNotificationAsync({ - content: { - title: chapter.name, - body: `Download failed: ${error.message}`, - }, - trigger: null, - }); - } finally { - // get the newtest queue; - queue = getMMKVObject(DOWNLOAD_QUEUE) || []; - setMMKVObject(DOWNLOAD_QUEUE, queue.slice(1)); - } - - if (queue.length > 1 && taskData) { - await sleep(taskData?.delay); - } + }), + ); + // get the newtest queue; + queue = getMMKVObject(DOWNLOAD_QUEUE) || []; + setMMKVObject(DOWNLOAD_QUEUE, queue.slice(1)); + queue = getMMKVObject(DOWNLOAD_QUEUE) || []; + if (taskData) { + await sleep(taskData?.delay); } } } finally { + await Notifications.scheduleNotificationAsync({ + content: { + title: 'Downloader', + body: 'Download completed', + }, + trigger: null, + }); MMKVStorage.delete(BACKGROUND_ACTION); BackgroundService.stop(); } diff --git a/src/screens/settings/SettingsGeneralScreen/SettingsGeneralScreen.tsx b/src/screens/settings/SettingsGeneralScreen/SettingsGeneralScreen.tsx index 2ec08f18..e71f72a2 100644 --- a/src/screens/settings/SettingsGeneralScreen/SettingsGeneralScreen.tsx +++ b/src/screens/settings/SettingsGeneralScreen/SettingsGeneralScreen.tsx @@ -49,8 +49,6 @@ const GenralSettings: React.FC = ({ navigation }) => { ['lastReadAt', 'libraryScreen.bottomSheet.sortOrders.lastRead'], ['lastUpdatedAt', 'libraryScreen.bottomSheet.sortOrders.lastUpdated'], ]); - console.log(sortOrderNameMap.get(sortOrderDisplay[0])); - const { updateLibraryOnLaunch, downloadNewChapters, diff --git a/src/utils/fetch/fetch.ts b/src/utils/fetch/fetch.ts index d49211df..8c5b7efe 100644 --- a/src/utils/fetch/fetch.ts +++ b/src/utils/fetch/fetch.ts @@ -35,6 +35,9 @@ export const fetchApi = async ( return await fetch(url, init); }; +const FILE_READER_PREFIX_LENGTH = 'data:application/octet-stream;base64,' + .length; + export const fetchFile = async (url: string, init: any): Promise => { if (!init) { init = {}; @@ -45,11 +48,13 @@ export const fetchFile = async (url: string, init: any): Promise => { throw new Error(); } const blob = await res.blob(); - return new Promise(resolve => { + return await new Promise((resolve, reject) => { const fr = new FileReader(); fr.onloadend = () => { - resolve(fr.result.toString().replace(/data:.+\/.+;base64,/, '')); + resolve(fr.result.slice(FILE_READER_PREFIX_LENGTH) as string); }; + fr.onerror = () => reject(); + fr.onabort = () => reject(); fr.readAsDataURL(blob); }); } catch (e) {