cancellation_token
Easy async task cancellation for tasks using cancellation tokens in Dart.
2.0.1
23K downloads/mo
#2077 most downloaded on pub.dev
jonathanpetercole/dart-cancellation-token
What this package is like to depend on
Last release 3 years ago
no release in 18 months
Ships fairly regularly
a new release about every 2 months
Nearly every release is documented
notes for 15 of 15 stable releases
Nothing withdrawn
no release was ever pulled
5 years old
15 releases · first in 2022
0 releases in the last 12 months
see the full history below
Release timeline
15 releases · Jan 2022 to Jul 2023Releases
latest 15-
2.0.111 Jul 2023Release notes
Open source →- Fixed a
Bad state: Future already completedexception that could occur when nesting cancellable futures.
Release notes
Open source →- Fixed a
Bad state: Future already completedexception that could occur when nesting cancellable futures.
- Fixed a
-
2.0.008 May 2023Release notes
Open source →This release aims to make it easier to implement custom Cancellables and provide more useful information for debugging.
- Added a
.cancelWithReason()method toCancellationTokenfor a convenient way to set provide a cancellation reason for debugging. CancelledExceptionnow overrides.toString()to give a more useful message for debugging, including the cancellation reason.- Cancellation stack traces now show the call stack leading up to the operation that was cancelled, rather than the call stack leading up to the token's cancellation. This should make it easier to identify the origin of uncaught cancellation exceptions.
- Added a
.detach()method to theCancellablemixin. When paired with.maybeAttach(), this will detach your cancellable from the token. - Fixed a bug where the
ignoreCancellation()wouldn't callwhenCompleteifonErrorthrew an exception. - Fixed a bug where
asCancellable()could result in uncaught exceptions.
Breaking changes
These changes are isolated to projects using custom cancellables or cancellation tokens. Other projects are unaffected.
- The
CancellationToken.attach()and.detach()methods have been renamed to.attachCancellable()and.detachCancellable(). - The
CancellationToken.exceptiongetter now returns null if the token hasn't been cancelled yet. - The
CancellationToken.cancel()method'sexceptionparameter is now nullable, rather than using a default value. - Removed the
[StackTrace? stackTrace]parameter from theCancellablemixin'sonCancelmethod. Instead, use the newcancellationStackTrace, which returns the stack trace at the time the cancellable was created. - Overridden
Cancellablemixin methods must now call super.
To migrate your custom cancellation tokens:
- If you're overriding the
.attach()and.detach()methods, rename them to.attachCancellable()and.detachCancellable(). - If you're overriding
.exception, update it to be nullable and only return an exception if the token's been cancelled. - If you're overriding
.cancel(), update it to make theexceptionparameter nullable. If you were previously setting a default value, consider setting this within the method instead:@override void cancel([Exception? exception]) { exception ??= YourCustomDefaultException(); super.cancel(exception); }
To migrate your custom cancellables:
- Replace calls to
cancellationToken.attach(this)withmaybeAttach(cancellationToken). - Replace calls to
cancellationToken.detach(this)withdetach(). - Update
onCancel()overrides to callsuper.onCancel()and remove thestackTraceparameter. To get the stack trace, usecancellationStackTraceinstead:// Old @override void onCancel(Exception cancelException, [StackTrace? stackTrace]) { _internalCompleter.completeError( cancelException, stackTrace ?? StackTrace.current, ); } // New @override void onCancel(Exception cancelException) { super.onCancel(cancelException); _internalCompleter.completeError(cancelException, cancellationStackTrace); }
Release notes
Open source →This release aims to make it easier to implement custom Cancellables and provide more useful information for debugging.
- Added a
.cancelWithReason()method toCancellationTokenfor a convenient way to set provide a cancellation reason for debugging. CancelledExceptionnow overrides.toString()to give a more useful message for debugging, including the cancellation reason.- Cancellation stack traces now show the call stack leading up to the operation that was cancelled, rather than the call stack leading up to the token's cancellation. This should make it easier to identify the origin of uncaught cancellation exceptions.
- Added a
.detach()method to theCancellablemixin. When paired with.maybeAttach(), this will detach your cancellable from the token. - Fixed a bug where the
ignoreCancellation()wouldn't callwhenCompleteifonErrorthrew an exception. - Fixed a bug where
asCancellable()could result in uncaught exceptions.
Breaking changes
These changes are isolated to projects using custom cancellables or cancellation tokens. Other projects are unaffected.
- The
CancellationToken.attach()and.detach()methods have been renamed to.attachCancellable()and.detachCancellable(). - The
CancellationToken.exceptiongetter now returns null if the token hasn't been cancelled yet. - The
CancellationToken.cancel()method'sexceptionparameter is now nullable, rather than using a default value. - Removed the
[StackTrace? stackTrace]parameter from theCancellablemixin'sonCancelmethod. Instead, use the newcancellationStackTrace, which returns the stack trace at the time the cancellable was created. - Overridden
Cancellablemixin methods must now call super.
To migrate your custom cancellation tokens:
- If you're overriding the
.attach()and.detach()methods, rename them to.attachCancellable()and.detachCancellable(). - If you're overriding
.exception, update it to be nullable and only return an exception if the token's been cancelled. - If you're overriding
.cancel(), update it to make theexceptionparameter nullable. If you were previously setting a default value, consider setting this within the method instead:@override void cancel([Exception? exception]) { exception ??= YourCustomDefaultException(); super.cancel(exception); }
To migrate your custom cancellables:
- Replace calls to
cancellationToken.attach(this)withmaybeAttach(cancellationToken). - Replace calls to
cancellationToken.detach(this)withdetach(). - Update
onCancel()overrides to callsuper.onCancel()and remove thestackTraceparameter. To get the stack trace, usecancellationStackTraceinstead:// Old @override void onCancel(Exception cancelException, [StackTrace? stackTrace]) { _internalCompleter.completeError( cancelException, stackTrace ?? StackTrace.current, ); } // New @override void onCancel(Exception cancelException) { super.onCancel(cancelException); _internalCompleter.completeError(cancelException, cancellationStackTrace); }
- Added a
-
1.6.129 Jan 2023 -
1.6.028 Jan 2023Release notes
Open source →- Added
CancellableIsolate.run(), based on the newIsolate.run()method in Dart 2.19.0. - Updated
cancellableComputeto useCancellableIsolate.run()internally. - Increased minimum Dart SDK to 2.19.0.
Release notes
Open source →- Added
CancellableIsolate.run(), based on the newIsolate.run()method in Dart 2.19.0. - Updated
cancellableComputeto useCancellableIsolate.run()internally. - Increased minimum Dart SDK to 2.19.0.
- Added
-
1.5.002 Oct 2022Release notes
Open source →- Added
MergedCancellationTokento combine multiple cancellation tokens into one. - Added
cancellableFutureOr()to simplify cancellation when working withFutureOrtypes. - Added
onError,whenComplete, andwhenCompleteOrCancelledparams toignoreCancellation(). This change doesn't impact existing usage.
Release notes
Open source →- Added
MergedCancellationTokento combine multiple cancellation tokens into one. - Added
cancellableFutureOr()to simplify cancellation when working withFutureOrtypes. - Added
onError,whenComplete, andwhenCompleteOrCancelledparams toignoreCancellation(). This change doesn't impact existing usage.
- Added
-
1.4.011 May 2022Release notes
Open source →- Added new static functions to
CancellableFutureto make the API more similar to Dart'sFuture:Future()➡️CancellableFuture.from()Future.microtask()➡️CancellableFuture.microtask()Future.sync()➡️CancellableFuture.sync()Future.value()➡️CancellableFuture.value()Future.delayed()➡️CancellableFuture.delayed()
- Breaking: The
CancellableFutureconstructor is now private. Calls to this constuctor should be replaced with.asCancellable()orCancellableFuture.value():// Removed: // await CancellableFuture(exampleFuture, cancellationToken).future; // Recommended: await exampleFuture.asCancellable(cancellationToken);
- Updated
cancellableComputewith the latest changes from the Flutter SDK'scomputefunction (see flutter/flutter#99527).
Release notes
Open source →- Added new static functions to
CancellableFutureto make the API more similar to Dart'sFuture:Future()➡️CancellableFuture.from()Future.microtask()➡️CancellableFuture.microtask()Future.sync()➡️CancellableFuture.sync()Future.value()➡️CancellableFuture.value()Future.delayed()➡️CancellableFuture.delayed()
- Breaking: The
CancellableFutureconstructor is now private. Calls to this constuctor should be replaced with.asCancellable()orCancellableFuture.value():// Removed: // await CancellableFuture(exampleFuture, cancellationToken).future; // Recommended: await exampleFuture.asCancellable(cancellationToken); - Updated
cancellableComputewith the latest changes from the Flutter SDK'scomputefunction (see flutter/flutter#99527).
- Added new static functions to
-
1.3.412 Mar 2022Release notes
Open source →- Rename the
onCancelmethod'straceparameter tostackTrace. - Add Cancellation Token HTTP example to README.
Release notes
Open source →- Rename the
onCancelmethod'straceparameter tostackTrace. - Add Cancellation Token HTTP example to README.
- Rename the
-
1.3.327 Feb 2022 -
1.3.223 Feb 2022 -
1.3.121 Feb 2022Release notes
Open source →- Bugfix: Fix exception if a Cancellable calls
cancellationToken.detach(this)in itsonCancelmethod.
Release notes
Open source →- Bugfix: Fix exception if a Cancellable calls
cancellationToken.detach(this)in itsonCancelmethod.
- Bugfix: Fix exception if a Cancellable calls
-
1.3.020 Feb 2022Release notes
Open source →- Added
CancellableCompleter.syncconstructor to match Dart's Completer. - Added
ignoreCancellations()convenience function for silently catching cancellation exceptions. - Added example project.
- StackTraces are now included when cancelling (experimental).
- Bugfix: Calling the
.exceptiongetter on a CancellationToken will no longer create a new CancelledException instance every time. - Bugfix: Fixed uncaught exceptions.
- Added
-
1.2.006 Feb 2022Release notes
Open source →- Added support for nullable CancellationTokens, allowing functions/classes to be made cancellable without breaking existing implementations.
-
1.1.120 Jan 2022 -
1.1.020 Jan 2022Release notes
Open source →- Added
cancellableCompute()for running isolates that can be killed using a CancellationToken. - Increased minimum Dart SDK to 2.15.0.
- Added
-
1.0.019 Jan 2022