Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

2.7 KB
Raw Blame
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 The Bento authors
// bento-sync for bento/spaces — the kernel engine, bound to this app's shape.
//
// A FACADE, exactly as slides/src/sync/crdt.ts is. The engine lives in
// kernel/src/sync/crdt.ts and takes its document shape as two property names,
// so the same algebra serves both apps without either one's vocabulary living
// in the kernel. The kernel's own DocShape comment already names this binding
// (`'slides' | 'pages'`, `'elements' | 'blocks'`) — the seam was designed for
// it before there was anything to bind.

export * from '../../../kernel/src/sync/crdt.ts'

import { SyncEngine, shape } from '../../../kernel/src/sync/crdt.ts'
import type { DocShape } from '../../../kernel/src/sync/crdt.ts'
import { DOC_MAPS } from '../docmaps.ts'
import { SPACES_FIELDS } from '../docclass.ts'

/**
 * bento/spaces: the document holds `pages`, a page holds `blocks`.
 *
 * These two strings are the whole difference between this app's engine and
 * slides'. Treat them as frozen from the first shared file: every persisted
 * SyncStateJSON and every relay frame is minted under them, so changing either
 * would fork a space from its own copies.
 *
 * Note what is NOT synced structurally here. A page's `parent` is an ordinary
 * property — the page TREE is derived at read time from that field
 * (`model.ts effectiveParents`), so the CRDT only has to agree on the flat
 * pre-order array and the parent value, and cycle repair stays a pure function
 * of the document. Trying to sync the tree as a hierarchy would put two sources
 * of truth in the file.
 *
 * `DOC_MAPS` (docmaps.ts) are the doc-level keys merged PER KEY rather than as
 * one value: today `periods`, so two people defining different periods in one
 * week both keep theirs. `assets` and `blobs` are per-key for every app already.
 *
 * `trail` and `revisions` are not synced at all — the kernel's shape() skips
 * every history-class field, so each replica keeps its own record.
 *
 * The list is docmaps.ts's, not a second copy: `model.ts parseDoc` folds dotted
 * keys back using the same names, and two copies of that list is exactly how
 * the hazard and its mitigation drift apart.
 */
// The field map is bound (kernel #683): doc-level keys sync by ALLOWLIST, so a
// replica emits only what docclass.ts classes 'content' and never echoes a key a
// newer build added. Every non-content field spaces has is also on the kernel's
// skipDoc floor; the map adds the unknown-key half.
export const SPACES_SHAPE: DocShape = shape('pages', 'blocks', 'html', DOC_MAPS, SPACES_FIELDS)

/** The engine bound to bento/spaces. */
export class SyncState extends SyncEngine {
  constructor(actor: string) {
    super(actor, SPACES_SHAPE)
  }
}