Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: performance issue with atom storage persistence #4281

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
/**
* 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),

Check warning on line 31 in web/helpers/atoms/ChatMessage.atom.ts

View workflow job for this annotation

GitHub Actions / coverage-check

31 line is not covered with tests
(_get, set, newValue: Record<string, ThreadMessage[]>) => {
set(cachedMessages, newValue)
;(() => set(chatMessagesStorage, newValue))()

Check warning on line 34 in web/helpers/atoms/ChatMessage.atom.ts

View workflow job for this annotation

GitHub Actions / coverage-check

33-34 lines are not covered with tests
}
)

/**
Expand All @@ -45,7 +54,7 @@
if (!activeThreadId) return []
const messages = get(chatMessages)[activeThreadId]
if (!Array.isArray(messages)) return []
return messages ?? []

Check warning on line 57 in web/helpers/atoms/ChatMessage.atom.ts

View workflow job for this annotation

GitHub Actions / coverage-check

57 line is not covered with tests
})

export const setConvoMessagesAtom = atom(
Expand All @@ -69,31 +78,31 @@
export const addOldMessagesAtom = atom(
null,
(get, set, newMessages: ThreadMessage[]) => {
const currentConvoId = get(getActiveThreadIdAtom)
if (!currentConvoId) return

Check warning on line 82 in web/helpers/atoms/ChatMessage.atom.ts

View workflow job for this annotation

GitHub Actions / coverage-check

81-82 lines are not covered with tests

const currentMessages = get(chatMessages)[currentConvoId] ?? []
const updatedMessages = [...currentMessages, ...newMessages]

Check warning on line 85 in web/helpers/atoms/ChatMessage.atom.ts

View workflow job for this annotation

GitHub Actions / coverage-check

84-85 lines are not covered with tests

const newData: Record<string, ThreadMessage[]> = {

Check warning on line 87 in web/helpers/atoms/ChatMessage.atom.ts

View workflow job for this annotation

GitHub Actions / coverage-check

87 line is not covered with tests
...get(chatMessages),
}
newData[currentConvoId] = updatedMessages
set(chatMessages, newData)

Check warning on line 91 in web/helpers/atoms/ChatMessage.atom.ts

View workflow job for this annotation

GitHub Actions / coverage-check

90-91 lines are not covered with tests
}
)

export const addNewMessageAtom = atom(
null,
(get, set, newMessage: ThreadMessage) => {
const currentMessages = get(chatMessages)[newMessage.thread_id] ?? []
const updatedMessages = [...currentMessages, newMessage]

Check warning on line 99 in web/helpers/atoms/ChatMessage.atom.ts

View workflow job for this annotation

GitHub Actions / coverage-check

98-99 lines are not covered with tests

const newData: Record<string, ThreadMessage[]> = {

Check warning on line 101 in web/helpers/atoms/ChatMessage.atom.ts

View workflow job for this annotation

GitHub Actions / coverage-check

101 line is not covered with tests
...get(chatMessages),
}
newData[newMessage.thread_id] = updatedMessages
set(chatMessages, newData)

Check warning on line 105 in web/helpers/atoms/ChatMessage.atom.ts

View workflow job for this annotation

GitHub Actions / coverage-check

104-105 lines are not covered with tests

// Update thread last message
if (newMessage.content.length)
Expand Down
Loading