cancelIf method

void cancelIf(
  1. Future<bool> whetherToCancel
)

Register whetherToCancel to later decide whether to cancel. It is expected that whatever async processing required to determine whether to cancel the action is started by the action receiver. The resolved value will only be looked at once the action broadcaster has attempted to execute.

If whetherToContinue returns true, the action will be cancelled.

Implementation

void cancelIf(Future<bool> whetherToCancel) {
  // Do nothing, it will be cancelled anyway.
  if (cancelled) return;

  if (isDone!) {
    throw StateError('Cannot register. Action is complete.');
  }

  // Don't allow more registrations.
  if (_waitingForDone!) {
    throw StateError('Cannot register. Already waiting.');
  }

  // Register a future.
  _futureCancellations.add(whetherToCancel);
}