fix(mobile): no unread status update if no changes

This commit is contained in:
Stephen Zhou 2025-05-07 10:40:23 +08:00
parent 26a3d83027
commit f3313e68e2
No known key found for this signature in database
3 changed files with 30 additions and 20 deletions

View File

@ -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"]),
})
})
}
}

View File

@ -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<SubscriptionId, number>) {
const tx = createTransaction()
async upsertMany(
unreads: UnreadSchema[] | Record<SubscriptionId, number>,
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()
}

View File

@ -2,3 +2,7 @@ export interface PublishAtTimeRangeFilter {
startTime: number
endTime: number
}
export interface UnreadUpdateOptions {
reset?: boolean
}