本文整理汇总了Java中rx.observables.ConnectableObservable.connect方法的典型用法代码示例。如果您正苦于以下问题:Java ConnectableObservable.connect方法的具体用法?Java ConnectableObservable.connect怎么用?Java ConnectableObservable.connect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rx.observables.ConnectableObservable
的用法示例。
在下文中一共展示了ConnectableObservable.connect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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);
}
示例2: 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);
}
示例3: 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);
}
};
}
示例4: 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);
}
示例5: 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();
}
示例6: 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);
}
示例7: 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();
}
示例8: 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();
}
示例9: 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();
}
示例10: 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());
}
示例11: 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);
}
});
}
示例12: onStart
import rx.observables.ConnectableObservable; //导入方法依赖的package包/类
@Override
public void onStart() {
super.onStart();
//将普通的Observable转换为可连接的Observable
ConnectableObservable<Object> tapEventEmitter = _rxBus.toObserverable().publish();
tapEventEmitter
.compose(this.bindToLifecycle())
.subscribe(new Action1<Object>() { //一个一旦被触发就会显示TapText的监听者
@Override
public void call(Object event) {
if (event instanceof RxBusDemoFragment.TapEvent) {
_showTapText();
}
}
});
tapEventEmitter
.compose(this.bindUntilEvent(FragmentEvent.DESTROY))
.publish(new Func1<Observable<Object>, Observable<List<Object>>>() {//一个出发后缓存一秒内的点击数并显示的监听者
@Override
public Observable<List<Object>> call(Observable<Object> stream) {
return stream.buffer(stream.debounce(1, TimeUnit.SECONDS)); //进行缓冲1秒,打包发送
}
}).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<List<Object>>() {
@Override
public void call(List<Object> taps) {
_showTapCount(taps.size());
}
});
tapEventEmitter.connect(); //可连接的Observable并不在订阅时触发,而需手动调用connect()方法
}
示例13: addTextListeners
import rx.observables.ConnectableObservable; //导入方法依赖的package包/类
private void addTextListeners(final EditText et) {
final ConnectableObservable<String> textSource =
RxTextView.textChanges(et)
.skip(1)
.map(CharSequence::toString)
.publish();
addSpaceHandler(et);
final Subscription suggestionSub =
textSource
.filter(input -> input.length() > 0)
.flatMap(this::getWordSuggestion)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(
this::handleWordSuggestion,
throwable -> LogUtil.e(getClass(), throwable.toString())
);
final Subscription uiSub =
textSource
.subscribe(
this::updateUi,
throwable -> LogUtil.e(getClass(), throwable.toString())
);
final Subscription connectSub = textSource.connect();
this.subscriptions.addAll(
suggestionSub,
uiSub,
connectSub
);
}
示例14: initSearch
import rx.observables.ConnectableObservable; //导入方法依赖的package包/类
private void initSearch() {
final ConnectableObservable<String> sourceObservable =
RxTextView
.textChangeEvents(this.activity.getBinding().search)
.skip(1)
.debounce(500, TimeUnit.MILLISECONDS)
.map(event -> event.text().toString())
.publish();
final Subscription searchSub =
sourceObservable
.filter(query -> query.length() > 0)
.subscribe(
this::runSearchQuery,
throwable -> LogUtil.exception(getClass(), "Error while searching for user", throwable)
);
final Subscription uiSub =
sourceObservable
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
this::updateSearchUi,
throwable -> LogUtil.exception(getClass(), "Error while updating user search ui", throwable)
);
final Subscription sourceSub = sourceObservable.connect();
this.subscriptions.addAll(sourceSub, searchSub, uiSub);
}
示例15: getResults
import rx.observables.ConnectableObservable; //导入方法依赖的package包/类
/**
* Get an Observable wrapping a PreResponse. We first connect to the BroadcastChannel to ensure that we do not
* miss any notifications. We then check the PreResponseStore for the PreResponse. If no PreResponse is available,
* we check to see if we got a notification from the BroadcastChannel before the async timeout. If we get a
* notification before timeout, we retrieve the PreResponse from the PreResponseStore else we return an empty
* Observable.
*
* @param ticket The ticket for which the PreResponse needs to be retrieved.
* @param asyncAfter The minimum duration the request is allowed to last before becoming asynchronous
*
* @return An Observable wrapping a PreResponse or an empty Observable in case a timeout occurs.
*/
protected Observable<PreResponse> getResults(@NotNull String ticket, long asyncAfter) {
if (asyncAfter == JobsApiRequest.ASYNCHRONOUS_ASYNC_AFTER_VALUE) {
// If the user specifies that they always want the asynchronous payload, then we need to force the system
// to behave like the results are not ready in the store, and the asynchronous timeout has expired even
// if the results are available.
return Observable.empty();
} else {
/*
* BroadCastChannel is a hot observable i.e. it emits notification irrespective of whether it has any
* subscribers. We use the replay operator so that the preResponseObservable upon connection, will begin
* collecting values.
* Once a new observer subscribes to the observable, it will have all the collected values replayed to it.
*/
ConnectableObservable<String> broadcastChannelNotifications = broadcastChannel.getNotifications()
.filter(ticket::equals)
.take(1)
.replay(1);
broadcastChannelNotifications.connect();
/*
* In the cases where we may get a synchronous response (asyncAfter is a number, or
* ApiRequest.SYNCHRONOUS_ASYNC_AFTER_VALUE ), then we start the timer, and
* go to the store and check to see if it has the results. If it doesn't, and 'asyncAfter' is a number
* then it starts listening to the broadcast channel, and waiting for the timer to expire.
*
* What this means is that in the case of `asyncAfter=0`, we have the following semantics:
* If the results are already in the response store, then return them to me. Otherwise, very quickly
* send back the asynchronous payload.
*/
return preResponseStore.get(ticket).switchIfEmpty(
applyTimeoutIfNeeded(broadcastChannelNotifications, asyncAfter).flatMap(preResponseStore::get)
);
}
}