rxdart
rxdart copied to clipboard
BehaviorSubject.isEmpty never finishes when stream is empty
Consider this case:
test('test', () async {
final sub = BehaviorSubject<int>();
final empty = await sub.isEmpty;
print(empty);
});
When run it never finishes.
![CleanShot 2021-12-01 at 11 09 03@2x](https://user-images.githubusercontent.com/16854239/144216624-10c18002-6166-46ed-9940-94859861b6b9.png)
However, when using .hasValue
it works just fine:
test('test', () async {
final sub = BehaviorSubject<int>();
final hasValue = await sub.hasValue;
print(hasValue);
});
I wonder if this is a known/expected behavior or bug.
I think you just need to close that BehaviorSubject
Stream.isEmpty completes when the Stream emits the first value or done event.
Future<bool> get isEmpty {
_Future<bool> future = new _Future<bool>();
StreamSubscription<T> subscription =
this.listen(null, onError: future._completeError, onDone: () {
future._complete(true);
}, cancelOnError: true);
subscription.onData((_) {
_cancelAndValue(subscription, future, false);
});
return future;
}
Fix:
test('test', () async {
final sub = BehaviorSubject<int>();
sub.close();
final empty = await sub.isEmpty;
print(empty);
});
test('test', () async {
final sub = BehaviorSubject<int>();
sub.add(1);
final empty = await sub.isEmpty;
print(empty);
});