LCOV - code coverage report

Current view
top level - src/_impl/xplat - _forward_stream_controller.dart
Test
lcov.info
Date
2026-02-21
Legend
Lines
hit
not hit
Branches
taken
not taken
# not executed
HitTotalCoverage
Lines293096.7%
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';
5import '../../utils.dart';
6
7class ForwardStreamController<T> {
810 ForwardStreamController(
9 {void Function()? onListen, FutureOr<void> Function()? onCancel}) {
1020 _controller = StreamController(
11 onListen: onListen,
1210 onPause: _pause,
1310 onResume: _resume,
14 onCancel: onCancel,
15 );
16 }
17
18 late final StreamController<T> _controller;
19
2030 Stream<T> get stream => _controller.stream;
21
2230 Future<void> get done => _controller.done;
23
2430 bool get isClosed => _controller.isClosed;
25
26 StreamSubscription<T>? _sub;
27
2820 StreamSubscription<T>? get subscription => _sub;
29
3030 void safeAdd(T data) => _controller.safeAdd(data);
31
3221 void safeAddError(SquadronException ex) => _controller.safeAddError(ex);
33
3410 void close() {
3520 _controller.close();
3610 if (_sub != null) {
3720 _sub!.cancel();
3810 _sub = null;
39 }
40 }
41
42 int _pauses = 0;
43
441 void _pause() {
45 // per Dart documentation https://api.dart.dev/dart-async/StreamSubscription/pause.html
46 // If the subscription is paused more than once, an equal number of resumes
47 // must be performed to resume the stream
482 _pauses++;
49 }
50
511 void _resume() {
522 if (_pauses > 0) {
53 // per Dart documentation https://api.dart.dev/dart-async/StreamSubscription/pause.html
54 // If the subscription is paused more than once, an equal number of resumes
55 // must be performed to resume the stream
562 _pauses--;
57 } else {
58 // per Dart documentation https://api.dart.dev/dart-async/StreamSubscription/resume.html
59 // It is safe to resume even when the subscription is not paused, and the
60 // resume will have no effect.
61 }
62 }
63
6410 void attachSubscription(StreamSubscription<T> sub) {
6510 if (_sub != null) {
660 throw SquadronErrorImpl.create(
67 'Invalid state: a subscription is already attached');
68 }
6910 _sub = sub;
70 // pass pending pause events on to the subscription
7120 while (_pauses > 0) {
722 _pauses--;
731 sub.pause();
74 }
75 // have the subscription handle pause/resume events from now on
7630 _controller.onPause = sub.pause;
7730 _controller.onResume = sub.resume;
78 // have the subscription handle the cancel event if the controller doesn't
79 // handle it already
8025 _controller.onCancel ??= sub.cancel;
81 }
82}
Choose Features