Merge pull request #4281 from janhq/fix/performance-issue-with-atom-storage

fix: performance issue with atom storage persistence
This commit is contained in:
Louis 2024-12-17 18:03:42 +07:00 committed by GitHub
commit 14b1e61576
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -19,11 +19,20 @@ const CHAT_MESSAGE_NAME = 'chatMessages'
/**
* Stores all chat messages for all threads
*/
export const chatMessages = atomWithStorage<Record<string, ThreadMessage[]>>(
CHAT_MESSAGE_NAME,
{},
undefined,
{ getOnInit: true }
export const chatMessagesStorage = atomWithStorage<
Record<string, ThreadMessage[]>
>(CHAT_MESSAGE_NAME, {}, undefined, { getOnInit: true })
export const cachedMessages = atom<Record<string, ThreadMessage[]>>()
/**
* Retrieve chat messages for all threads
*/
export const chatMessages = atom(
(get) => get(cachedMessages) ?? get(chatMessagesStorage),
(_get, set, newValue: Record<string, ThreadMessage[]>) => {
set(cachedMessages, newValue)
;(() => set(chatMessagesStorage, newValue))()
}
)
/**