cancel method

bool cancel({
  1. bool clearQueued = true,
})

Interrupt ownership of the current execution.

Returns false when no authoritative execution is active. The Runtime receives cooperative interruption through EffectExecution.interrupt. The caller-visible Command Future completes with ExitInterrupted while physical work remains Runtime-owned until it finishes.

When clearQueued is true, queued executions complete as interrupted without starting. When false, the next queued operation starts immediately.

Implementation

bool cancel({bool clearQueued = true}) {
  _ensureNotDisposed();

  final executionId = _activeExecutionId;
  if (!_value.isRunning || executionId == null || _inFlight == null) {
    if (clearQueued) {
      _interruptQueued();
    }
    return false;
  }

  final completion = _activeCompleter;
  final runtimeExecution = _activeRuntimeExecution;

  _activeExecutionId = null;
  _inFlight = null;
  _activeCompleter = null;
  _activeRuntimeExecution = null;
  _pendingCompletions.remove(executionId);

  try {
    onCancel?.call();
  } catch (error, stackTrace) {
    final defect = ExitDefect<A, E>(error, stackTrace);
    _lastExit = defect;
    if (completion != null && !completion.isCompleted) {
      completion.complete(defect);
    }
    _setState(
      EffectCommandDefect<A, E>._(
        revision: _nextRevision(),
        executionId: executionId,
        defect: error,
        stackTrace: stackTrace,
        completedAt: DateTime.now(),
        previous: _retainedData,
      ),
    );
    runtimeExecution?.interrupt(reason: 'command-cancelled');

    if (clearQueued) {
      _interruptQueued();
    } else {
      _startNextQueuedIfPossible();
    }
    return true;
  }

  runtimeExecution?.interrupt(reason: 'command-cancelled');
  final interrupted = ExitInterrupted<A, E>();
  _lastExit = interrupted;
  if (completion != null && !completion.isCompleted) {
    completion.complete(interrupted);
  }
  _setState(
    EffectCommandInterrupted<A, E>._(
      revision: _nextRevision(),
      executionId: executionId,
      interruptedAt: DateTime.now(),
      previous: _retainedData,
    ),
  );

  if (clearQueued) {
    _interruptQueued();
  } else {
    _startNextQueuedIfPossible();
  }

  return true;
}