timelines: add createControlsForLookup

This commit is contained in:
thislight 2024-11-08 00:13:31 +08:00
parent acde7609ba
commit ad729f4f34
No known key found for this signature in database
GPG key ID: FCFE5192241CCD4E

View file

@ -18,6 +18,30 @@ type Timeline<T extends mastodon.DefaultPaginationParams> = {
type TimelineParamsOf<T> = T extends Timeline<infer P> ? P : never;
function createControlsForLookup(
lookup: ReactiveMap<string, TreeNode<mastodon.v1.Status>>,
) {
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 },