| 1 | // resolutions.mjs — each chip's state for this viewer (spec §6.8). The |
| 2 | // editor's data island carries the answer for every link in the text it |
| 3 | // mounts over; a link typed after mount is asked of POST /links/resolve, |
| 4 | // at most 32 at a time, and drawn meanwhile from what the browser can |
| 5 | // tell alone: a date is a date, anything else is not written yet. |
| 6 | // Answers are never node attributes: a chip's NodeView asks here, and is |
| 7 | // told when an answer arrives (chipview.mjs). |
| 8 | |
| 9 | import { isDate, isDateShaped } from './markup.mjs' |
| 10 | import { dayLabel } from './dates.mjs' |
| 11 | |
| 12 | // dayLabel is locale.Locale.Day, in the reader's locale (dates.mjs). |
| 13 | export { dayLabel } |
| 14 | |
| 15 | const batch = 32 |
| 16 | |
| 17 | // linkKey is how the server names a link: its target as typed, with |
| 18 | // "#^id" for a block (island.go). |
| 19 | export function linkKey (target, anchor) { |
| 20 | return anchor ? `${target}#^${anchor}` : target |
| 21 | } |
| 22 | |
| 23 | // answerOf is a server LinkState as chips.mjs draws it. |
| 24 | export function answerOf (state) { |
| 25 | switch (state?.kind) { |
| 26 | case 'broken': return { broken: true } |
| 27 | case 'date': return { href: state.href, day: state.title } |
| 28 | case 'pending': return { href: state.href, pending: true } |
| 29 | case 'team': |
| 30 | case 'block': |
| 31 | return { href: state.href, title: state.title, alias: Boolean(state.alias), icon: state.icon || null } |
| 32 | case 'block gone': |
| 33 | return { href: state.href, title: state.title, alias: Boolean(state.alias), blockGone: true } |
| 34 | } |
| 35 | return null |
| 36 | } |
| 37 | |
| 38 | // guess is a chip before its answer: what needs no server. A date is |
| 39 | // labelled in the reader's locale, exactly as the server would (L4). |
| 40 | export function guess (target, thisYear = new Date().getFullYear()) { |
| 41 | if (isDateShaped(target)) { |
| 42 | return isDate(target) ? { href: `/d/${target}`, day: dayLabel(target, thisYear) } : { broken: true } |
| 43 | } |
| 44 | const query = new URLSearchParams({ title: target }).toString() |
| 45 | return { href: `/new?${query}`, pending: true } |
| 46 | } |
| 47 | |
| 48 | export class Resolutions { |
| 49 | // links is the island's map; ask(keys) answers POST /links/resolve's |
| 50 | // JSON for up to 32 keys. |
| 51 | constructor (links = {}, ask = null) { |
| 52 | this.known = new Map(Object.entries(links)) |
| 53 | this.ask = ask |
| 54 | this.waiting = new Set() |
| 55 | this.asked = new Set() |
| 56 | this.watchers = new Set() |
| 57 | this.timer = null |
| 58 | } |
| 59 | |
| 60 | // answer is the chip state for a link now; a link never answered is |
| 61 | // asked for, once. |
| 62 | answer (target, anchor = '') { |
| 63 | const key = linkKey(target, anchor) |
| 64 | const known = answerOf(this.known.get(key)) |
| 65 | if (known) return known |
| 66 | if (this.ask && !this.asked.has(key)) { |
| 67 | this.waiting.add(key) |
| 68 | this.soon() |
| 69 | } |
| 70 | return guess(target) |
| 71 | } |
| 72 | |
| 73 | soon () { |
| 74 | if (this.timer !== null) return |
| 75 | this.timer = setTimeout(() => { |
| 76 | this.timer = null |
| 77 | this.flush() |
| 78 | }, 0) |
| 79 | } |
| 80 | |
| 81 | async flush () { |
| 82 | const keys = [...this.waiting].slice(0, batch) |
| 83 | if (keys.length === 0) return |
| 84 | for (const key of keys) { |
| 85 | this.waiting.delete(key) |
| 86 | this.asked.add(key) |
| 87 | } |
| 88 | if (this.waiting.size > 0) this.soon() |
| 89 | let reply |
| 90 | try { |
| 91 | reply = await this.ask(keys) |
| 92 | } catch { |
| 93 | // Unanswered links keep their guess; the next mention asks again. |
| 94 | for (const key of keys) this.asked.delete(key) |
| 95 | return |
| 96 | } |
| 97 | const changed = new Set() |
| 98 | for (const [key, state] of Object.entries(reply ?? {})) { |
| 99 | this.known.set(key, state) |
| 100 | changed.add(key) |
| 101 | } |
| 102 | if (changed.size) for (const watcher of this.watchers) watcher(changed) |
| 103 | } |
| 104 | |
| 105 | // lookup is the states of up to 32 keys, asked at once (for the [[ |
| 106 | // list), and kept for the chips. |
| 107 | async lookup (keys) { |
| 108 | const wanted = keys.filter(key => !this.known.has(key)).slice(0, batch) |
| 109 | if (wanted.length && this.ask) { |
| 110 | try { |
| 111 | for (const [key, state] of Object.entries(await this.ask(wanted) ?? {})) { |
| 112 | this.known.set(key, state) |
| 113 | } |
| 114 | } catch {} |
| 115 | } |
| 116 | return Object.fromEntries(keys.map(key => [key, this.known.get(key)])) |
| 117 | } |
| 118 | |
| 119 | // watch calls fn with the keys whose answers arrived. |
| 120 | watch (fn) { |
| 121 | this.watchers.add(fn) |
| 122 | return () => this.watchers.delete(fn) |
| 123 | } |
| 124 | } |
| 125 | |