hasUnpublishedChanges property

Computed<bool> hasUnpublishedChanges
latefinal

True when the document has unsaved (unpublished) changes.

Compares the document's current DeskDocument.crdtHlc against the DocumentVersion.snapshotHlc of the highest-numbered published version.

  • Returns true if no published version exists yet.
  • Returns true if the document's HLC is lexicographically greater than the latest published version's snapshot HLC.
  • Returns false otherwise.

Implementation

late final hasUnpublishedChanges = Computed<bool>(() {
  final docId = selectedDocumentId.value;
  if (docId == null) return false;

  final versionsState = versionsContainer(docId).value;
  final versions = versionsState.map(
    data: (d) => d.versions,
    loading: () => const <DocumentVersion>[],
    error: (_, _) => const <DocumentVersion>[],
  );

  final docState = selectedDocumentContainer(docId).value;
  final crdtHlc = docState.map(
    data: (doc) => doc?.crdtHlc,
    loading: () => null,
    error: (_, _) => null,
  );

  if (crdtHlc == null) return false;

  final latestPublished = versions
      .where((v) => v.status == DocumentVersionStatus.published)
      .fold<DocumentVersion?>(
        null,
        (acc, v) =>
            acc == null || v.versionNumber > acc.versionNumber ? v : acc,
      );

  if (latestPublished == null) return true; // never published
  if (latestPublished.snapshotHlc == null) return true;
  return crdtHlc.compareTo(latestPublished.snapshotHlc!) > 0;
}, debugLabel: 'hasUnpublishedChanges');