diff --git a/lib/src/operators/index.dart b/lib/src/operators/index.dart index ce06258..37a6406 100644 --- a/lib/src/operators/index.dart +++ b/lib/src/operators/index.dart @@ -5,5 +5,6 @@ export 'do_on.dart'; export 'done_on_error.dart'; export 'flat_map_batches.dart'; export 'ignore.dart'; +export 'start_with_future.dart'; export 'to_single_subscription.dart'; export 'void.dart'; diff --git a/lib/src/operators/start_with_future.dart b/lib/src/operators/start_with_future.dart new file mode 100644 index 0000000..513450e --- /dev/null +++ b/lib/src/operators/start_with_future.dart @@ -0,0 +1,22 @@ +import 'package:rxdart/rxdart.dart'; + +/// Just like [startWith], but accepts a [Future] +/// +/// ### Example +/// +/// Stream.fromIterable([1, 2, 3]) +/// .startWithFuture(Future(() async => 0)) +/// .listen(print); // prints 0, 1, 2, 3 +/// +extension StartWithFuture on Stream { + /// Just like [startWith], but accepts a [Future] + /// + /// ### Example + /// + /// Stream.fromIterable([1, 2, 3]) + /// .startWithFuture(Future(() async => 0)) + /// .listen(print); // prints 0, 1, 2, 3 + /// + Stream startWithFuture(Future startFuture) => + Rx.concatEager([startFuture.asStream(), this]); +} diff --git a/test/operators/start_with_future_test.dart b/test/operators/start_with_future_test.dart new file mode 100644 index 0000000..106b56a --- /dev/null +++ b/test/operators/start_with_future_test.dart @@ -0,0 +1,29 @@ +import 'dart:async'; + +import 'package:rxdart_ext/rxdart_ext.dart'; +import 'package:test/test.dart'; + +void main() { + test( + 'Stream.startWithFuture', + () async { + final stream = _createStreamForTest().startWithFuture(_startFuture()); + + expect(stream, emitsInOrder([0, 1, 2, 3])); + }, + ); +} + +Future _startFuture() async { + await Future.delayed(Duration(seconds: 1, milliseconds: 500)); + return 0; +} + +Stream _createStreamForTest() async* { + yield 1; + + await Future.delayed(Duration(seconds: 1)); + + yield 2; + yield 3; +}