refactor: don't pass down context

This commit is contained in:
Ayo 2023-01-21 20:00:48 +01:00
parent 6e5ef04bb6
commit 6c114c6f14
4 changed files with 7 additions and 10 deletions

View file

@ -11,15 +11,13 @@ const {
virtualScroller = false, virtualScroller = false,
eventType = 'update', eventType = 'update',
preprocess, preprocess,
context = 'public',
} = defineProps<{ } = defineProps<{
paginator: Paginator<T[], O> paginator: Paginator<T[], O>
keyProp?: keyof T keyProp?: keyof T
virtualScroller?: boolean virtualScroller?: boolean
stream?: Promise<WsEvents> stream?: Promise<WsEvents>
eventType?: 'notification' | 'update' eventType?: 'notification' | 'update'
preprocess?: (items: (U | T)[], context?: mastodon.v2.FilterContext) => U[] preprocess?: (items: (U | T)[]) => U[]
context?: mastodon.v2.FilterContext
}>() }>()
defineSlots<{ defineSlots<{
@ -44,7 +42,7 @@ defineSlots<{
const { t } = useI18n() const { t } = useI18n()
const { items, prevItems, update, state, endAnchor, error } = usePaginator(paginator, $$(stream), eventType, preprocess, context) const { items, prevItems, update, state, endAnchor, error } = usePaginator(paginator, $$(stream), eventType, preprocess)
</script> </script>
<template> <template>

View file

@ -22,7 +22,7 @@ const showOriginSite = $computed(() =>
</script> </script>
<template> <template>
<CommonPaginator v-bind="{ paginator, stream, preprocess, buffer }" :virtual-scroller="virtualScroller" :context="context"> <CommonPaginator v-bind="{ paginator, stream, preprocess, buffer }" :virtual-scroller="virtualScroller">
<template #updater="{ number, update }"> <template #updater="{ number, update }">
<button py-4 border="b base" flex="~ col" p-3 w-full text-primary font-bold @click="update"> <button py-4 border="b base" flex="~ col" p-3 w-full text-primary font-bold @click="update">
{{ $t('timeline.show_new_items', number, { named: { v: formatNumber(number) } }) }} {{ $t('timeline.show_new_items', number, { named: { v: formatNumber(number) } }) }}

View file

@ -6,8 +6,7 @@ export function usePaginator<T, P, U = T>(
_paginator: Paginator<T[], P>, _paginator: Paginator<T[], P>,
stream: Ref<Promise<WsEvents> | undefined>, stream: Ref<Promise<WsEvents> | undefined>,
eventType: 'notification' | 'update' = 'update', eventType: 'notification' | 'update' = 'update',
preprocess: (items: (T | U)[], context?: mastodon.v2.FilterContext) => U[] = items => items as unknown as U[], preprocess: (items: (T | U)[]) => U[] = items => items as unknown as U[],
context?: mastodon.v2.FilterContext,
buffer = 10, buffer = 10,
) { ) {
// called `next` method will mutate the internal state of the variable, // called `next` method will mutate the internal state of the variable,
@ -27,7 +26,7 @@ export function usePaginator<T, P, U = T>(
const deactivated = useDeactivated() const deactivated = useDeactivated()
async function update() { async function update() {
(items.value as U[]).unshift(...preprocess(prevItems.value as T[], context)) (items.value as U[]).unshift(...preprocess(prevItems.value as T[]))
prevItems.value = [] prevItems.value = []
} }
@ -74,7 +73,7 @@ export function usePaginator<T, P, U = T>(
const result = await paginator.next() const result = await paginator.next()
if (!result.done && result.value.length) { if (!result.done && result.value.length) {
const preprocessedItems = preprocess([...nextItems.value, ...result.value] as (U | T)[], context) const preprocessedItems = preprocess([...nextItems.value, ...result.value] as (U | T)[])
const itemsToShowCount const itemsToShowCount
= preprocessedItems.length <= buffer = preprocessedItems.length <= buffer
? preprocessedItems.length ? preprocessedItems.length

View file

@ -9,7 +9,7 @@ function areStatusesConsecutive(a: mastodon.v1.Status, b: mastodon.v1.Status) {
return !!inReplyToId && (inReplyToId === a.reblog?.id || inReplyToId === a.id) return !!inReplyToId && (inReplyToId === a.reblog?.id || inReplyToId === a.id)
} }
export function reorderedTimeline(items: mastodon.v1.Status[], context: mastodon.v1.FilterContext = 'public') { export function reorderedTimeline(items: mastodon.v1.Status[], context: mastodon.v1.FilterContext) {
let steps = 0 let steps = 0
const isStrict = (filter: mastodon.v1.FilterResult) => filter.filter.filterAction === 'hide' && filter.filter.context.includes(context) const isStrict = (filter: mastodon.v1.FilterResult) => filter.filter.filterAction === 'hide' && filter.filter.context.includes(context)