本文整理汇总了Java中io.reactivex.subjects.ReplaySubject类的典型用法代码示例。如果您正苦于以下问题:Java ReplaySubject类的具体用法?Java ReplaySubject怎么用?Java ReplaySubject使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ReplaySubject类属于io.reactivex.subjects包,在下文中一共展示了ReplaySubject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createFaker
import io.reactivex.subjects.ReplaySubject; //导入依赖的package包/类
private static ReplaySubject<Faker> createFaker() {
final ReplaySubject<Faker> subject = ReplaySubject.create();
Observable.create(new ObservableOnSubscribe<Faker>() {
@Override
public void subscribe(ObservableEmitter<Faker> e) throws Exception {
final Faker faker = new Faker();
if (!e.isDisposed()) {
e.onNext(faker);
e.onComplete();
}
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subject);
return subject;
}
示例2: doSomeWork
import io.reactivex.subjects.ReplaySubject; //导入依赖的package包/类
private void doSomeWork() {
ReplaySubject<Integer> source = ReplaySubject.create();
source.subscribe(getFirstObserver()); // it will get 1, 2, 3, 4
source.onNext(1);
source.onNext(2);
source.onNext(3);
source.onNext(4);
source.onComplete();
/*
* it will emit 1, 2, 3, 4 for second observer also as we have used replay
*/
source.subscribe(getSecondObserver());
}
示例3: doSomeWork
import io.reactivex.subjects.ReplaySubject; //导入依赖的package包/类
/**
* ReplaySubject会发射所有来自原始Observable的数据给观察者,无论它们是何时订阅的。
* 也有其它版本的ReplaySubject,在重放缓存增长到一定大小的时候或过了一段时间后会丢弃旧的数据(原始Observable发射的)。
*
* 如果你把ReplaySubject当作一个观察者使用,注意不要从多个线程中调用它的onNext方法(包括其它的on系列方法),
* 这可能导致同时(非顺序)调用,这会违反Observable协议, 给Subject的结果增加了不确定性。
*/
private void doSomeWork() {
ReplaySubject<Integer> source = ReplaySubject.create();
source.subscribe(getFirstObserver()); // it will get 1, 2, 3, 4
source.onNext(1);
source.onNext(2);
source.onNext(3);
source.onNext(4);
source.onComplete();
/*
* it will emit 1, 2, 3, 4 for second observer also as we have used replay
*/
source.subscribe(getSecondObserver());
}
示例4: testUnsubscribeBeforeEmit
import io.reactivex.subjects.ReplaySubject; //导入依赖的package包/类
@Test public void testUnsubscribeBeforeEmit() {
TestObserver<String> observer = new TestObserver<>();
ReplaySubject<String> subject = ReplaySubject.create();
SubscriptionProxy<String> proxy = SubscriptionProxy.create(subject);
proxy.subscribe(observer);
proxy.dispose();
observer.assertNotComplete();
observer.assertNoValues();
subject.onNext("Avanti!");
subject.onComplete();
// disposable observables may not be resused in RxJava2
observer = new TestObserver<>();
proxy.subscribe(observer);
observer.assertComplete();
observer.assertValue("Avanti!");
}
示例5: shouldCacheResultsWhileUnsubscribedAndDeliverAfterResubscription
import io.reactivex.subjects.ReplaySubject; //导入依赖的package包/类
@Test public void shouldCacheResultsWhileUnsubscribedAndDeliverAfterResubscription() {
TestObserver<String> observer = new TestObserver<>();
ReplaySubject<String> subject = ReplaySubject.create();
SubscriptionProxy<String> proxy = SubscriptionProxy.create(subject);
proxy.subscribe(observer);
proxy.dispose();
observer.assertNoValues();
subject.onNext("Avanti!");
subject.onComplete();
// disposable observables may not be resused in RxJava2
observer = new TestObserver<>();
proxy.subscribe(observer);
observer.awaitTerminalEvent(3, TimeUnit.SECONDS);
observer.assertValue("Avanti!");
}
示例6: shouldRedeliverSameResultsToDifferentSubscriber
import io.reactivex.subjects.ReplaySubject; //导入依赖的package包/类
@Test public void shouldRedeliverSameResultsToDifferentSubscriber() {
// Use case: When rotating an activity, ObservableManager will re-subscribe original request's
// Observable to a new Observer, which is a member of the new activity instance. In this
// case, we may want to redeliver any previous results (if the request is still being
// managed by ObservableManager).
TestObserver<String> observer = new TestObserver<>();
ReplaySubject<String> subject = ReplaySubject.create();
SubscriptionProxy<String> proxy = SubscriptionProxy.create(subject);
proxy.subscribe(observer);
subject.onNext("Avanti!");
subject.onComplete();
proxy.dispose();
TestObserver<String> newSubscriber = new TestObserver<>();
proxy.subscribe(newSubscriber);
newSubscriber.awaitTerminalEvent(3, TimeUnit.SECONDS);
newSubscriber.assertComplete();
newSubscriber.assertValue("Avanti!");
observer.assertComplete();
observer.assertValue("Avanti!");
}
示例7: main
import io.reactivex.subjects.ReplaySubject; //导入依赖的package包/类
public static void main(String[] args) {
Subject<String> subject =
ReplaySubject.create();
subject.subscribe(s -> System.out.println("Observer 1: " +
s));
subject.onNext("Alpha");
subject.onNext("Beta");
subject.onNext("Gamma");
subject.onComplete();
subject.subscribe(s -> System.out.println("Observer 2: " +
s));
}
示例8: getInstance
import io.reactivex.subjects.ReplaySubject; //导入依赖的package包/类
public static ReplaySubject<Faker> getInstance() {
if (rxFaker == null) {
rxFaker = createFaker();
}
return rxFaker;
}
示例9: testSubmitJobEventListenersEchoStdoutWhenExecutorEchoesStdout
import io.reactivex.subjects.ReplaySubject; //导入依赖的package包/类
@Test
public void testSubmitJobEventListenersEchoStdoutWhenExecutorEchoesStdout() throws InterruptedException {
final Subject<byte[]> stdoutSubject = ReplaySubject.create();
final byte[] expectedStdoutBytes = generateRandomBytes();
stdoutSubject.onNext(expectedStdoutBytes);
final JobExecutor jobExecutor = MockJobExecutor.thatUses(stdoutSubject, Observable.never());
final JobManager jobManager = createManagerWith(jobExecutor);
final Semaphore s = new Semaphore(1);
s.acquire();
final JobEventListeners listeners = JobEventListeners.createStdoutListener(new Observer<byte[]>() {
@Override
public void onSubscribe(@NonNull Disposable disposable) {}
@Override
public void onNext(@NonNull byte[] bytes) {
assertThat(bytes).isEqualTo(expectedStdoutBytes);
s.release();
}
@Override
public void onError(@NonNull Throwable throwable) {
fail("Error from observable");
s.release();
}
@Override
public void onComplete() {}
});
jobManager.submit(STANDARD_VALID_REQUEST, listeners);
if (!s.tryAcquire(1, SECONDS)) {
fail("Timed out before any bytes received");
}
}
示例10: testSubmitJobEventListenersEchoStderrWhenExecutorEchoesStderr
import io.reactivex.subjects.ReplaySubject; //导入依赖的package包/类
@Test
public void testSubmitJobEventListenersEchoStderrWhenExecutorEchoesStderr() throws InterruptedException {
final Subject<byte[]> stderr = ReplaySubject.create();
final byte[] stderrBytes = generateRandomBytes();
stderr.onNext(stderrBytes);
final JobExecutor jobExecutor = MockJobExecutor.thatUses(Observable.never(), stderr);
final JobManager jobManager = createManagerWith(jobExecutor);
final Semaphore s = new Semaphore(1);
s.acquire();
final JobEventListeners listeners = JobEventListeners.createStderrListener(new Observer<byte[]>() {
@Override
public void onSubscribe(@NonNull Disposable disposable) {}
@Override
public void onNext(@NonNull byte[] bytes) {
assertThat(bytes).isEqualTo(stderrBytes);
s.release();
}
@Override
public void onError(@NonNull Throwable throwable) {
fail("Error from observable");
s.release();
}
@Override
public void onComplete() {}
});
jobManager.submit(STANDARD_VALID_REQUEST, listeners);
if (!s.tryAcquire(1, SECONDS)) {
fail("Timed out before any bytes received");
}
}
示例11: shouldKeepDeliveringEventsAfterResubscribed
import io.reactivex.subjects.ReplaySubject; //导入依赖的package包/类
@Test public void shouldKeepDeliveringEventsAfterResubscribed() {
TestObserver<String> observer = new TestObserver<>();
ReplaySubject<String> subject = ReplaySubject.create();
SubscriptionProxy<String> proxy = SubscriptionProxy.create(subject);
proxy.subscribe(observer);
subject.onNext("Avanti 1");
proxy.dispose();
observer = new TestObserver<>();
proxy.subscribe(observer);
subject.onNext("Avanti!");
observer.assertValues("Avanti 1", "Avanti!");
}
示例12: apply
import io.reactivex.subjects.ReplaySubject; //导入依赖的package包/类
@Override
public ObservableSource<T> apply(Observable<T> upstream) {
final ReplaySubject<Notification<T>> subject = ReplaySubject.create();
final DisposableObserver<Notification<T>> observer = upstream.materialize()
.subscribeWith(new DisposableObserver<Notification<T>>() {
@Override
public void onComplete() {
subject.onComplete();
}
@Override
public void onError(Throwable e) {
subject.onError(e);
}
@Override
public void onNext(Notification<T> value) {
subject.onNext(value);
}
});
return view
.switchMap(new Function<Boolean, Observable<Notification<T>>>() {
@Override
public Observable<Notification<T>> apply(final Boolean flag) {
if (flag) {
return subject;
} else {
return Observable.empty();
}
}
})
.doOnDispose(new Action() {
@Override
public void run() throws Exception {
observer.dispose();
}
})
.dematerialize();
}
示例13: setup
import io.reactivex.subjects.ReplaySubject; //导入依赖的package包/类
@Before
public void setup() throws Exception {
super.setup();
sessionPub = ReplaySubject.create();
sessionPub.onNext(rxProcessSession);
when(rxProcess.open()).thenAnswer(invocation -> {
when(rxProcessSession.waitFor()).thenReturn(Single.create(e -> waitForEmitter = e));
return sessionPub.firstOrError();
});
cmdStream = new MockOutputStream(new MockOutputStream.Listener() {
@Override
public void onNewLine(String line) {
if (line.equals("exit" + LineReader.getLineSeparator())) {
try {
cmdStream.close();
} catch (IOException e) {
Timber.e(e);
} finally {
waitForEmitter.onSuccess(0);
}
}
}
@Override
public void onClose() {
}
});
outputStream = new MockInputStream();
errorStream = new MockInputStream();
when(rxProcessSession.input()).thenReturn(cmdStream);
when(rxProcessSession.output()).thenReturn(outputStream);
when(rxProcessSession.error()).thenReturn(errorStream);
when(rxProcessSession.isAlive()).thenReturn(Single.create(e -> e.onSuccess(cmdStream.isOpen())));
when(rxProcessSession.destroy()).then(invocation -> Completable.create(e -> {
cmdStream.close();
waitForEmitter.onSuccess(1);
e.onComplete();
}));
}
示例14: main
import io.reactivex.subjects.ReplaySubject; //导入依赖的package包/类
public static void main(String[] args) {
// TODO Auto-generated method stub
Observer<Long> observer=new Observer<Long>() {
@Override
public void onComplete() {
// TODO Auto-generated method stub
System.out.println("It's Done");
}
@Override
public void onError(Throwable throwable) {
// TODO Auto-generated method stub
throwable.printStackTrace();
}
@Override
public void onNext(Long value) {
// TODO Auto-generated method stub
System.out.println(":"+value);
}
@Override
public void onSubscribe(Disposable disposable) {
// TODO Auto-generated method stub
System.out.println("onSubscribe");
}
};
ReplaySubject<Long> replaySubject=ReplaySubject.create();
replaySubject.onNext(1l);
replaySubject.onNext(2l);
replaySubject.subscribe(observer);
replaySubject.onNext(10l);
replaySubject.onComplete();
}
示例15: initObservable
import io.reactivex.subjects.ReplaySubject; //导入依赖的package包/类
@Override
protected final void initObservable() {
subject = ReplaySubject.create();
subject.observeOn(EventThread.getScheduler(observeThread))
.subscribeOn(EventThread.getScheduler(subscribeThread));
}