LCOV - code coverage report

Current view
top level - src/_impl/xplat - _forward_stream_controller.dart
Test
lcov.info
Date
2024-11-13
Legend
Lines
hit
not hit
Branches
taken
not taken
# not executed
HitTotalCoverage
Lines383997.4%
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> {
718 ForwardStreamController(
8 {void Function()? onListen, FutureOr<void> Function()? onCancel}) {
927 _controller = StreamController(
10 onListen: onListen,
1118 onPause: _pause,
1218 onResume: _resume,
13 onCancel: onCancel,
14 );
155 }
16
1710 late final StreamController<T> _controller;
18
1927 Stream<T> get stream => _controller.stream;
20
2115 Future<void> get done => _controller.done;
22
2327 bool get isClosed => _controller.isClosed;
24
25 StreamSubscription? _sub;
26
2727 StreamSubscription? get subscription => _sub;
28
2915 void add(T data) {
3046 if (!_controller.isClosed) _controller.add(data);
316 }
32
336 void addError(SquadronException ex) {
3432 if (!_controller.isClosed) _controller.addError(ex);
35 }
36
3718 Future<void> close() async {
3827 await _sub?.cancel();
3918 _sub = null;
4027 _controller.close();
419 }
42
43 int _pauses = 0;
44
452 void _pause() {
46 // per Dart documentation https://api.dart.dev/dart-async/StreamSubscription/pause.html
47 // If the subscription is paused more than once, an equal number of resumes
48 // must be performed to resume the stream
492 _pauses++;
501 }
51
522 void _resume() {
533 if (_pauses > 0) {
54 // per Dart documentation https://api.dart.dev/dart-async/StreamSubscription/pause.html
55 // If the subscription is paused more than once, an equal number of resumes
56 // must be performed to resume the stream
573 _pauses--;
58 } else {
59 // per Dart documentation https://api.dart.dev/dart-async/StreamSubscription/resume.html
60 // It is safe to resume even when the subscription is not paused, and the
61 // resume will have no effect.
62 }
631 }
64
6518 void attachSubscription(StreamSubscription sub) {
6618 if (_sub != null) {
670 throw SquadronErrorExt.create(
68 'Invalid state: a subscription is already attached');
69 }
7018 _sub = sub;
71 // pass pending pause events on to the subscription
7227 while (_pauses > 0) {
733 _pauses--;
742 sub.pause();
75 }
76 // have the subscription handle pause/resume events from now on
7736 _controller.onPause = sub.pause;
7836 _controller.onResume = sub.resume;
79 // have the subscription handle the cancel event if the controller doesn't
80 // handle it already
8136 _controller.onCancel ??= sub.cancel;
8218 _sub = sub;
839 }
84}
Choose Features