feat: avoid reordering pagination border effects (#877)

This commit is contained in:
patak 2023-01-08 17:04:26 +01:00 committed by GitHub
parent f8692ed480
commit efe406df5b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 19 additions and 10 deletions

View file

@ -6,6 +6,7 @@ export function usePaginator<T, P>(
stream?: Promise<WsEvents>,
eventType: 'notification' | 'update' = 'update',
preprocess: (items: T[]) => T[] = (items: T[]) => items,
buffer = 10,
) {
const state = ref<PaginatorState>(isMastoInitialised.value ? 'idle' : 'loading')
const items = ref<T[]>([])
@ -62,8 +63,10 @@ export function usePaginator<T, P>(
const result = await paginator.next()
if (result.value?.length) {
nextItems.value = preprocess(result.value) as any
items.value.push(...nextItems.value)
const preprocessedItems = preprocess([...nextItems.value, ...result.value]) as any
const itemsToShowCount = preprocessedItems.length - buffer
nextItems.value = preprocessedItems.slice(itemsToShowCount)
items.value.push(...preprocessedItems.slice(0, itemsToShowCount))
state.value = 'idle'
}
else {
@ -108,7 +111,6 @@ export function usePaginator<T, P>(
return {
items,
prevItems,
nextItems,
update,
state,
error,