-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves #76 From the original PR: This PR changes the "Disposable already set!" and "Subscription already set!" messages on the standard consumer classes (DisposableSubscriber, DisposableObserver, etc.) to something more meaningful: "It is not allowed to subscribe with a(n) <class name> multiple times. Please create a fresh instance of <class name> and subscribe that to the target source instead." Where <class name> is a placeholder for the getClass().getName() of the subclass of those consumer types. It should clearly state to avoid subscribing with them multiple times as well as printing the full class name to indicate the problem is with the use of the implementor class, and not with the abstract RxJava class. Inspired by [this Stackoverflow](http://stackoverflow.com/questions/43482263/rxjava2-protocolviolationexception-disposable-already-set) question, one of many such questions. For the internal operators, the original error message stays because when they appear, that is still likely due to an implementation bug (or a misbehaving user-created custom implementation).
- Loading branch information
Showing
7 changed files
with
175 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
autodispose/src/main/java/com/uber/autodispose/AutoDisposeEndConsumerHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright (c) 2016-present, RxJava Contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is | ||
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See | ||
* the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package com.uber.autodispose; | ||
|
||
import io.reactivex.disposables.Disposable; | ||
import io.reactivex.exceptions.ProtocolViolationException; | ||
import io.reactivex.plugins.RxJavaPlugins; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import org.reactivestreams.Subscription; | ||
|
||
/** | ||
* Utility class to help report multiple subscriptions with the same | ||
* consumer type instead of the internal "Disposable already set!" message | ||
* that is practically reserved for internal operators and indicate bugs in them. | ||
* | ||
* Copied from the RxJava implementation. | ||
*/ | ||
final class AutoDisposeEndConsumerHelper { | ||
|
||
/** | ||
* Utility class. | ||
*/ | ||
private AutoDisposeEndConsumerHelper() { | ||
throw new IllegalStateException("No instances!"); | ||
} | ||
|
||
/** | ||
* Atomically updates the target upstream AtomicReference from null to the non-null | ||
* next Disposable, otherwise disposes next and reports a ProtocolViolationException | ||
* if the AtomicReference doesn't contain the shared disposed indicator. | ||
* | ||
* @param upstream the target AtomicReference to update | ||
* @param next the Disposable to set on it atomically | ||
* @param observer the class of the consumer to have a personalized | ||
* error message if the upstream already contains a non-cancelled Disposable. | ||
* @return true if successful, false if the content of the AtomicReference was non null | ||
*/ | ||
public static boolean setOnce(AtomicReference<Disposable> upstream, Disposable next, | ||
Class<?> observer) { | ||
AutoDisposeUtil.checkNotNull(next, "next is null"); | ||
if (!upstream.compareAndSet(null, next)) { | ||
next.dispose(); | ||
if (upstream.get() != AutoDisposableHelper.DISPOSED) { | ||
reportDoubleSubscription(observer); | ||
} | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Atomically updates the target upstream AtomicReference from null to the non-null | ||
* next Subscription, otherwise cancels next and reports a ProtocolViolationException | ||
* if the AtomicReference doesn't contain the shared cancelled indicator. | ||
* | ||
* @param upstream the target AtomicReference to update | ||
* @param next the Subscription to set on it atomically | ||
* @param subscriber the class of the consumer to have a personalized | ||
* error message if the upstream already contains a non-cancelled Subscription. | ||
* @return true if successful, false if the content of the AtomicReference was non null | ||
*/ | ||
public static boolean setOnce(AtomicReference<Subscription> upstream, Subscription next, | ||
Class<?> subscriber) { | ||
AutoDisposeUtil.checkNotNull(next, "next is null"); | ||
if (!upstream.compareAndSet(null, next)) { | ||
next.cancel(); | ||
if (upstream.get() != AutoSubscriptionHelper.CANCELLED) { | ||
reportDoubleSubscription(subscriber); | ||
} | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Builds the error message with the consumer class. | ||
* | ||
* @param consumer the class of the consumer | ||
* @return the error message string | ||
*/ | ||
public static String composeMessage(String consumer) { | ||
return "It is not allowed to subscribe with a(n) " | ||
+ consumer | ||
+ " multiple times. " | ||
+ "Please create a fresh instance of " | ||
+ consumer | ||
+ " and subscribe that to the target source instead."; | ||
} | ||
|
||
/** | ||
* Report a ProtocolViolationException with a personalized message referencing | ||
* the simple type name of the consumer class and report it via | ||
* RxJavaPlugins.onError. | ||
* | ||
* @param consumer the class of the consumer | ||
*/ | ||
public static void reportDoubleSubscription(Class<?> consumer) { | ||
RxJavaPlugins.onError(new ProtocolViolationException(composeMessage(consumer.getName()))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.