rewindToStep method

  1. @override
Future<void> rewindToStep(
  1. String runId,
  2. String stepName
)
override

Rewinds a run to the specified stepName.

Implementation

@override
/// Rewinds a run to the specified [stepName].
Future<void> rewindToStep(String runId, String stepName) async {
  final steps = _steps[runId];
  if (steps == null) return;
  final state = _runs[runId];
  if (state == null) return;
  _removeWatcherForRun(runId);
  final entries = steps.entries.toList();
  final baseIndexMap = <String, int>{};
  var nextIndex = 0;
  final entryIndexes = <int>[];
  for (final entry in entries) {
    final base = _baseStepName(entry.key);
    baseIndexMap.putIfAbsent(base, () => nextIndex++);
    entryIndexes.add(baseIndexMap[base]!);
  }
  final targetIndex = baseIndexMap[stepName];
  if (targetIndex == null) {
    return;
  }
  final retained = <MapEntry<String, Object?>>[];
  for (var i = 0; i < entries.length; i++) {
    final baseIndex = entryIndexes[i];
    if (baseIndex < targetIndex) {
      retained.add(entries[i]);
    } else if (baseIndex == targetIndex) {
      // Drop all iterations for the target step so the runtime restarts from
      // iteration 0.
      continue;
    } else {
      break;
    }
  }
  _steps[runId] = LinkedHashMap.fromEntries(retained);
  _runs[runId] = state.copyWith(
    status: WorkflowStatus.suspended,
    cursor: targetIndex,
    suspensionData: _freeze(<String, Object?>{
      'step': stepName,
      'iteration': 0,
      'iterationStep': stepName,
    }),
  );
}