Skip the adapter-triggered reconnect while the stream is up

This commit is contained in:
Roland Geider
2026-08-22 20:30:04 +02:00
parent b6aa38c219
commit b2ff0fb81c
2 changed files with 46 additions and 5 deletions

View File

@@ -107,15 +107,16 @@ Future<PowerSyncDatabase> powerSyncInstance(Ref ref) async {
// Gated on the network adapter only, never on the reachability probe: a
// probe failure is an indication, an unreachable backend is PowerSync's own
// retry loop to handle (the connector throttles its log output while the
// backend does not answer). Reconnecting while already connected is safe and
// doubles as the "retry now" signal when the adapter comes back.
// backend does not answer).
void syncConnection(bool hasAdapter) {
if (hasAdapter) {
final serverUrl = ref.read(wgerBaseProvider).serverUrl;
if (serverUrl != null) {
connectPowerSync(db, serverUrl, client, watchdog, reason: 'network adapter available');
} else {
if (serverUrl == null) {
_logger.info('Network adapter available, but no server configured: not connecting');
} else if (skipAdapterReconnect(db.currentStatus)) {
_logger.fine('Sync already connected, skipping reconnect');
} else {
connectPowerSync(db, serverUrl, client, watchdog, reason: 'network adapter available');
}
} else {
_logger.info('No network adapter, disconnecting from the sync service');
@@ -214,6 +215,14 @@ Future<void> _createRawTables(PowerSyncDatabase db) async {
});
}
/// Whether an adapter-triggered connect may be skipped: only while the stream
/// is already up. The SDK's connect() is never a no-op and would abort a
/// healthy stream; every other state (connecting, error, retry delay) still
/// reconnects, which doubles as the "retry now" signal when the adapter comes
/// back. Deliberate reconnects (login, manual retry) bypass this guard.
@visibleForTesting
bool skipAdapterReconnect(SyncStatus status) => status.connected;
/// Creates a fresh [DjangoConnector] for [baseUrl] and connects [db] to it.
/// Used both at initial creation and after a logout/login cycle to pick up
/// the new user's server URL / credentials. [client] is the authenticated

View File

@@ -19,6 +19,7 @@
import 'package:drift/drift.dart' show DriftSqlType, Table, TableInfo;
import 'package:drift/native.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:powersync/powersync.dart' show SyncStatus;
import 'package:wger/core/http_overrides.dart';
import 'package:wger/database/powersync/database.dart';
import 'package:wger/database/powersync/powersync.dart';
@@ -115,4 +116,35 @@ void main() {
expect(syncOptionsFor('https://gym.example.com'), isNull);
});
});
group('skipAdapterReconnect', () {
// SyncStatus has only @internal constructors, so there is no public way
// to build one in a test.
SyncStatus status({bool connected = false, bool connecting = false, Object? downloadError}) =>
// ignore: invalid_use_of_internal_member
SyncStatus(
connected: connected,
connecting: connecting,
lastSyncedAt: null,
downloadProgress: null,
downloading: false,
uploading: false,
downloadError: downloadError,
uploadError: null,
priorityStatusEntries: const [],
streamSubscriptions: null,
);
test('skips only while the stream is up', () {
expect(skipAdapterReconnect(status(connected: true)), isTrue);
});
test('still reconnects in every other state', () {
// connecting and the retry loop after an error are the "retry now"
// cases an adapter comeback must be able to interrupt.
expect(skipAdapterReconnect(status(connecting: true)), isFalse);
expect(skipAdapterReconnect(status(downloadError: Exception('stream died'))), isFalse);
expect(skipAdapterReconnect(status()), isFalse);
});
});
}