fix: Streams slowing down page loads or not loading at all (#620)
This commit is contained in:
parent
e9b1f17235
commit
b6f0bd356a
12 changed files with 62 additions and 47 deletions
|
@ -4,7 +4,7 @@ import type { PaginatorState } from '~/types'
|
|||
|
||||
export function usePaginator<T>(
|
||||
paginator: Paginator<any, T[]>,
|
||||
stream?: WsEvents,
|
||||
stream?: Promise<WsEvents>,
|
||||
eventType: 'notification' | 'update' = 'update',
|
||||
preprocess: (items: T[]) => T[] = (items: T[]) => items,
|
||||
) {
|
||||
|
@ -24,32 +24,34 @@ export function usePaginator<T>(
|
|||
prevItems.value = []
|
||||
}
|
||||
|
||||
stream?.on(eventType, (status) => {
|
||||
if ('uri' in status)
|
||||
stream?.then((s) => {
|
||||
s.on(eventType, (status) => {
|
||||
if ('uri' in status)
|
||||
cacheStatus(status, undefined, true)
|
||||
|
||||
const index = prevItems.value.findIndex((i: any) => i.id === status.id)
|
||||
if (index >= 0)
|
||||
prevItems.value.splice(index, 1)
|
||||
|
||||
prevItems.value.unshift(status as any)
|
||||
})
|
||||
|
||||
// TODO: update statuses
|
||||
s.on('status.update', (status) => {
|
||||
cacheStatus(status, undefined, true)
|
||||
|
||||
const index = prevItems.value.findIndex((i: any) => i.id === status.id)
|
||||
if (index >= 0)
|
||||
prevItems.value.splice(index, 1)
|
||||
const index = items.value.findIndex((s: any) => s.id === status.id)
|
||||
if (index >= 0)
|
||||
items.value[index] = status as any
|
||||
})
|
||||
|
||||
prevItems.value.unshift(status as any)
|
||||
})
|
||||
s.on('delete', (id) => {
|
||||
removeCachedStatus(id)
|
||||
|
||||
// TODO: update statuses
|
||||
stream?.on('status.update', (status) => {
|
||||
cacheStatus(status, undefined, true)
|
||||
|
||||
const index = items.value.findIndex((s: any) => s.id === status.id)
|
||||
if (index >= 0)
|
||||
items.value[index] = status as any
|
||||
})
|
||||
|
||||
stream?.on('delete', (id) => {
|
||||
removeCachedStatus(id)
|
||||
|
||||
const index = items.value.findIndex((s: any) => s.id === id)
|
||||
if (index >= 0)
|
||||
items.value.splice(index, 1)
|
||||
const index = items.value.findIndex((s: any) => s.id === id)
|
||||
if (index >= 0)
|
||||
items.value.splice(index, 1)
|
||||
})
|
||||
})
|
||||
|
||||
async function loadNext() {
|
||||
|
@ -95,9 +97,9 @@ export function usePaginator<T>(
|
|||
() => {
|
||||
if (
|
||||
isInScreen
|
||||
&& state.value === 'idle'
|
||||
// No new content is loaded when the keepAlive page enters the background
|
||||
&& deactivated.value === false
|
||||
&& state.value === 'idle'
|
||||
// No new content is loaded when the keepAlive page enters the background
|
||||
&& deactivated.value === false
|
||||
)
|
||||
loadNext()
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue