import type { Token } from './token_list_watcher'; /** What a router needs from a parsed token: the identifier naming its Owner's tag. */ export interface RoutedDescriptor { identifier: string | undefined; } /** * What a subscriber hears about the tokens routed to it, each with the descriptor parsed from it once, when it was * matched. The same descriptor comes with the token's `tokenUnmatched`. */ export interface TokenRouterDelegate { tokenMatched?: (token: Token, descriptor: D) => void; tokenUnmatched?: (token: Token, descriptor: D) => void; } /** * One document-wide `[attributeName]` token watcher shared by every subscribing element, so the per-mutation cost of * the document observer stays constant instead of growing with the number of live instances (each `watchTokenList` * call is another catch-all selector that every added node is matched against, plus a full document scan on start). * * Each token is parsed with `parse` once, when it is matched, and indexed under the closest ancestor (or self) of its * element that the descriptor's identifier matches. It is delivered to that element only when the element has * subscribed and the identifier is its own name, its tag name lowercased, so a subscriber is handed only its own tokens, * with the descriptors parsed then, and needs to check neither; an identifier that is some other selector (`div.x`, * `:is(x-a)`, `X-A`) reaches no one. Tokens are remembered per element whether or not it has subscribed yet, so one that * subscribes later gets its already-present tokens replayed synchronously, and one that unsubscribes gets * `tokenUnmatched` for every token it still owns. The indexes are weak, so an element named by a stray token that never * subscribes is not kept alive by the router. */ export default class TokenRouter { private readonly attributeName; private readonly parse; private readonly subscriptions; private readonly tokensByOwner; private readonly routes; private walking; private subscriberCount; private stopWatching?; constructor(attributeName: string, parse: (content: string) => D); /** * Routes `owner`'s tokens to `delegate`. Tokens already on the page are delivered synchronously, including those on * nodes inserted earlier in the same task. Returns a stop function that fires `tokenUnmatched` for every token the * owner still holds and then forgets the delegate; every token is reported and the subscription released even if the * delegate throws, and the first error is rethrown afterwards. Calling it again is a no-op. */ subscribe(owner: Element, delegate: TokenRouterDelegate): () => void; /** * Whether a scan of the document as it stands would route to `owner` a token on `element` whose descriptor `accepts`. * It parses what `element` lists each time it is asked. The index only catches up when the record that moved or * removed an element, or rewrote its attribute, is delivered, so part-way through a mutation batch it can be behind * what this answers. */ routesTo(owner: Element, element: Element, accepts: (descriptor: D) => boolean): boolean; private unsubscribe; private tokenMatched; private tokenUnmatched; private ownedTokens; /** * The single place that decides which element a token is indexed under: the nearest ancestor-or-self the identifier * matches. Whether it is delivered there is decided by comparing the identifier with the subscriber's own name, which * is computed once, as it subscribes, rather than for every token: for a plain tag name the two agree, and an * identifier that is some other selector matches an element it does not name. */ private ownerFor; } //# sourceMappingURL=token_router.d.ts.map