From 613fdf85b7dc7148f4ade612805f9f3bd98f2268 Mon Sep 17 00:00:00 2001 From: Innei Date: Thu, 15 Aug 2024 17:08:18 +0800 Subject: [PATCH] fix: unfollow Signed-off-by: Innei --- src/renderer/src/store/subscription/store.ts | 2 +- src/renderer/src/store/utils/helper.ts | 51 ++++++++++++-------- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/renderer/src/store/subscription/store.ts b/src/renderer/src/store/subscription/store.ts index 4c35d5b24..177fd4a40 100644 --- a/src/renderer/src/store/subscription/store.ts +++ b/src/renderer/src/store/subscription/store.ts @@ -246,7 +246,7 @@ class SubscriptionActions { }) .then(() => feed), () => SubscriptionService.removeSubscription(whoami()!.id, feedId), - ) + ).then(() => feed) } async changeCategoryView( diff --git a/src/renderer/src/store/utils/helper.ts b/src/renderer/src/store/utils/helper.ts index 0847a2c6f..604248437 100644 --- a/src/renderer/src/store/utils/helper.ts +++ b/src/renderer/src/store/utils/helper.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-unsafe-finally */ import { runTransactionInScope } from "@renderer/database" import { unstable_batchedUpdates } from "react-dom" import type { StateCreator } from "zustand" @@ -89,33 +90,43 @@ export type MutationAndTranscationOptions = { */ doTranscationWhenMutationFail?: boolean } -export const doMutationAndTransaction = async ( - mutationFn: () => Promise, - transaction: () => Promise, +export const doMutationAndTransaction = async ( + mutationFn: () => Promise, + transaction: () => Promise, options?: MutationAndTranscationOptions, -) => { +): Promise<[M | null | void, T | null | void]> => { const isOnline = navigator.onLine const { waitMutation = false, doTranscationWhenMutationFail = !isOnline } = options || {} - const wrappedTransaction = () => - runTransactionInScope(() => unstable_batchedUpdates(() => transaction())) + const wrappedTransaction = () => { + const ret = runTransactionInScope(() => + unstable_batchedUpdates(() => transaction()), + ) + + if (ret instanceof Promise) { + return ret + } + return null + } const wrappedMutation = () => (isOnline ? mutationFn() : Promise.resolve()) if (waitMutation) { - let runOnce = false - await wrappedMutation() - .then(() => { - runOnce = true - return wrappedTransaction() - }) - .finally(() => { - if (runOnce) return - if (doTranscationWhenMutationFail) { - return wrappedTransaction() - } - return - }) + let runTransactionOnce = false + + try { + const mutationRet = await wrappedMutation() + runTransactionOnce = true + const transactionRet = await wrappedTransaction() + return [mutationRet, transactionRet] + } finally { + if (!runTransactionOnce && doTranscationWhenMutationFail) { + const transactionRet = await wrappedTransaction() + + return [null, transactionRet] + } + return [null, null] + } } else { - await Promise.all([wrappedMutation(), wrappedTransaction()]) + return await Promise.all([wrappedMutation(), wrappedTransaction()]) } }