added createTimelineChunk

This commit is contained in:
thislight 2024-10-13 16:09:11 +08:00
parent 6592d71a46
commit 67720a6f95
No known key found for this signature in database
GPG key ID: A50F9451AC56A63E

View file

@ -82,6 +82,7 @@ export type TimelineFetchDirection = mastodon.Direction;
export type TimelineChunk = {
tl: Timeline;
rebuilt: boolean;
chunk: readonly mastodon.v1.Status[];
done?: boolean;
direction: TimelineFetchDirection;
@ -107,13 +108,10 @@ function collectPath<T>(node: TreeNode<T>) {
return path;
}
export function createTimeline(
function createTimelineChunk(
timeline: Accessor<Timeline>,
limit: Accessor<number>,
) {
const lookup = new ReactiveMap<string, TreeNode<mastodon.v1.Status>>();
const [threads, setThreads] = createStore([] as mastodon.v1.Status["id"][]);
let vpMaxId: string | undefined, vpMinId: string | undefined;
const fetchExtendingPage = async (
@ -146,7 +144,7 @@ export function createTimeline(
}
};
const [chunk, { refetch }] = createResource(
return createResource(
() => [timeline(), limit()] as const,
async (
[tl, limit],
@ -161,12 +159,11 @@ export function createTimeline(
if (rebuildTimeline) {
vpMaxId = undefined;
vpMinId = undefined;
lookup.clear();
setThreads([]);
}
const posts = await fetchExtendingPage(tl, direction, limit);
return {
tl,
rebuilt: rebuildTimeline,
chunk: posts.value ?? [],
done: posts.done,
direction,
@ -174,6 +171,16 @@ export function createTimeline(
};
},
);
}
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 }] = createTimelineChunk(timeline, limit);
createEffect(() => {
const chk = catchError(chunk, (e) => console.error(e));
@ -181,6 +188,11 @@ export function createTimeline(
return;
}
if (chk.rebuilt) {
lookup.clear();
setThreads([]);
}
const existence = [] as boolean[];
for (const [idx, status] of chk.chunk.entries()) {