forked from Mirrors/elk
refactor: optimize paginator typings
This commit is contained in:
parent
162b56f5cb
commit
01a24a6015
1 changed files with 11 additions and 9 deletions
|
@ -1,16 +1,16 @@
|
|||
import type { Paginator, WsEvents } from 'masto'
|
||||
import type { Paginator, WsEvents, mastodon } from 'masto'
|
||||
import type { PaginatorState } from '~/types'
|
||||
|
||||
export function usePaginator<T, P, U = T>(
|
||||
paginator: Paginator<T[], P>,
|
||||
stream?: Promise<WsEvents>,
|
||||
eventType: 'notification' | 'update' = 'update',
|
||||
preprocess: (items: T[]) => U[] = (items: T[]) => items as unknown as U[],
|
||||
preprocess: (items: T[]) => U[] = items => items as unknown as U[],
|
||||
buffer = 10,
|
||||
) {
|
||||
const state = ref<PaginatorState>(isMastoInitialised.value ? 'idle' : 'loading')
|
||||
const items = ref<T[]>([])
|
||||
const nextItems = ref<T[]>([])
|
||||
const items = ref<U[]>([])
|
||||
const nextItems = ref<U[]>([])
|
||||
const prevItems = ref<T[]>([])
|
||||
|
||||
const endAnchor = ref<HTMLDivElement>()
|
||||
|
@ -20,7 +20,7 @@ export function usePaginator<T, P, U = T>(
|
|||
const deactivated = useDeactivated()
|
||||
|
||||
async function update() {
|
||||
items.value.unshift(...preprocess(prevItems.value as any) as any)
|
||||
(items.value as U[]).unshift(...preprocess(prevItems.value as T[]))
|
||||
prevItems.value = []
|
||||
}
|
||||
|
||||
|
@ -40,17 +40,19 @@ export function usePaginator<T, P, U = T>(
|
|||
s.on('status.update', (status) => {
|
||||
cacheStatus(status, undefined, true)
|
||||
|
||||
const index = items.value.findIndex((s: any) => s.id === status.id)
|
||||
const data = items.value as mastodon.v1.Status[]
|
||||
const index = data.findIndex(s => s.id === status.id)
|
||||
if (index >= 0)
|
||||
items.value[index] = status as any
|
||||
data[index] = status
|
||||
})
|
||||
|
||||
s.on('delete', (id) => {
|
||||
removeCachedStatus(id)
|
||||
|
||||
const index = items.value.findIndex((s: any) => s.id === id)
|
||||
const data = items.value as mastodon.v1.Status[]
|
||||
const index = data.findIndex(s => s.id === id)
|
||||
if (index >= 0)
|
||||
items.value.splice(index, 1)
|
||||
data.splice(index, 1)
|
||||
})
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in a new issue