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 +}