fix: regex freeze app

This commit is contained in:
nyagami
2024-01-18 23:16:09 +07:00
parent 2ec6032673
commit 665bca00ea
3 changed files with 38 additions and 42 deletions
+31 -38
View File
@@ -23,50 +23,43 @@ interface TaskData {
const downloadChapterAction = async (taskData?: TaskData) => {
try {
MMKVStorage.set(BACKGROUND_ACTION, BackgoundAction.DOWNLOAD_CHAPTER);
while (true) {
let queue = getMMKVObject<DownloadData[]>(DOWNLOAD_QUEUE) || [];
if (queue.length === 0) {
await Notifications.scheduleNotificationAsync({
let queue = getMMKVObject<DownloadData[]>(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<DownloadData[]>(DOWNLOAD_QUEUE) || [];
setMMKVObject(DOWNLOAD_QUEUE, queue.slice(1));
}
if (queue.length > 1 && taskData) {
await sleep(taskData?.delay);
}
}),
);
// get the newtest queue;
queue = getMMKVObject<DownloadData[]>(DOWNLOAD_QUEUE) || [];
setMMKVObject(DOWNLOAD_QUEUE, queue.slice(1));
queue = getMMKVObject<DownloadData[]>(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();
}
@@ -49,8 +49,6 @@ const GenralSettings: React.FC<GenralSettingsProps> = ({ navigation }) => {
['lastReadAt', 'libraryScreen.bottomSheet.sortOrders.lastRead'],
['lastUpdatedAt', 'libraryScreen.bottomSheet.sortOrders.lastUpdated'],
]);
console.log(sortOrderNameMap.get(sortOrderDisplay[0]));
const {
updateLibraryOnLaunch,
downloadNewChapters,
+7 -2
View File
@@ -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<string> => {
if (!init) {
init = {};
@@ -45,11 +48,13 @@ export const fetchFile = async (url: string, init: any): Promise<string> => {
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) {