// resolutions.mjs — each chip's state for this viewer (spec §6.8). The // editor's data island carries the answer for every link in the text it // mounts over; a link typed after mount is asked of POST /links/resolve, // at most 32 at a time, and drawn meanwhile from what the browser can // tell alone: a date is a date, anything else is not written yet. // Answers are never node attributes: a chip's NodeView asks here, and is // told when an answer arrives (chipview.mjs). import { isDate, isDateShaped } from './markup.mjs' import { dayLabel } from './dates.mjs' // dayLabel is locale.Locale.Day, in the reader's locale (dates.mjs). export { dayLabel } const batch = 32 // linkKey is how the server names a link: its target as typed, with // "#^id" for a block (island.go). export function linkKey (target, anchor) { return anchor ? `${target}#^${anchor}` : target } // answerOf is a server LinkState as chips.mjs draws it. export function answerOf (state) { switch (state?.kind) { case 'broken': return { broken: true } case 'date': return { href: state.href, day: state.title } case 'pending': return { href: state.href, pending: true } case 'team': case 'block': return { href: state.href, title: state.title, alias: Boolean(state.alias), icon: state.icon || null } case 'block gone': return { href: state.href, title: state.title, alias: Boolean(state.alias), blockGone: true } } return null } // guess is a chip before its answer: what needs no server. A date is // labelled in the reader's locale, exactly as the server would (L4). export function guess (target, thisYear = new Date().getFullYear()) { if (isDateShaped(target)) { return isDate(target) ? { href: `/d/${target}`, day: dayLabel(target, thisYear) } : { broken: true } } const query = new URLSearchParams({ title: target }).toString() return { href: `/new?${query}`, pending: true } } export class Resolutions { // links is the island's map; ask(keys) answers POST /links/resolve's // JSON for up to 32 keys. constructor (links = {}, ask = null) { this.known = new Map(Object.entries(links)) this.ask = ask this.waiting = new Set() this.asked = new Set() this.watchers = new Set() this.timer = null } // answer is the chip state for a link now; a link never answered is // asked for, once. answer (target, anchor = '') { const key = linkKey(target, anchor) const known = answerOf(this.known.get(key)) if (known) return known if (this.ask && !this.asked.has(key)) { this.waiting.add(key) this.soon() } return guess(target) } soon () { if (this.timer !== null) return this.timer = setTimeout(() => { this.timer = null this.flush() }, 0) } async flush () { const keys = [...this.waiting].slice(0, batch) if (keys.length === 0) return for (const key of keys) { this.waiting.delete(key) this.asked.add(key) } if (this.waiting.size > 0) this.soon() let reply try { reply = await this.ask(keys) } catch { // Unanswered links keep their guess; the next mention asks again. for (const key of keys) this.asked.delete(key) return } const changed = new Set() for (const [key, state] of Object.entries(reply ?? {})) { this.known.set(key, state) changed.add(key) } if (changed.size) for (const watcher of this.watchers) watcher(changed) } // lookup is the states of up to 32 keys, asked at once (for the [[ // list), and kept for the chips. async lookup (keys) { const wanted = keys.filter(key => !this.known.has(key)).slice(0, batch) if (wanted.length && this.ask) { try { for (const [key, state] of Object.entries(await this.ask(wanted) ?? {})) { this.known.set(key, state) } } catch {} } return Object.fromEntries(keys.map(key => [key, this.known.get(key)])) } // watch calls fn with the keys whose answers arrived. watch (fn) { this.watchers.add(fn) return () => this.watchers.delete(fn) } }