oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1// scene-frame.mjs — one scene's sealed frame and its conversation
2// (scenes and clips spec §3; docs/scenes.md, the contract).
3//
4// The frame's document comes from a sealed route (scenes.go): an
5// origin of its own that matches nothing, no network, nothing of the
6// page. Once it has loaded it is told init (its state, its size, its
7// words and data, whether the reader asked for less motion, and the
8// deck faces it named), then state on every move. It answers ready,
9// once; until then it is not shown, and the block's still under it is
10// what the reader sees. It is given up — taken out, the still left —
11// when it has not said ready 3 seconds after init, when it navigates
12// (a second load), or when it says error. Messages count only from the
13// frame's own window. (A reader who asked for less motion is shown the
14// stills and no frame at all: scene-layer.mjs makes none.)
15//
16// createSceneFrame({el, win, scene: {words, data}, state, reducedMotion,
17// fonts: () => Promise<[{family, src, weight, style}]>, onLive(live),
18// onFail(why), timers?}) → {state(id, animate), live, failed, destroy()}
19// el is the <iframe>, its src not yet set, so its first load is seen.
20
21/** How long a scene has to say ready once it has been told init. */
22export const READY_MS = 3000
23/** How long init waits for the faces a scene named. */
24const FONTS_MS = 2000
25
26const realTimers = { set: (fn, ms) => setTimeout(fn, ms), clear: id => clearTimeout(id) }
27
28export function createSceneFrame ({ el, win, scene = {}, state, reducedMotion = false, fonts = () => Promise.resolve([]), onLive = () => {}, onFail = () => {}, timers = realTimers }) {
29 let current = state
30 let loads = 0
31 let told = false
32 let ready = false
33 let failed = false
34 let live = false
35 let wait = null
36
37 function post (m) {
38 try {
39 // The scene's origin matches nothing, so '*' is the only target.
40 el.contentWindow.postMessage(m, '*')
41 } catch { /* a frame on its way out */ }
42 }
43
44 function update () {
45 const now = ready && told && !failed
46 // Opacity, not visibility: a layer hidden by its page (a blackout,
47 // the font hold) stays hidden whatever its frames say.
48 el.style.opacity = now ? '1' : '0'
49 if (now !== live) {
50 live = now
51 onLive(live)
52 }
53 }
54
55 function stop () {
56 win.removeEventListener('message', heard)
57 el.removeEventListener('load', loaded)
58 if (wait !== null) timers.clear(wait)
59 wait = null
60 }
61
62 function fail (why) {
63 if (failed) return
64 failed = true
65 stop()
66 update()
67 el.remove()
68 onFail(why)
69 }
70
71 async function loaded () {
72 loads++
73 if (loads > 1) return fail('navigated')
74 let faces = []
75 try {
76 faces = await new Promise((resolve, reject) => {
77 const t = timers.set(() => resolve([]), FONTS_MS)
78 Promise.resolve(fonts()).then(v => { timers.clear(t); resolve(Array.isArray(v) ? v : []) }, reject)
79 })
80 } catch { faces = [] }
81 if (failed) return
82 const size = { w: Math.round(el.clientWidth || 0), h: Math.round(el.clientHeight || 0) }
83 post({ type: 'init', state: current, size, words: scene.words || {}, data: scene.data === undefined ? null : scene.data, reducedMotion: !!reducedMotion, fonts: faces })
84 told = true
85 wait = timers.set(() => { wait = null; if (!ready) fail('silent') }, READY_MS)
86 update()
87 }
88
89 function heard (e) {
90 if (failed || !el.contentWindow || e.source !== el.contentWindow) return
91 const m = e.data
92 if (!m || typeof m !== 'object') return
93 if (m.type === 'ready' && !ready) {
94 ready = true
95 if (wait !== null) timers.clear(wait)
96 wait = null
97 update()
98 } else if (m.type === 'error') {
99 fail('error')
100 }
101 }
102
103 el.style.opacity = '0'
104 el.addEventListener('load', loaded)
105 win.addEventListener('message', heard)
106
107 return {
108 el,
109 /** Moves the scene to state id; animate is false for a jump. */
110 state (id, animate) {
111 if (failed) return
112 if (!told) {
113 current = id
114 return
115 }
116 current = id
117 post({ type: 'state', state: id, animate: !!animate })
118 },
119 get live () { return live },
120 get failed () { return failed },
121 destroy () {
122 if (failed) return
123 failed = true
124 stop()
125 update()
126 el.remove()
127 }
128 }
129}
130