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>
79 lines
2.9 KiB
Dart
79 lines
2.9 KiB
Dart
/// Pulls the peers whose logs changed, all at once and off the UI thread.
|
|
///
|
|
/// Split out of `sync_service.dart` for file size. A peer's log is about
|
|
/// 2 MB; pulling eight of them one after another, each parsed on the UI
|
|
/// isolate, kept the phone's sync spinner up for 3.9 s (todo:0c575b891b).
|
|
library;
|
|
|
|
import 'package:crdt_sync/crdt_sync.dart';
|
|
import 'package:todo/sync/off_thread.dart';
|
|
|
|
/// One peer file's pull result: its decoded [log] and revision, or a null
|
|
/// [log] when the file vanished, failed decryption or did not parse.
|
|
typedef PulledPeer = ({String name, String peer, Log? log, String? rev});
|
|
|
|
/// Fetches [names] (file names under [dir]) concurrently.
|
|
///
|
|
/// [knownRevs] holds each peer's published revision; a peer without one gets
|
|
/// its revision computed from the downloaded text. Any failure other than an
|
|
/// [E2eDecryptError] fails the pull, exactly as a sequential loop would.
|
|
Future<List<PulledPeer>> pullPeers(
|
|
RemoteStore remote,
|
|
String dir,
|
|
List<String> names,
|
|
Map<String, String> knownRevs,
|
|
) => Future.wait([
|
|
for (final name in names) _pull(remote, dir, name, knownRevs[peerOf(name)]),
|
|
], eagerError: true);
|
|
|
|
/// Peer id of a `<peer>.json` file name.
|
|
String peerOf(String name) =>
|
|
name.endsWith('.json') ? name.substring(0, name.length - 5) : name;
|
|
|
|
Future<PulledPeer> _pull(
|
|
RemoteStore remote,
|
|
String dir,
|
|
String name,
|
|
String? knownRev,
|
|
) async {
|
|
final peer = peerOf(name);
|
|
final String? text;
|
|
try {
|
|
text = await remote.getFileText('$dir/$name');
|
|
} on E2eDecryptError {
|
|
// Wrong key, tampered blob or a newer format: skipped and reported,
|
|
// never merged and never fatal.
|
|
return (name: name, peer: peer, log: null, rev: null);
|
|
}
|
|
if (text == null) return (name: name, peer: peer, log: null, rev: null);
|
|
final (log, rev) = await _decodeOffThread(text, knownRev);
|
|
return (name: name, peer: peer, log: log, rev: rev);
|
|
}
|
|
|
|
/// Below this many characters a log parses faster than an isolate starts.
|
|
const int _offThreadMinLength = 64 * 1024;
|
|
|
|
/// Kept separate so the isolate closure captures only [text] and [knownRev].
|
|
Future<(Log?, String)> _decodeOffThread(String text, String? knownRev) async {
|
|
(Log?, String) decode() => (decodeLog(text), knownRev ?? revisionOf(text));
|
|
return text.length < _offThreadMinLength ? decode() : await offThread(decode);
|
|
}
|
|
|
|
/// Parses a remote note log, returning `null` for a corrupt or wrong-shape
|
|
/// payload so one bad device file never aborts the whole sync.
|
|
Log? decodeLog(String text) {
|
|
try {
|
|
return logFromJson(text);
|
|
} on FormatException {
|
|
return null;
|
|
}
|
|
// crdt_sync's logFromJson throws a raw TypeError (not a custom
|
|
// exception) for a wrong-shape JSON payload from an untrusted peer
|
|
// file; must be caught too so one bad device file never aborts the
|
|
// whole sync (see doc comment above).
|
|
// ignore: avoid_catching_errors
|
|
on TypeError {
|
|
return null;
|
|
}
|
|
}
|