Committed by
GitHub
Allow listening to Rx with an initial value
Showing
1 changed file
with
18 additions
and
0 deletions
@@ -111,6 +111,24 @@ mixin RxObjectMixin<T> on NotifyManager<T> { | @@ -111,6 +111,24 @@ mixin RxObjectMixin<T> on NotifyManager<T> { | ||
111 | 111 | ||
112 | Stream<T> get stream => subject.stream; | 112 | Stream<T> get stream => subject.stream; |
113 | 113 | ||
114 | + /// Returns a [StreamSubscription] similar to [listen], but with the | ||
115 | + /// added benefit that it primes the stream with the current [value], rather | ||
116 | + /// than waiting for the next [value]. This should not be called in [onInit] | ||
117 | + /// or anywhere else during the build process. | ||
118 | + StreamSubscription<T> listenAndPump(void Function(T event) onData, | ||
119 | + {Function? onError, void Function()? onDone, bool? cancelOnError}) { | ||
120 | + final subscription = listen( | ||
121 | + onData, | ||
122 | + onError: onError, | ||
123 | + onDone: onDone, | ||
124 | + cancelOnError: cancelOnError, | ||
125 | + ); | ||
126 | + | ||
127 | + subject.add(value); | ||
128 | + | ||
129 | + return subscription; | ||
130 | + } | ||
131 | + | ||
114 | /// Binds an existing `Stream<T>` to this Rx<T> to keep the values in sync. | 132 | /// Binds an existing `Stream<T>` to this Rx<T> to keep the values in sync. |
115 | /// You can bind multiple sources to update the value. | 133 | /// You can bind multiple sources to update the value. |
116 | /// Closing the subscription will happen automatically when the observer | 134 | /// Closing the subscription will happen automatically when the observer |
-
Please register or login to post a comment