Skip to content

Commit

Permalink
Add a "next" property to help RxCommand interop with Futures
Browse files Browse the repository at this point in the history
`next` is a new property which lets us await on the next invocation of
the command. This is nice on its own, as well as making it easier to
chain RxCommands together.

Fixes fluttercommunity#16
  • Loading branch information
anaisbetts committed Mar 28, 2019
1 parent da1fdf6 commit 667ed37
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/rx_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ abstract class RxCommand<TParam, TResult> extends Observable<TResult> {
/// If no subscription exists the Exception will be rethrown
Observable<dynamic> get thrownExceptions => _thrownExceptionsSubject;

/// This property is a utility which allows us to chain RxCommands together.
Future<TResult> get next => Observable.merge([this, this.thrownExceptions.cast<TResult>()]).take(1).last;

Subject<CommandResult<TResult>> _commandResultsSubject;
Subject<TResult> _resultsSubject;
final BehaviorSubject<bool> _isExecutingSubject = new BehaviorSubject<bool>();
Expand Down
29 changes: 29 additions & 0 deletions test/rx_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,35 @@ void main() {
expect(command.isExecuting, emits(false));
});

test("async function should be next'able", () async {
final cmd = RxCommand.createAsync((_) async {
await Future.delayed(Duration(milliseconds: 1));
return 42;
});

cmd.execute();
final result = await cmd.next;

expect(result, 42);
});

test("async functions that throw should be next'able", () async {
final cmd = RxCommand.createAsync((_) async {
await Future.delayed(Duration(milliseconds: 1));
throw Exception("oh no");
});

cmd.execute();
var didntThrow = true;
try {
await cmd.next;
} catch (e) {
didntThrow = false;
}

expect(didntThrow, false);
});

Stream<int> testProvider(int i) async* {
yield i;
yield i + 1;
Expand Down

0 comments on commit 667ed37

Please sign in to comment.