-
Notifications
You must be signed in to change notification settings - Fork 7.6k
1.x: add Completable.safeSubscribe option + RxJavaPlugins hook support #3942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
fcb201a
9469e9a
98c0315
159f868
aa7d82f
ebb38eb
f3958da
1c1f37d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,7 +173,7 @@ public void onSubscribe(Subscription d) { | |
} | ||
|
||
// no need to have separate subscribers because inner is stateless | ||
c.subscribe(inner); | ||
c.unsafeSubscribe(inner); | ||
} | ||
} | ||
}); | ||
|
@@ -301,7 +301,7 @@ public void onSubscribe(Subscription d) { | |
} | ||
|
||
// no need to have separate subscribers because inner is stateless | ||
c.subscribe(inner); | ||
c.unsafeSubscribe(inner); | ||
} | ||
} | ||
}); | ||
|
@@ -416,7 +416,7 @@ public void call(CompletableSubscriber s) { | |
return; | ||
} | ||
|
||
c.subscribe(s); | ||
c.unsafeSubscribe(s); | ||
} | ||
}); | ||
} | ||
|
@@ -899,7 +899,7 @@ public void call(final CompletableSubscriber s) { | |
|
||
final AtomicBoolean once = new AtomicBoolean(); | ||
|
||
cs.subscribe(new CompletableSubscriber() { | ||
cs.unsafeSubscribe(new CompletableSubscriber() { | ||
Subscription d; | ||
void dispose() { | ||
d.unsubscribe(); | ||
|
@@ -999,7 +999,7 @@ public final void await() { | |
final CountDownLatch cdl = new CountDownLatch(1); | ||
final Throwable[] err = new Throwable[1]; | ||
|
||
subscribe(new CompletableSubscriber() { | ||
unsafeSubscribe(new CompletableSubscriber() { | ||
|
||
@Override | ||
public void onCompleted() { | ||
|
@@ -1050,7 +1050,7 @@ public final boolean await(long timeout, TimeUnit unit) { | |
final CountDownLatch cdl = new CountDownLatch(1); | ||
final Throwable[] err = new Throwable[1]; | ||
|
||
subscribe(new CompletableSubscriber() { | ||
unsafeSubscribe(new CompletableSubscriber() { | ||
|
||
@Override | ||
public void onCompleted() { | ||
|
@@ -1190,7 +1190,7 @@ public void call(final CompletableSubscriber s) { | |
final Scheduler.Worker w = scheduler.createWorker(); | ||
set.add(w); | ||
|
||
subscribe(new CompletableSubscriber() { | ||
unsafeSubscribe(new CompletableSubscriber() { | ||
|
||
|
||
@Override | ||
|
@@ -1302,7 +1302,7 @@ protected final Completable doOnLifecycle( | |
return create(new CompletableOnSubscribe() { | ||
@Override | ||
public void call(final CompletableSubscriber s) { | ||
subscribe(new CompletableSubscriber() { | ||
unsafeSubscribe(new CompletableSubscriber() { | ||
|
||
@Override | ||
public void onCompleted() { | ||
|
@@ -1434,7 +1434,7 @@ public final Throwable get() { | |
final CountDownLatch cdl = new CountDownLatch(1); | ||
final Throwable[] err = new Throwable[1]; | ||
|
||
subscribe(new CompletableSubscriber() { | ||
unsafeSubscribe(new CompletableSubscriber() { | ||
|
||
@Override | ||
public void onCompleted() { | ||
|
@@ -1478,7 +1478,7 @@ public final Throwable get(long timeout, TimeUnit unit) { | |
final CountDownLatch cdl = new CountDownLatch(1); | ||
final Throwable[] err = new Throwable[1]; | ||
|
||
subscribe(new CompletableSubscriber() { | ||
unsafeSubscribe(new CompletableSubscriber() { | ||
|
||
@Override | ||
public void onCompleted() { | ||
|
@@ -1530,7 +1530,7 @@ public void call(CompletableSubscriber s) { | |
|
||
CompletableSubscriber sw = onLift.call(s); | ||
|
||
subscribe(sw); | ||
unsafeSubscribe(sw); | ||
} catch (NullPointerException ex) { | ||
throw ex; | ||
} catch (Throwable ex) { | ||
|
@@ -1571,7 +1571,7 @@ public void call(final CompletableSubscriber s) { | |
|
||
s.onSubscribe(ad); | ||
|
||
subscribe(new CompletableSubscriber() { | ||
unsafeSubscribe(new CompletableSubscriber() { | ||
|
||
@Override | ||
public void onCompleted() { | ||
|
@@ -1633,7 +1633,7 @@ public final Completable onErrorComplete(final Func1<? super Throwable, Boolean> | |
return create(new CompletableOnSubscribe() { | ||
@Override | ||
public void call(final CompletableSubscriber s) { | ||
subscribe(new CompletableSubscriber() { | ||
unsafeSubscribe(new CompletableSubscriber() { | ||
|
||
@Override | ||
public void onCompleted() { | ||
|
@@ -1682,7 +1682,7 @@ public final Completable onErrorResumeNext(final Func1<? super Throwable, ? exte | |
@Override | ||
public void call(final CompletableSubscriber s) { | ||
final SerialSubscription sd = new SerialSubscription(); | ||
subscribe(new CompletableSubscriber() { | ||
unsafeSubscribe(new CompletableSubscriber() { | ||
|
||
@Override | ||
public void onCompleted() { | ||
|
@@ -1708,7 +1708,7 @@ public void onError(Throwable e) { | |
return; | ||
} | ||
|
||
c.subscribe(new CompletableSubscriber() { | ||
c.unsafeSubscribe(new CompletableSubscriber() { | ||
|
||
@Override | ||
public void onCompleted() { | ||
|
@@ -1844,7 +1844,7 @@ public final <T> Observable<T> startWith(Observable<T> other) { | |
*/ | ||
public final Subscription subscribe() { | ||
final MultipleAssignmentSubscription mad = new MultipleAssignmentSubscription(); | ||
subscribe(new CompletableSubscriber() { | ||
unsafeSubscribe(new CompletableSubscriber() { | ||
@Override | ||
public void onCompleted() { | ||
mad.unsubscribe(); | ||
|
@@ -1877,7 +1877,7 @@ public final Subscription subscribe(final Action0 onComplete) { | |
requireNonNull(onComplete); | ||
|
||
final MultipleAssignmentSubscription mad = new MultipleAssignmentSubscription(); | ||
subscribe(new CompletableSubscriber() { | ||
unsafeSubscribe(new CompletableSubscriber() { | ||
@Override | ||
public void onCompleted() { | ||
try { | ||
|
@@ -1919,7 +1919,7 @@ public final Subscription subscribe(final Action1<? super Throwable> onError, fi | |
requireNonNull(onComplete); | ||
|
||
final MultipleAssignmentSubscription mad = new MultipleAssignmentSubscription(); | ||
subscribe(new CompletableSubscriber() { | ||
unsafeSubscribe(new CompletableSubscriber() { | ||
@Override | ||
public void onCompleted() { | ||
try { | ||
|
@@ -1963,7 +1963,7 @@ private static void deliverUncaughtException(Throwable e) { | |
* @param s the CompletableSubscriber, not null | ||
* @throws NullPointerException if s is null | ||
*/ | ||
public final void subscribe(CompletableSubscriber s) { | ||
public final void unsafeSubscribe(CompletableSubscriber s) { | ||
requireNonNull(s); | ||
try { | ||
// TODO plugin wrapping the subscriber | ||
|
@@ -1980,11 +1980,11 @@ public final void subscribe(CompletableSubscriber s) { | |
|
||
/** | ||
* Subscribes the given CompletableSubscriber to this Completable instance | ||
* and handles exceptions throw by its onXXX methods. | ||
* and handles exceptions thrown by its onXXX methods. | ||
* @param s the CompletableSubscriber, not null | ||
* @throws NullPointerException if s is null | ||
*/ | ||
public final void safeSubscribe(CompletableSubscriber s) { | ||
public final void subscribe(CompletableSubscriber s) { | ||
requireNonNull(s); | ||
try { | ||
// TODO plugin wrapping the subscriber | ||
|
||
|
@@ -2000,12 +2000,12 @@ public final void safeSubscribe(CompletableSubscriber s) { | |
} | ||
|
||
/** | ||
* Subscribes a reactive-streams Subscriber to this Completable instance which | ||
* Subscribes a regular Subscriber to this Completable instance which | ||
* will receive only an onError or onComplete event. | ||
* @param s the reactive-streams Subscriber, not null | ||
* @throws NullPointerException if s is null | ||
*/ | ||
public final <T> void subscribe(Subscriber<T> s) { | ||
public final <T> void unsafeSubscribe(Subscriber<T> s) { | ||
requireNonNull(s); | ||
try { | ||
final Subscriber<?> sw = s; // FIXME hooking in 1.x is kind of strange to me | ||
|
@@ -2014,7 +2014,7 @@ public final <T> void subscribe(Subscriber<T> s) { | |
throw new NullPointerException("The RxJavaPlugins.onSubscribe returned a null Subscriber"); | ||
|
||
} | ||
|
||
subscribe(new CompletableSubscriber() { | ||
unsafeSubscribe(new CompletableSubscriber() { | ||
@Override | ||
public void onCompleted() { | ||
sw.onCompleted(); | ||
|
@@ -2041,12 +2041,13 @@ public void onSubscribe(Subscription d) { | |
} | ||
|
||
/** | ||
* Subscribes a reactive-streams Subscriber to this Completable instance which | ||
* will receive only an onError or onComplete event. | ||
* Subscribes a regular Subscriber to this Completable instance which | ||
* will receive only an onError or onComplete event | ||
* and handles exceptions thrown by its onXXX methods. | ||
* @param s the reactive-streams Subscriber, not null | ||
* @throws NullPointerException if s is null | ||
*/ | ||
public final <T> void safeSubscribe(Subscriber<T> s) { | ||
public final <T> void subscribe(Subscriber<T> s) { | ||
requireNonNull(s); | ||
try { | ||
final Subscriber<?> sw = s; // FIXME hooking in 1.x is kind of strange to me | ||
|
@@ -2055,7 +2056,7 @@ public final <T> void safeSubscribe(Subscriber<T> s) { | |
throw new NullPointerException("The RxJavaPlugins.onSubscribe returned a null Subscriber"); | ||
} | ||
|
||
subscribe(new SafeCompletableSubscriber(new CompletableSubscriber() { | ||
unsafeSubscribe(new SafeCompletableSubscriber(new CompletableSubscriber() { | ||
@Override | ||
public void onCompleted() { | ||
sw.onCompleted(); | ||
|
@@ -2102,7 +2103,7 @@ public void call(final CompletableSubscriber s) { | |
@Override | ||
public void call() { | ||
try { | ||
subscribe(s); | ||
unsafeSubscribe(s); | ||
} finally { | ||
w.unsubscribe(); | ||
} | ||
|
@@ -2205,7 +2206,7 @@ public final <T> Observable<T> toObservable() { | |
return Observable.create(new OnSubscribe<T>() { | ||
@Override | ||
public void call(Subscriber<? super T> s) { | ||
subscribe(s); | ||
unsafeSubscribe(s); | ||
} | ||
}); | ||
} | ||
|
@@ -2222,7 +2223,7 @@ public final <T> Single<T> toSingle(final Func0<? extends T> completionValueFunc | |
return Single.create(new rx.Single.OnSubscribe<T>() { | ||
@Override | ||
public void call(final SingleSubscriber<? super T> s) { | ||
subscribe(new CompletableSubscriber() { | ||
unsafeSubscribe(new CompletableSubscriber() { | ||
|
||
@Override | ||
public void onCompleted() { | ||
|
@@ -2286,7 +2287,7 @@ public final Completable unsubscribeOn(final Scheduler scheduler) { | |
return create(new CompletableOnSubscribe() { | ||
@Override | ||
public void call(final CompletableSubscriber s) { | ||
subscribe(new CompletableSubscriber() { | ||
unsafeSubscribe(new CompletableSubscriber() { | ||
|
||
@Override | ||
public void onCompleted() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SafeCompletableSubscriber
is not used here nor the otherssubscribe
methods, except forsubscribe(Subscriber)
. I thinksubscribe
methods should call between themselves (as inObservable#subscribe
methods) to refactor code and avoid forgetting this kind of things.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unsafeSubscribe
should call between themselves too.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should call
subscribe
now instead, doesn't it? And the otherssubscribe
methods.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the rest are intermediate operators or already perform custom logic in addition to reporting the error to a plugin. Plus, this particulare use case, the subscriber is not expected to crash.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the idea of the safe subscribe is to ensure the caller that the contract will be met. Why in this particular case it won't?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But
SafeSubscriber
in the first line of the javadocs says:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. However, this method doesn't run any user supplied code and thus no need to wrap it into a safe completable subscriber.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. However, the other 2
subscribe
methods that don't useSafeCompletableSubscriber
should do it. To assert that theAction
instances are called at most once, and not both, complying with the contract.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the safeguard to those methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Why not reusing
SafeCompletableSubscriber
?