From 15b8d79567fdeb84434c0be93ffeb81b47781735 Mon Sep 17 00:00:00 2001 From: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Date: Thu, 19 Jun 2025 17:38:52 +0800 Subject: [PATCH] fix: local data update for batchUpdateSubscription --- .../internal/store/src/subscription/store.ts | 147 ++++++++++-------- 1 file changed, 82 insertions(+), 65 deletions(-) diff --git a/packages/internal/store/src/subscription/store.ts b/packages/internal/store/src/subscription/store.ts index 505dfb8ef..1c2ec7d7b 100644 --- a/packages/internal/store/src/subscription/store.ts +++ b/packages/internal/store/src/subscription/store.ts @@ -379,23 +379,92 @@ class SubscriptionSyncService { async batchUpdateSubscription({ feedIds, - category, - view, + category: newCategory, + view: newView, }: { feedIds: string[] category?: string | null view: FeedViewType }) { - // TODO: handle local state update - await apiClient().subscriptions.batch.$patch({ - json: { - feedIds, - category, - view, - }, + const current = feedIds + .map((id) => get().data[id]) + .map((i) => + i + ? { + view: i.view, + category: i.category, + } + : null, + ) + + const tx = createTransaction() + tx.store(() => { + immerSet((draft) => { + for (const feedId of feedIds) { + const subscription = draft.data[feedId] + if (!subscription) continue + + const currentView = subscription.view + draft.feedIdByView[currentView].delete(feedId) + draft.feedIdByView[newView].add(feedId) + subscription.view = newView + + if (newCategory) { + const currentCategory = subscription.category + if (currentCategory) { + draft.categories[newView].delete(currentCategory) + } + draft.categories[newView].add(newCategory) + subscription.category = newCategory + } + } + }) }) - await this.fetch(view) + tx.request(async () => { + await apiClient().subscriptions.batch.$patch({ + json: { + feedIds, + category: newCategory, + view: newView, + }, + }) + }) + + tx.rollback(() => { + immerSet((draft) => { + for (const [index, feedId] of feedIds.entries()) { + const subscription = draft.data[feedId] + if (!subscription) continue + if (!current[index]) continue + + subscription.view = current[index].view + draft.feedIdByView[newView].delete(feedId) + draft.feedIdByView[current[index].view].add(feedId) + + if (newCategory) { + const currentCategory = current[index].category + draft.categories[newView].delete(newCategory) + if (currentCategory) { + draft.categories[current[index].view].add(currentCategory) + } + subscription.category = currentCategory + } + } + }) + }) + + tx.persist(() => { + return SubscriptionService.patchMany({ + feedIds, + data: { + view: newView, + category: newCategory, + }, + }) + }) + + await tx.run() } async changeListView({ listId, view }: { listId: string; view: FeedViewType }) { @@ -512,62 +581,10 @@ class SubscriptionSyncService { }) { const folderFeedIds = getCategoryFeedIds(category, currentView) - const tx = createTransaction() - tx.store(() => { - immerSet((draft) => { - for (const feedId of folderFeedIds) { - const subscription = draft.data[feedId] - if (!subscription) continue - subscription.view = newView - - draft.feedIdByView[currentView].delete(feedId) - draft.feedIdByView[newView].add(feedId) - - if (subscription.category) { - draft.categories[newView].add(subscription.category) - draft.categories[currentView].delete(subscription.category) - } - } - }) + await this.batchUpdateSubscription({ + feedIds: folderFeedIds, + view: newView, }) - - tx.rollback(() => { - immerSet((draft) => { - for (const feedId of folderFeedIds) { - const subscription = draft.data[feedId] - if (!subscription) continue - subscription.view = currentView - - draft.feedIdByView[newView].delete(feedId) - draft.feedIdByView[currentView].add(feedId) - - if (subscription.category) { - draft.categories[currentView].add(subscription.category) - draft.categories[newView].delete(subscription.category) - } - } - }) - }) - - tx.request(async () => { - await apiClient().subscriptions.batch.$patch({ - json: { - feedIds: folderFeedIds, - view: newView, - }, - }) - }) - - tx.persist(() => { - return SubscriptionService.patchMany({ - feedIds: folderFeedIds, - data: { - view: newView, - }, - }) - }) - - await tx.run() } async renameCategory({