updateDocumentData method

  1. @override
Future<DeskDocument> updateDocumentData(
  1. String documentId,
  2. Map<String, dynamic> updates, {
  3. String? sessionId,
})
override

Updates document data using CRDT operations (partial updates). Only changed fields need to be provided - they will be merged automatically.

documentId - The ID of the document to update updates - Map of field updates (only changed fields) sessionId - Optional session ID for collaborative editing tracking

Returns the updated DeskDocument with merged data.

Note: This uses CRDT operations for conflict-free collaborative editing.

Throws DeskDataSourceException if the operation fails. Throws DeskAuthenticationException if authentication is required.

Implementation

@override
Future<DeskDocument> updateDocumentData(
  String documentId,
  Map<String, dynamic> updates, {
  String? sessionId,
}) async {
  final docVersions = _versions[documentId]?.values.toList() ?? [];
  docVersions.sort((a, b) => b.versionNumber.compareTo(a.versionNumber));
  if (docVersions.isNotEmpty) {
    final latestVersionId = docVersions.first.id!;
    final existingData = _versionData[latestVersionId] ?? {};
    existingData.addAll(updates);
    _versionData[latestVersionId] = existingData;
  }

  final doc = _documents[documentId]!;
  final updatedData = Map<String, dynamic>.from(doc.activeVersionData ?? {});
  updatedData.addAll(updates);
  // Advance crdtHlc so hasUnpublishedChanges can detect unsaved changes.
  final hlc = DateTime.now().microsecondsSinceEpoch.toString();
  _documents[documentId] = doc.copyWith(
    activeVersionData: updatedData,
    updatedAt: DateTime.now(),
    crdtHlc: hlc,
  );

  return _documents[documentId]!;
}