| 1 | // talk-link.mjs — a window's link to the server while it presents |
| 2 | // (spec §6, "Moving through the talk"). Shared by the presenter window |
| 3 | // and the slide window. |
| 4 | // |
| 5 | // The window moves at once, from what it holds, and then tells the |
| 6 | // link. The link tells the server, and decides which of the server's |
| 7 | // positions the window should take: |
| 8 | // |
| 9 | // - Presses go to the server one at a time, in order, each with its own |
| 10 | // id, so a press sent twice counts once (presenting_position.go). |
| 11 | // - While a press is unanswered, the server's positions (its answers |
| 12 | // and the stream's events) are recorded but not applied. When the |
| 13 | // queue empties, the newest is applied if its seq is higher than the |
| 14 | // last applied and it differs from where the window stands. |
| 15 | // - A send that fails or takes over 3 seconds marks the link offline. |
| 16 | // Unanswered presses are dropped: the position they reached is kept, |
| 17 | // and the server is told it on reconnecting. |
| 18 | // - On reconnecting, a window that pressed while offline (or lost a |
| 19 | // press) sends resume with where it stands, black screen included; |
| 20 | // one that did not takes the server's position. |
| 21 | // - The talk's timer comes with every position (timerAt, Unix ms, or |
| 22 | // null while it waits for a press), and is taken from the newest one |
| 23 | // at the same moment, whether or not the window moves: another |
| 24 | // window's first press starts it here, and a reset there resets it. |
| 25 | // - Every command carries the link's id and its number, rising. The |
| 26 | // server applies a link's command only if its number is above every |
| 27 | // one it has applied, so a press given up on cannot reach the talk |
| 28 | // after the resume that replaced it (presenting_position.go). |
| 29 | // |
| 30 | // post(fields) sends one press and resolves with the server's answer, |
| 31 | // {slide, step, mode, black, seq, deckSeq, timerAt}. stream({open, error, |
| 32 | // position}) starts hearing the server's positions in the same shape |
| 33 | // and returns a function that stops. now() is the time in ms, and |
| 34 | // after(ms, fn) a timer that returns its cancel: both are injected so |
| 35 | // a test can run on a fake clock. |
| 36 | |
| 37 | // How long a press may wait for its answer before the link is offline. |
| 38 | export const ANSWER_TIMEOUT_MS = 3000 |
| 39 | |
| 40 | // How often an offline link that owes the server a resume tries again, |
| 41 | // when the stream has not already said the network is back. |
| 42 | export const RETRY_MS = 2000 |
| 43 | |
| 44 | // How long the stream may be gone before the link is offline. The |
| 45 | // server ends every stream after five minutes and the browser opens it |
| 46 | // again three seconds later; that is not a split, and a room should not |
| 47 | // see the slide window say it is waiting each time. |
| 48 | export const STREAM_GRACE_MS = 5000 |
| 49 | |
| 50 | const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_' |
| 51 | |
| 52 | /** A press id: 22 characters from the alphabet the server accepts, from crypto.getRandomValues. */ |
| 53 | export function pressID () { |
| 54 | const bytes = new Uint8Array(22) |
| 55 | globalThis.crypto.getRandomValues(bytes) |
| 56 | let out = '' |
| 57 | // 64 letters divide 256 evenly, so each is equally likely. |
| 58 | for (const b of bytes) out += ALPHABET[b & 63] |
| 59 | return out |
| 60 | } |
| 61 | |
| 62 | // The ops moves.mjs names, as the command form names them. |
| 63 | const WIRE = { back: 'prev', black: 'blackout' } |
| 64 | |
| 65 | /** The form fields for a move: {op, slide} in moves.mjs's words. */ |
| 66 | export function pressFields (move) { |
| 67 | const fields = { op: WIRE[move.op] || move.op } |
| 68 | if (move.slide) fields.slide = String(move.slide) |
| 69 | return fields |
| 70 | } |
| 71 | |
| 72 | /** Whether two positions differ in anything the window draws. */ |
| 73 | export function differs (a, b) { |
| 74 | if (!a || !b) return a !== b |
| 75 | return a.slide !== b.slide || (a.step || 0) !== (b.step || 0) || a.mode !== b.mode || !!a.black !== !!b.black |
| 76 | } |
| 77 | |
| 78 | const defaultAfter = (ms, fn) => { |
| 79 | const id = setTimeout(fn, ms) |
| 80 | return () => clearTimeout(id) |
| 81 | } |
| 82 | |
| 83 | export function createLink ({ |
| 84 | post, |
| 85 | stream, |
| 86 | now = () => Date.now(), |
| 87 | after = defaultAfter, |
| 88 | onPosition = () => {}, |
| 89 | onStatus = () => {}, |
| 90 | onDeckSeq = () => {}, |
| 91 | onTimer = () => {}, |
| 92 | makeID = pressID |
| 93 | }) { |
| 94 | let online = true |
| 95 | let queue = [] // presses not yet answered: [{fields}] |
| 96 | let sending = null // the press on the wire, while it is |
| 97 | let owed = false // the server has not heard where this window went |
| 98 | let standing = null // where the window stands |
| 99 | let newest = null // the newest position the server has said |
| 100 | let applied = 0 // the seq of the last position taken or matched |
| 101 | let retry = null |
| 102 | let gap = null // the stream's grace, while it is gone |
| 103 | const linkID = makeID() // this window's link, for as long as it is loaded |
| 104 | let count = 0 // the number of the newest command made |
| 105 | const command = fields => ({ id: makeID(), n: ++count, fields }) |
| 106 | |
| 107 | function status () { return online ? 'online' : 'offline' } |
| 108 | |
| 109 | function say (s) { |
| 110 | const was = online |
| 111 | online = s === 'online' |
| 112 | if (was !== online) onStatus(status()) |
| 113 | } |
| 114 | |
| 115 | // hear is every position the server says, from any source. |
| 116 | function hear (pos) { |
| 117 | if (!pos || typeof pos.seq !== 'number') return |
| 118 | if (typeof pos.deckSeq === 'number') onDeckSeq(pos.deckSeq) |
| 119 | if (!newest || pos.seq > newest.seq) newest = pos |
| 120 | } |
| 121 | |
| 122 | // take applies the newest position once nothing is waiting: its |
| 123 | // timer always, the position where it differs. |
| 124 | function take () { |
| 125 | if (!online || sending || queue.length || !newest || newest.seq <= applied) return |
| 126 | applied = newest.seq |
| 127 | if ('timerAt' in newest) onTimer(newest.timerAt === null ? null : Number(newest.timerAt)) |
| 128 | if (!differs(newest, standing)) return |
| 129 | standing = { slide: newest.slide, step: newest.step || 0, mode: newest.mode, black: !!newest.black } |
| 130 | onPosition({ ...standing, seq: newest.seq }) |
| 131 | } |
| 132 | |
| 133 | function pump () { |
| 134 | if (!online || sending || !queue.length) return |
| 135 | const item = queue[0] |
| 136 | const sentAt = now() |
| 137 | // mine is this send; once the link has given up on it (lost), what |
| 138 | // comes back for it is nobody's. |
| 139 | const mine = { item } |
| 140 | sending = mine |
| 141 | const cancel = after(ANSWER_TIMEOUT_MS, () => { if (sending === mine) lost() }) |
| 142 | Promise.resolve() |
| 143 | .then(() => post({ ...item.fields, press: item.id, link: linkID, n: String(item.n) })) |
| 144 | .then(answer => { |
| 145 | if (sending !== mine) return |
| 146 | cancel() |
| 147 | // An answer that took too long is as good as lost: the window |
| 148 | // may have been throttled and the timer late. |
| 149 | if (now() - sentAt > ANSWER_TIMEOUT_MS) { lost(); return } |
| 150 | sending = null |
| 151 | queue.shift() |
| 152 | hear(answer) |
| 153 | if (queue.length) pump() |
| 154 | else take() |
| 155 | }, () => { |
| 156 | if (sending !== mine) return |
| 157 | cancel() |
| 158 | lost() |
| 159 | }) |
| 160 | } |
| 161 | |
| 162 | // lost is the network gone: every unanswered press is dropped, and |
| 163 | // the window's position is owed to the server instead. |
| 164 | function lost () { |
| 165 | if (sending || queue.length) owed = true |
| 166 | sending = null |
| 167 | queue = [] |
| 168 | say('offline') |
| 169 | schedule() |
| 170 | } |
| 171 | |
| 172 | // schedule tries the resume again while offline and owing one. |
| 173 | function schedule () { |
| 174 | if (retry || online || !owed) return |
| 175 | retry = after(RETRY_MS, () => { |
| 176 | retry = null |
| 177 | if (online || !owed) return |
| 178 | reconnect() |
| 179 | }) |
| 180 | } |
| 181 | |
| 182 | // reconnect is the network back: the resume if one is owed, which |
| 183 | // is a press like any other (it may fail, and the link is offline |
| 184 | // again); otherwise the server's newest position is taken. |
| 185 | function reconnect () { |
| 186 | if (retry) { retry(); retry = null } |
| 187 | say('online') |
| 188 | if (owed && standing) { |
| 189 | owed = false |
| 190 | queue = [command({ |
| 191 | op: 'resume', |
| 192 | at_slide: standing.slide, |
| 193 | at_step: String(standing.step || 0), |
| 194 | at_mode: standing.mode, |
| 195 | at_black: standing.black ? 'true' : 'false' |
| 196 | })] |
| 197 | pump() |
| 198 | return |
| 199 | } |
| 200 | take() |
| 201 | } |
| 202 | |
| 203 | function heardFrom () { |
| 204 | if (gap) { gap(); gap = null } |
| 205 | } |
| 206 | |
| 207 | stream({ |
| 208 | open () { |
| 209 | heardFrom() |
| 210 | if (!online) reconnect() |
| 211 | }, |
| 212 | error () { |
| 213 | // The stream opens again by itself. Gone for longer than its |
| 214 | // grace, nothing is reaching this window: offline. |
| 215 | if (!online || gap) return |
| 216 | gap = after(STREAM_GRACE_MS, () => { gap = null; if (online) lost() }) |
| 217 | }, |
| 218 | position (pos) { |
| 219 | heardFrom() |
| 220 | hear(pos) |
| 221 | // Hearing anything means the network is back. |
| 222 | if (!online) reconnect() |
| 223 | else take() |
| 224 | } |
| 225 | }) |
| 226 | |
| 227 | return { |
| 228 | /** The window has moved by move to landedAt; tell the server. */ |
| 229 | press (move, landedAt) { |
| 230 | if (landedAt) standing = { ...landedAt } |
| 231 | if (!online) { owed = true; schedule(); return } |
| 232 | queue.push(command(pressFields(move))) |
| 233 | pump() |
| 234 | }, |
| 235 | /** The window stands at position without a press (it started there, or a newer deck settled it). */ |
| 236 | resume (position) { |
| 237 | standing = position ? { ...position } : null |
| 238 | }, |
| 239 | status |
| 240 | } |
| 241 | } |
| 242 | |