Skip to content

Commit

Permalink
refactor: Check processing first
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Ovler-Young committed Jun 29, 2024
1 parent 605d0c5 commit 84e4cf9
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/lib/server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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` +
Expand Down Expand Up @@ -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)))
Expand All @@ -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();
}
Expand Down

0 comments on commit 84e4cf9

Please sign in to comment.