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 benchmark page: one workspace column of a feed (Bench.svelte), drawn
// from the tree the runner (../scroll.mjs) resolves at /tree, and
// `window.bench` for the runner to drive and read it.
// What it samples it reads without forcing layout: frame times from
// requestAnimationFrame, and which items are in view from an
// IntersectionObserver, so the trace it is measured with holds the list's
// own work alone.
import type { UiTree } from '@hud/renderer';
import '@hud/renderer/theme.css';
import { mount, tick } from 'svelte';
import Bench from './Bench.svelte';
/** The feed's tree: `posts` posts from the `from`th. */
const page = async (posts: number, from: number): Promise<UiTree> => (await fetch(`/tree?posts=${posts}&from=${from}`)).json();
const count = Number(new URLSearchParams(location.search).get('posts') ?? 300);
const bench = mount(Bench, {
target: document.body,
props: { first: await page(count, 0) }
}) as unknown as { more: (next: UiTree) => void; remount: () => Promise<void> };
const scroller = () => document.querySelector<HTMLElement>('.body')!;
function frame() {
const { promise, resolve } = Promise.withResolvers<number>();
requestAnimationFrame(resolve);
return promise;
}
/** Frame times: the gaps between animation frames while running. */
let frames: number[] | null = null;
function frameLoop(last: number) {
if (!frames) return;
requestAnimationFrame((now) => {
if (!frames) return;
frames.push(now - last);
frameLoop(now);
});
}
/** The items in view, as the IntersectionObserver last saw them. */
const inView = new Set<Element>();
const seeing = new IntersectionObserver(
(entries) => {
for (const entry of entries) {
if (entry.isIntersecting) inView.add(entry.target);
else inView.delete(entry.target);
}
},
{ root: scroller() }
);
/** Every item, watched as it is added: the items of a list added, or the list. */
new MutationObserver((records) => {
for (const record of records) {
for (const node of record.addedNodes) {
if (!(node instanceof HTMLElement)) continue;
if (node.matches('li.item')) seeing.observe(node);
else for (const item of node.querySelectorAll('li.item')) seeing.observe(item);
}
}
}).observe(scroller(), { childList: true, subtree: true });
for (const item of document.querySelectorAll('.body li.item')) seeing.observe(item);
/** Each frame, whether an item in view was let go (drawn as space). */
let blanks: { frames: number; blank: number } | null = null;
function blankLoop() {
if (!blanks) return;
requestAnimationFrame(() => {
if (!blanks) return;
blanks.frames++;
for (const item of inView) {
if (item.isConnected && item.classList.contains('idle')) {
blanks.blank++;
break;
}
}
blankLoop();
});
}
/** The first item in view: its index, and how far its top is from the scroller's. */
type Anchor = { index: number; offset: number } | null;
function anchor(): Anchor {
const box = scroller().getBoundingClientRect();
for (const item of document.querySelectorAll<HTMLElement>('.body li.item')) {
const at = item.getBoundingClientRect();
if (at.bottom > box.top) return { index: Number(item.dataset.index), offset: at.top - box.top };
}
return null;
}
Object.assign(window, {
bench: {
items: () => document.querySelectorAll('.body li.item').length,
drawn: () => document.querySelectorAll('.body li.item:not(.idle)').length,
start() {
frames = [];
blanks = { frames: 0, blank: 0 };
const now = performance.now();
frameLoop(now);
blankLoop();
},
stop() {
const result = { frames, blanks };
frames = null;
blanks = null;
return result;
},
/** Scrolled to the end: whether the scroller's bottom is reached. */
atEnd() {
const s = scroller();
return s.scrollTop + s.clientHeight >= s.scrollHeight - 2;
},
scrollTop: () => scroller().scrollTop,
box() {
const at = scroller().getBoundingClientRect();
return { x: at.x, y: at.y, width: at.width, height: at.height };
},
/** Draw the column again, as one coming back into view: how far what was at its top moved, then and once settled. */
async remount(settle: number) {
const before = anchor();
await bench.remount();
await frame();
await frame();
const drawn = anchor();
const { promise, resolve } = Promise.withResolvers();
setTimeout(resolve, settle);
await promise;
await frame();
const settled = anchor();
const moved = (after: Anchor) =>
before && after ? (after.index === before.index ? after.offset - before.offset : `item ${before.index} → ${after.index}`) : null;
return { before, drawn: moved(drawn), settled: moved(settled) };
},
async more(n: number) {
bench.more(await page(n, document.querySelectorAll('.body li.item').length));
await tick();
}
}
});