Giter VIP home page Giter VIP logo

Comments (5)

georeith avatar georeith commented on May 24, 2024 2

@hanspagel just tested this and works well 😀 . I had a suspicion something wasn't working quite right I thought you made synced separate to onCreateDocument incase it was slow to create, didn't realise that was the bug!

from hocuspocus.

hanspagel avatar hanspagel commented on May 24, 2024 1

Oh, this appears to happen when the onCreateDocument takes "longer". I refactored the server to wait for all onCreateDocument hooks before sending the first sync step.

synced should now work for your use with the new version. Would you mind to try again? I’m doing a release now.

EDIT: Published new versions.

from hocuspocus.

hanspagel avatar hanspagel commented on May 24, 2024

Oh, good catch!

from hocuspocus.

hanspagel avatar hanspagel commented on May 24, 2024

The connect event is emitted, when the first message is received (and before it is applied). I think that’s what I’d expect.

const message = new IncomingMessage(event.data)
this.emit('message', { event, message })
const encoder = new MessageReceiver(message, this).apply(this)

And the synced event seems to be emitted when the update is applied already.

const syncMessageType = syncProtocol.readSyncMessage(
this.message.decoder,
this.message.encoder,
provider.document,
provider,
)
if (emitSynced && syncMessageType === syncProtocol.messageYjsSyncStep2) {
provider.synced = true
}

Tested in:
https://github.com/ueberdosis/hocuspocus/blob/main/tests/provider/onSynced.js#L75-L101

Or do I miss something?

from hocuspocus.

georeith avatar georeith commented on May 24, 2024

@hanspagel I didn't actually check the synced event as the document is not ready when synced is called if onCreateDocument needs to be called so can ignore that.

The issue I'm seeing is that the message event which carries my initial document (as returned from onCreateDocument in @hocuspocus/server) is fired before that update is actually applied to the document because of:

const syncMessageType = syncProtocol.readSyncMessage(
this.message.decoder,
this.message.encoder,
provider.document,
provider,
)
if (emitSynced && syncMessageType === syncProtocol.messageYjsSyncStep2) {
provider.synced = true
}

To show a loading state and remove it when the document is definitely received from the server I currently have to do the following:

const document = new Y.Doc();
const provider = new HocusPocus({ document, ... });

let timeout: number;
function checkIfLoaded() {
    // have to wrap this in setTimeout because the message event is fired before the update is applied to the document
    clearTimeout(timeout);
    timeout = setTimeout(() => {
        // checks if data actually exists in the document
        if (isLoaded(document)) {
            provider.off('synced', checkIfLoaded);
            provider.off('message', checkIfLoaded);
            onLoad();
        }
    }, 0);
}

// document may be empty after synced if it hits onCreateDocument in @hocuspocus/server
provider.on('synced', checkIfLoaded);
provider.on('message', checkIfLoaded);

from hocuspocus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.