powersync.dart
powersync.dart copied to clipboard
Refactor sync client and options
This merges all options affecting the sync client (retry duration, crud throttle and client parameters) into a single object (SyncOptions) that can be passed to connect(). The old field for the retry duration and the independent parameters for throttling and client params have been deprecated in favor of those options.
The main motivation for the options class is that we'll have additional options in the future (e.g. to control whether to use the Dart or the Rust sync client, or whether to use websockets vs. HTTP streams). Having these options all in one place makes it easier to expand them later.
This also applies some refactoring to the client, with a focus on how we handle aborting syncs and errors:
- Instead of passing individual callbacks to the
StreamingSyncImplementation, connector methods are now grouped with an interface. The old callbacks didn't have the same name as the methods on the connector, which was confusing to me (e.g.invalidCredentialsCallbackshould callprefetchCredentials- not exactly obvious). - Fix
addBroadcastto also cancel all streams when one of them emits adoneevent. Previously, we might leak subscriptions. - The sync client had a "local ping" controller emitting
nullto advance the sync iteration. I've introduced sealed classes to be more precise about the event advancing the iteration. - This simplifies closing the HTTP client and active streams. Previously, we would close the client to abort the current connection, and there was a comment there mentioning that "there is no other known way to cancel open streams". I'm not sure if I'm overlooking anything, but it seems to me that we can and should just use
StreamSubscription.cancel? Doing that on the response stream will clean resources, and we can simply close the client afterwards. Thanks to fix number two, we can just close the "local ping" controller to cancel all source streams, which will make_streamingSyncIterationbreak out of theawait forloop after everything has been cleaned up. So we no longer have to close the HTTP client just to cancel the stream.