// talk-store.mjs — the whole talk, in memory (spec §6, "Getting // ready"; S17). Shared by the presenter window and the slide window. // // GET /p/{session}/talk (presenting_talk.go) is every slide of the full // talk, rendered at press 0, with the pictures it shows (a clip's poster // among them, and its video fetched beside them) and, for whoever // may drive, its notes. A slide is ready once its families are on (the // font gate's prepareSection) and its pictures are decoded, so a press // that lands on it needs no network. Until it is ready the window keeps // what it shows (has, ready). // // The talk lives in memory only. A newer version of the deck (refresh) // is loaded beside the one in use, quietly, and taken when the window // says so (adopt), which it does at its next change of slide. // // view is the version in moves.mjs's shape, so talkOrder, advance and // settleNear answer for the talk as the window holds it: each slide's // talk ('full' or in the talk; parked slides never reach a reader) and // one box per press, which is all presses() needs to count them. // How many slides get ready at once. const AT_ONCE = 4 /** A slide's presses as boxes presses() counts: n boxes on steps 1 to n. */ function pressBoxes (n) { const out = [] for (let k = 1; k <= n; k++) out.push({ id: 'press' + k, type: 'image', step: k }) return out } function version (payload, etag) { const slides = Array.isArray(payload.slides) ? payload.slides : [] return { deckSeq: Number(payload.deckSeq) || 0, arrive: payload.arrive || 'appear', etag, slides, byID: new Map(slides.map(s => [s.id, s])), view: { slides: slides.map(s => ({ id: s.id, talk: s.full ? 'full' : '', blocks: pressBoxes(Number(s.presses) || 0) })) }, ready: new Set(), failed: new Set(), waiters: new Map(), // The deck's uploaded fonts as the font manifest carries them // (presenting_talk.go): learned before a slide is made ready. uploaded: payload.uploaded || null, // The scenes its slides show (scene_pages.go), as the frame layer // runs them: each bundle's address, name, words and data. scenes: payload.scenes && typeof payload.scenes === 'object' ? payload.scenes : {} } } // The pictures decoded, held so the browser keeps them for the talk. const held = [] // The scene bundles already fetched. const fetched = new Set() // Clips' videos are warmed into the browser's HTTP cache (assets.go // answers them private, immutable, for a year), one at a time, each // body read and dropped as it streams, never held. Warming stops once // 300 MiB have come: what is not warm loads when its slide plays it. const WARM_LIMIT = 300 * 1024 * 1024 const warmed = new Set() let warmedBytes = 0 let warming = Promise.resolve() function warmClip (fetchImpl, src) { if (typeof src !== 'string' || warmed.has(src)) return warmed.add(src) warming = warming.then(async () => { if (warmedBytes >= WARM_LIMIT) return const r = await fetchImpl(src, { credentials: 'same-origin' }) const reader = r && r.ok && r.body && r.body.getReader ? r.body.getReader() : null if (!reader) return // One that would go past the limit is not begun, and ends warming. if (warmedBytes + (Number(r.headers && r.headers.get('Content-Length')) || 0) > WARM_LIMIT) { warmedBytes = WARM_LIMIT return reader.cancel() } for (;;) { const { done, value } = await reader.read() if (done) return warmedBytes += value.byteLength if (warmedBytes >= WARM_LIMIT) return reader.cancel() } }).catch(() => { warmed.delete(src) }) } /** A picture fetched and decoded, so drawing it later waits for nothing. */ export function decodePicture (url) { const img = new Image() img.decoding = 'async' img.src = url held.push(img) return img.decode() } export async function loadTalk ({ href, fonts = () => globalThis.slidesFonts, onProgress = () => {}, from = 0, fetchImpl = (...args) => globalThis.fetch(...args), picture = decodePicture }) { const fontsOf = typeof fonts === 'function' ? fonts : () => fonts async function fetchTalk (etag) { const headers = { Accept: 'application/json' } if (etag) headers['If-None-Match'] = etag const r = await fetchImpl(href, { credentials: 'same-origin', cache: 'no-store', headers }) if (r.status === 304) return null if (!r.ok) throw new Error('talk: ' + r.status) return version(await r.json(), r.headers.get('ETag')) } // prepare makes one slide ready: its families on, its pictures // decoded. A slide that fails is tried again by retry(). async function prepare (v, s) { try { const f = fontsOf() // A font the deck took on after this window opened is learned from // the version, so the slide waits for it (final review, I6). if (f && typeof f.learnUploaded === 'function' && v.uploaded) f.learnUploaded(v.uploaded) await Promise.all([ f && f.prepareSection ? f.prepareSection(s.html) : null, ...(Array.isArray(s.assets) ? s.assets : []).map(picture) ]) v.ready.add(s.id) v.failed.delete(s.id) const waiting = v.waiters.get(s.id) || [] v.waiters.delete(s.id) for (const resolve of waiting) resolve(true) } catch { v.failed.add(s.id) } } // prepareAll gets every slide of v ready, a few at a time, starting // at index start (the slide on screen), saying how far it has got. async function prepareAll (v, start, report) { // Each scene's bundle into the cache, so its frame needs no network; // each clip's video warmed (warmClip), which no slide waits for: a // clip not yet here shows its poster, and its press still counts. for (const sc of Object.values(v.scenes)) { if (sc && typeof sc.src === 'string' && !fetched.has(sc.src)) { fetched.add(sc.src) Promise.resolve().then(() => fetchImpl(sc.src, { credentials: 'same-origin' })).catch(() => fetched.delete(sc.src)) } } const n = v.slides.length const order = [] for (let i = 0; i < n; i++) order.push(v.slides[(start + i) % n]) // From the slide on screen on. for (const s of order) for (const src of (Array.isArray(s.clips) ? s.clips : [])) warmClip(fetchImpl, src) const say = finished => report && onProgress({ done: v.ready.size, total: n, failed: v.failed.size, finished }) let next = 0 say(false) await Promise.all(Array.from({ length: Math.min(AT_ONCE, n) }, async () => { while (next < n) { const s = order[next++] if (v.ready.has(s.id)) continue await prepare(v, s) say(false) } })) say(true) } let current = await fetchTalk(null) let pending = null let loading = null prepareAll(current, Math.max(0, Math.min(from, current.slides.length - 1)), true) return { get deckSeq () { return current.deckSeq }, get arrive () { return current.arrive }, get view () { return current.view }, get slides () { return current.slides }, get scenes () { return current.scenes }, /** The deckSeq of a newer version held and not yet taken, or 0. */ get pending () { return pending ? pending.deckSeq : 0 }, /** That newer version's view, or null. */ get pendingView () { return pending ? pending.view : null }, slide (id) { return current.byID.get(id) || null }, has (id) { return current.ready.has(id) }, /** Resolves true once the slide is ready, false if this version has no such slide. */ ready (id) { if (current.ready.has(id)) return Promise.resolve(true) if (!current.byID.has(id)) return Promise.resolve(false) const v = current return new Promise(resolve => { if (!v.waiters.has(id)) v.waiters.set(id, []) v.waiters.get(id).push(resolve) }) }, /** * Asks the server for a newer version and, if there is one, holds * it beside this one (pending, from the moment it arrives) and gets * it ready. Resolves true once a newer version is held and every * slide of it has been tried. */ refresh () { if (loading) return loading const have = pending || current loading = fetchTalk(have.etag).then(async v => { if (!v || v.deckSeq <= have.deckSeq) return false pending = v await prepareAll(v, 0, false) return true }).catch(() => false).finally(() => { loading = null }) return loading }, /** Takes the newer version, if one is held, and returns the view it replaced (or null). */ adopt () { if (!pending) return null const before = current.view // A slide both versions draw the same is ready in the new one. for (const s of pending.slides) { const old = current.byID.get(s.id) if (old && current.ready.has(s.id) && old.html === s.html) pending.ready.add(s.id) } current = pending pending = null prepareAll(current, 0, false) return before }, /** Tries again the slides that did not get ready. */ retry () { for (const v of [current, pending]) { if (v && v.failed.size) prepareAll(v, 0, v === current) } } } }