-
Notifications
You must be signed in to change notification settings - Fork 23
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
WIP: Use sync worker #169
WIP: Use sync worker #169
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the amazing work. From a quick scan this looks great to me. I'll try and test this soon.
Could you update this to target the main
branch? main
currently contains the latest changes and web support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This worked well in my tests. In addition to adding better handling for closed tabs, it would be nice if we could broadcast the logs from the Shared Sync Worker to the main context. This makes debugging a lot easier. For example: Currently if there is an authentication error or network error (in the shared sync worker) - the only indication (on the main context) is on the SyncStatus' error
field, when the shared sync worker is disabled console.error
messages are available in the main context console
.
Done! I've also implemented automatic reconnects to another tab if the first tab hosting the sync worker database is closed (this also requires my PR to Passing |
@@ -0,0 +1,258 @@ | |||
/// This file needs to be compiled to JavaScript with the command | |||
/// dart compile js -O4 packages/powersync/lib/src/web/sync_worker.worker.dart -o assets/db_worker.js |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a small typo in this command. With the current file structure and default worker path it should be:
dart compile js -O4 packages/powersync/lib/src/web/sync_worker.dart -o assets/powersync_sync_worker.js
We currently compile the DB worker here. I think the sync worker should also be compiled there.
We also need to add this file to our Github releases workflow here.
We have a small CLI tool which devs can use to download a precompiled worker. This sync worker's release file should be added to the tool.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested this with the provided flags. Closing tabs seems to work wonderfully :D
Hey @simolus3 I got around to testing this on my side. When passing these flags |
Hm they should have access to the same files in a sound way, but since they're using independent workers they're also on their own sqlite3 connection / updates stream. In drift I'm using broadcast channels to work around this issue. I'm not sure where this functionality should live in the powersync stack, I think I can add it to |
Understood, we will continue to merge this as it has support for IndexedDB. We can wait for the syncing with OPFS and separate workers in a future PR. |
|
Just tested syncing across tabs in chrome using sqlite_async v0.10.1 and it works perfectly! |
Thanks for you help getting this released 👍 |
This offloads the task of synchronizing databases to a separate worker, allowing it to be coordinated across tabs even when the database itself is not in a shared worker.
To test this, compile
sync_worker.dart
with dart2js and put it intoweb/
of an example project aspowersync_sync.worker.js
.The web database will try to use the shared sync worker, but fall back to the existing implementation if that doesn't work. This uses the new
exposeEndpoint
method introduced in powersync-ja/sqlite_async.dart#63, giving the sync worker efficient access to the opened database. Requests to upload data and to fetch tokens are forwarded to a connected client.One thing not yet implemented: The sync worker currently only uses the database connection established by the first tab. When multiple tabs connect to the worker and the first one closes, we'll have to setup a new sync implementation backed by a new tab.