PackageTrack
Sign in Get early access

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 2023
2023 2024 2025 2026
Release Pre-release

Releases

latest 15
  1. 2.0.1 11 Jul 2023
    Release notes
    • Fixed a Bad state: Future already completed exception that could occur when nesting cancellable futures.
    Open source →
    Release notes
    • Fixed a Bad state: Future already completed exception that could occur when nesting cancellable futures.
    Open source →
  2. 2.0.0 08 May 2023
    Release notes

    This release aims to make it easier to implement custom Cancellables and provide more useful information for debugging.

    • Added a .cancelWithReason() method to CancellationToken for a convenient way to set provide a cancellation reason for debugging.
    • CancelledException now 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 the Cancellable mixin. When paired with .maybeAttach(), this will detach your cancellable from the token.
    • Fixed a bug where the ignoreCancellation() wouldn't call whenComplete if onError threw 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.exception getter now returns null if the token hasn't been cancelled yet.
    • The CancellationToken.cancel() method's exception parameter is now nullable, rather than using a default value.
    • Removed the [StackTrace? stackTrace] parameter from the Cancellable mixin's onCancel method. Instead, use the new cancellationStackTrace, which returns the stack trace at the time the cancellable was created.
    • Overridden Cancellable mixin 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 the exception parameter 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) with maybeAttach(cancellationToken).
    • Replace calls to cancellationToken.detach(this) with detach().
    • Update onCancel() overrides to call super.onCancel() and remove the stackTrace parameter. To get the stack trace, use cancellationStackTrace instead:
      // 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);
      }
    Open source →
    Release notes

    This release aims to make it easier to implement custom Cancellables and provide more useful information for debugging.

    • Added a .cancelWithReason() method to CancellationToken for a convenient way to set provide a cancellation reason for debugging.
    • CancelledException now 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 the Cancellable mixin. When paired with .maybeAttach(), this will detach your cancellable from the token.
    • Fixed a bug where the ignoreCancellation() wouldn't call whenComplete if onError threw 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.exception getter now returns null if the token hasn't been cancelled yet.
    • The CancellationToken.cancel() method's exception parameter is now nullable, rather than using a default value.
    • Removed the [StackTrace? stackTrace] parameter from the Cancellable mixin's onCancel method. Instead, use the new cancellationStackTrace, which returns the stack trace at the time the cancellable was created.
    • Overridden Cancellable mixin 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 the exception parameter 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) with maybeAttach(cancellationToken).
    • Replace calls to cancellationToken.detach(this) with detach().
    • Update onCancel() overrides to call super.onCancel() and remove the stackTrace parameter. To get the stack trace, use cancellationStackTrace instead:
      // 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);
      }
      
    Open source →
  3. 1.6.1 29 Jan 2023
    Release notes
    • Include Dart SDK license in license file.
    Open source →
    Release notes
    • Include Dart SDK license in license file.
    Open source →
  4. 1.6.0 28 Jan 2023
    Release notes
    • Added CancellableIsolate.run(), based on the new Isolate.run() method in Dart 2.19.0.
    • Updated cancellableCompute to use CancellableIsolate.run() internally.
    • Increased minimum Dart SDK to 2.19.0.
    Open source →
    Release notes
    • Added CancellableIsolate.run(), based on the new Isolate.run() method in Dart 2.19.0.
    • Updated cancellableCompute to use CancellableIsolate.run() internally.
    • Increased minimum Dart SDK to 2.19.0.
    Open source →
  5. 1.5.0 02 Oct 2022
    Release notes
    • Added MergedCancellationToken to combine multiple cancellation tokens into one.
    • Added cancellableFutureOr() to simplify cancellation when working with FutureOr types.
    • Added onError, whenComplete, and whenCompleteOrCancelled params to ignoreCancellation(). This change doesn't impact existing usage.
    Open source →
    Release notes
    • Added MergedCancellationToken to combine multiple cancellation tokens into one.
    • Added cancellableFutureOr() to simplify cancellation when working with FutureOr types.
    • Added onError, whenComplete, and whenCompleteOrCancelled params to ignoreCancellation(). This change doesn't impact existing usage.
    Open source →
  6. 1.4.0 11 May 2022
    Release notes
    • Added new static functions to CancellableFuture to make the API more similar to Dart's Future:
      • Future() ➡️ CancellableFuture.from()
      • Future.microtask() ➡️ CancellableFuture.microtask()
      • Future.sync() ➡️ CancellableFuture.sync()
      • Future.value() ➡️ CancellableFuture.value()
      • Future.delayed() ➡️ CancellableFuture.delayed()
    • Breaking: The CancellableFuture constructor is now private. Calls to this constuctor should be replaced with .asCancellable() or CancellableFuture.value():
      // Removed:
      // await CancellableFuture(exampleFuture, cancellationToken).future;
      
      // Recommended:
      await exampleFuture.asCancellable(cancellationToken);
    • Updated cancellableCompute with the latest changes from the Flutter SDK's compute function (see flutter/flutter#99527).
    Open source →
    Release notes
    • Added new static functions to CancellableFuture to make the API more similar to Dart's Future:
      • Future() ➡️ CancellableFuture.from()
      • Future.microtask() ➡️ CancellableFuture.microtask()
      • Future.sync() ➡️ CancellableFuture.sync()
      • Future.value() ➡️ CancellableFuture.value()
      • Future.delayed() ➡️ CancellableFuture.delayed()
    • Breaking: The CancellableFuture constructor is now private. Calls to this constuctor should be replaced with .asCancellable() or CancellableFuture.value():
      // Removed:
      // await CancellableFuture(exampleFuture, cancellationToken).future;
      
      // Recommended:
      await exampleFuture.asCancellable(cancellationToken);
      
    • Updated cancellableCompute with the latest changes from the Flutter SDK's compute function (see flutter/flutter#99527).
    Open source →
  7. 1.3.4 12 Mar 2022
    Release notes
    • Rename the onCancel method's trace parameter to stackTrace.
    • Add Cancellation Token HTTP example to README.
    Open source →
    Release notes
    • Rename the onCancel method's trace parameter to stackTrace.
    • Add Cancellation Token HTTP example to README.
    Open source →
  8. 1.3.3 27 Feb 2022
    Release notes
    • Fix CancellableCompute web implementation.
    Open source →
    Release notes
    • Fix CancellableCompute web implementation.
    Open source →
  9. 1.3.2 23 Feb 2022
    Release notes
    • Added hasCancellables to CancellationToken.
    Open source →
    Release notes
    • Added hasCancellables to CancellationToken.
    Open source →
  10. 1.3.1 21 Feb 2022
    Release notes
    • Bugfix: Fix exception if a Cancellable calls cancellationToken.detach(this) in its onCancel method.
    Open source →
    Release notes
    • Bugfix: Fix exception if a Cancellable calls cancellationToken.detach(this) in its onCancel method.
    Open source →
  11. 1.3.0 20 Feb 2022
    Release notes
    • Added CancellableCompleter.sync constructor 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 .exception getter on a CancellationToken will no longer create a new CancelledException instance every time.
    • Bugfix: Fixed uncaught exceptions.
    Open source →
  12. 1.2.0 06 Feb 2022
    Release notes
    • Added support for nullable CancellationTokens, allowing functions/classes to be made cancellable without breaking existing implementations.
    Open source →
  13. 1.1.1 20 Jan 2022
    Release notes
    • Bugfix: Add missing cancellableCompute export.
    Open source →
  14. 1.1.0 20 Jan 2022
    Release notes
    • Added cancellableCompute() for running isolates that can be killed using a CancellationToken.
    • Increased minimum Dart SDK to 2.15.0.
    Open source →
  15. 1.0.0 19 Jan 2022
    Release notes

    Initial release

    Open source →

Every package, every release, already written down.

The archive is open and free. Watching your own project is what we are building next.

Browse the archive