oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// The operations the inspector and the drive surface apply (spec §7.2),
2// over a Y.Doc in the browser — the same vocabulary as core/src/ops.rs,
3// with the same result. A new op is added to both or to neither; the
4// vectors under vectors/ hold the two to one view.
5//
6// applyOps(ydoc, ops, origin) parses and checks the whole batch against a
7// model of the deck first (ops-model.mjs) and throws an OpsError — with
8// the core's own message, prefixed `op N:` — having written nothing;
9// only then does it apply every op (ops-apply.mjs) inside one
10// ydoc.transact with the given origin, so the undo manager and the
11// save queue see one change.
12//
13// Ids: the core never draws entropy and neither does this module. Every
14// new slide or block id arrives in the op (`id`, `new_id`,
15// `new_block_ids`); the browser mints them with ids.mjs before calling.
16//
17// A batch with add_scene or set_scene in it is also applied to a copy
18// of the document first, and refused (OpsError, tooBig) when the update
19// it makes is over MAX_UPDATE_BYTES: data within its 960 KiB of JSON can
20// still encode past what one live update may carry, and an edit the
21// server will refuse must not be taken here first. core/src/ops.rs does
22// the same, message for message.
23import * as Y from 'yjs'
24import { roots } from './ydoc.mjs'
25import { Model, OpsError, parseOp } from './ops-model.mjs'
26import { applyOne } from './ops-apply.mjs'
27import { MAX_UPDATE_BYTES } from './scenes.mjs'
28
29export { OpsError } from './ops-model.mjs'
30export { MAX_UPDATE_BYTES } from './scenes.mjs'
31export { view } from './view.mjs'
32
33function at (i, e, applying = false) {
34 const msg = e instanceof Error ? e.message : String(e)
35 return new OpsError(`op ${i}${applying ? ' (applying)' : ''}: ${msg}`, i)
36}
37
38/**
39 * Applies ops (an array of §7.2 operations) to ydoc in one transaction.
40 * Throws OpsError, applying nothing, when any op is malformed or names
41 * something the deck (as the batch so far leaves it) does not have.
42 */
43export function applyOps (ydoc, ops, origin = 'ops') {
44 if (!Array.isArray(ops)) throw new OpsError('ops must be a JSON array', -1)
45 const parsed = ops.map((raw, i) => {
46 try { return parseOp(raw) } catch (e) { throw at(i, e) }
47 })
48 const r = roots(ydoc)
49 const model = Model.read(r)
50 parsed.forEach((op, i) => {
51 try { model.check(op) } catch (e) { throw at(i, e) }
52 })
53 const scene = parsed.findIndex(op => op.op === 'add_scene' || op.op === 'set_scene')
54 if (scene !== -1 && updateSize(ydoc, parsed) > MAX_UPDATE_BYTES) {
55 const e = at(scene, `scene ${JSON.stringify(parsed[scene].id)} would make an update larger than 1 MiB, the most one live update may carry (number-heavy data encodes larger than its JSON)`)
56 e.tooBig = true
57 throw e
58 }
59 ydoc.transact(() => {
60 parsed.forEach((op, i) => {
61 try { applyOne(r, op) } catch (e) { throw at(i, e, true) }
62 })
63 }, origin)
64}
65
66/**
67 * The size of the update parsed makes on a copy of ydoc, as the same
68 * client. The client id is set after the copy takes ydoc's state: Yjs
69 * gives a document a new one when an update it applies carries its own,
70 * and the measure is of this client, at its clock.
71 */
72function updateSize (ydoc, parsed) {
73 const copy = new Y.Doc({ guid: ydoc.guid })
74 try {
75 Y.applyUpdate(copy, Y.encodeStateAsUpdate(ydoc))
76 copy.clientID = ydoc.clientID
77 let size = 0
78 copy.on('update', u => { size += u.length })
79 const r = roots(copy)
80 copy.transact(() => { for (const op of parsed) applyOne(r, op) })
81 return size
82 } finally {
83 copy.destroy()
84 }
85}
86