23 lines
750 B
Dart
23 lines
750 B
Dart
/// Unions [remote] and [local] by ID, keeping whichever copy of each
|
|
/// record has the newer `updatedAt` when a record exists on both sides.
|
|
/// This lets two devices' independent new records both survive a sync;
|
|
/// it does not propagate deletions (see plan doc's Known limitations).
|
|
List<T> mergeById<T>(
|
|
List<T> remote,
|
|
List<T> local,
|
|
String Function(T) idOf,
|
|
DateTime Function(T) updatedAtOf,
|
|
) {
|
|
final merged = <String, T>{};
|
|
for (final item in remote) {
|
|
merged[idOf(item)] = item;
|
|
}
|
|
for (final item in local) {
|
|
final id = idOf(item);
|
|
final existing = merged[id];
|
|
if (existing == null || !updatedAtOf(existing).isAfter(updatedAtOf(item))) {
|
|
merged[id] = item;
|
|
}
|
|
}
|
|
return merged.values.toList();
|
|
}
|