powersync.dart icon indicating copy to clipboard operation
powersync.dart copied to clipboard

Refactor sync client and options

Open simolus3 opened this issue 7 months ago • 0 comments

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:

  1. 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. invalidCredentialsCallback should call prefetchCredentials - not exactly obvious).
  2. Fix addBroadcast to also cancel all streams when one of them emits a done event. Previously, we might leak subscriptions.
  3. The sync client had a "local ping" controller emitting null to advance the sync iteration. I've introduced sealed classes to be more precise about the event advancing the iteration.
  4. 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 _streamingSyncIteration break out of the await for loop after everything has been cleaned up. So we no longer have to close the HTTP client just to cancel the stream.

simolus3 avatar May 15 '25 11:05 simolus3