composablesys / collabs

Collabs library monorepo
https://collabs.readthedocs.io/
Apache License 2.0
246 stars 11 forks source link

Linked documents #262

Open mweidner037 opened 1 year ago

mweidner037 commented 1 year ago

Many apps require multiple documents (CRuntimes), so that different parts of the app can have different permissions, be loaded independently, be stored in different places, etc.

Sometimes these documents are linked to each other. E.g.:

  1. A main document could store a CPresence for shared cursors in a separate document, so that CPresence heartbeats don't cause the main document to re-save constantly.
  2. A large text CRDT could be split into sub-documents for different sections so that they don't all have to load at once. It's easiest if the sub-documents all use Positions from a single CPositionSource, which would live in a separate always-loaded document.
  3. One text document hyperlinks to a section in another text document.

To make 1 & 2 possible, we would need a way to link documents' causal orders. In case 1, a shared cursor movement in the CPresence doesn't make sense until you receive causally prior operations that created the cursor's Position. In case 2, each text CRDT depends on causally prior operations in the CPositionSource to understand the list order.

Specifically, we need the ability to have one CRuntime buffer messages until causally prior messages are received by another CRuntime. This seems like a neat and relatively easy feature to add.

We could also make it possible to implement case 3 without keeping both documents in memory simultaneously. E.g., store the reference as a CollabID + Position, and add fallback code in case the user clicks on a link to a section that you haven't received yet. This mostly requires an example in the docs, but we also need a way to safely ask whether a Position exists. (Currently, asking a list about a not-yet-existent Position will throw an error.)