tutu/src/masto/timelines.ts

322 lines
8 KiB
TypeScript
Raw Normal View History

import { ReactiveMap } from "@solid-primitives/map";
2024-07-14 20:28:44 +08:00
import { type mastodon } from "masto";
import {
Accessor,
batch,
catchError,
createEffect,
createResource,
untrack,
type ResourceFetcherInfo,
} from "solid-js";
import { createStore } from "solid-js/store";
2024-07-14 20:28:44 +08:00
type TimelineFetchTips = {
direction?: "new" | "old";
};
type Timeline = {
list(params: {
2024-08-22 15:31:15 +08:00
readonly limit?: number;
2024-07-14 20:28:44 +08:00
}): mastodon.Paginator<mastodon.v1.Status[], unknown>;
};
2024-08-22 15:31:15 +08:00
export function useTimeline(
timeline: Accessor<Timeline>,
cfg?: {
/**
* Use full refresh mode. This mode ignores paging, it will refetch the specified number
* of toots at every refetch().
*/
fullRefresh?: number;
},
) {
2024-07-14 20:28:44 +08:00
let otl: Timeline | undefined;
2024-08-12 22:29:55 +08:00
let npager: mastodon.Paginator<mastodon.v1.Status[], unknown> | undefined;
let opager: mastodon.Paginator<mastodon.v1.Status[], unknown> | undefined;
const [snapshot, { refetch }] = createResource<
2024-08-12 22:29:55 +08:00
{
records: mastodon.v1.Status[];
2024-08-22 15:31:15 +08:00
direction: "new" | "old" | "items";
2024-08-12 22:29:55 +08:00
tlChanged: boolean;
},
2024-07-14 20:28:44 +08:00
[Timeline],
TimelineFetchTips | undefined
>(
() => [timeline()] as const,
async ([tl], info) => {
2024-08-12 22:29:55 +08:00
let tlChanged = false;
2024-07-14 20:28:44 +08:00
if (otl !== tl) {
2024-08-12 22:29:55 +08:00
npager = opager = undefined;
2024-07-14 20:28:44 +08:00
otl = tl;
2024-08-12 22:29:55 +08:00
tlChanged = true;
2024-07-14 20:28:44 +08:00
}
2024-08-22 15:31:15 +08:00
const fullRefresh = cfg?.fullRefresh;
if (typeof fullRefresh !== "undefined") {
const records = await tl
.list({
limit: fullRefresh,
})
.next();
return {
direction: "items",
records: records.value ?? [],
end: records.done,
tlChanged,
};
}
2024-07-14 20:28:44 +08:00
const direction =
typeof info.refetching !== "boolean"
2024-08-12 22:29:55 +08:00
? (info.refetching?.direction ?? "old")
2024-07-14 20:28:44 +08:00
: "old";
if (direction === "old") {
2024-08-12 22:29:55 +08:00
if (!opager) {
opager = tl.list({}).setDirection("next");
2024-07-14 20:28:44 +08:00
}
2024-08-12 22:29:55 +08:00
const next = await opager.next();
return {
2024-08-12 22:29:55 +08:00
direction,
records: next.value ?? [],
end: next.done,
tlChanged,
};
2024-07-14 20:28:44 +08:00
} else {
2024-08-12 22:29:55 +08:00
if (!npager) {
npager = tl.list({}).setDirection("prev");
2024-07-14 20:28:44 +08:00
}
2024-08-12 22:29:55 +08:00
const next = await npager.next();
const page = next.value ?? [];
return { direction, records: page, end: next.done, tlChanged };
2024-07-14 20:28:44 +08:00
}
},
);
const [store, setStore] = createStore([] as mastodon.v1.Status[]);
createEffect(() => {
const shot = snapshot();
if (!shot) return;
2024-08-12 22:29:55 +08:00
const { direction, records, tlChanged } = shot;
if (tlChanged) {
setStore(() => []);
}
2024-08-22 15:31:15 +08:00
if (direction === "new") {
setStore((x) => [...records, ...x]);
2024-08-22 15:31:15 +08:00
} else if (direction === "old") {
setStore((x) => [...x, ...records]);
2024-08-22 15:31:15 +08:00
} else if (direction === "items") {
setStore(() => records);
}
});
return [
store,
snapshot,
2024-07-14 20:28:44 +08:00
{
refetch,
mutate: setStore,
2024-07-14 20:28:44 +08:00
},
] as const;
2024-07-14 20:28:44 +08:00
}
2024-10-10 16:24:06 +08:00
export function createTimelineSnapshot(
timeline: Accessor<Timeline>,
limit: Accessor<number>,
) {
const [shot, { refetch }] = createResource(
2024-10-10 16:24:06 +08:00
() => [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 = catchError(shot, (e) => console.error(e));
2024-10-10 16:24:06 +08:00
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;
2024-10-10 16:24:06 +08:00
}
export type TimelineFetchDirection = mastodon.Direction;
export type TimelineChunk = {
pager: mastodon.Paginator<mastodon.v1.Status[], unknown>;
chunk: readonly mastodon.v1.Status[];
done?: boolean;
direction: TimelineFetchDirection;
limit: number;
};
function checkOrCreatePager(
timeline: Timeline,
limit: number,
lastPager: TimelineChunk["pager"] | undefined,
newDirection: TimelineFetchDirection,
) {
if (!lastPager) {
2024-10-12 19:48:34 +08:00
return timeline.list({}).setDirection(newDirection);
} else {
let pager = lastPager;
if (pager.getDirection() !== newDirection) {
pager = pager.setDirection(newDirection);
}
return pager;
}
}
type TreeNode<T> = {
parent?: TreeNode<T>;
value: T;
children?: TreeNode<T>[];
};
/** Collect the path of a node for the root.
* The first element is the node itself, the last element is the root.
*/
function collectPath<T>(node: TreeNode<T>) {
const path = [node] as TreeNode<T>[];
let current = node;
while (current.parent) {
path.push(current.parent);
current = current.parent;
}
return path;
}
export function createTimeline(
timeline: Accessor<Timeline>,
limit: Accessor<number>,
) {
const lookup = new ReactiveMap<string, TreeNode<mastodon.v1.Status>>();
const [threads, setThreads] = createStore([] as mastodon.v1.Status["id"][]);
const [chunk, { refetch }] = createResource(
() => [timeline(), limit()] as const,
async (
[tl, limit],
info: ResourceFetcherInfo<
Readonly<TimelineChunk>,
TimelineFetchDirection
>,
) => {
const direction =
typeof info.refetching === "boolean" ? "prev" : info.refetching;
const rebuildTimeline = limit !== info.value?.limit;
const pager = rebuildTimeline
? checkOrCreatePager(tl, limit, undefined, direction)
: checkOrCreatePager(tl, limit, info.value?.pager, direction);
if (rebuildTimeline) {
lookup.clear();
setThreads([]);
}
const posts = await pager.next();
return {
pager,
chunk: posts.value ?? [],
done: posts.done,
direction,
limit,
};
},
);
createEffect(() => {
const chk = catchError(chunk, (e) => console.error(e));
if (!chk) {
return;
}
2024-10-12 19:48:34 +08:00
console.debug("fetched chunk", chk);
batch(() => {
for (const status of chk.chunk) {
lookup.set(status.id, {
value: status,
});
}
for (const status of chk.chunk) {
const node = lookup.get(status.id)!;
if (status.inReplyToId) {
const parent = lookup.get(status.inReplyToId);
if (parent) {
const children = parent.children ?? [];
if (!children.find((x) => x.value.id == status.id)) {
children.push(node);
}
parent.children = children;
node.parent = parent;
}
}
}
if (chk.direction === "next") {
setThreads((threads) => [...threads, ...chk.chunk.map((x) => x.id)]);
} else if (chk.direction === "prev") {
setThreads((threads) => [...chk.chunk.map((x) => x.id), ...threads]);
}
setThreads((threads) =>
threads.filter((id) => (lookup.get(id)!.children?.length ?? 0) === 0),
);
});
});
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);
},
},
chunk,
{ refetch },
] as const;
2024-10-10 16:24:06 +08:00
}