import '../../models/document_model.dart'; /// Result of merging the server's document list with local corrections that /// haven't made it back to the server yet. class MergedDocuments { final List docs; final Set unsyncedIds; const MergedDocuments(this.docs, this.unsyncedIds); } /// Merges the server's authoritative document list with local corrections for /// documents whose `PUT` sync failed (`syncFailed` in the pending queue). /// /// Without this, refreshing the documents list would silently overwrite a /// driver's corrected-but-unsynced edit with the server's stale pre-edit copy /// on every successful fetch - the correction is still recoverable via the /// pending queue's retry action, but the history list would show the wrong /// data (or the document would vanish from it entirely) until the retry /// succeeds. `unsyncedOverrides` wins over `serverDocs` for matching ids, and /// is kept even if the server list doesn't (yet) include that id at all. MergedDocuments mergeDocumentsWithUnsyncedOverrides({ required List serverDocs, required Map unsyncedOverrides, }) { final merged = { for (final doc in serverDocs) doc.id: doc, }; for (final entry in unsyncedOverrides.entries) { merged[entry.key] = entry.value; } return MergedDocuments(merged.values.toList(), unsyncedOverrides.keys.toSet()); }