Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263// What a card is.//// The popup grew one feature at a time, and each one landed in the same// module: a state variable in the shared block at the top, a line in// `render()`, a line in `loadState()`, a reset in the sign-out handler, and a// hundred lines of body somewhere in the middle. Five edit points per feature,// all of them in the file every other feature was also editing.//// A card owns those five things instead. It keeps its own state, draws its own// part of the popup, fetches what it needs off the render path, and forgets// what belonged to an account that just signed out. `popup.ts` holds the list// and calls them in turn, so adding one is a new file and a line in the// registry rather than five edits spread through a thousand lines.//// The rules a card has to keep://// - `render` is synchronous, idempotent, and draws only from what is// already known. It runs on every redraw, including ones another card// caused, so it must never fetch and never assume it is the reason.// - `load` is where fetching happens. It may fail; a card that cannot get// its answer hides its own row rather than reporting an error, because// none of this is what the popup is for.// - a card touches only the elements it owns.// - a card whose answer has not arrived may stand a placeholder where it// will go, but only while `host.loading(id)` says so, and only where the// answer is one that reliably arrives — a placeholder for a row that// turns out to be empty is a layout shift the popup did to itself// (loading.ts).
import type { SiteAccount } from '../../lib/site-handle'import type { PageState, SessionInfo } from '../../lib/types'
/** What a card can ask the popup for. Methods, because the answers change. */export interface CardHost { /** The worker's detection state for the current tab, if any. */ state(): PageState | undefined /** The page's host as an atproto account, when it is one. */ siteAccount(): SiteAccount | undefined /** The signed-in account mirror, if any. */ session(): SessionInfo | undefined /** Redraw every card: what a finished `load` calls. */ rerender(): void /** * Whether this card's `load` has been running long enough to be worth a * placeholder (see loading.ts). Answered for a card id rather than for the * caller, because `render` gets the host and not itself. */ loading(id: string): boolean}
export interface Card { /** Stable name, for the registry and for debug logs. */ id: string /** Wire DOM listeners, once, at startup. */ mount?(host: CardHost): void /** Draw from what is known. No fetching, no surprises. */ render(host: CardHost): void /** Fetch what the card needs. `refresh` is the user asking again. */ load?(host: CardHost, refresh: boolean): Promise<void> /** Forget what was remembered for the account that just signed out. */ reset?(): void}