| 1 | // scenes.mjs — scenes in the document (scenes and clips spec §2): the |
| 2 | // deck-level scenes map, the checks its ops make, its tolerant view, |
| 3 | // the scene and clip block props, and a scene's version (plan call R1). |
| 4 | // core/src/scene.rs is the twin, message for message. |
| 5 | // |
| 6 | // In the document, scenes is a root Y.Map of scene id to one Y.Map per |
| 7 | // scene: name, author and code (strings), states (a plain list of {id, |
| 8 | // name, description}), words (a Y.Map of key to text, so edits to two |
| 9 | // words merge), data (one plain JSON value, replaced whole) and stills |
| 10 | // (a Y.Map of state id to a plain {ref, made}). |
| 11 | // |
| 12 | // A scene's version is not stored: scene-version.mjs works it out. |
| 13 | import * as Y from 'yjs' |
| 14 | import { outToJSON, anyToJSON, put } from './ydoc.mjs' |
| 15 | import { canonicalJSON, utf8 } from './scene-version.mjs' |
| 16 | |
| 17 | export { canonicalJSON, sceneCanonical, sceneVersion, stillsCurrent } from './scene-version.mjs' |
| 18 | |
| 19 | // 960 KiB of JSON; ops.mjs also holds a scene batch to one update (live.go maxUpdateBytes). |
| 20 | export const MAX_SCENE_DATA = 960 * 1024 |
| 21 | export const MAX_UPDATE_BYTES = 1 << 20 |
| 22 | export const CLIP_PLAYS = ['press', 'loop', 'once'] |
| 23 | |
| 24 | const fail = msg => { throw new Error(msg) } |
| 25 | const q = JSON.stringify |
| 26 | const isObject = v => v !== null && typeof v === 'object' && !Array.isArray(v) && !(v instanceof Y.AbstractType) |
| 27 | export const hex64 = s => typeof s === 'string' && /^[0-9a-f]{64}$/.test(s) |
| 28 | // A scene id: the document's id pattern (ids.mjs), 12 lowercase base32. |
| 29 | export const sceneId = s => typeof s === 'string' && /^[a-z2-7]{12}$/.test(s) |
| 30 | // The key no scene may use as an id, a state id, a word key or anywhere |
| 31 | // in its data: lib0, Yjs's decoder for plain values, assigns it through |
| 32 | // the prototype setter, so a browser would lose it. |
| 33 | export const PROTO = '__proto__' |
| 34 | const own = (o, k) => Object.prototype.hasOwnProperty.call(o, k) |
| 35 | /** Whether v has an object key __proto__ at any depth. */ |
| 36 | const holdsProto = v => v !== null && typeof v === 'object' && (Array.isArray(v) ? v.some(holdsProto) : own(v, PROTO) || Object.values(v).some(holdsProto)) |
| 37 | |
| 38 | // --- what the ops take --- |
| 39 | |
| 40 | function textField (o, key, at) { |
| 41 | const v = o[key] |
| 42 | if (v === undefined || v === null) return '' |
| 43 | if (typeof v !== 'string') fail(`${at}.${key} must be a string`) |
| 44 | return v |
| 45 | } |
| 46 | |
| 47 | /** A scene's states checked and rebuilt from their known fields, and the ids in order. */ |
| 48 | export function sceneStates (v) { |
| 49 | if (!Array.isArray(v)) fail('states must be a list') |
| 50 | if (v.length === 0) fail('states must have at least one state') |
| 51 | const ids = [] |
| 52 | const out = v.map((s, i) => { |
| 53 | const at = `states[${i}]` |
| 54 | if (!isObject(s)) fail(`${at} must be a map`) |
| 55 | if (typeof s.id !== 'string') fail(`${at}.id must be a string`) |
| 56 | if (s.id === '') fail(`${at}.id is empty`) |
| 57 | if (s.id === PROTO) fail(`${at}.id may not be "${PROTO}"`) |
| 58 | if (ids.includes(s.id)) fail(`state ${q(s.id)} is used twice`) |
| 59 | ids.push(s.id) |
| 60 | return { id: s.id, name: textField(s, 'name', at), description: textField(s, 'description', at) } |
| 61 | }) |
| 62 | return { out, ids } |
| 63 | } |
| 64 | |
| 65 | /** words as an op sends them: key to text, or on set_scene to text or null. */ |
| 66 | export function sceneWords (v, patch) { |
| 67 | if (!isObject(v)) fail('words must be a map') |
| 68 | if (own(v, PROTO)) fail(`words may not have the key "${PROTO}"`) |
| 69 | for (const [k, w] of Object.entries(v)) { |
| 70 | if (typeof w === 'string' || (patch && w === null)) continue |
| 71 | fail(patch ? `words.${k} must be a string or null` : `words.${k} must be a string`) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** data within MAX_SCENE_DATA, measured in UTF-8 bytes of its canonical JSON. */ |
| 76 | export function sceneData (v) { |
| 77 | if (holdsProto(v)) fail(`data may not hold the key "${PROTO}"`) |
| 78 | if (utf8(canonicalJSON(v)).length > MAX_SCENE_DATA) fail('data is larger than 960 KiB') |
| 79 | } |
| 80 | |
| 81 | /** stills as set_scene sends them: state id to {ref, made} or null, each naming one of ids. */ |
| 82 | export function sceneStills (v, scene, ids) { |
| 83 | if (!isObject(v)) fail('stills must be a map') |
| 84 | for (const [state, still] of Object.entries(v)) { |
| 85 | // null removes, and may name a state the scene no longer has |
| 86 | if (still === null) continue |
| 87 | if (!ids.includes(state)) fail(`scene ${q(scene)} has no state ${q(state)}`) |
| 88 | if (!isObject(still)) fail(`stills.${state} must be a map or null`) |
| 89 | for (const key of ['ref', 'made']) { |
| 90 | if (!hex64(still[key])) fail(`stills.${state}.${key} is not 64 lowercase hex digits`) |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | const still = v => ({ ref: v.ref, made: v.made }) |
| 96 | |
| 97 | function checkCode (code) { |
| 98 | if (!hex64(code)) fail('code is not 64 lowercase hex digits') |
| 99 | } |
| 100 | |
| 101 | /** add_scene, set_scene and remove_scene against the ops' model (ops-model.mjs). */ |
| 102 | export function checkSceneOp (model, op) { |
| 103 | switch (op.op) { |
| 104 | case 'add_scene': { |
| 105 | if (!sceneId(op.id)) fail(`scene id ${q(op.id)} is not 12 lowercase base32 characters`) |
| 106 | if (model.scenes.has(op.id)) fail(`scene ${q(op.id)} already exists`) |
| 107 | checkCode(op.code) |
| 108 | const { ids } = sceneStates(op.states) |
| 109 | if (op.words !== undefined) sceneWords(op.words, false) |
| 110 | if (op.data !== undefined) sceneData(op.data) |
| 111 | model.scenes.set(op.id, ids) |
| 112 | return |
| 113 | } |
| 114 | case 'set_scene': |
| 115 | if (!model.scenes.has(op.id)) fail(`no scene ${q(op.id)}`) |
| 116 | if (op.code !== undefined) checkCode(op.code) |
| 117 | if (op.states !== undefined) model.scenes.set(op.id, sceneStates(op.states).ids) |
| 118 | if (op.words !== undefined) sceneWords(op.words, true) |
| 119 | if (op.data !== undefined && op.data !== null) sceneData(op.data) |
| 120 | if (op.stills !== undefined) sceneStills(op.stills, op.id, model.scenes.get(op.id)) |
| 121 | return |
| 122 | case 'remove_scene': |
| 123 | // The blocks that show it stay as they are (Review Focus 3). |
| 124 | if (!model.scenes.delete(op.id)) fail(`no scene ${q(op.id)}`) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // --- scene and clip blocks --- |
| 129 | |
| 130 | const SCENE_KEYS = ['scene', 'from', 'to'] |
| 131 | |
| 132 | /** What a scene block's props point at: the strings among scene, from and to. */ |
| 133 | export function sceneRefOf (props) { |
| 134 | const out = {} |
| 135 | for (const k of SCENE_KEYS) out[k] = typeof props[k] === 'string' ? props[k] : null |
| 136 | return out |
| 137 | } |
| 138 | |
| 139 | /** Applies an op's props to ref, checking each key it sets; whether it touched any. */ |
| 140 | function patchRef (ref, props) { |
| 141 | let touched = false |
| 142 | for (const key of SCENE_KEYS) { |
| 143 | const v = props[key] |
| 144 | if (v === undefined) continue |
| 145 | if (v !== null && typeof v !== 'string') fail(`scene block props.${key} must be a string`) |
| 146 | ref[key] = v |
| 147 | touched = true |
| 148 | } |
| 149 | return touched |
| 150 | } |
| 151 | |
| 152 | /** Whether ref names a scene the deck has, and two of its states in order. */ |
| 153 | function checkRef (model, ref) { |
| 154 | const { scene, from, to } = ref |
| 155 | if (scene === null) { |
| 156 | if (from !== null || to !== null) fail('scene block props.from and props.to need props.scene') |
| 157 | return |
| 158 | } |
| 159 | const ids = model.scenes.get(scene) ?? fail(`no scene ${q(scene)}`) |
| 160 | const at = s => { |
| 161 | if (s === null) return null |
| 162 | const i = ids.indexOf(s) |
| 163 | if (i === -1) fail(`scene ${q(scene)} has no state ${q(s)}`) |
| 164 | return i |
| 165 | } |
| 166 | const f = at(from) |
| 167 | const t = at(to) |
| 168 | if (f !== null && t !== null && f > t) fail(`from ${q(from)} comes after to ${q(to)} in scene ${q(scene)}`) |
| 169 | } |
| 170 | |
| 171 | /** A clip block's props as an op sets them: play one of CLIP_PLAYS, asset a string; null removes either. */ |
| 172 | function clipProps (props) { |
| 173 | const play = props.play |
| 174 | if (play !== undefined && play !== null && !CLIP_PLAYS.includes(play)) fail(`play ${q(play)} is not one of ${CLIP_PLAYS.join(', ')}`) |
| 175 | const asset = props.asset |
| 176 | if (asset !== undefined && asset !== null && typeof asset !== 'string') fail('clip block props.asset must be a string') |
| 177 | } |
| 178 | |
| 179 | /** add_block's props on a block of kind: checked; the model's scene ref (null unless a scene block). */ |
| 180 | export function newBlockScene (model, kind, props) { |
| 181 | if (kind === 'clip') clipProps(props) |
| 182 | if (kind !== 'scene') return null |
| 183 | const ref = sceneRefOf({}) |
| 184 | if (patchRef(ref, props)) checkRef(model, ref) |
| 185 | return ref |
| 186 | } |
| 187 | |
| 188 | /** set_block's props on the model's block b, checked and kept in step. */ |
| 189 | export function setBlockScene (model, b, props) { |
| 190 | if (b.kind === 'clip') clipProps(props) |
| 191 | if (b.scene) { |
| 192 | const ref = { ...b.scene } |
| 193 | if (patchRef(ref, props)) checkRef(model, ref) |
| 194 | b.scene = ref |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // --- the view, and the model's reading --- |
| 199 | |
| 200 | /** A scene's states as the view shows them: maps with a non-empty string id, the first of each. */ |
| 201 | function viewStates (v) { |
| 202 | if (!Array.isArray(v)) return [] |
| 203 | const seen = new Set() |
| 204 | const out = [] |
| 205 | for (const s of v) { |
| 206 | if (!isObject(s) || typeof s.id !== 'string' || s.id === '' || seen.has(s.id)) continue |
| 207 | seen.add(s.id) |
| 208 | const field = k => (typeof s[k] === 'string' ? s[k] : '') |
| 209 | out.push({ id: s.id, name: field('name'), description: field('description') }) |
| 210 | } |
| 211 | return out |
| 212 | } |
| 213 | |
| 214 | /** One scene as the view shows it, from its entry read as JSON; null for an entry that is not a map. */ |
| 215 | export function sceneView (entry) { |
| 216 | if (!isObject(entry)) return null |
| 217 | const text = k => (typeof entry[k] === 'string' ? entry[k] : '') |
| 218 | const words = {} |
| 219 | if (isObject(entry.words)) for (const [k, w] of Object.entries(entry.words)) if (typeof w === 'string') put(words, k, w) |
| 220 | const stills = {} |
| 221 | if (isObject(entry.stills)) { |
| 222 | for (const [k, s] of Object.entries(entry.stills)) { |
| 223 | if (isObject(s) && typeof s.ref === 'string' && typeof s.made === 'string') put(stills, k, still(s)) |
| 224 | } |
| 225 | } |
| 226 | return { |
| 227 | name: text('name'), |
| 228 | author: text('author'), |
| 229 | code: text('code'), |
| 230 | states: viewStates(entry.states), |
| 231 | words, |
| 232 | data: entry.data === undefined ? null : entry.data, |
| 233 | stills |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** Each scene's state ids, as the view shows them: reading only states, never the data. */ |
| 238 | export function sceneStatesOf (scenes) { |
| 239 | const out = new Map() |
| 240 | scenes.forEach((v, k) => { |
| 241 | if (v instanceof Y.Map) out.set(k, viewStates(outToJSON(v.get('states'))).map(s => s.id)) |
| 242 | else if (isObject(v)) out.set(k, viewStates(anyToJSON(v.states)).map(s => s.id)) |
| 243 | }) |
| 244 | return out |
| 245 | } |
| 246 | |
| 247 | // --- apply --- |
| 248 | |
| 249 | function patchMap (m, key, patch, value) { |
| 250 | let target = m.get(key) |
| 251 | if (!(target instanceof Y.Map)) { |
| 252 | const old = isObject(target) ? target : {} |
| 253 | target = new Y.Map() |
| 254 | m.set(key, target) |
| 255 | for (const k of Object.keys(old)) target.set(k, old[k]) |
| 256 | } |
| 257 | for (const [k, v] of Object.entries(patch)) { |
| 258 | if (v === null) target.delete(k) |
| 259 | else target.set(k, value(v)) |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /** add_scene, set_scene and remove_scene on the roots (ydoc.mjs), after the model passed them. */ |
| 264 | export function applySceneOp (r, op) { |
| 265 | switch (op.op) { |
| 266 | case 'add_scene': { |
| 267 | const m = new Y.Map() |
| 268 | r.scenes.set(op.id, m) |
| 269 | m.set('name', op.name) |
| 270 | m.set('code', op.code) |
| 271 | m.set('states', sceneStates(op.states).out) |
| 272 | const words = new Y.Map() |
| 273 | m.set('words', words) |
| 274 | for (const [k, w] of Object.entries(op.words ?? {})) words.set(k, w) |
| 275 | m.set('data', op.data ?? null) |
| 276 | m.set('stills', new Y.Map()) |
| 277 | if (op.author !== undefined) m.set('author', op.author) |
| 278 | return |
| 279 | } |
| 280 | case 'set_scene': { |
| 281 | let m = r.scenes.get(op.id) |
| 282 | if (!(m instanceof Y.Map)) { |
| 283 | // a scene a peer wrote as one plain map: a Y.Map of its fields |
| 284 | const fields = isObject(m) ? m : fail(`no scene ${q(op.id)}`) |
| 285 | m = new Y.Map() |
| 286 | r.scenes.set(op.id, m) |
| 287 | for (const k of Object.keys(fields)) m.set(k, fields[k]) |
| 288 | } |
| 289 | for (const key of ['name', 'code', 'author']) if (op[key] !== undefined) m.set(key, op[key]) |
| 290 | if (op.states !== undefined) m.set('states', sceneStates(op.states).out) |
| 291 | if (isObject(op.words)) patchMap(m, 'words', op.words, w => w) |
| 292 | if (op.data !== undefined) m.set('data', op.data) |
| 293 | if (isObject(op.stills)) patchMap(m, 'stills', op.stills, still) |
| 294 | return |
| 295 | } |
| 296 | case 'remove_scene': |
| 297 | r.scenes.delete(op.id) |
| 298 | } |
| 299 | } |
| 300 | |