1 | | | import 'dart:async'; |
2 | | |
|
3 | | | import '../../exceptions/squadron_error.dart'; |
4 | | | import '../../exceptions/squadron_exception.dart'; |
5 | | |
|
6 | | | class ForwardStreamController<T> { |
7 | | 18 | ForwardStreamController( |
8 | | | {void Function()? onListen, FutureOr<void> Function()? onCancel}) { |
9 | | 27 | _controller = StreamController( |
10 | | | onListen: onListen, |
11 | | 18 | onPause: _pause, |
12 | | 18 | onResume: _resume, |
13 | | | onCancel: onCancel, |
14 | | | ); |
15 | | 5 | } |
16 | | |
|
17 | | 10 | late final StreamController<T> _controller; |
18 | | |
|
19 | | 27 | Stream<T> get stream => _controller.stream; |
20 | | |
|
21 | | 15 | Future<void> get done => _controller.done; |
22 | | |
|
23 | | 27 | bool get isClosed => _controller.isClosed; |
24 | | |
|
25 | | | StreamSubscription? _sub; |
26 | | |
|
27 | | 27 | StreamSubscription? get subscription => _sub; |
28 | | |
|
29 | | 15 | void add(T data) { |
30 | | 46 | if (!_controller.isClosed) _controller.add(data); |
31 | | 6 | } |
32 | | |
|
33 | | 6 | void addError(SquadronException ex) { |
34 | | 32 | if (!_controller.isClosed) _controller.addError(ex); |
35 | | | } |
36 | | |
|
37 | | 18 | Future<void> close() async { |
38 | | 27 | await _sub?.cancel(); |
39 | | 18 | _sub = null; |
40 | | 27 | _controller.close(); |
41 | | 9 | } |
42 | | |
|
43 | | | int _pauses = 0; |
44 | | |
|
45 | | 2 | 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 |
49 | | 2 | _pauses++; |
50 | | 1 | } |
51 | | |
|
52 | | 2 | void _resume() { |
53 | | 3 | 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 |
57 | | 3 | _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 | | | } |
63 | | 1 | } |
64 | | |
|
65 | | 18 | void attachSubscription(StreamSubscription sub) { |
66 | | 18 | if (_sub != null) { |
67 | | 0 | throw SquadronErrorExt.create( |
68 | | | 'Invalid state: a subscription is already attached'); |
69 | | | } |
70 | | 18 | _sub = sub; |
71 | | | // pass pending pause events on to the subscription |
72 | | 27 | while (_pauses > 0) { |
73 | | 3 | _pauses--; |
74 | | 2 | sub.pause(); |
75 | | | } |
76 | | | // have the subscription handle pause/resume events from now on |
77 | | 36 | _controller.onPause = sub.pause; |
78 | | 36 | _controller.onResume = sub.resume; |
79 | | | // have the subscription handle the cancel event if the controller doesn't |
80 | | | // handle it already |
81 | | 36 | _controller.onCancel ??= sub.cancel; |
82 | | 18 | _sub = sub; |
83 | | 9 | } |
84 | | | } |