Make onSuccess optional
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..90ee98e
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,7 @@
+# 0.2.0
+
+* Make `onSuccess` optional.
+
+# 0.1.0
+
+* Initial version.
\ No newline at end of file
diff --git a/README.md b/README.md
index 4e67e10..27882dc 100644
--- a/README.md
+++ b/README.md
@@ -40,8 +40,8 @@
var file = new File(path);
return when(
() => exists(file),
- (doesExist) => doesExist ?
- when(() => read(file), JSON.decode) :
+ onSuccess: (doesExist) => doesExist ?
+ when(() => read(file), onSuccess: JSON.decode) :
onAbsent());
}
@@ -52,7 +52,6 @@
print('Async json: $asyncJson');
});
}
-
```
[Process.run]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:io.Process#id_run
diff --git a/example/read_json_file.dart b/example/read_json_file.dart
index 2b01b2b..0582604 100644
--- a/example/read_json_file.dart
+++ b/example/read_json_file.dart
@@ -24,8 +24,8 @@
var file = new File(path);
return when(
() => exists(file),
- (doesExist) => doesExist ?
- when(() => read(file), JSON.decode) :
+ onSuccess: (doesExist) => doesExist ?
+ when(() => read(file), onSuccess: JSON.decode) :
onAbsent());
}
diff --git a/lib/when.dart b/lib/when.dart
index 6c3b6c8..ba9bcd1 100644
--- a/lib/when.dart
+++ b/lib/when.dart
@@ -10,17 +10,18 @@
/// [onComplete] that are provided are registered on the future,
/// and the resulting future is returned.
///
-/// Otherwise, if [callback] did not throw, [onSuccess] is called with the
-/// result of [callback], and the return value of [onSuccess] is captured.
+/// Otherwise, if [callback] did not throw, if [onSuccess] is provided, it is
+/// called with the with the result of [callback], and the return value of
+/// [onSuccess] is captured.
///
/// Otherwise, if [onError] was provided, it is called. It can take either
/// just an error, or a stack trace as well. If [onError] was not provided,
-/// the error is thrown not caught.
+/// the error is not caught.
///
/// [onComplete] is then called synchronously.
///
/// The captured value is then returned.
-when(callback, onSuccess(result), {onError, onComplete}) {
+when(callback, {onSuccess(result), onError, onComplete}) {
var result, hasResult = false;
try {
@@ -41,11 +42,20 @@
}
} finally {
if (result is Future) {
- result = result.then(onSuccess, onError: onError);
- if (onComplete != null) result = result.whenComplete(onComplete);
+ if (onSuccess != null) {
+ result = result.then(onSuccess);
+ }
+ if (onError != null) {
+ result = result.catchError(onError);
+ }
+ if (onComplete != null) {
+ result = result.whenComplete(onComplete);
+ }
} else {
if (hasResult) {
- result = onSuccess(result);
+ if (onSuccess != null) {
+ result = onSuccess(result);
+ }
}
if (onComplete != null) onComplete();
}
diff --git a/pubspec.yaml b/pubspec.yaml
index f904287..ac206a0 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: when
-version: 0.1.0
+version: 0.2.0
author: Sean Eagan <seaneagan1@gmail.com>
description: Register callbacks on code which is conditionally sync or async.
homepage: https://github.com/seaneagan/when.dart
diff --git a/test/test_when.dart b/test/test_when.dart
index c95ffa6..c8eef31 100644
--- a/test/test_when.dart
+++ b/test/test_when.dart
@@ -15,7 +15,7 @@
var onCompleteCalled = false;
var ret = when(
() => 5,
- (x) {
+ onSuccess: (x) {
expect(x, 5);
onSuccessCalled = true;
return 10;
@@ -36,7 +36,7 @@
var onCompleteCalled = false;
var ret = when(
() => throw 'e',
- (_) => onSuccessCalled = true,
+ onSuccess: (_) => onSuccessCalled = true,
onError: (e) {
expect(e, 'e');
onErrorCalled = true;
@@ -54,7 +54,6 @@
var onErrorCalled = false;
when(
() => throw 'e',
- (_) {},
onError: (e, s) {
onErrorCalled = true;
expect(s, isNotNull);
@@ -65,19 +64,21 @@
test('should throw callback error if no onError provided', () {
try {
when(
- () => throw 'e',
- (_) {});
+ () => throw 'e');
fail('callback error was swallowed');
} catch (e) {
expect(e, 'e');
}
});
+ test('should just return callback result if onSuccess not provided', () {
+ expect(when(() => 5), 5);
+ });
+
test('should not swallow onComplete error', () {
try {
when(
() {},
- (_) {},
onComplete: () => throw 'e');
fail('onComplete error was swallowed');
} catch (e) {
@@ -93,7 +94,7 @@
var onCompleteCalled = false;
var result = when(
() => new Future.value(5),
- (x) {
+ onSuccess: (x) {
expect(x, 5);
onSuccessCalled = true;
return 10;
@@ -119,7 +120,7 @@
var onCompleteCalled = false;
var result = when(
() => new Future.error('e'),
- (_) => onSuccessCalled = true,
+ onSuccess: (_) => onSuccessCalled = true,
onError: (e) {
expect(e, 'e');
onErrorCalled = true;
@@ -142,7 +143,6 @@
var onErrorCalled = false;
return when(
() => new Future.error('e'),
- (_) {},
onError: (e, s) {
onErrorCalled = true;
// TODO: Why is the stack trace null?
@@ -154,8 +154,7 @@
test('should throw callback error if no onError provided', () {
return when(
- () => new Future.error('e'),
- (x) {}
+ () => new Future.error('e')
).then((_) {
fail('callback error was swallowed');
}, onError: (e) {
@@ -163,10 +162,15 @@
});
});
+ test('should just return callback result if onSuccess not provided', () {
+ return when(() => new Future.value(5)).then((result) {
+ expect(result, 5);
+ });
+ });
+
test('should not swallow onComplete error', () {
return when(
() => new Future.value(),
- (x) {},
onComplete: () => throw 'e')
.then((ret) {
fail('onComplete error was swallowed');