Skip to content

Commit be560b5

Browse files
committed
Incorporate review suggestions.
- Changes finally0 to finallyDo. - Removes unnecessary subscription-wrapping. - Handle exceptions in onCompleted/onError
1 parent fb0e8b0 commit be560b5

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

rxjava-core/src/main/java/rx/Observable.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,8 +1192,8 @@ public static <T> Observable<T> concat(Observable<T>... source) {
11921192
* @return an Observable that emits the same objects, then calls the action.
11931193
* @see <a href="http://msdn.microsoft.com/en-us/library/hh212133(v=vs.103).aspx">MSDN: Observable.Finally Method</a>
11941194
*/
1195-
public static <T> Observable<T> finally0(Observable source, Action0 action) {
1196-
return _create(OperationFinally.finally0(source, action));
1195+
public static <T> Observable<T> finallyDo(Observable source, Action0 action) {
1196+
return _create(OperationFinally.finallyDo(source, action));
11971197
}
11981198

11991199
/**
@@ -2463,8 +2463,8 @@ public Observable<T> filter(Func1<T, Boolean> predicate) {
24632463
* @return an Observable that emits the same objects as this observable, then calls the action.
24642464
* @see <a href="http://msdn.microsoft.com/en-us/library/hh212133(v=vs.103).aspx">MSDN: Observable.Finally Method</a>
24652465
*/
2466-
public Observable<T> finally0(Action0 action) {
2467-
return _create(OperationFinally.finally0(this, action));
2466+
public Observable<T> finallyDo(Action0 action) {
2467+
return _create(OperationFinally.finallyDo(this, action));
24682468
}
24692469

24702470
/**

rxjava-core/src/main/java/rx/operators/OperationFinally.java

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,10 @@ public final class OperationFinally {
3333
/**
3434
* Call a given action when a sequence completes (with or without an
3535
* exception). The returned observable is exactly as threadsafe as the
36-
* source observable; in particular, any situation allowing the source to
37-
* call onComplete or onError multiple times allows the returned observable
38-
* to call the final action multiple times.
36+
* source observable.
3937
* <p/>
4038
* Note that "finally" is a Java reserved word and cannot be an identifier,
41-
* so we use "finally0".
39+
* so we use "finallyDo".
4240
*
4341
* @param sequence An observable sequence of elements
4442
* @param action An action to be taken when the sequence is complete or throws an exception
@@ -48,7 +46,7 @@ public final class OperationFinally {
4846
* the given action will be called.
4947
* @see <a href="http://msdn.microsoft.com/en-us/library/hh212133(v=vs.103).aspx">MSDN Observable.Finally method</a>
5048
*/
51-
public static <T> Func1<Observer<T>, Subscription> finally0(final Observable<T> sequence, final Action0 action) {
49+
public static <T> Func1<Observer<T>, Subscription> finallyDo(final Observable<T> sequence, final Action0 action) {
5250
return new Func1<Observer<T>, Subscription>() {
5351
@Override
5452
public Subscription call(Observer<T> observer) {
@@ -60,26 +58,14 @@ public Subscription call(Observer<T> observer) {
6058
private static class Finally<T> implements Func1<Observer<T>, Subscription> {
6159
private final Observable<T> sequence;
6260
private final Action0 finalAction;
63-
private Subscription s;
6461

6562
Finally(final Observable<T> sequence, Action0 finalAction) {
6663
this.sequence = sequence;
6764
this.finalAction = finalAction;
6865
}
6966

70-
private final AtomicObservableSubscription Subscription = new AtomicObservableSubscription();
71-
72-
private final Subscription actualSubscription = new Subscription() {
73-
@Override
74-
public void unsubscribe() {
75-
if (null != s)
76-
s.unsubscribe();
77-
}
78-
};
79-
8067
public Subscription call(Observer<T> observer) {
81-
s = sequence.subscribe(new FinallyObserver(observer));
82-
return Subscription.wrap(actualSubscription);
68+
return sequence.subscribe(new FinallyObserver(observer));
8369
}
8470

8571
private class FinallyObserver implements Observer<T> {
@@ -91,14 +77,20 @@ private class FinallyObserver implements Observer<T> {
9177

9278
@Override
9379
public void onCompleted() {
94-
observer.onCompleted();
95-
finalAction.call();
80+
try {
81+
observer.onCompleted();
82+
} finally {
83+
finalAction.call();
84+
}
9685
}
9786

9887
@Override
9988
public void onError(Exception e) {
100-
observer.onError(e);
101-
finalAction.call();
89+
try {
90+
observer.onError(e);
91+
} finally {
92+
finalAction.call();
93+
}
10294
}
10395

10496
@Override
@@ -117,7 +109,7 @@ public void before() {
117109
aObserver = mock(Observer.class);
118110
}
119111
private void checkActionCalled(Observable<String> input) {
120-
Observable.create(finally0(input, aAction0)).subscribe(aObserver);
112+
Observable.create(finallyDo(input, aAction0)).subscribe(aObserver);
121113
verify(aAction0, times(1)).call();
122114
}
123115
@Test

0 commit comments

Comments
 (0)