feat: stream tags, home, and public timelines (#190)
This commit is contained in:
parent
d94bed686b
commit
5560fe66cc
7 changed files with 55 additions and 16 deletions
|
@ -1,11 +1,12 @@
|
|||
import type { Paginator } from 'masto'
|
||||
import type { Paginator, WsEvents } from 'masto'
|
||||
import { useDeactivated } from './lifecycle'
|
||||
import type { PaginatorState } from '~/types'
|
||||
|
||||
export function usePaginator<T>(paginator: Paginator<any, T[]>) {
|
||||
export function usePaginator<T>(paginator: Paginator<any, T[]>, stream?: WsEvents) {
|
||||
const state = ref<PaginatorState>('idle')
|
||||
const items = ref<T[]>([])
|
||||
const newItems = ref<T[]>([])
|
||||
const nextItems = ref<T[]>([])
|
||||
const prevItems = ref<T[]>([])
|
||||
|
||||
const endAnchor = ref<HTMLDivElement>()
|
||||
const bound = reactive(useElementBounding(endAnchor))
|
||||
|
@ -13,6 +14,22 @@ export function usePaginator<T>(paginator: Paginator<any, T[]>) {
|
|||
const error = ref<unknown | undefined>()
|
||||
const deactivated = useDeactivated()
|
||||
|
||||
async function update() {
|
||||
items.value.unshift(...prevItems.value)
|
||||
prevItems.value = []
|
||||
}
|
||||
|
||||
stream?.on('update', (status) => {
|
||||
prevItems.value.unshift(status as any)
|
||||
})
|
||||
|
||||
// TODO: update statuses
|
||||
stream?.on('status.update', (status) => {
|
||||
const index = items.value.findIndex((s: any) => s.id === status.id)
|
||||
if (index >= 0)
|
||||
items.value[index] = status as any
|
||||
})
|
||||
|
||||
async function loadNext() {
|
||||
if (state.value !== 'idle')
|
||||
return
|
||||
|
@ -22,8 +39,8 @@ export function usePaginator<T>(paginator: Paginator<any, T[]>) {
|
|||
const result = await paginator.next()
|
||||
|
||||
if (result.value?.length) {
|
||||
newItems.value = result.value
|
||||
items.value.push(...newItems.value)
|
||||
nextItems.value = result.value
|
||||
items.value.push(...nextItems.value)
|
||||
state.value = 'idle'
|
||||
}
|
||||
else {
|
||||
|
@ -59,7 +76,9 @@ export function usePaginator<T>(paginator: Paginator<any, T[]>) {
|
|||
|
||||
return {
|
||||
items,
|
||||
newItems,
|
||||
prevItems,
|
||||
nextItems,
|
||||
update,
|
||||
state,
|
||||
error,
|
||||
endAnchor,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue