|
1 | | -import 'dart:async' |
2 | | - show MultiStreamController, Stream, StreamController, StreamSubscription; |
| 1 | +import 'dart:async' show Stream, StreamController, StreamSubscription; |
3 | 2 |
|
4 | 3 | /// @private |
5 | 4 | /// Prepends a value to the source [Stream]. |
6 | 5 | extension StartWithExtension<T> on Stream<T> { |
7 | 6 | /// @private |
8 | 7 | /// Prepends a value to the source [Stream]. |
9 | 8 | Stream<T> startWith(T Function() seededProvider) { |
10 | | - MultiStreamController<T> controller; |
11 | | - StreamSubscription<T> subscription; |
12 | | - |
13 | | - final listenUpStream = () => listen( |
14 | | - controller.addSync, |
15 | | - onError: controller.addErrorSync, |
16 | | - onDone: () { |
17 | | - subscription = null; |
18 | | - controller.closeSync(); |
19 | | - }, |
20 | | - ); |
21 | | - |
22 | | - final onListen = (MultiStreamController<T> c) { |
23 | | - if (controller != null) { |
24 | | - return; |
25 | | - } |
26 | | - |
27 | | - controller = c; |
28 | | - controller.addSync(seededProvider()); |
29 | | - |
30 | | - subscription = listenUpStream(); |
31 | | - controller.onCancel = () { |
32 | | - subscription?.cancel(); |
33 | | - subscription = null; |
34 | | - }; |
| 9 | + final controller = StreamController<T>(sync: true); |
| 10 | + StreamSubscription subscription; |
| 11 | + |
| 12 | + controller.onListen = () { |
| 13 | + controller.add(seededProvider()); |
| 14 | + subscription = listen( |
| 15 | + controller.add, |
| 16 | + onError: controller.addError, |
| 17 | + onDone: controller.close, |
| 18 | + ); |
35 | 19 | }; |
36 | | - |
37 | | - return Stream.multi(onListen, isBroadcast: false).toSingleSubscription(); |
38 | | - } |
39 | | -} |
40 | | - |
41 | | -/// @private |
42 | | -/// Convert a [Stream] to Single-Subscription [Stream]. |
43 | | -extension ToSingleSubscriptionStreamExtension<T> on Stream<T> { |
44 | | - /// @private |
45 | | - /// Convert a [Stream] to Single-Subscription [Stream]. |
46 | | - Stream<T> toSingleSubscription() { |
47 | | - StreamController<T> controller; |
48 | | - StreamSubscription<T> subscription; |
49 | | - |
50 | | - controller = StreamController<T>( |
51 | | - sync: true, |
52 | | - onListen: () { |
53 | | - subscription = listen( |
54 | | - controller.add, |
55 | | - onError: controller.addError, |
56 | | - onDone: controller.close, |
57 | | - ); |
58 | | - }, |
59 | | - onPause: () => subscription.pause(), |
60 | | - onResume: () => subscription.resume(), |
61 | | - onCancel: () => subscription.cancel(), |
62 | | - ); |
| 20 | + controller.onPause = () => subscription.pause(); |
| 21 | + controller.onResume = () => subscription.resume(); |
| 22 | + controller.onCancel = () => subscription.cancel(); |
63 | 23 |
|
64 | 24 | return controller.stream; |
65 | 25 | } |
|
0 commit comments