confirmHistoryImport method

EntityGraphDevtoolsHistoryImportRestoreResult confirmHistoryImport(
  1. String candidateId,
  2. int cursor
)

Implementation

EntityGraphDevtoolsHistoryImportRestoreResult confirmHistoryImport(
  String candidateId,
  int cursor,
) {
  EntityGraphDevtoolsHistoryImportRestoreRejectedReceipt reject(
    EntityGraphDevtoolsHistoryImportRestoreRejectionReason reason,
    String message,
  ) => EntityGraphDevtoolsHistoryImportRestoreRejectedReceipt(
    reason: reason,
    message: message,
  );
  if (isDisposed) {
    return reject(
      EntityGraphDevtoolsHistoryImportRestoreRejectionReason.disposed,
      'DevTools controller is disposed',
    );
  }
  if (!timeTravelEnabled) {
    return reject(
      EntityGraphDevtoolsHistoryImportRestoreRejectionReason
          .timeTravelUnavailable,
      'Time travel is disabled for this controller',
    );
  }
  if (_previewReceipts.isNotEmpty) {
    return reject(
      EntityGraphDevtoolsHistoryImportRestoreRejectionReason.activePreview,
      'Restore active entity previews before entering time travel',
    );
  }
  final candidate = _importCandidate;
  if (candidate == null || candidate.candidateId != candidateId) {
    return reject(
      EntityGraphDevtoolsHistoryImportRestoreRejectionReason
          .candidateNotFound,
      'Import candidate $candidateId is no longer awaiting confirmation',
    );
  }
  final snapshot = candidate.snapshots
      .where((candidate) => candidate.cursor == cursor)
      .firstOrNull;
  if (snapshot == null) {
    return reject(
      EntityGraphDevtoolsHistoryImportRestoreRejectionReason.snapshotNotFound,
      'Import candidate $candidateId does not contain cursor $cursor',
    );
  }
  final transition = _enterRewind(
    this,
    cursor: cursor,
    source: EntityGraphDevtoolsSnapshotSource.imported,
    target: snapshot.data,
  );
  if (transition == null) {
    return reject(
      EntityGraphDevtoolsHistoryImportRestoreRejectionReason.restoreFailed,
      'Import candidate $candidateId could not be restored',
    );
  }
  _importCandidate = null;
  _projectionFailureRevision += 1;
  final event = _publishTimeTravel(
    state: EntityGraphDevtoolsHistoryMode.rewound,
    cursor: cursor,
    previousCursor: transition.previousCursor,
    source: EntityGraphDevtoolsSnapshotSource.imported,
    previousSource: transition.previousSource,
    reason: EntityGraphDevtoolsTimeTravelReason.imported,
  );
  return EntityGraphDevtoolsHistoryImportRestoreReceipt(
    candidateId: candidateId,
    cursor: cursor,
    previousCursor: transition.previousCursor,
    previousSource: transition.previousSource,
    changedAt: event.observedAt,
  );
}