Skip to content

Commit

Permalink
fix: long task, batch add entry into store (#43)
Browse files Browse the repository at this point in the history
* fix: long task, batch add entry into store

Signed-off-by: Innei <i@innei.in>

* chore: cleanup

Signed-off-by: Innei <i@innei.in>

---------

Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei authored Jun 6, 2024
1 parent 7a60e61 commit 9deadc6
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/renderer/src/store/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ interface EntryActions {

pageParam?: string
}) => Promise<InferResponseType<typeof apiClient.entries.$post>>
upsert: (feedId: string, entry: EntryModel) => void
upsertMany: (entries: EntryModel[]) => void

optimisticUpdate: (entryId: string, changed: Partial<EntryModel>) => void
optimisticUpdateManyByFeedId: (
feedId: string,
Expand Down Expand Up @@ -68,9 +69,7 @@ export const useEntryStore = createZustandStore<EntryState & EntryActions>(
const data = await res.json()

if (data.data) {
data.data.forEach((entry: EntryModel) => {
get().upsert(entry.feeds.id, entry)
})
get().upsertMany(data.data)
}
return data
},
Expand Down Expand Up @@ -112,14 +111,17 @@ export const useEntryStore = createZustandStore<EntryState & EntryActions>(
}),
)
},
upsert(feedId: string, entry: EntryModel) {

upsertMany(entries) {
set((state) =>
produce(state, (draft) => {
if (!draft.entries[feedId]) {
draft.entries[feedId] = []
for (const entry of entries) {
if (!draft.entries[entry.feeds.id]) {
draft.entries[entry.feeds.id] = []
}
draft.entries[entry.feeds.id].push(entry.entries.id)
draft.flatMapEntries[entry.entries.id] = entry
}
draft.entries[feedId].push(entry.entries.id)
draft.flatMapEntries[entry.entries.id] = entry
return draft
}),
)
Expand Down

0 comments on commit 9deadc6

Please sign in to comment.