LCOV - code coverage report

Current view
top level - /src - composite_token.dart
Test
lcov.info
Date
2022-04-02
Legend
Lines
hit
not hit
Branches
taken
not taken
# not executed
HitTotalCoverage
Lines343597.1%
Functions00-
Branches00-
Each row represents a line of source code
LineBranchHitsSource code
1import 'cancellable_token.dart';
2import 'cancellation_token.dart';
3import 'squadron_error.dart';
4import 'worker_exception.dart';
5
6/// Composite token cancellation mode
71enum CompositeMode {
8 /// the [CompositeToken] is cancelled iif all tokens get cancelled
9 all,
10
11 /// the [CompositeToken] is cancelled as soon as one of the tokens gets cancelled
12 any
130}
14
15/// Time-out cancellation tokens used by callers of worker services. The token is cancelled automatically after
16/// a period of time indicated by [duration] with a countdown starting only when the task is assigned to a
17/// platform worker.
18class CompositeToken extends CancellableToken {
192 CompositeToken(Iterable<CancellationToken> tokens, this.mode,
20 [String? message])
213 : assert(tokens.isNotEmpty),
221 _tokens = tokens.toList(),
23 _signaled = 0,
241 super(message) {
253 for (var token in _tokens) {
261 if (token.cancelled) _signaled++;
271 _register(token);
28 }
291 }
30
31 /// Throws an exception, composite tokens may not be cancelled programmatically.
321 @override
332 void cancel() => throw newSquadronError(
341 'CompositeToken cannot be cancelled programmatically');
35
36 final CompositeMode mode;
37
38 final List<CancellationToken> _tokens;
39 int _signaled;
40
41 /// Called just before processing a [WorkerRequest]. The method actually calls the [start] method for all
42 /// tokens registered with this [CompositeToken]. The [onTimeout] callback is mandatory if one of these
43 /// tokens is a [TimeOutToken].
441 @override
454 void start() => _tokens.forEach(_starter);
46
473 void _starter(CancellationToken token) => token.start();
48
492 void _signal() {
503 _signaled++;
512 _check();
521 }
53
542 void _check() {
552 if (!cancelled) {
565 if ((mode == CompositeMode.any && _signaled >= 1) ||
577 (mode == CompositeMode.all && _signaled >= _tokens.length)) {
582 if (mode == CompositeMode.all) {
594 setException(CancelledException(message: message ?? 'Cancelled'));
60 } else {
617 setException(_tokens.map((e) => e.exception).firstWhere(
622 (e) => e != null,
631 orElse: () =>
641 CancelledException(message: message ?? 'Cancelled'))!);
65 }
664 _tokens.forEach(_unregister);
672 super.cancel();
68 }
69 }
701 }
71
724 void _register(CancellationToken token) => token.addListener(_signal);
73
744 void _unregister(CancellationToken token) => token.removeListener(_signal);
75}
Choose Features