mirror of
https://github.com/kuhyx/todo-app.git
synced 2026-11-19 13:54:24 +01:00
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
file-length / file length <= 250 lines (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
The board reported a 3.9 s sync.run spinner on the Pixel (todo:0c575b891b). Eight ~2 MB peer logs were fetched, parsed on the UI isolate and merged one by one, each merge rewriting the whole local store; a device without a GitHub token still mirrored to GitHub (404s); settings and analytics waited for the note sync. Now changed peers download together, logs over 64 KB parse on an isolate (inline on web), peers merge into one local write, the revision map and listing share a round trip, the mirror is skipped without a token, and settings/analytics run alongside the notes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
141 lines
5.5 KiB
Dart
141 lines
5.5 KiB
Dart
/// One place that decides *how* a sync runs, for every screen that
|
|
/// triggers one.
|
|
///
|
|
/// The capture screen syncs on a timer and on demand, the settings screen on a
|
|
/// button; before this they each built their own client, so a change to the
|
|
/// backend meant editing four call sites and hoping none was missed.
|
|
library;
|
|
|
|
import 'package:crdt_sync/crdt_sync.dart';
|
|
import 'package:crdt_sync_flutter/crdt_sync_flutter.dart' show keystoreKeyCache;
|
|
import 'package:flutter/foundation.dart' show ValueNotifier, visibleForTesting;
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:todo/analytics/analytics_service.dart';
|
|
import 'package:todo/data/app_settings.dart';
|
|
import 'package:todo/data/note_repository.dart';
|
|
import 'package:todo/sync/e2e_passphrase_source.dart';
|
|
import 'package:todo/sync/firebase_backend.dart';
|
|
import 'package:todo/sync/sync_service.dart';
|
|
import 'package:todo/sync/sync_settings.dart';
|
|
import 'package:todo/sync/sync_state_factory.dart';
|
|
|
|
/// Argon2id cost for new headers; only tests lower it.
|
|
@visibleForTesting
|
|
Argon2Params e2eArgon2Params = Argon2Params.recommended;
|
|
|
|
/// Whether the last sync found no usable end-to-end key; drives the lock
|
|
/// tile in settings.
|
|
final ValueNotifier<bool> e2eLocked = ValueNotifier<bool>(false);
|
|
|
|
/// Bundles a [SyncResult] with the [AppSettings] as reconciled against the
|
|
/// same Firebase session — so a settings change made on another device is
|
|
/// picked up without a second authenticated round trip.
|
|
class SyncRunResult {
|
|
/// Creates a [SyncRunResult] from a completed run's outcomes.
|
|
const SyncRunResult({required this.syncResult, this.appSettings});
|
|
|
|
/// Outcome of the note sync itself.
|
|
final SyncResult syncResult;
|
|
|
|
/// [AppSettings] after reconciling with the remote mirror, or null when
|
|
/// the caller did not pass one in to reconcile.
|
|
final AppSettings? appSettings;
|
|
}
|
|
|
|
/// Runs one sync, choosing the backend and carrying the revision cache.
|
|
///
|
|
/// Firebase is the primary when this device has been set up for it, with
|
|
/// GitHub kept as a mirror so a device that has not moved yet still
|
|
/// converges. Not being set up is a normal state, not an error — sync runs
|
|
/// over GitHub exactly as it did before.
|
|
///
|
|
/// [appSettings] and [analytics], when given, are reconciled/flushed
|
|
/// against the Firebase mirror using this same call's already-open client —
|
|
/// riding along on the one authenticated session this function opens,
|
|
/// rather than each concern (notes, settings, analytics) opening and
|
|
/// closing its own.
|
|
///
|
|
/// [firebaseFactory], [stateStore] and [readPassphrase] exist for tests, which
|
|
/// must not reach the OS keystore or an application-support directory.
|
|
///
|
|
/// Everything pushed is end-to-end encrypted under the shared crdt-sync
|
|
/// passphrase; without a usable key the run is a no-op flagged
|
|
/// [SyncResult.locked] rather than a plaintext sync.
|
|
Future<SyncRunResult> runSync(
|
|
NoteRepository repository,
|
|
SyncSettings settings, {
|
|
AppSettings? appSettings,
|
|
AnalyticsService? analytics,
|
|
http.Client? httpClient,
|
|
Future<FirebaseRestClient?> Function()? firebaseFactory,
|
|
SyncStateStore? stateStore,
|
|
Future<String?> Function()? readPassphrase,
|
|
}) async {
|
|
final github = GitHubClient(
|
|
owner: settings.owner,
|
|
repo: settings.repo,
|
|
token: settings.token,
|
|
httpClient: httpClient,
|
|
);
|
|
final factory = firebaseFactory ?? openFirebase;
|
|
final firebase = await factory();
|
|
try {
|
|
final service = SyncService(
|
|
// The production default resolves a real application-support directory
|
|
// through path_provider, which has no binding under `flutter test`;
|
|
// every test injects a store instead.
|
|
// coverage:ignore-start
|
|
stateStore: stateStore ?? await openSyncStateStore(),
|
|
// coverage:ignore-end
|
|
);
|
|
// No GitHub token means nothing to mirror: an unconfigured client still
|
|
// spends two round trips per call on 404s.
|
|
final backend = switch (firebase) {
|
|
null => github,
|
|
_ when !settings.isConfigured => firebase,
|
|
_ => MirrorStore(primary: firebase, mirror: github),
|
|
};
|
|
final e2e = await openEncryptedStore(
|
|
store: backend,
|
|
oldPrefix: service.notesDir,
|
|
readPassphrase: readPassphrase ?? readE2ePassphrase,
|
|
params: e2eArgon2Params,
|
|
// Argon2id once per device, not once per tick.
|
|
keyCache: keystoreKeyCache(service.notesDir),
|
|
);
|
|
e2eLocked.value = e2e is! E2eReady;
|
|
if (e2e is! E2eReady) {
|
|
return const SyncRunResult(
|
|
syncResult: SyncResult(mergedDevices: 0, pushed: false, locked: true),
|
|
);
|
|
}
|
|
final layout = E2eLayout.of(service.notesDir);
|
|
// Settings and analytics touch other Firebase paths than the notes, so
|
|
// they ride alongside the note sync instead of after it.
|
|
final reconciled = appSettings?.reconcileWithRemote(firebase);
|
|
final flushed = analytics?.flush(firebase);
|
|
final notes = SyncService(
|
|
notesDir: layout.prefix,
|
|
revsPath: layout.revsPath,
|
|
stateStore: service.stateStore,
|
|
).sync(repository, e2e.store);
|
|
final done = await Future.wait<Object?>([
|
|
notes,
|
|
?reconciled,
|
|
?flushed,
|
|
// Not eager: the clients are closed below, so nothing may still be in
|
|
// flight when this returns or throws.
|
|
]);
|
|
final result = done.first! as SyncResult;
|
|
return SyncRunResult(
|
|
syncResult: result.withFirebaseConnected(value: firebase != null),
|
|
appSettings: await reconciled,
|
|
);
|
|
} finally {
|
|
// Close both: unlike the long-lived desktop callers, each screen builds
|
|
// these per run.
|
|
github.close();
|
|
firebase?.close();
|
|
}
|
|
}
|