Skip to content
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

Fix issue that sync callback is started twice #2642

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions app/lib/common/providers/notifiers/sync_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import 'package:acter/common/models/sync_state/sync_state.dart';
import 'package:acter/features/home/providers/client_providers.dart';
import 'package:acter_flutter_sdk/acter_flutter_sdk_ffi.dart' as ffi;
import 'package:logging/logging.dart';
import 'package:riverpod/riverpod.dart';
import 'package:sentry_flutter/sentry_flutter.dart';

final _log = Logger('a3::common::sync_notifier');

Check warning on line 12 in app/lib/common/providers/notifiers/sync_notifier.dart

View check run for this annotation

Codecov / codecov/patch

app/lib/common/providers/notifiers/sync_notifier.dart#L12

Added line #L12 was not covered by tests

// ignore_for_file: avoid_print
class SyncNotifier extends Notifier<SyncState> {
late ffi.Client client;
Expand All @@ -23,7 +26,7 @@
@override
SyncState build() {
_providerSubscription = ref.listen<AsyncValue<ffi.Client?>>(
alwaysClientProvider,
clientProvider,

Check warning on line 29 in app/lib/common/providers/notifiers/sync_notifier.dart

View check run for this annotation

Codecov / codecov/patch

app/lib/common/providers/notifiers/sync_notifier.dart#L29

Added line #L29 was not covered by tests
(AsyncValue<ffi.Client?>? oldVal, AsyncValue<ffi.Client?> newVal) {
final newClient = newVal.valueOrNull;
if (newClient == null) {
Expand All @@ -36,10 +39,10 @@
// hack unfortunately means we have two wait a bit but that means
// we get past the threshold where it is okay to schedule...
client = newClient;
Future.delayed(
const Duration(milliseconds: 1500),
() => _restartSync(),
);
Future.delayed(const Duration(milliseconds: 1500), () {
_log.info('**************** _restartSync 1 *****************');
_restartSync();

Check warning on line 44 in app/lib/common/providers/notifiers/sync_notifier.dart

View check run for this annotation

Codecov / codecov/patch

app/lib/common/providers/notifiers/sync_notifier.dart#L42-L44

Added lines #L42 - L44 were not covered by tests
});
},
fireImmediately: true,
);
Expand All @@ -51,13 +54,17 @@
state.countDown.map(
(countDown) {
if (countDown == 0) {
_log.info('**************** _restartSync 2 *****************');

Check warning on line 57 in app/lib/common/providers/notifiers/sync_notifier.dart

View check run for this annotation

Codecov / codecov/patch

app/lib/common/providers/notifiers/sync_notifier.dart#L57

Added line #L57 was not covered by tests
_restartSync();
} else {
// just count down.
state = state.copyWith(countDown: countDown - 1);
}
},
orElse: () => _restartSync(),
orElse: () {
_log.info('**************** _restartSync 3 *****************');
_restartSync();

Check warning on line 66 in app/lib/common/providers/notifiers/sync_notifier.dart

View check run for this annotation

Codecov / codecov/patch

app/lib/common/providers/notifiers/sync_notifier.dart#L64-L66

Added lines #L64 - L66 were not covered by tests
},
);
}

Expand All @@ -69,6 +76,7 @@
_syncPoller?.cancel();
_errorPoller?.cancel();

_log.info('================= startSync ====================');

Check warning on line 79 in app/lib/common/providers/notifiers/sync_notifier.dart

View check run for this annotation

Codecov / codecov/patch

app/lib/common/providers/notifiers/sync_notifier.dart#L79

Added line #L79 was not covered by tests
final sync = syncState = client.startSync();

_syncListener = sync.firstSyncedRx(); // keep it resident in memory
Expand Down
Loading