Closed sgwilym closed 3 years ago
Aha, yes, this is a conceptual bug with the sync algorithm.
Currently, live sync opens a steam of new events from the server. It should open a stream of events since a given time, so it can recover from gaps. The server-sent events spec has a way to handle this, Last-Event-Id, which we're not using yet.
For now the only way to recover from gaps is to do a full sync again.
This PR is a reasonable fix until the core sync algorithm is fixed, I support it 👍
Various thoughts:
visibilitychange
and focus
events happen frequently with every tab change in a browser, I think? We don't need to do a full sync then since the server-sent event stream should remain connected, we only need to do it when the connection is dropped. So this will increase load on pubs by quite a bit, but that's ok for now.
It would be better to detect a connection drop but that would have to happen in the core library, and anyway the Last-Event-Id is the right answer
Another option is to run an interval timer once a second and check if the time since the last interval is more than 10 seconds, indicating a phone has gone to sleep and then woken up
If rapidly switching tabs over and over, will this start several sync operations simultaneously? Let's see... no it's fine, the core sync code is smart about that, no problem
Recently I noticed that, when reopening a tab with a react-earthstar app, workspace data would be missing new documents that were created since putting the tab in the background. This is especially noticeable on a phone.
What I've added here is an internal component which kicks off a manual sync for all workspaces when a tab is refocused.
This only happens if EarthstarPeer's
isLive
state istrue
.