forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ReactiveX#231 from mairbek/multicast
Multicast
- Loading branch information
Showing
6 changed files
with
462 additions
and
119 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
31 changes: 31 additions & 0 deletions
31
rxjava-core/src/main/java/rx/observables/ConnectableObservable.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,31 @@ | ||
/** | ||
* 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.observables; | ||
|
||
import rx.Observable; | ||
import rx.Observer; | ||
import rx.Subscription; | ||
import rx.util.functions.Func1; | ||
|
||
public abstract class ConnectableObservable<T> extends Observable<T> { | ||
|
||
protected ConnectableObservable(Func1<Observer<T>, Subscription> onSubscribe) { | ||
super(onSubscribe); | ||
} | ||
|
||
public abstract Subscription connect(); | ||
|
||
} |
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
243 changes: 243 additions & 0 deletions
243
rxjava-core/src/main/java/rx/operators/OperatorMulticast.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,243 @@ | ||
/** | ||
* 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 org.junit.Test; | ||
import rx.Observable; | ||
import rx.Observer; | ||
import rx.Subscription; | ||
import rx.observables.ConnectableObservable; | ||
import rx.subjects.DefaultSubject; | ||
import rx.subjects.Subject; | ||
import rx.util.functions.Func1; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
public class OperatorMulticast { | ||
public static <T, R> ConnectableObservable<R> multicast(Observable<T> source, final Subject<T, R> subject) { | ||
return new MulticastConnectableObservable<T, R>(source, subject); | ||
} | ||
|
||
private static class MulticastConnectableObservable<T, R> extends ConnectableObservable<R> { | ||
private final Object lock = new Object(); | ||
|
||
private final Observable<T> source; | ||
private final Subject<T, R> subject; | ||
|
||
private Subscription subscription; | ||
|
||
public MulticastConnectableObservable(Observable<T> source, final Subject<T, R> subject) { | ||
super(new Func1<Observer<R>, Subscription>() { | ||
@Override | ||
public Subscription call(Observer<R> observer) { | ||
return subject.subscribe(observer); | ||
} | ||
}); | ||
this.source = source; | ||
this.subject = subject; | ||
} | ||
|
||
public Subscription connect() { | ||
synchronized (lock) { | ||
if (subscription == null) { | ||
subscription = source.subscribe(new Observer<T>() { | ||
@Override | ||
public void onCompleted() { | ||
subject.onCompleted(); | ||
} | ||
|
||
@Override | ||
public void onError(Exception e) { | ||
subject.onError(e); | ||
} | ||
|
||
@Override | ||
public void onNext(T args) { | ||
subject.onNext(args); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
|
||
return new Subscription() { | ||
@Override | ||
public void unsubscribe() { | ||
synchronized (lock) { | ||
if (subscription != null) { | ||
subscription.unsubscribe(); | ||
subscription = null; | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
|
||
} | ||
|
||
public static class UnitTest { | ||
|
||
@Test | ||
public void testMulticast() { | ||
TestObservable source = new TestObservable(); | ||
|
||
ConnectableObservable<String> multicasted = OperatorMulticast.multicast(source, | ||
DefaultSubject.<String>create()); | ||
|
||
Observer<String> observer = mock(Observer.class); | ||
multicasted.subscribe(observer); | ||
|
||
source.sendOnNext("one"); | ||
source.sendOnNext("two"); | ||
|
||
multicasted.connect(); | ||
|
||
source.sendOnNext("three"); | ||
source.sendOnNext("four"); | ||
source.sendOnCompleted(); | ||
|
||
verify(observer, never()).onNext("one"); | ||
verify(observer, never()).onNext("two"); | ||
verify(observer, times(1)).onNext("three"); | ||
verify(observer, times(1)).onNext("four"); | ||
verify(observer, times(1)).onCompleted(); | ||
|
||
} | ||
|
||
@Test | ||
public void testMulticastConnectTwice() { | ||
TestObservable source = new TestObservable(); | ||
|
||
ConnectableObservable<String> multicasted = OperatorMulticast.multicast(source, | ||
DefaultSubject.<String>create()); | ||
|
||
Observer<String> observer = mock(Observer.class); | ||
multicasted.subscribe(observer); | ||
|
||
source.sendOnNext("one"); | ||
|
||
multicasted.connect(); | ||
multicasted.connect(); | ||
|
||
source.sendOnNext("two"); | ||
source.sendOnCompleted(); | ||
|
||
verify(observer, never()).onNext("one"); | ||
verify(observer, times(1)).onNext("two"); | ||
verify(observer, times(1)).onCompleted(); | ||
|
||
} | ||
|
||
@Test | ||
public void testMulticastDisconnect() { | ||
TestObservable source = new TestObservable(); | ||
|
||
ConnectableObservable<String> multicasted = OperatorMulticast.multicast(source, | ||
DefaultSubject.<String>create()); | ||
|
||
Observer<String> observer = mock(Observer.class); | ||
multicasted.subscribe(observer); | ||
|
||
source.sendOnNext("one"); | ||
|
||
Subscription connection = multicasted.connect(); | ||
source.sendOnNext("two"); | ||
|
||
connection.unsubscribe(); | ||
source.sendOnNext("three"); | ||
|
||
multicasted.connect(); | ||
source.sendOnNext("four"); | ||
source.sendOnCompleted(); | ||
|
||
verify(observer, never()).onNext("one"); | ||
verify(observer, times(1)).onNext("two"); | ||
verify(observer, never()).onNext("three"); | ||
verify(observer, times(1)).onNext("four"); | ||
verify(observer, times(1)).onCompleted(); | ||
|
||
} | ||
|
||
|
||
private static class TestObservable extends Observable<String> { | ||
|
||
Observer<String> observer = new Observer<String>() { | ||
@Override | ||
public void onCompleted() { | ||
// Do nothing | ||
} | ||
|
||
@Override | ||
public void onError(Exception e) { | ||
// Do nothing | ||
} | ||
|
||
@Override | ||
public void onNext(String args) { | ||
// Do nothing | ||
} | ||
}; | ||
Subscription s = new Subscription() { | ||
@Override | ||
public void unsubscribe() { | ||
observer = new Observer<String>() { | ||
@Override | ||
public void onCompleted() { | ||
// Do nothing | ||
} | ||
|
||
@Override | ||
public void onError(Exception e) { | ||
// Do nothing | ||
} | ||
|
||
@Override | ||
public void onNext(String args) { | ||
// Do nothing | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
public TestObservable() { | ||
} | ||
|
||
/* used to simulate subscription */ | ||
public void sendOnCompleted() { | ||
observer.onCompleted(); | ||
} | ||
|
||
/* used to simulate subscription */ | ||
public void sendOnNext(String value) { | ||
observer.onNext(value); | ||
} | ||
|
||
/* used to simulate subscription */ | ||
public void sendOnError(Exception e) { | ||
observer.onError(e); | ||
} | ||
|
||
@Override | ||
public Subscription subscribe(final Observer<String> observer) { | ||
this.observer = observer; | ||
return s; | ||
} | ||
|
||
} | ||
|
||
} | ||
} |
Oops, something went wrong.