diff --git a/src/masto/timelines.ts b/src/masto/timelines.ts index 6e5eb9b..556bc83 100644 --- a/src/masto/timelines.ts +++ b/src/masto/timelines.ts @@ -18,6 +18,30 @@ type Timeline = { type TimelineParamsOf = T extends Timeline ? P : never; +function createControlsForLookup( + lookup: ReactiveMap>, +) { + return { + get(id: string) { + return lookup.get(id); + }, + getPath(id: string) { + const node = lookup.get(id); + if (!node) return; + const path = collectPath(node); + for (const sym of path) { + lookup.get(sym.value.id); // Track every node on the path + } + return path; + }, + set(id: string, value: mastodon.v1.Status) { + const node = untrack(() => lookup.get(id)); + if (!node) return; + lookup.set(id, {...node, value}); + }, + }; +} + export function createTimelineControlsForArray( status: () => mastodon.v1.Status[] | undefined, ): TimelineControls { @@ -68,20 +92,7 @@ export function createTimelineControlsForArray( return { list: threads, - get(id: string) { - return lookup.get(id); - }, - getPath(id: string) { - const node = lookup.get(id); - if (!node) return; - return collectPath(node); - }, - set(id: string, value: mastodon.v1.Status) { - const node = untrack(() => lookup.get(id)); - if (!node) return; - node.value = value; - lookup.set(id, node); - }, + ...createControlsForLookup(lookup), }; } @@ -307,20 +318,7 @@ export function createTimeline< return [ { list: threads, - get(id: string) { - return lookup.get(id); - }, - getPath(id: string) { - const node = lookup.get(id); - if (!node) return; - return collectPath(node); - }, - set(id: string, value: mastodon.v1.Status) { - const node = untrack(() => lookup.get(id)); - if (!node) return; - node.value = value; - lookup.set(id, node); - }, + ...createControlsForLookup(lookup), }, chunk, { refetch },