1
- import 'dart:async' ;
1
+ import 'dart:async'
2
+ show MultiStreamController, Stream, StreamController, StreamSubscription;
2
3
3
- import 'package:flutter/foundation.dart' ;
4
- import 'package:rxdart/rxdart.dart' ;
4
+ import 'package:flutter/foundation.dart' show ValueListenable ;
5
+ import 'package:rxdart/rxdart.dart' show ErrorAndStackTrace, ValueStream ;
5
6
6
7
import 'listenable_to_stream.dart' ;
7
8
@@ -51,14 +52,12 @@ class ValueListenableStream<T> extends Stream<T> implements ValueStream<T> {
51
52
void Function () onDone,
52
53
bool cancelOnError,
53
54
}) {
55
+ final getValue = ([void _]) => _valueListenable.value;
56
+
54
57
if (_replayValue) {
55
- _stream ?? = _valueListenable
56
- .toStream ()
57
- .map ((_) => _valueListenable.value)
58
- .shareBehavior (value);
58
+ _stream ?? = _valueListenable.toStream ().map (getValue).startWith (getValue);
59
59
} else {
60
- _stream ?? =
61
- _valueListenable.toStream ().map ((_) => _valueListenable.value);
60
+ _stream ?? = _valueListenable.toStream ().map (getValue);
62
61
}
63
62
64
63
return _stream.listen (
@@ -70,59 +69,59 @@ class ValueListenableStream<T> extends Stream<T> implements ValueStream<T> {
70
69
}
71
70
}
72
71
73
- extension _ShareValueExtension <T > on Stream <T > {
74
- Stream <T > shareBehavior (T seeded) {
75
- final controllers = < MultiStreamController <T >> [];
76
- StreamSubscription <T > subscription;
77
-
78
- var latestValue = seeded;
79
- var cancel = false ;
80
- var done = false ;
72
+ /// TODO
73
+ extension StartWithExtension <T > on Stream <T > {
74
+ /// TODO
75
+ Stream <T > startWith (T Function () seededProvider) {
76
+ MultiStreamController <T > multiController;
77
+ StreamSubscription <T > upstreamSubscription;
81
78
82
79
final listenUpStream = () => listen (
83
- (event) {
84
- latestValue = event;
85
- controllers.forEach ((c) => c.addSync (event));
86
- },
87
- onError: (e, StackTrace st) =>
88
- controllers.forEach ((c) => c.addErrorSync (e, st)),
80
+ multiController.addSync,
81
+ onError: multiController.addErrorSync,
89
82
onDone: () {
90
- done = true ;
91
- subscription = null ;
92
-
93
- controllers.forEach ((c) {
94
- c.onCancel = null ;
95
- c.closeSync ();
96
- });
97
- controllers.clear ();
83
+ upstreamSubscription = null ;
84
+ multiController.closeSync ();
98
85
},
99
86
);
100
87
101
- final onListen = (MultiStreamController <T > controller) {
102
- if (cancel) {
103
- return controller.closeSync ();
104
- }
105
- controller.addSync (latestValue);
106
- if (done) {
107
- return controller.closeSync ();
88
+ final onListen = (MultiStreamController <T > c) {
89
+ if (multiController != null ) {
90
+ return ;
108
91
}
109
92
110
- final wasEmpty = controllers.isEmpty;
111
- controllers.add (controller);
112
- if (wasEmpty) {
113
- subscription = listenUpStream ();
114
- }
93
+ multiController = c;
94
+ multiController.addSync (seededProvider ());
115
95
116
- controller.onCancel = () {
117
- controllers.remove (controller);
118
- if (controllers.isEmpty) {
119
- subscription? .cancel ();
120
- subscription = null ;
121
- cancel = true ;
122
- }
96
+ upstreamSubscription = listenUpStream ();
97
+ multiController.onCancel = () {
98
+ upstreamSubscription? .cancel ();
99
+ upstreamSubscription = null ;
123
100
};
124
101
};
125
102
126
- return Stream .multi (onListen, isBroadcast: true );
103
+ return Stream .multi (onListen, isBroadcast: false ).toSingleSubscription ();
104
+ }
105
+
106
+ /// TODO
107
+ Stream <T > toSingleSubscription () {
108
+ StreamController <T > controller;
109
+ StreamSubscription <T > subscription;
110
+
111
+ controller = StreamController <T >(
112
+ sync : true ,
113
+ onListen: () {
114
+ subscription = listen (
115
+ controller.add,
116
+ onError: controller.addError,
117
+ onDone: controller.close,
118
+ );
119
+ },
120
+ onPause: () => subscription.pause (),
121
+ onResume: () => subscription.resume (),
122
+ onCancel: () => subscription.cancel (),
123
+ );
124
+
125
+ return controller.stream;
127
126
}
128
127
}
0 commit comments