本文整理汇总了Java中rx.observables.ConnectableObservable类的典型用法代码示例。如果您正苦于以下问题:Java ConnectableObservable类的具体用法?Java ConnectableObservable怎么用?Java ConnectableObservable使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ConnectableObservable类属于rx.observables包,在下文中一共展示了ConnectableObservable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: create
import rx.observables.ConnectableObservable; //导入依赖的package包/类
static <T> ConnectableObservable<T> create(Observable<? extends T> source, final Func0<? extends ReplayBuffer<T>> bufferFactory) {
final AtomicReference<ReplaySubscriber<T>> curr = new AtomicReference();
return new OperatorReplay(new Observable$OnSubscribe<T>() {
public void call(Subscriber<? super T> child) {
ReplaySubscriber<T> r;
ReplaySubscriber<T> u;
do {
r = (ReplaySubscriber) curr.get();
if (r != null) {
break;
}
u = new ReplaySubscriber(curr, (ReplayBuffer) bufferFactory.call());
u.init();
} while (!curr.compareAndSet(r, u));
r = u;
InnerProducer<T> inner = new InnerProducer(r, child);
r.add(inner);
child.add(inner);
child.setProducer(inner);
}
}, source, curr, bufferFactory);
}
示例2: testExpectedReplayBehavior
import rx.observables.ConnectableObservable; //导入依赖的package包/类
@Test
public void testExpectedReplayBehavior() {
final TestScheduler scheduler = new TestScheduler();
final TestSubject<Integer> subject = TestSubject.create(scheduler);
final TestSubscriber<Integer> subscriber = new TestSubscriber<>();
final ConnectableObservable<Integer> sums = subject.scan((a, b) -> a + b).replay(1);
sums.connect();
subject.onNext(1);
subject.onNext(2);
subject.onNext(3);
scheduler.triggerActions();
sums.subscribe(subscriber);
subscriber.assertValueCount(1);
subscriber.assertValues(6);
}
示例3: testFlakyReplayBehavior
import rx.observables.ConnectableObservable; //导入依赖的package包/类
@Test
public void testFlakyReplayBehavior() {
final TestScheduler scheduler = new TestScheduler();
final TestSubject<Integer> subject = TestSubject.create(scheduler);
final TestSubscriber<Integer> subscriber = new TestSubscriber<>();
final ConnectableObservable<Integer> sums = subject.scan(1, (a, b) -> a + b).replay(1);
sums.connect();
subject.onNext(2);
subject.onNext(3);
scheduler.triggerActions();
sums.subscribe(subscriber);
// subscriber.assertValueCount(1);
subscriber.assertValues(6);
}
示例4: create
import rx.observables.ConnectableObservable; //导入依赖的package包/类
public static <T> ConnectableObservable<T> create(Observable<? extends T> source) {
final AtomicReference<PublishSubscriber<T>> curr = new AtomicReference();
return new OperatorPublish(new Observable$OnSubscribe<T>() {
public void call(Subscriber<? super T> child) {
while (true) {
PublishSubscriber<T> r = (PublishSubscriber) curr.get();
if (r == null || r.isUnsubscribed()) {
PublishSubscriber<T> u = new PublishSubscriber(curr);
u.init();
if (curr.compareAndSet(r, u)) {
r = u;
} else {
continue;
}
}
InnerProducer<T> inner = new InnerProducer(r, child);
if (r.add(inner)) {
child.add(inner);
child.setProducer(inner);
return;
}
}
}
}, source, curr);
}
示例5: multicastSelector
import rx.observables.ConnectableObservable; //导入依赖的package包/类
public static <T, U, R> Observable<R> multicastSelector(final Func0<? extends ConnectableObservable<U>> connectableFactory, final Func1<? super Observable<U>, ? extends Observable<R>> selector) {
return Observable.create(new Observable$OnSubscribe<R>() {
public void call(final Subscriber<? super R> child) {
try {
ConnectableObservable<U> co = (ConnectableObservable) connectableFactory.call();
((Observable) selector.call(co)).subscribe(child);
co.connect(new Action1<Subscription>() {
public void call(Subscription t) {
child.add(t);
}
});
} catch (Throwable e) {
Exceptions.throwOrReport(e, child);
}
}
});
}
示例6: observeOn
import rx.observables.ConnectableObservable; //导入依赖的package包/类
public static <T> ConnectableObservable<T> observeOn(final ConnectableObservable<T> co, Scheduler scheduler) {
final Observable<T> observable = co.observeOn(scheduler);
return new ConnectableObservable<T>(new Observable$OnSubscribe<T>() {
public void call(final Subscriber<? super T> child) {
observable.unsafeSubscribe(new Subscriber<T>(child) {
public void onNext(T t) {
child.onNext(t);
}
public void onError(Throwable e) {
child.onError(e);
}
public void onCompleted() {
child.onCompleted();
}
});
}
}) {
public void connect(Action1<? super Subscription> connection) {
co.connect(connection);
}
};
}
示例7: testHotObservable
import rx.observables.ConnectableObservable; //导入依赖的package包/类
@Test
public void testHotObservable() throws InterruptedException {
Observable<Long> observable =
Observable.interval(1, TimeUnit.SECONDS).publish();
observable.subscribe(
val -> System.out.println("Subscriber 1>> " + val));
((ConnectableObservable) observable).connect();
sleep(3000);
observable.subscribe(
val -> System.out.println("Subscriber 2>> " + val));
sleep(5000);
}
示例8: testFlakyReplayBehavior2
import rx.observables.ConnectableObservable; //导入依赖的package包/类
@Test
public void testFlakyReplayBehavior2() {
final PublishSubject<Integer> subject = PublishSubject.create();
final TestSubscriber<Integer> subscriber = new TestSubscriber<>();
final ConnectableObservable<Integer> sums = subject.scan(1, (a, b) -> a + b).replay(1);
sums.connect();
subject.onNext(2);
subject.onNext(3);
sums.subscribe(subscriber);
// subscriber.assertValueCount(1);
subscriber.assertValues(6);
}
示例9: hotObservable
import rx.observables.ConnectableObservable; //导入依赖的package包/类
public static void hotObservable() {
Observable<Integer> observable = Observable.create(subscriber -> {
Log.i("Sample", "Creating obseravble");
subscriber.onNext(10);
subscriber.onCompleted();
});
observable.subscribe();
ConnectableObservable<Integer> hot = observable.publish();
hot.subscribe(integer -> Log.i("Sample", "aaa"));
hot.subscribe(integer -> Log.i("Sample", "bbb"));
hot.subscribe(integer -> Log.i("Sample", "ccc"));
//call connect to emit the same stream for all subscribers
hot.connect();
}
示例10: accept
import rx.observables.ConnectableObservable; //导入依赖的package包/类
@Override
public void accept(Subscriber<? super TResult> child) {
Observable<TResult> observable;
ConnectableObservable<TIntermediate> connectable;
try {
connectable = new OperatorMulticast<TInput, TIntermediate>(source, subjectFactory);
observable = resultSelector.apply(connectable);
} catch (Throwable t) {
child.onError(t);
return;
}
DisposableSubscriber<? super TResult> ds = DisposableSubscriber.from(child);
observable.subscribe(ds);
connectable.connect(ds::add);
}
示例11: testMulticast
import rx.observables.ConnectableObservable; //导入依赖的package包/类
@Test
public void testMulticast() {
Subject<String, String> source = PublishSubject.create();
ConnectableObservable<String> multicasted = new OperatorMulticast<String, String>(source, new PublishSubjectFactory());
@SuppressWarnings("unchecked")
Observer<String> observer = mock(Observer.class);
multicasted.subscribe(observer);
source.onNext("one");
source.onNext("two");
multicasted.connect();
source.onNext("three");
source.onNext("four");
source.onComplete();
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)).onComplete();
}
示例12: testMulticastConnectTwice
import rx.observables.ConnectableObservable; //导入依赖的package包/类
@Test
public void testMulticastConnectTwice() {
Subject<String, String> source = PublishSubject.create();
ConnectableObservable<String> multicasted = new OperatorMulticast<String, String>(source, new PublishSubjectFactory());
@SuppressWarnings("unchecked")
Observer<String> observer = mock(Observer.class);
multicasted.subscribe(observer);
source.onNext("one");
multicasted.connect();
multicasted.connect();
source.onNext("two");
source.onComplete();
verify(observer, never()).onNext("one");
verify(observer, times(1)).onNext("two");
verify(observer, times(1)).onComplete();
}
示例13: testHot
import rx.observables.ConnectableObservable; //导入依赖的package包/类
public void testHot() throws Exception {
ConnectableObservable<Long> o = Observable.interval(200, TimeUnit.MILLISECONDS).publish();
Subscription s = o.connect();
o.subscribe(i -> System.out.println("first: " + i));
// Thread.sleep(500);
Thread.sleep(1000);
System.out.println("unsubscribe");
s.unsubscribe();
// o.subscribe(i -> System.out.println("second: " + i));
Thread.sleep(1000);
System.out.println("connect");
s = o.connect();
System.in.read();
}
示例14: testReplay
import rx.observables.ConnectableObservable; //导入依赖的package包/类
@Test
public void testReplay() {
ConnectableObservable<Integer> o = Observable.create(new OnSubscribe<Integer>() {
volatile boolean firstTime = true;
@Override
public void call(Subscriber<? super Integer> sub) {
if (firstTime) {
firstTime = false;
sub.onNext(1);
}
sub.onCompleted();
}
}).replay();
o.connect();
assertEquals(1, (int) o.count().toBlocking().single());
assertEquals(1, (int) o.count().toBlocking().single());
}
示例15: call
import rx.observables.ConnectableObservable; //导入依赖的package包/类
@Override
public void call(Subscriber<? super TResult> child) {
Observable<TResult> observable;
ConnectableObservable<TIntermediate> connectable;
try {
connectable = new OperatorMulticast<TInput, TIntermediate>(source, subjectFactory);
observable = resultSelector.call(connectable);
} catch (Throwable t) {
child.onError(t);
return;
}
final SafeSubscriber<TResult> s = new SafeSubscriber<TResult>(child);
observable.unsafeSubscribe(s);
connectable.connect(new Action1<Subscription>() {
@Override
public void call(Subscription t1) {
s.add(t1);
}
});
}