mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-14 19:11:00 +01:00
Pure-Dart mirror of the Python crdt-sync package, module-for-module and test-for-test, so the merge algorithm canonically agrees on both sides of a sync between a Dart client and a Python one. No Flutter SDK dependency (package:http only) per Workstream D's scoping decision (2026-07-04). 59 tests, clean dart analyze. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017McuA1nAi6jBT1EaS5BrVf
21 lines
660 B
Dart
21 lines
660 B
Dart
import 'record.dart';
|
|
|
|
/// A collection of [Record]s keyed by id.
|
|
typedef Log = Map<String, Record>;
|
|
|
|
/// Returns the union of [local] and [remote], merging shared ids.
|
|
///
|
|
/// Commutative and idempotent: `mergeLogs(a, b) == mergeLogs(b, a)` and
|
|
/// `mergeLogs(x, x) == x`, so pull order between devices never matters and a
|
|
/// repeated sync tick is a no-op.
|
|
Log mergeLogs(Log local, Log remote) {
|
|
final merged = Map<String, Record>.from(local);
|
|
for (final entry in remote.entries) {
|
|
final existing = merged[entry.key];
|
|
merged[entry.key] = existing == null
|
|
? entry.value
|
|
: mergeRecord(existing, entry.value);
|
|
}
|
|
return merged;
|
|
}
|