yjs / y-indexeddb

IndexedDB database adapter for Yjs
https://docs.yjs.dev/ecosystem/database-provider/y-indexeddb
Other
196 stars 30 forks source link

reliable 'synced' or loaded event #35

Closed ben-bourdin451 closed 10 months ago

ben-bourdin451 commented 10 months ago

Is your feature request related to a problem? Please describe.

The current synced event has the following description in the readme

The "synced" event is fired when the connection to the database has been established and all available content has been loaded. The event is also fired when no content is available yet.

Describe the solution you'd like I would like to be able to have an event that is fired once the content is guaranteed to be available. It could be 'synced' or another event.

Knowing when a ydoc has been 'loaded' is important to state external to this plugin, an example use case is: handling empty docs. From my testing this is not currently possible.

Describe alternatives you've considered I've considered a few different approaches (timeouts, update event from ydoc) but none seem to work 100%

Additional context I've tried looking for similar issues or discussions related to this but couldn't find any. Perhaps this is due to an implementation detail that I'm not aware of since I'm not familiar with the inner workings of this plugin. Something similar to the state provided in the WS plugin would be ideal.

Some guidance or clarity on this subject would be greatly appreciated 🙏

raineorshine commented 10 months ago

As far as I know, what you are wanting is how synced works currently. Here is a demo that shows the synced event firing consistently when the doc loads:

https://codesandbox.io/p/sandbox/y-indexeddb-updates-test-forked-6mf9xl

Feel free to fork that sandbox to create a reproducible demo of the problem you're experiencing.

ben-bourdin451 commented 10 months ago

@raineorshine thanks for the quick reply and providing the sandbox link, I will try to replicate it there.

For reference the event does fire consistently however for the same document the handler will sometimes show empty content on that event. Is it possible that there is a race condition between that event being fired and the content being available to read in the ydoc object?

perhaps I also misinterpreted the README and specifically this wording:

The event is also fired when no content is available yet.

This seems to suggest that content will be available after that event is fired

raineorshine commented 10 months ago

The synced event will only fire after all updates have been read from IndexedDB and applied to the YJS Doc.

Given this, I interpret the README to mean that the synced event will still fire even when no doc exists in IndexedDB with the given name.

ben-bourdin451 commented 10 months ago

After more investigation I can confirm that the issue was elsewhere. I must admit I did jump to conclusions after seeing yet as part of

The event is also fired when no content is available yet.

thinking that the content would somehow be loaded or available after the event was fired

raineorshine commented 10 months ago

Great, I'm glad you got to the bottom of it.

I agree the language is ambiguous. If you have the time, please consider opening a PR to change the wording so that others do not experience the same confusion.

ben-bourdin451 commented 10 months ago

@raineorshine sorry it took me so long to make this propose this simple change but here it is: https://github.com/yjs/y-indexeddb/pull/37