1212
1313import 'dart:async' show FutureOr;
1414
15- import 'package:df_type/df_type.dart' show SequentialController;
16- import 'package:flutter/foundation.dart'
17- show kDebugMode, mustCallSuper, nonVirtual;
15+ import 'package:df_type/df_type.dart' show OperationWaiter;
16+ import 'package:flutter/foundation.dart' show kDebugMode, mustCallSuper, nonVirtual;
1817
1918// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
2019
@@ -27,8 +26,7 @@ import 'package:flutter/foundation.dart'
2726/// invoked on each resource wrapped with [willCancel] .
2827mixin WillCancelMixin on CancelMixin {
2928 /// The list of resources marked for cancel via [willCancel] .
30- Set <_ToCancelResource <dynamic >> get toCancelResources =>
31- Set .unmodifiable (_toCancelResources);
29+ Set <_ToCancelResource <dynamic >> get toCancelResources => Set .unmodifiable (_toCancelResources);
3230
3331 final Set <_ToCancelResource <dynamic >> _toCancelResources = {};
3432
@@ -50,13 +48,11 @@ mixin WillCancelMixin on CancelMixin {
5048 _verifyCancelMethod (resource);
5149 final disposable = (
5250 resource: resource as dynamic ,
53- onBeforeCancel:
54- onBeforeCancel != null ? (dynamic e) => onBeforeCancel (e as T ) : null ,
51+ onBeforeCancel: onBeforeCancel != null ? (dynamic e) => onBeforeCancel (e as T ) : null ,
5552 );
5653
5754 // Check for any duplicate resource.
58- final duplicate =
59- _toCancelResources.where ((e) => e.resource == resource).firstOrNull;
55+ final duplicate = _toCancelResources.where ((e) => e.resource == resource).firstOrNull;
6056
6157 if (duplicate != null ) {
6258 if (kDebugMode) {
@@ -78,41 +74,15 @@ mixin WillCancelMixin on CancelMixin {
7874 @mustCallSuper
7975 @override
8076 FutureOr <void > cancel () {
81- final sc = SequentialController <void >();
82-
83- try {
84- // Call the parent's cancel method.
85- sc.add ((_) => super .cancel ());
86-
87- for (final disposable in _toCancelResources) {
88- final resource = disposable.resource;
89- // Skip invalid resources.
90- if (! hasValidCancelMethod (resource)) continue ;
91-
92- // Attempt to call onBeforeCancel, catching and copying any exceptions.
93- Object ? onBeforeCancelError;
94- try {
95- sc.add ((_) => disposable.onBeforeCancel? .call (resource));
96- } catch (e) {
97- onBeforeCancelError = e;
98- }
99-
100- // Attempt to call cancel on the resource.
101- sc.add ((_) => resource.cancel ());
102-
103- // If successful, rethrow any exception from onBeforeCancel.
104- if (onBeforeCancelError != null ) {
105- throw onBeforeCancelError;
106- }
107- }
108- } catch (e) {
109- // Collect exceptions to throw them all at the end, ensuring cancel gets
110- // called on all resources.
111- sc.addException (e);
77+ final waiter = OperationWaiter <void >();
78+ waiter.add (super .cancel);
79+ for (final disposable in _toCancelResources) {
80+ final resource = disposable.resource;
81+ if (! hasValidCancelMethod (resource)) continue ;
82+ waiter.add (() => disposable.onBeforeCancel? .call (resource));
83+ waiter.add (() => resource.cancel ());
11284 }
113-
114- // Return a Future or complete synchronously.
115- return sc.complete ();
85+ return waiter.wait ();
11686 }
11787
11888 /// Throws [NoCancelMethodDebugError] if [resource] does not have a `cancel`
@@ -167,8 +137,7 @@ final class WillAlreadyCancelDebugError<T> extends Error {
167137 WillAlreadyCancelDebugError (this .resource);
168138
169139 @override
170- String toString () =>
171- '[$WillAlreadyCancelDebugError ] willCancel has already '
140+ String toString () => '[$WillAlreadyCancelDebugError ] willCancel has already '
172141 'been called on the resource ${resource .hashCode } and of type $T .' ;
173142}
174143
@@ -182,8 +151,7 @@ mixin CancelMixin {
182151
183152// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
184153
185- typedef _ToCancelResource <T > =
186- ({T resource, _OnBeforeCallback <T >? onBeforeCancel});
154+ typedef _ToCancelResource <T > = ({T resource, _OnBeforeCallback <T >? onBeforeCancel});
187155
188156typedef _FutureOrCallback <T > = FutureOr <void > Function ();
189157typedef _OnBeforeCallback <T > = FutureOr <void > Function (T resource);
0 commit comments