From 67720a6f950ea451c11fd63cd07418ffa2d52f84 Mon Sep 17 00:00:00 2001 From: thislight Date: Sun, 13 Oct 2024 16:09:11 +0800 Subject: [PATCH] added createTimelineChunk --- src/masto/timelines.ts | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/masto/timelines.ts b/src/masto/timelines.ts index b8082c2..8eb9c81 100644 --- a/src/masto/timelines.ts +++ b/src/masto/timelines.ts @@ -82,6 +82,7 @@ export type TimelineFetchDirection = mastodon.Direction; export type TimelineChunk = { tl: Timeline; + rebuilt: boolean; chunk: readonly mastodon.v1.Status[]; done?: boolean; direction: TimelineFetchDirection; @@ -107,13 +108,10 @@ function collectPath(node: TreeNode) { return path; } -export function createTimeline( +function createTimelineChunk( timeline: Accessor, limit: Accessor, ) { - const lookup = new ReactiveMap>(); - const [threads, setThreads] = createStore([] as mastodon.v1.Status["id"][]); - let vpMaxId: string | undefined, vpMinId: string | undefined; const fetchExtendingPage = async ( @@ -146,7 +144,7 @@ export function createTimeline( } }; - const [chunk, { refetch }] = createResource( + return createResource( () => [timeline(), limit()] as const, async ( [tl, limit], @@ -161,12 +159,11 @@ export function createTimeline( if (rebuildTimeline) { vpMaxId = undefined; vpMinId = undefined; - lookup.clear(); - setThreads([]); } const posts = await fetchExtendingPage(tl, direction, limit); return { tl, + rebuilt: rebuildTimeline, chunk: posts.value ?? [], done: posts.done, direction, @@ -174,6 +171,16 @@ export function createTimeline( }; }, ); +} + +export function createTimeline( + timeline: Accessor, + limit: Accessor, +) { + const lookup = new ReactiveMap>(); + const [threads, setThreads] = createStore([] as mastodon.v1.Status["id"][]); + + const [chunk, { refetch }] = createTimelineChunk(timeline, limit); createEffect(() => { const chk = catchError(chunk, (e) => console.error(e)); @@ -181,6 +188,11 @@ export function createTimeline( return; } + if (chk.rebuilt) { + lookup.clear(); + setThreads([]); + } + const existence = [] as boolean[]; for (const [idx, status] of chk.chunk.entries()) {