Skip to content

Commit 2d17a90

Browse files
AchoArnoldCopilot
andcommitted
perf(web): speed up thread switching
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 5bca4ca commit 2d17a90

1 file changed

Lines changed: 46 additions & 21 deletions

File tree

web/app/pages/threads/[id]/index.vue

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ const selectedMenuItem = ref(-1)
5252
const contactDialog = ref(false)
5353
const messageBody = ref<HTMLElement | null>(null)
5454
const form = ref<{ validate: () => Promise<{ valid: boolean }> } | null>(null)
55+
let messageRequestId = 0
56+
57+
threadsStore.setThreadId(route.params.id as string)
5558
5659
const formMessageRules = [
5760
(v: string) =>
@@ -128,8 +131,10 @@ function scrollToElement() {
128131
hideMessages.value = false
129132
}
130133
131-
async function resetCurrentThreadUnreadCount(force = false) {
132-
const threadId = route.params.id as string
134+
async function resetCurrentThreadUnreadCount(
135+
force = false,
136+
threadId = route.params.id as string,
137+
) {
133138
try {
134139
await threadsStore.resetThreadUnreadCount(threadId, force)
135140
} catch (error) {
@@ -154,36 +159,46 @@ async function handleInboundMessage() {
154159
}
155160
}
156161
157-
function loadMessages(hide = true, resetUnreadCount = true) {
162+
async function loadMessages(hide = true, resetUnreadCount = true) {
163+
const requestId = ++messageRequestId
158164
loadingMessages.value = true
159165
const threadId = route.params.id as string
160-
if (resetUnreadCount) void resetCurrentThreadUnreadCount()
161-
threadsStore
162-
.loadThreadMessages(threadId)
163-
.then((response: EntitiesMessage[]) => {
164-
messages.value = [...response].reverse()
165-
})
166-
.finally(() => {
167-
setTimeout(() => {
168-
loadingMessages.value = false
169-
}, 1100)
170-
})
171166
hideMessages.value = hide
172-
setTimeout(() => {
167+
168+
if (resetUnreadCount) {
169+
void resetCurrentThreadUnreadCount(false, threadId)
170+
}
171+
172+
try {
173+
const response = await threadsStore.loadThreadMessages(threadId)
174+
if (requestId !== messageRequestId) return
175+
176+
messages.value = [...response].reverse()
177+
await nextTick()
178+
if (requestId !== messageRequestId) return
179+
173180
scrollToElement()
174-
}, 950)
181+
} finally {
182+
if (requestId === messageRequestId) {
183+
loadingMessages.value = false
184+
}
185+
}
175186
}
176187
177188
async function loadData() {
178-
await authStore.loadUser()
179-
await phonesStore.loadPhones()
180-
await threadsStore.loadThreads()
189+
const threadId = route.params.id as string
181190
182-
if (!threadsStore.hasThreadId(route.params.id as string)) {
191+
if (!threadsStore.hasThreadId(threadId)) {
192+
if (!authStore.user) await authStore.loadUser()
193+
await phonesStore.loadPhones()
194+
await threadsStore.loadThreads()
195+
}
196+
197+
if (!threadsStore.hasThreadId(threadId)) {
183198
await router.push('/threads')
184199
return
185200
}
186-
loadMessages()
201+
await loadMessages()
187202
}
188203
189204
async function archiveThread() {
@@ -282,6 +297,16 @@ onMounted(async () => {
282297
})
283298
})
284299
300+
watch(
301+
() => route.params.id as string,
302+
(threadId, previousThreadId) => {
303+
if (!previousThreadId || threadId === previousThreadId) return
304+
305+
threadsStore.setThreadId(threadId)
306+
void loadMessages()
307+
},
308+
)
309+
285310
onBeforeUnmount(() => {
286311
if (webhookChannel) webhookChannel.unsubscribe()
287312
})

0 commit comments

Comments
 (0)