hocuspocus: onCreateDocument does not have the client document's data

Description

onCreateDocument does not have the initial value from the client

Steps to reproduce the bug

  1. update a document in a client
  2. start the server
  3. see an empty value in onCreateDocument. then that value shows up in onChange immediately afterwards.

@hocuspocus/server": "^1.0.0-alpha.53

import { Server } from "@hocuspocus/server";

const hocuspocus = Server.configure({
  port: 1234,

  async onCreateDocument(data) {
    console.log("create is empty:", data.document.isEmpty("monaco"));
    console.log("create value:", data.document.getText("monaco").toString());
    return data.document;
  },

  async onChange(data) {
    console.log("change is empty:", data.document.isEmpty("monaco"));
    console.log("change value:", data.document.getText("monaco").toString());
  },
});

hocuspocus.listen().then(() => console.log("started"));

Expected behavior

onCreateDocument should have the value

Screenshot, video, or GIF

bug

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 16 (5 by maintainers)

Most upvoted comments

This is more important now that we have seen a few YJS documents become corrupted causing changes to be applied oddly.

If we could create the document with the first connection and dispose it when all connections disconnect, then it would reduce the document lifetime and avoid corruption.

Sorry, for the delay here.

Currently, I’m on holiday. I’ll tackle this in August (and will generally put more time into hocuspocus starting September).

It would be nice to have a hook for the “If the user started typing already, don’t care about getting content from an external data source”.

That way if RocksDB data gets corrupted for some reason, reinitialization can be done safely.