From 3cb573681f0c20f4659ccddf2a4615bb4af697aa Mon Sep 17 00:00:00 2001 From: Marcin Majewski Date: Sat, 26 Nov 2022 02:18:53 +0100 Subject: [PATCH] fix(delete_chapter): Excessive number of pending callbacks warn (#481) * fix(delete_chapter): fix delete all not working Sometimes there would be "Excessive number of pending callbacks" warn shown. Removing chapters in batches of 10 (I tried 30 but it was still an issue) seems to work fine now. TODO: Add some icon about deletion status and make it not lag the entire app. * fix(delete_chapter): always-coccurent deletion Make deleting chapters do 3 coccurent deletion at a time making it work similarly to bluebird's `Promise.map(,{coccurent:3})` and removing the `Excessive number of pending callbacks` warn gone. * Remove unnecesarry console.log --- src/database/queries/ChapterQueries.ts | 2 +- src/redux/novel/novel.actions.js | 59 ++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/database/queries/ChapterQueries.ts b/src/database/queries/ChapterQueries.ts index c48dbf12..8ed85f7d 100644 --- a/src/database/queries/ChapterQueries.ts +++ b/src/database/queries/ChapterQueries.ts @@ -379,7 +379,7 @@ const deleteDownloadedImages = async ( } } } catch (error) { - showToast(error.message); + showToast((error as Error).message); } }; diff --git a/src/redux/novel/novel.actions.js b/src/redux/novel/novel.actions.js index ca2ae3a6..389b5bdc 100644 --- a/src/redux/novel/novel.actions.js +++ b/src/redux/novel/novel.actions.js @@ -444,16 +444,57 @@ export const deleteChapterAction = */ export const deleteAllChaptersAction = (sourceId, chapters) => async dispatch => { - await Promise.all( - chapters.map(async chapter => { - await deleteChapter(sourceId, chapter.novelId, chapter.chapterId); + await new Promise((res, rej) => { + (async () => { + const downloaded = chapters.filter(f => !!f.downloaded); + if (downloaded.length === 0) { + res(); + } - dispatch({ - type: CHAPTER_DELETED, - payload: chapter.chapterId, - }); - }), - ); + const coccurent = 5; + /** + * @type {Promise[][]} + */ + const removeBatches = []; + for (let i = 0; i < downloaded.length; i++) { + if (i % coccurent === 0) { + removeBatches.push([]); + } + removeBatches[removeBatches.length - 1].push( + new Promise(resolveRemoval => { + (async () => { + const chapter = downloaded[i]; + await deleteChapter( + sourceId, + chapter.novelId, + chapter.chapterId, + ); + + dispatch({ + type: CHAPTER_DELETED, + payload: chapter.chapterId, + }); + resolveRemoval(); + })(); + }), + ); + } + for (const rb of removeBatches) { + await Promise.all(rb); + } + res(); + })(); + }); + // await Promise.all( + // chapters.map(async chapter => { + // await deleteChapter(sourceId, chapter.novelId, chapter.chapterId); + + // dispatch({ + // type: CHAPTER_DELETED, + // payload: chapter.chapterId, + // }); + // }), + // ); showToast('Chapters deleted'); };