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
// When an item of a list counts as seen, as Bluesky's app counts it: half
// of it on screen (in the top-level viewport, through any frames) for a
// second, once. One observer watches every item of a view.
const VISIBLE = 0.5;
const FOR_MS = 1000;
export type SeenTracker = {
/** Watch an item's element; the returned function stops watching it. */
observe(element: Element, item: unknown): () => void;
/** Stop watching everything. */
disconnect(): void;
};
export function seenTracker(onseen: (item: unknown) => void): SeenTracker {
const items = new Map<Element, unknown>();
const timers = new Map<Element, number>();
const forget = (element: Element) => {
clearTimeout(timers.get(element));
timers.delete(element);
items.delete(element);
observer.unobserve(element);
};
const observer = new IntersectionObserver(
(entries) => {
for (const { target, isIntersecting, intersectionRatio } of entries) {
if (isIntersecting && intersectionRatio >= VISIBLE) {
if (timers.has(target)) continue;
timers.set(
target,
window.setTimeout(() => {
const item = items.get(target);
forget(target);
onseen(item);
}, FOR_MS)
);
} else {
clearTimeout(timers.get(target));
timers.delete(target);
}
}
},
{ threshold: VISIBLE }
);
return {
observe(element, item) {
items.set(element, item);
observer.observe(element);
return () => forget(element);
},
disconnect() {
for (const timer of timers.values()) clearTimeout(timer);
timers.clear();
items.clear();
observer.disconnect();
}
};
}