From ab29e5fd70980b41f18caf2028b6aa326a945165 Mon Sep 17 00:00:00 2001 From: thislight Date: Thu, 10 Oct 2024 16:24:06 +0800 Subject: [PATCH] added createTimelineSnapshot --- src/masto/timelines.ts | 54 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/masto/timelines.ts b/src/masto/timelines.ts index 2e49145..4e307f7 100644 --- a/src/masto/timelines.ts +++ b/src/masto/timelines.ts @@ -109,3 +109,57 @@ export function useTimeline( }, ] as const; } + +export function createTimelineSnapshot( + timeline: Accessor, + limit: Accessor, +) { + const [shot, {refetch}] = createResource( + () => [timeline(), limit()] as const, + async ([tl, limit]) => { + const ls = await tl.list({ limit }).next(); + return ls.value?.map((x) => [x]) ?? []; + }, + ); + + const [snapshot, setSnapshot] = createStore([] as mastodon.v1.Status[][]); + + createEffect(() => { + const nls = shot(); + if (!nls) return; + const ols = Array.from(snapshot); + // The algorithm below assumes the snapshot is not changing + for (let i = 0; i < nls.length; i++) { + if (i >= ols.length) { + setSnapshot(i, nls[i]); + } else { + if (nls[i].length !== ols[i].length) { + setSnapshot(i, nls[i]); + } else { + const oth = ols[i], + nth = nls[i]; + for (let j = 0; j < oth.length; j++) { + const ost = oth[j], + nst = nth[j]; + for (const key of Object.keys( + nst, + ) as unknown as (keyof mastodon.v1.Status)[]) { + if (ost[key] !== nst[key]) { + setSnapshot(i, j, key, nst[key]); + } + } + } + } + } + } + }); + + return [snapshot, shot, { + refetch, + mutate: setSnapshot + }] as const; +} + +export function createTimeline(timeline: Accessor) { + // TODO +}