// 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>(); /** A part's own state, reactive. */ export class Kept { 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(scope: unknown, where: object, first: () => T): Kept { 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 | undefined; if (!found) parts.set(where, (found = new Kept(first()))); return found; }