diff --git a/apps/mobile/src/services/unread.ts b/apps/mobile/src/services/unread.ts index 9686a66ce..85be3590e 100644 --- a/apps/mobile/src/services/unread.ts +++ b/apps/mobile/src/services/unread.ts @@ -2,6 +2,7 @@ import { db } from "../database" import { unreadTable } from "../database/schemas" import type { UnreadSchema } from "../database/schemas/types" import { unreadActions } from "../store/unread/store" +import type { UnreadUpdateOptions } from "../store/unread/types" import type { Hydratable, Resetable } from "./internal/base" import { conflictUpdateAllExcept } from "./internal/utils" @@ -14,15 +15,20 @@ class UnreadServiceStatic implements Hydratable, Resetable { unreadActions.upsertManyInSession(unreads) } - async upsertMany(unreads: UnreadSchema[]) { + async upsertMany(unreads: UnreadSchema[], options?: UnreadUpdateOptions) { if (unreads.length === 0) return - await db - .insert(unreadTable) - .values(unreads) - .onConflictDoUpdate({ - target: [unreadTable.subscriptionId], - set: conflictUpdateAllExcept(unreadTable, ["subscriptionId"]), - }) + await db.transaction(async (tx) => { + if (options?.reset) { + await tx.delete(unreadTable).execute() + } + await tx + .insert(unreadTable) + .values(unreads) + .onConflictDoUpdate({ + target: [unreadTable.subscriptionId], + set: conflictUpdateAllExcept(unreadTable, ["subscriptionId"]), + }) + }) } } diff --git a/apps/mobile/src/store/unread/store.ts b/apps/mobile/src/store/unread/store.ts index 87c46532c..fdf37f88a 100644 --- a/apps/mobile/src/store/unread/store.ts +++ b/apps/mobile/src/store/unread/store.ts @@ -12,7 +12,7 @@ import { createTransaction, createZustandStore } from "../internal/helper" import { getListFeedIds } from "../list/getters" import { getSubscriptionByView } from "../subscription/getter" import { getAllUnreadCount } from "./getter" -import type { PublishAtTimeRangeFilter } from "./types" +import type { PublishAtTimeRangeFilter, UnreadUpdateOptions } from "./types" type SubscriptionId = string interface UnreadStore { @@ -29,8 +29,7 @@ class UnreadSyncService { query: {}, }) - await unreadActions.reset() - await unreadActions.upsertMany(res.data) + await unreadActions.upsertMany(res.data, { reset: true }) return res.data } @@ -184,9 +183,9 @@ class UnreadSyncService { } class UnreadActions { - upsertManyInSession(unreads: UnreadSchema[]) { + upsertManyInSession(unreads: UnreadSchema[], options?: UnreadUpdateOptions) { const state = useUnreadStore.getState() - const nextData = { ...state.data } + const nextData = options?.reset ? {} : { ...state.data } for (const unread of unreads) { nextData[unread.subscriptionId] = unread.count } @@ -195,16 +194,17 @@ class UnreadActions { }) } - async upsertMany(unreads: UnreadSchema[] | Record) { - const tx = createTransaction() - + async upsertMany( + unreads: UnreadSchema[] | Record, + options?: UnreadUpdateOptions, + ) { const normalizedUnreads = Array.isArray(unreads) ? unreads : Object.entries(unreads).map(([subscriptionId, count]) => ({ subscriptionId, count })) - tx.store(() => this.upsertManyInSession(normalizedUnreads)) - tx.persist(() => { - return UnreadService.upsertMany(normalizedUnreads) - }) + + const tx = createTransaction() + tx.store(() => this.upsertManyInSession(normalizedUnreads, options)) + tx.persist(() => UnreadService.upsertMany(normalizedUnreads, options)) await tx.run() } diff --git a/apps/mobile/src/store/unread/types.ts b/apps/mobile/src/store/unread/types.ts index 79962ae25..d19df6b70 100644 --- a/apps/mobile/src/store/unread/types.ts +++ b/apps/mobile/src/store/unread/types.ts @@ -2,3 +2,7 @@ export interface PublishAtTimeRangeFilter { startTime: number endTime: number } + +export interface UnreadUpdateOptions { + reset?: boolean +}