Skip to content

Allow onLoadDocument hook to return a state delta instead of a Y.Doc #271

@HalfVoxel

Description

@HalfVoxel

The problem I am facing
Hocuspocus will immediately convert any Y.Doc returned by the onLoadDocument hook into an update.
This is the relevant code from hocuspocus:

await this.hooks('onLoadDocument', hookPayload, (loadedDocument: Doc | undefined) => {
      // if a hook returns a Y-Doc, encode the document state as update
      // and apply it to the newly created document
      // Note: instanceof doesn't work, because Doc !== Doc for some reason I don't understand
      if (
        loadedDocument?.constructor.name === 'Document'
        || loadedDocument?.constructor.name === 'Doc'
      ) {
        applyUpdate(document, encodeStateAsUpdate(loadedDocument))
      }
    })

Since we load documents in the update format anyway, it's just redundant work for us to convert our updates to Y.Docs just to be able to return them from the onLoadDocument hook.

The solution I would like
Change the onLoadDocument hook to have the return type Y.Doc | { update: Uint8Array, encodingVersion: number } instead of just Y.Doc.

CC: @ViktorQvarfordt

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions