watershed Collaborative data structures for Gleam

← watershed · Data structures / Transforms

Transforms

One shared document, kept in agreement as everyone edits.

The families above converge by merge rules: each replica applies the same commutative rule and lands the same state. This family converges the other way: operational transform, where concurrent ops are rewritten to account for one another.

watershed’s json_ot kernel is a faithful port of the ottypes json0 algebra with the single-op-in-flight client protocol. Every client edits one shared JSON document optimistically, a central server sequences each op, and concurrent ops are transformed past one another so all replicas reach identical state, indices and all. SharedRichText runs that same protocol over quill-delta's rich-text algebra (retain/insert/delete spans, attribute patches, embeds) for collaborative Quill editors. Both are OT-backed. SharedText, in the Sequences family, covers collaborative plain text with identity-based CRDT merge.

JsonOt

OT
json_ot

One shared JSON document that many people can edit at once and always agree on.

Open the live JsonOt demo →

watershed’s json_ot kernel is a faithful port of the ottypes json0 algebra (the operational-transform model behind ShareDB). Instead of merging keys by rule, it edits one shared JSON document with a small algebra of operations addressed by a path into the tree: set a key, insert or delete a list item, splice a string.

It runs the single-op-in-flight client protocol: a client applies an edit optimistically and sends it, keeping at most one op in flight. A central server sequences every op, and concurrent ops are transformed past one another (a concurrent list insert has its index shifted), so all replicas reach identical state, indices and all. This is the other convergence family: transform rather than merge.

Best for

  • Collaboratively edited structured documents (JSON trees, outlines, form models changed by many clients at once)
  • Cases where last-write-wins would clobber a concurrent edit but you want one shared document rather than per-key CRDTs
  • Studying the json0 algebra used by ottypes and ShareDB
Merge rule
one shared JSON document; simultaneous edits are adjusted to fit around each other
Optimistic behavior
you edit instantly; your in-flight change is adjusted as other people’s confirmed edits arrive
Summary shape
the document reloads to the same value everywhere, list positions and all
Model
Operational transform: converges by transforming concurrent operations

SharedRichText

OT
rich_text_kernel

The json_ot protocol turned on rich text: three Quill editors, one document, real concurrent formatting.

Open the live SharedRichText demo →

SharedRichText runs the same single-op-in-flight client-transform protocol as json_ot, over a different algebra: a faithful port of rich-text/quill-delta. Operations retain, insert, or delete spans of text, each optionally carrying an attribute patch (bold, color, …) or wrapping an embed (an image) instead of plain text. Positions are counted in UTF-16 code units (the same units Quill and JavaScript strings use), so the runtime and the editor never disagree about where an edit lands.

Each client keeps at most one op in flight; anything typed while it's outstanding composes into a single buffered op behind it. Remote deltas arrive already advanced into the client's optimistic view, and the runtime applies them incrementally to the editor rather than replacing the whole document. Cursor and selection positions are transformed through every local and remote edit the same way the text itself is. This is watershed's OT-backed rich text. SharedText is the CRDT-backed plain-text counterpart: it indexes graphemes by stable identity and converges by merge instead of transform.

Best for

  • Collaborative rich-text editors (Quill, and anything built on the quill-delta/rich-text algebra)
  • Documents where formatting and embeds must survive concurrent edits, not just plain characters
  • Teams already invested in the OT model (ShareDB-style) who want rich text alongside json_ot's structured documents
Merge rule
one shared rich-text document; concurrent typing, formatting, and deletes are transformed to fit around each other
Optimistic behavior
you edit instantly in Quill; your in-flight delta is adjusted as other people’s confirmed edits arrive
Summary shape
the document reloads to the same text, formatting, and embeds everywhere
Model
Operational transform: converges by transforming concurrent operations