~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
31 lines · 1.3 KB Raw History
// What a person did to a part of a drawing (a like made, a warning seen
// through, a text opened in full), kept by the tree's node it drew and what
// part of it, so that the part drawn again (a list's item scrolled
// back to, a column come near again) shows as they left it rather than as
// the data first said. Kept as long as the data is: by its object, weakly.
// A scope that is no object keeps nothing, nor a server: what it draws is
// never drawn again there, and the browser's first drawing of it starts
// from the data, as the server's did.

import { BROWSER } from 'esm-env';

const kept = new WeakMap<object, Map<unknown, unknown>>();

/** A part's own state, reactive. */
export class Kept<T> {
	value = $state() as T;
	constructor(value: T) {
		this.value = value;
	}
}

/** The state of the part `where` (its layout node) draws of `scope`: made
 * with `first` the first time, the same after while `scope` lives. */
export function remembered<T>(scope: unknown, where: object, first: () => T): Kept<T> {
	if (!BROWSER || typeof scope !== 'object' || scope === null) return new Kept(first());
	let parts = kept.get(scope);
	if (!parts) kept.set(scope, (parts = new Map()));
	let found = parts.get(where) as Kept<T> | undefined;
	if (!found) parts.set(where, (found = new Kept(first())));
	return found;
}