LCOV - code coverage report

Current view
top level - src/_impl/xplat - _forward_stream_controller.dart
Test
lcov.info
Date
2025-03-26
Legend
Lines
hit
not hit
Branches
taken
not taken
# not executed
HitTotalCoverage
Lines313296.9%
Functions00-
Branches00-
Each row represents a line of source code
LineBranchHitsSource code
1import 'dart:async';
2
3import '../../exceptions/squadron_error.dart';
4import '../../exceptions/squadron_exception.dart';
5
6class ForwardStreamController<T> {
710 ForwardStreamController(
8 {void Function()? onListen, FutureOr<void> Function()? onCancel}) {
920 _controller = StreamController(
10 onListen: onListen,
1110 onPause: _pause,
1210 onResume: _resume,
13 onCancel: onCancel,
14 );
15 }
16
17 late final StreamController<T> _controller;
18
1930 Stream<T> get stream => _controller.stream;
20
2130 Future<void> get done => _controller.done;
22
23 var _closed = false;
2440 bool get isClosed => _closed || _controller.isClosed;
25
26 StreamSubscription<T>? _sub;
27
2820 StreamSubscription<T>? get subscription => _sub;
29
3010 void add(T data) {
3130 if (!isClosed) _controller.add(data);
32 }
33
347 void addError(SquadronException ex) {
3521 if (!isClosed) _controller.addError(ex);
36 }
37
3810 Future<void> close() async {
3910 _closed = true;
4020 await _sub?.cancel();
4110 _sub = null;
4220 _controller.close();
43 }
44
45 int _pauses = 0;
46
471 void _pause() {
48 // per Dart documentation https://api.dart.dev/dart-async/StreamSubscription/pause.html
49 // If the subscription is paused more than once, an equal number of resumes
50 // must be performed to resume the stream
512 _pauses++;
52 }
53
541 void _resume() {
552 if (_pauses > 0) {
56 // per Dart documentation https://api.dart.dev/dart-async/StreamSubscription/pause.html
57 // If the subscription is paused more than once, an equal number of resumes
58 // must be performed to resume the stream
592 _pauses--;
60 } else {
61 // per Dart documentation https://api.dart.dev/dart-async/StreamSubscription/resume.html
62 // It is safe to resume even when the subscription is not paused, and the
63 // resume will have no effect.
64 }
65 }
66
6710 void attachSubscription(StreamSubscription<T> sub) {
6810 if (_sub != null) {
690 throw SquadronErrorImpl.create(
70 'Invalid state: a subscription is already attached');
71 }
7210 _sub = sub;
73 // pass pending pause events on to the subscription
7420 while (_pauses > 0) {
752 _pauses--;
761 sub.pause();
77 }
78 // have the subscription handle pause/resume events from now on
7930 _controller.onPause = sub.pause;
8030 _controller.onResume = sub.resume;
81 // have the subscription handle the cancel event if the controller doesn't
82 // handle it already
8330 _controller.onCancel ??= sub.cancel;
84 }
85}
Choose Features