Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/bloc_concurrency/lib/src/droppable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ class _ExhaustMapStreamTransformer<T> extends StreamTransformerBase<T, T> {
StreamSubscription<T>? mappedSubscription;

final controller = StreamController<T>(
onCancel: () async {
await mappedSubscription?.cancel();
return subscription.cancel();
onCancel: () {
// A slow mapped cancellation must not delay releasing the source.
final mappedCancellation = mappedSubscription?.cancel();
final sourceCancellation = subscription.cancel();
if (mappedCancellation == null) return sourceCancellation;
return Future.wait<void>(
[mappedCancellation, sourceCancellation],
).then<void>((_) {});
},
sync: true,
);
Expand Down
46 changes: 46 additions & 0 deletions packages/bloc_concurrency/test/src/droppable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,51 @@ void main() {

expect(states, isEmpty);
});

test('cancels source promptly if mapped cancellation stalls', () async {
final mappedCancelStarted = Completer<void>();
final mappedCancelRelease = Completer<void>();
final sourceCancelled = Completer<void>();
final source = StreamController<int>(
onCancel: sourceCancelled.complete,
);
final mapped = StreamController<int>(
onCancel: () {
mappedCancelStarted.complete();
return mappedCancelRelease.future;
},
);

final output = droppable<int>()(source.stream, (_) => mapped.stream);
final subscription = output.listen((_) {});
source.add(1);
await tick();

final cancelling = subscription.cancel();
await mappedCancelStarted.future.timeout(const Duration(seconds: 1));
try {
await sourceCancelled.future.timeout(const Duration(seconds: 1));
} finally {
mappedCancelRelease.complete();
}
await cancelling.timeout(const Duration(seconds: 1));
await source.close();
await mapped.close();
});

test('releases the source when cancelled while idle', () async {
final source = StreamController<int>.broadcast();
final output = droppable<int>()(
source.stream,
(event) => Stream<int>.value(event),
);
final subscription = output.listen((_) {});

expect(source.hasListener, isTrue);
await subscription.cancel();
expect(source.hasListener, isFalse);

await source.close();
});
});
}