Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reliable 'synced' or loaded event #35

Closed
ben-bourdin451 opened this issue Oct 2, 2023 · 7 comments · May be fixed by #37
Closed

reliable 'synced' or loaded event #35

ben-bourdin451 opened this issue Oct 2, 2023 · 7 comments · May be fixed by #37
Assignees

Comments

@ben-bourdin451
Copy link

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
Copy link

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
Copy link
Author

@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
Copy link

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
Copy link
Author

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
Copy link

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 added a commit to ben-bourdin451/y-indexeddb that referenced this issue Oct 19, 2023
attempt to clarify slight ambiguity around the sync event description

fix yjs#35
@ben-bourdin451
Copy link
Author

@raineorshine sorry it took me so long to make this propose this simple change but here it is: #37

@vastava
Copy link

vastava commented Jan 20, 2025

Hi @ben-bourdin451 what issue were you running into? I am also seeing the 'synced' event fire twice, once before the existing updates are applied and once after. Wondering if this is similar to what you experienced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants