fix: paginator state lost after HMR

zio/stable
三咲智子 2023-01-10 01:43:28 +08:00
parent 56ab163369
commit 0a75205309
No known key found for this signature in database
GPG Key ID: 69992F2250DFD93E
1 changed files with 9 additions and 2 deletions

View File

@ -1,13 +1,20 @@
import type { Paginator, WsEvents, mastodon } from 'masto' import { Paginator } from 'masto'
import type { WsEvents, mastodon } from 'masto'
import type { PaginatorState } from '~/types' import type { PaginatorState } from '~/types'
export function usePaginator<T, P, U = T>( export function usePaginator<T, P, U = T>(
paginator: Paginator<T[], P>, _paginator: Paginator<T[], P>,
stream?: Promise<WsEvents>, stream?: Promise<WsEvents>,
eventType: 'notification' | 'update' = 'update', eventType: 'notification' | 'update' = 'update',
preprocess: (items: (T | U)[]) => U[] = items => items as unknown as U[], preprocess: (items: (T | U)[]) => U[] = items => items as unknown as U[],
buffer = 10, buffer = 10,
) { ) {
// TODO: wait PR https://github.com/neet/masto.js/pull/801
// called `next` method will mutate the internal state of the variable, and we need its initial state after HMR
// so clone it
// @ts-expect-error clone it
const paginator: Paginator<T[], P> = new Paginator(_paginator.http, _paginator.nextPath, _paginator.nextParams)
const state = ref<PaginatorState>(isMastoInitialised.value ? 'idle' : 'loading') const state = ref<PaginatorState>(isMastoInitialised.value ? 'idle' : 'loading')
const items = ref<U[]>([]) const items = ref<U[]>([])
const nextItems = ref<U[]>([]) const nextItems = ref<U[]>([])