A DocHandle is a wrapper around a single Automerge document that lets us listen for changes and notify the network and storage of new changes.

Remarks

A DocHandle represents a document which is being managed by a Repo. You shouldn't ever instantiate this yourself. To obtain DocHandle use Repo.find or Repo.create.

To modify the underlying document use either DocHandle.change or DocHandle.changeAt. These methods will notify the Repo that some change has occured and the Repo will save any new changes to the attached StorageAdapter and send sync messages to connected peers.

Type Parameters

  • T

Hierarchy

Properties

documentId: DocumentId
prefixed: string | boolean

Accessors

Methods

  • Type Parameters

    Parameters

    • event: T
    • fn: ((...args) => void)
    • Optional context: any

    Returns this

  • Sends an arbitrary ephemeral message out to all reachable peers who would receive sync messages from you. It has no guarantee of delivery, and is not persisted to the underlying automerge doc in any way. Messages will have a sending PeerId but this is not a useful user identifier (a user could have multiple tabs open and would appear as multiple PeerIds). Every message source must have a unique PeerId.

    Parameters

    • message: unknown

    Returns void

  • All changes to an Automerge document should be made through this method. Inside the callback, the document should be treated as mutable: all edits will be recorded using a Proxy and translated into operations as part of a single recorded "change".

    Note that assignment via ES6 spread operators will result in replacing the object instead of mutating it which will prevent clean merges. This may be what you want, but doc.foo = { ...doc.foo, bar: "baz" } is not equivalent to doc.foo.bar = "baz".

    Local changes will be stored (by the StorageSubsystem) and synchronized (by the DocSynchronizer) to any peers you are sharing it with.

    Parameters

    • callback: ChangeFn<T>

      A function that takes the current document and mutates it.

    • options: ChangeOptions<T> = {}

    Returns void

  • Makes a change as if the document were at heads.

    Parameters

    Returns undefined | string[]

    A set of heads representing the concurrent change that was made.

  • Returns a set of Patch operations that will move a materialized document from one state to another if applied.

    Parameters

    Returns undefined | Patch[]

    Automerge patches that go from one document state to the other. Use view() to get the full state.

    Remarks

    We allow specifying both a from/to heads or just a single comparison point, in which case the base will be the current document heads.

  • Parameters

    • awaitStates: HandleState[] = ...

      states to wait for, such as "LOADING". mostly for internal use.

    Returns Promise<undefined | Doc<T>>

    the current state of this handle's Automerge document.

    This is the recommended way to access a handle's document. Note that this waits for the handle to be ready if necessary. If loading (or synchronization) fails, this will never resolve.

  • Synchronously returns the current state of the Automerge document this handle manages, or undefined. Consider using await handle.doc() instead. Check isReady(), or use whenReady() if you want to make sure loading is complete first.

    Not to be confused with the SyncState of the document, which describes the state of the synchronization process.

    Note that undefined is not a valid Automerge document, so the return from this function is unambigous.

    Returns undefined | Doc<T>

    the current document, or undefined if the document is not ready.

  • doneLoading is called by the repo after it decides it has all the changes it's going to get during setup. This might mean it was created locally, or that it was loaded from storage, or that it was received from a peer.

    Returns void

  • Calls each of the listeners registered for a given event.

    Type Parameters

    Parameters

    Returns boolean

  • Return an array listing the events for which the emitter has registered listeners.

    Returns (keyof DocHandleEvents<T>)[]

  • Returns the current "heads" of the document, akin to a git commit. This precisely defines the state of a document.

    Returns undefined | Heads

    the current document's heads, or undefined if the document is not ready

  • Creates a fixed "view" of an automerge document at the given point in time represented by the heads passed in. The return value is the same type as docSync() and will return undefined if the object hasn't finished loading.

    Returns undefined | Heads[]

    The individual heads for every change in the document.

    Remarks

    A point-in-time in an automerge document is an array of heads since there may be concurrent edits. This API just returns a topologically sorted history of all edits so every previous entry will be (in some sense) before later ones, but the set of all possible history views would be quite large under concurrency (every thing in each branch against each other). There might be a clever way to think about this, but we haven't found it yet, so for now at least we present a single traversable view which excludes concurrency.

  • Returns boolean

    true if the document has been marked as deleted.

    Deleted documents are removed from local storage and the sync process. It's not currently possible at runtime to undelete a document.

  • Returns boolean

    true if the document is ready for accessing or changes.

    Note that for documents already stored locally this occurs before synchronization with any peers. We do not currently have an equivalent whenSynced().

  • Returns boolean

    true if the document is currently unavailable.

    This will be the case if the document is not found in storage and no peers have shared it with us.

  • Returns boolean

    true if the document has been unloaded.

    Unloaded documents are freed from memory but not removed from local storage. It's not currently possible at runtime to reload an unloaded document.

  • Return the number of listeners listening to a given event.

    Parameters

    Returns number

  • Return the listeners registered for a given event.

    Type Parameters

    Parameters

    • event: T

    Returns ((...args) => void)[]

  • Merges another document into this document. Any peers we are sharing changes with will be notified of the changes resulting from the merge.

    Parameters

    • otherHandle: DocHandle<T>

      the handle of the document to merge into this one

    Returns void

    the merged document.

    Throws

    if either document is not ready or if otherHandle is unavailable.

  • Type Parameters

    Parameters

    • event: T
    • Optional fn: ((...args) => void)
    • Optional context: any
    • Optional once: boolean

    Returns this

  • Add a listener for a given event.

    Type Parameters

    Parameters

    • event: T
    • fn: ((...args) => void)
    • Optional context: any

    Returns this

  • Add a one-time listener for a given event.

    Type Parameters

    Parameters

    • event: T
    • fn: ((...args) => void)
    • Optional context: any

    Returns this

  • Remove all listeners, or those of the specified event.

    Parameters

    • Optional event: keyof DocHandleEvents<T>

    Returns this

  • Remove the listeners of a given event.

    Type Parameters

    Parameters

    • event: T
    • Optional fn: ((...args) => void)
    • Optional context: any
    • Optional once: boolean

    Returns this

  • Creates a fixed "view" of an automerge document at the given point in time represented by the heads passed in. The return value is the same type as docSync() and will return undefined if the object hasn't finished loading.

    Parameters

    Returns undefined | Doc<T>

    An Automerge.Doc at the point in time.

    Remarks

    Note that our Typescript types do not consider change over time and the current version of Automerge doesn't check types at runtime, so if you go back to an old set of heads that doesn't match the heads here, Typescript will not save you.

  • Parameters

    Returns Promise<void>

    a promise that resolves when the document is in one of the given states (if no states are passed, when the document is ready)

    Use this to block until the document handle has finished loading. The async equivalent to checking inState().