chadtmiller.com/hud
A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts
place.hud.hudkit 0.3.1 Latest 5 packages
0
0
// The last tree each screen drew, kept a while, so a screen opened again
// (a level pushed, a history entry gone forward to, a tab's root drawn
// afresh, a pager's page) draws what it last showed at once, marked as
// refreshing, while its call is made again (Stack.svelte), rather than its
// skeleton (the renderer's `recallSkeleton`, still drawn for a screen with
// none here). A page the browser loads is drawn by the server, fresh, and
// starts it empty.
//
// The trees are an account's: it holds one account's at a time (`who`, a
// DID, or null signed out), emptied when a tree comes for another, and as
// the app signs out. It is emptied too after any write the account makes
// (Shell.svelte), so no screen opens showing what its own write changed,
// and an answer to a call made before that is not kept. It keeps the last
// `kept` screens, none past `age`, and no more than `bytes` of them all
// told (by their JSON's length), the oldest let go first.
/** How long a tree is drawn again for, how many are kept, and how much of them all told. */
export type Limits = { age: number; kept: number; bytes: number };
export const LIMITS: Limits = { age: 10 * 60_000, kept: 30, bytes: 8_000_000 };
/** A call, as the key of its screen: its name and arguments as JSON (as routes.ts's `same` compares them). */
type Call = { name: string; args: Record<string, unknown> };
export class Recent<T> {
#who: string | null | undefined = undefined;
/** When the trees were last let go, by `forget`. */
#forgot = -Infinity;
readonly #kept = new Map<string, { tree: T; at: number; size: number }>();
#bytes = 0;
readonly #limits: Limits;
readonly #now: () => number;
constructor(limits: Limits = LIMITS, now: () => number = Date.now) {
this.#limits = limits;
this.#now = now;
}
/** A call's tree, as `who` was answered it, the call made at `asked`: the last of its screen, in place of any before; but not where it was made before the trees were let go. */
keep(who: string | null, call: Call, tree: T, asked: number) {
if (asked <= this.#forgot) return;
if (who !== this.#who) {
this.#clear();
this.#who = who;
}
const key = JSON.stringify(call);
const size = JSON.stringify(tree)?.length ?? 0;
if (size > this.#limits.bytes) return;
this.#drop(key);
this.#kept.set(key, { tree, at: this.#now(), size });
this.#bytes += size;
for (const [oldest] of this.#kept) {
if (this.#kept.size <= this.#limits.kept && this.#bytes <= this.#limits.bytes) break;
this.#drop(oldest);
}
}
/** The last tree of a call's screen, where one is kept and not past its age. */
recall(call: Call): T | null {
const key = JSON.stringify(call);
const found = this.#kept.get(key);
if (!found) return null;
if (this.#now() - found.at > this.#limits.age) {
this.#drop(key);
return null;
}
return found.tree;
}
/** Every tree let go: the account signed out, or wrote. */
forget() {
this.#clear();
this.#forgot = this.#now();
}
#clear() {
this.#kept.clear();
this.#bytes = 0;
}
#drop(key: string) {
const was = this.#kept.get(key);
if (!was) return;
this.#kept.delete(key);
this.#bytes -= was.size;
}
}
/** A node of a tree, as far as its page list goes: a List of `p.page` has its items, which a page more appends to. */
type Node<I> = { p?: Record<string, unknown>; k?: Node<I>[]; items?: I[] };
/** The items of a tree's page list, depth first; null where it has none (as HUD's `append` finds it). */
export function pageItems<I>(node: Node<I>): I[] | null {
if (node.p?.page === true && Array.isArray(node.items)) return node.items;
for (const child of node.k ?? []) {
const found = pageItems(child);
if (found) return found;
}
return null;
}
/** The items a screen drew past its first page (`first` of them), less any its fresh first page (`fresh`) has again: what it keeps after that page as it refreshes, so where it was scrolled to stays. */
export function pagedPast<I extends { open?: unknown }>(drawn: I[], first: number, fresh: { open?: unknown }[]): I[] {
const again = new Set(fresh.map((item) => JSON.stringify(item.open ?? item)));
return drawn.slice(first).filter((item) => !again.has(JSON.stringify(item.open ?? item)));
}