maybeAttach method
Attaches to the CancellationToken only if it hasn't already been cancelled. If the token has already been cancelled, onCancel is called instead.
Returns true
if the token is null or hasn't been cancelled yet, so the
async task should continue.
Returns false
if the token has already been cancelled and the async task
should not continue.
Implementation
@protected
bool maybeAttach(CancellationToken? token) {
if (token?.isCancelled ?? false) {
// Schedule the cancellation as a microtask to prevent Futures completing
// before error handlers are registered
final StackTrace trace = StackTrace.current;
scheduleMicrotask(() => onCancel(token!.exception, trace));
return false;
} else {
token?.attach(this);
return true;
}
}