-
Notifications
You must be signed in to change notification settings - Fork 0
/
cancelable_future_example.dart
206 lines (178 loc) · 4.76 KB
/
cancelable_future_example.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import 'dart:async';
import 'package:cancelable_future/cancelable_future.dart';
Future<void> main() async {
await cancelableFutureTest();
}
Future<void> someOperation(int i) async {
try {
print('operation $i 0%');
await Future<void>.delayed(const Duration(milliseconds: 100));
print('operation $i 25%');
await Future<void>.delayed(const Duration(milliseconds: 100));
print('operation $i 50%');
await Future<void>.delayed(const Duration(milliseconds: 100));
print('operation $i 75%');
await Future<void>.delayed(const Duration(milliseconds: 100));
print('operation $i 100%');
} finally {
print('operation $i finally');
}
}
Future<void> someLongOperation() async {
try {
print('operation 1');
await someOperation(1);
print('operation 2');
await someOperation(2);
print('operation 3');
await someOperation(3);
print('operation 4');
await someOperation(4);
} finally {
print('operations finally');
}
}
final class Resource {
Resource() {
print('create resource');
}
void dispose() {
print('dispose resource');
}
}
Future<void> cancelableFutureTest() async {
// Example 1:
// Cancel by timer.
//
// operation 1
// operation 1 0%
// operation 1 25%
// operation 1 50%
// operation 1 75%
// operation 1 100%
// operation 1 finally
// operation 2
// operation 2 0%
// operation 2 25%
// operation 2 50%
// --- cancel ---
// operation 2 75% <- nearest breakpoint
// operation 2 finally
// operations finally
// main finally
// result: null
// result: canceled
// exception: [AsyncCancelException] Async operation canceled
// --- really canceled ---
print('\nExample 1. Cancel by timer');
final f1 = CancelableFuture(() async {
try {
await someLongOperation();
return 'result';
} finally {
print('main finally');
}
});
final cancelfuture = Future<void>.delayed(
const Duration(milliseconds: 650),
() async {
print('--- cancel ---');
await f1.cancel();
print('--- really canceled ---');
},
);
print('result: ${await f1.orNull}');
print('result: ${await f1.onCancel(() => 'canceled')}');
try {
print(await f1);
} on AsyncCancelException catch (error) {
print('exception: [${error.runtimeType}] $error');
}
await cancelfuture; // Will really be canceled much later.
// Example 2:
// Cancel by timeout.
//
// operation 1
// operation 1 0%
// operation 1 25%
// operation 1 50%
// operation 1 75%
// --- timeout ---
// operation 1 100%
// operation 1 finally
// operation 2 <- function - don't stop, do the synchronized part
// operation 2 0% <- nearest breakpoint
// operation 2 finally
// operations finally
// main finally
// result: timeout
// result: null
// result: canceled
// exception: [AsyncCancelByTimeoutException]
// Async operation canceled by timeout: 0:00:00.350000
print('\nExample 2. Cancel by timeout');
final f2 = CancelableFuture(() async {
try {
await someLongOperation();
return 'result';
} finally {
print('main finally');
}
});
final f2t = f2.timeout(
const Duration(milliseconds: 350),
cancelOnTimeout: true,
onTimeout: () {
print('--- timeout ---');
return 'timeout';
},
);
print('result: ${await f2t}');
print('result: ${await f2.orNull}');
print('result: ${await f2.onCancel(() => 'canceled')}');
try {
print(await f2);
} on AsyncCancelException catch (error) {
print('exception: [${error.runtimeType}] $error');
}
// Example 3:
// Problem: If the code is not prepared to be canceled, resources may leak
// out.
//
// create resource
// safe async code without exceptions
// --- cancel ---
// exception: [AsyncCancelException] Async operation canceled
print('\nExample 3. If the code is not prepared to be canceled,'
' resources may leak out');
Future<void> safeAsyncCodeWhereNoExceptionsAreExpected() async {
print('safe async code without exceptions');
await Future<void>.delayed(const Duration(milliseconds: 100));
await Future<void>.delayed(const Duration(milliseconds: 100));
}
Future<void> fairlyCommonThirdPartyCode() async {
final resource = Resource();
await safeAsyncCodeWhereNoExceptionsAreExpected();
try {
print('code that may have exceptions');
} finally {
resource.dispose();
}
}
final f3 = CancelableFuture(() async {
await fairlyCommonThirdPartyCode();
return 'result';
});
Future<void>.delayed(
const Duration(milliseconds: 50),
() {
print('--- cancel ---');
f3.cancel();
},
);
try {
await f3;
} on AsyncCancelException catch (error) {
print('exception: [${error.runtimeType}] $error');
}
}