Skip to content

Commit

Permalink
Merge pull request janhq#4281 from janhq/fix/performance-issue-with-a…
Browse files Browse the repository at this point in the history
…tom-storage

fix: performance issue with atom storage persistence
  • Loading branch information
louis-jan authored Dec 17, 2024
2 parents f2db317 + 1395aa4 commit 14b1e61
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions web/helpers/atoms/ChatMessage.atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))()
}
)

/**
Expand Down

0 comments on commit 14b1e61

Please sign in to comment.