-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Implemented RefCount Operator #407
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9ec1715
RefCount work in progress
d3d2757
RefCount work in progress
60ad0cf
Merge and work on RefCount
johnhmarks 531ff06
Merge and work on RefCount
johnhmarks b407e6e
Merge and work on RefCount
johnhmarks bc7361f
Merge branch 'master' of github.com:Netflix/RxJava
johnhmarks 63ae2b1
Finished RefCount - all tests pass
johnhmarks 1402dbf
Removed classes dir
johnhmarks e0f57f1
Minor tweak
johnhmarks d4b04d8
Implemented publishLast
johnhmarks 490ef86
Removed static variant of refCount
johnhmarks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
66 changes: 66 additions & 0 deletions
66
rxjava-core/src/main/java/rx/operators/OperationRefCount.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,66 @@ | ||
/** | ||
* Copyright 2013 Netflix, Inc. | ||
* | ||
* 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 rx.operators; | ||
|
||
import rx.Observable; | ||
import rx.Observer; | ||
import rx.Subscription; | ||
import rx.observables.ConnectableObservable; | ||
import rx.subscriptions.Subscriptions; | ||
import rx.util.functions.Action0; | ||
|
||
/** | ||
* Returns an observable sequence that stays connected to the source as long | ||
* as there is at least one subscription to the observable sequence. | ||
*/ | ||
public final class OperationRefCount<T> { | ||
public static <T> Observable.OnSubscribeFunc<T> refCount(ConnectableObservable<T> connectableObservable) { | ||
return new RefCount<T>(connectableObservable); | ||
} | ||
|
||
private static class RefCount<T> implements Observable.OnSubscribeFunc<T> { | ||
private final ConnectableObservable<T> innerConnectableObservable; | ||
private final Object gate = new Object(); | ||
private int count = 0; | ||
private Subscription connection = null; | ||
|
||
public RefCount(ConnectableObservable<T> innerConnectableObservable) { | ||
this.innerConnectableObservable = innerConnectableObservable; | ||
} | ||
|
||
@Override | ||
public Subscription onSubscribe(Observer<? super T> observer) { | ||
final Subscription subscription = innerConnectableObservable.subscribe(observer); | ||
synchronized (gate) { | ||
if (count++ == 0) { | ||
connection = innerConnectableObservable.connect(); | ||
} | ||
} | ||
return Subscriptions.create(new Action0() { | ||
@Override | ||
public void call() { | ||
synchronized (gate) { | ||
if (--count == 0) { | ||
connection.unsubscribe(); | ||
connection = null; | ||
} | ||
} | ||
subscription.unsubscribe(); | ||
} | ||
}); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package rx; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.MockitoAnnotations; | ||
import rx.observables.ConnectableObservable; | ||
import rx.subscriptions.Subscriptions; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
public class RefCountTests { | ||
|
||
@Before | ||
public void setUp() { | ||
MockitoAnnotations.initMocks(this); | ||
} | ||
|
||
@Test | ||
public void subscriptionToUnderlyingOnFirstSubscription() { | ||
@SuppressWarnings("unchecked") | ||
ConnectableObservable<Integer> connectable = mock(ConnectableObservable.class); | ||
Observable<Integer> refCounted = ConnectableObservable.refCount(connectable); | ||
@SuppressWarnings("unchecked") | ||
Observer<Integer> observer = mock(Observer.class); | ||
when(connectable.subscribe(any(Observer.class))).thenReturn(Subscriptions.empty()); | ||
when(connectable.connect()).thenReturn(Subscriptions.empty()); | ||
refCounted.subscribe(observer); | ||
verify(connectable, times(1)).subscribe(any(Observer.class)); | ||
verify(connectable, times(1)).connect(); | ||
} | ||
|
||
@Test | ||
public void noSubscriptionToUnderlyingOnSecondSubscription() { | ||
@SuppressWarnings("unchecked") | ||
ConnectableObservable<Integer> connectable = mock(ConnectableObservable.class); | ||
Observable<Integer> refCounted = ConnectableObservable.refCount(connectable); | ||
@SuppressWarnings("unchecked") | ||
Observer<Integer> observer = mock(Observer.class); | ||
when(connectable.subscribe(any(Observer.class))).thenReturn(Subscriptions.empty()); | ||
when(connectable.connect()).thenReturn(Subscriptions.empty()); | ||
refCounted.subscribe(observer); | ||
refCounted.subscribe(observer); | ||
verify(connectable, times(2)).subscribe(any(Observer.class)); | ||
verify(connectable, times(1)).connect(); | ||
} | ||
|
||
@Test | ||
public void unsubscriptionFromUnderlyingOnLastUnsubscription() { | ||
@SuppressWarnings("unchecked") | ||
ConnectableObservable<Integer> connectable = mock(ConnectableObservable.class); | ||
Observable<Integer> refCounted = ConnectableObservable.refCount(connectable); | ||
@SuppressWarnings("unchecked") | ||
Observer<Integer> observer = mock(Observer.class); | ||
Subscription underlying = mock(Subscription.class); | ||
when(connectable.subscribe(any(Observer.class))).thenReturn(underlying); | ||
Subscription connection = mock(Subscription.class); | ||
when(connectable.connect()).thenReturn(connection); | ||
Subscription first = refCounted.subscribe(observer); | ||
first.unsubscribe(); | ||
verify(underlying, times(1)).unsubscribe(); | ||
verify(connection, times(1)).unsubscribe(); | ||
} | ||
|
||
@Test | ||
public void noUnsubscriptionFromUnderlyingOnFirstUnsubscription() { | ||
@SuppressWarnings("unchecked") | ||
ConnectableObservable<Integer> connectable = mock(ConnectableObservable.class); | ||
Observable<Integer> refCounted = ConnectableObservable.refCount(connectable); | ||
@SuppressWarnings("unchecked") | ||
Observer<Integer> observer = mock(Observer.class); | ||
Subscription underlying = mock(Subscription.class); | ||
when(connectable.subscribe(any(Observer.class))).thenReturn(underlying); | ||
Subscription connection = mock(Subscription.class); | ||
when(connectable.connect()).thenReturn(connection); | ||
Subscription first = refCounted.subscribe(observer); | ||
Subscription second = refCounted.subscribe(observer); | ||
first.unsubscribe(); | ||
second.unsubscribe(); | ||
verify(underlying, times(2)).unsubscribe(); | ||
verify(connection, times(1)).unsubscribe(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should this be a no arg instance method?
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.
Hi George, thanks for taking the time to review the code. Indeed there is a no arg instance method. I also included the static version to follow the pattern using in Observable.java where many operators have static and instance variants that delegate to the statics. It has an added bonus that it means you can test the operator on a mocked instance. I'm happy to remove the static method and test against a concrete instance though. Cheers, John
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.
Actually, on closer inspection, those operations on Observable that have both static and instance variants like concat, merge and zip could be special cases. I'll remove the static method and rejig the tests.