[go: up one dir, main page]

DocHandles

Once you have a Repo with a NetworkAdapter and a StorageAdapter you can get down to the business of creating and working with DocHandles.

It’s useful to understand a little about why we need a DocHandle. @automerge/automerge documents are fairly inert data structures. You can create a document, you can mutate it, you can generate sync messages to send elsewhere and you can receive sync messages from elsewhere. None of this is very “live” though. Because the document has no concept of a network, or of storage, you can’t say “every time I change a document, tell everyone else about it and save the change to storage”. This “live document” is what a DocHandle is. A DocHandle is a wrapper around a document managed by a Repo. It provides the following kinds of “liveness”:

DocHandles are very useful, how do you obtain one?

Creating a DocHandle

This is the easy one, just call Repo.create. This creates a new document, stores it, and then enqueues messages to all connected peers informing them of the new document.

Waiting for a DocHandle

Typically you are not creating a new document, but working with an existing one. Maybe the document URL was stored in localStorage, maybe the URL was in the hash fragment of the browser, etc. In this case you use Repo.find to lookup the document. This means the DocHandle can be in several different states, to understand this we’ll first look at the states in detail, then some convenience methods DocHandle exposes for waiting for different states.

DocHandle states

Repo.find will do two things simultaneously:

These actions are asynchronous, as they complete the state of the document changes. This state is represented most explicitly in the HandleState enum, which has the following states:

The transitions between these states look like this:

Note that every state can transition to DELETED, either via DocHandle.delete or Repo.delete.

One other point to note is that a DocHandle can be unavailable because we didn’t have it in storage and no peers responded to our request for it, but then another peer comes online and sends us sync messages for the document and so it transitions to READY.

You can check what state a handle is in using DocHandle.inState.

Waiting for a handle to be ready

If all we care about is whether the document is ready then we can use a few different methods.

Once the document is ready the value of the document (either DocHandle.doc() or DocHandle.docSync()) will be undefined if the document was unavailable, but otherwise will be an automerge document.