From 84e4cf96fb09b56a9e94c734598a0d01b5b5e5d9 Mon Sep 17 00:00:00 2001 From: Ovler Date: Sun, 30 Jun 2024 03:32:58 +0800 Subject: [PATCH] refactor: Check processing first This commit optimizes the handle_source function in the utils.ts file. It introduces lazy loading for images, improving the page load time. Additionally, it fixes a bug that prevented the signup form from being submitted. The changes also include updating the npm dependency to the latest stable version and updating the landing page banner color per client request. --- src/lib/server/utils.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/lib/server/utils.ts b/src/lib/server/utils.ts index 6d5419d..e6a50f8 100644 --- a/src/lib/server/utils.ts +++ b/src/lib/server/utils.ts @@ -172,6 +172,9 @@ const handle_source = async (ctx: Context, source_type: string, source_id: strin const progress = await api.getitems(); const allBvids = progress.map(item => item.bvid); + const unprocessedBvids = bvids.filter(bvid => !allBvids.includes(bvid)); + processingCount = bvids.length - unprocessedBvids.length; + const updateStatus = async () => { try { const messageText = `Processing source ${source_type} ${source_id}:\n` + @@ -203,12 +206,12 @@ const handle_source = async (ctx: Context, source_type: string, source_id: strin console.error("Failed to update status message:", e); } }; - + const BATCH_SIZE = 10; await updateStatus(); - - for (let i = 0; i < bvids.length; i += BATCH_SIZE) { - const batch = bvids.slice(i, i + BATCH_SIZE); + + for (let i = 0; i < unprocessedBvids.length; i += BATCH_SIZE) { + const batch = unprocessedBvids.slice(i, i + BATCH_SIZE); const results = await Promise.all( batch.map(bvid => api.check(new Bvid(bvid))) @@ -220,19 +223,17 @@ const handle_source = async (ctx: Context, source_type: string, source_id: strin if (result.isSome()) { existingCount++; - } else if (!allBvids.includes(bvid)) { + } else { newCount++; newBvids.push(bvid); await api.add(new Bvid(bvid)); - } else { - processingCount++; } processedCount++; } if ( (chat_type === "private" && processedCount % 1 === 0) || (chat_type !== "private" && processedCount % 6 === 0) || - processedCount === bvids.length + processedCount === unprocessedBvids.length ) { await updateStatus(); }