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
@mustCallSuper
bool maybeAttach(CancellationToken? token) {
_isCancelled = token?.isCancelled ?? false;
if (isCancelled) {
// Schedule the cancellation as a microtask to prevent Futures completing
// before error handlers are registered
scheduleMicrotask(() => onCancel(token!.exception!));
return false;
} else {
_attachedToken = token;
_attachedToken?.attachCancellable(this);
return true;
}
}