io.reactivex.subjects.ReplaySubject Java Examples
The following examples show how to use
io.reactivex.subjects.ReplaySubject.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ObservableTest.java From Java-programming-methodology-Rxjava-articles with Apache License 2.0 | 6 votes |
@Test void replaySubject_conf_test() { //ReplaySubject<Object> replaySubject = ReplaySubject.createWithSize(5);//只缓存订阅前最后5条数据 //ReplaySubject<Object> replaySubject = ReplaySubject.createWithTime(5,TimeUnit.SECONDS, Schedulers.computation()); //只缓存被订阅前5秒内的数据 ReplaySubject<Object> replaySubject =ReplaySubject.createWithTimeAndSize(5, TimeUnit.SECONDS, Schedulers.computation(),3); // 请结合以上两者注释 for (Long i=1l;i<10l;i++){ replaySubject.onNext(i); sleep(1,TimeUnit.SECONDS); } replaySubject.subscribe(x -> log("一郎神: " + x), Throwable::printStackTrace, () -> System.out.println("Emission completed"), disposable -> System.out.println("onSubscribe") ); replaySubject.onNext(10l); replaySubject.onComplete(); }
Example #2
Source File: SubscriptionProxyTest.java From RxGroups with Apache License 2.0 | 6 votes |
@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!"); }
Example #3
Source File: SubscriptionProxyTest.java From RxGroups with Apache License 2.0 | 6 votes |
@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!"); }
Example #4
Source File: SubscriptionProxyTest.java From RxGroups with Apache License 2.0 | 6 votes |
@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!"); }
Example #5
Source File: ReplaySubjectExampleActivity.java From RxJava2-Android-Samples with Apache License 2.0 | 6 votes |
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()); }
Example #6
Source File: SubscriptionProxyTest.java From RxGroups with Apache License 2.0 | 5 votes |
@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!"); }
Example #7
Source File: ObservableTest.java From Java-programming-methodology-Rxjava-articles with Apache License 2.0 | 5 votes |
@Test void replaySubject_test() { ReplaySubject<Object> replaySubject = ReplaySubject.create(); replaySubject.onNext(1l); replaySubject.onNext(2l); replaySubject.subscribe(x -> log("一郎神: " + x), Throwable::printStackTrace, () -> System.out.println("Emission completed"), disposable -> System.out.println("onSubscribe") ); replaySubject.onNext(10l); replaySubject.onComplete(); }
Example #8
Source File: UploadManager.java From smart-farmer-android with Apache License 2.0 | 5 votes |
/** * 上传多个数据 */ @SuppressWarnings("unchecked") public Observable<UploadResponse<T, R>> upload(List<T> tList) { checkUploadCallback(); //拷贝一份,防止在遍历列表时其它线程修改 tList final List<T> copyList = new ArrayList<>(tList); synchronized (monitor) { needRemoved.removeAll(copyList); errorTask.removeAll(copyList); } //不需要网络上传部分,已完成 List<T> needlessUpload = new ArrayList<>(); //未上传的数据 List<T> needUpload = new ArrayList<>(); synchronized (monitor) { for (T t : copyList) { if (completedTask.containsKey(t)) { needlessUpload.add(t); } else { needUpload.add(t); } } } final ReplaySubject<UploadResponse<T, R>> recorder = ReplaySubject.create(); recorderList.add(recorder); return Observable.fromArray(Pair.create(false, needlessUpload), new Pair<>(true, needUpload)) .flatMap(pair -> pair.first ? networkUpload(pair.second) : noUpload(pair.second)).observeOn(AndroidSchedulers.mainThread()) .doOnNext(recorder::onNext) .doOnComplete(recorder::onComplete); }
Example #9
Source File: AbstractDataSet.java From scava with Eclipse Public License 2.0 | 5 votes |
public AbstractDataSet(IPaged policy) { this.status = Status.CREATED; this.count = 0; this.tester = new LoggerUtil(); this.max = policy.hasMax() ? (policy.hasPerIteration() ? policy.perIteration() : 1) * policy.increment() * policy.max() : null; if (max != null) { this.subject = ReplaySubject.create(max > 1000 ? 1000 : max); } }
Example #10
Source File: AbstractDataSet.java From scava with Eclipse Public License 2.0 | 5 votes |
public AbstractDataSet() { this.status = Status.CREATED; this.count = 0; this.tester = new LoggerUtil(); this.max = null; this.subject = ReplaySubject.create(); }
Example #11
Source File: AbstractDataSet.java From scava with Eclipse Public License 2.0 | 5 votes |
public AbstractDataSet(Integer max) { this.status = Status.CREATED; this.count = 0; this.tester = new LoggerUtil(); this.max = max; this.subject = ReplaySubject.create(max); }
Example #12
Source File: ExampleUnitTest.java From RxAndroid-Sample with Apache License 2.0 | 5 votes |
@Test public void testSubjectExample() { /* * Step 1: Create an observable that emits an integer * from 1 to 5 * */ Observable<Integer> observable = Observable.range(1, 5) .subscribeOn(Schedulers.io()); /* * Step 2: Create a subject that observes * this emission from the observable. In this scenario, * the subject acts an an observer since it observes the * changes to the Observable. * */ ReplaySubject<Integer> subject = ReplaySubject.create(); observable.subscribe(subject); /* * Step 3: In this scenario, the subject acts an an Observable, * since it emits each item from the original Observable. * */ subject.subscribe(s -> System.out.println("subscriber one: " + s)); subject.subscribe(s -> System.out.println("subscriber two: " + s)); }
Example #13
Source File: ExampleUnitTest.java From RxAndroid-Sample with Apache License 2.0 | 5 votes |
@Test public void testSubjectMulticastExample() { /* * Step 1: Create an observable that emits an integer * from 1 to 5. Each item is squared by itself before it is * emitted. * */ Observable<Integer> observable = Observable.range(1, 5) .subscribeOn(Schedulers.io()) .map(integer -> { System.out.println(String.format("Squaring %d with itself", integer)); return integer * integer; }); /* * Step 2: Create a subject that observes * this emission from the observable. * */ ReplaySubject<Integer> subject = ReplaySubject.create(); observable.subscribe(subject); /* * Step 3: We are subscribing two subscribers to the Subject. * */ subject.subscribe(s -> System.out.println("subscriber one: " + s)); subject.subscribe(s -> System.out.println("subscriber two: " + s)); }
Example #14
Source File: JobManagerTest.java From jobson with Apache License 2.0 | 5 votes |
@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"); } }
Example #15
Source File: JobManagerTest.java From jobson with Apache License 2.0 | 5 votes |
@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"); } }
Example #16
Source File: RxJava2Proxies.java From RHub with Apache License 2.0 | 4 votes |
public static RxJava2SubjProxy serializedReplaySubjectProxy() { return new RxJava2SubjProxy(ReplaySubject.create().toSerialized(), Roxy.TePolicy.PASS); }
Example #17
Source File: RxShellTest.java From RxShell with Apache License 2.0 | 4 votes |
@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(); })); }
Example #18
Source File: UploadManager.java From smart-farmer-android with Apache License 2.0 | 4 votes |
/** * 等待上传完成 */ public Observable<UploadResponse<T, R>> waitComplete() { return ReplaySubject.mergeDelayError(recorderList) .filter(ftUploadResponse -> ftUploadResponse.status == UploadStatus.STATUS_OK || (ftUploadResponse.status == UploadStatus.STATUS_ERROR && errorTask.contains(ftUploadResponse.source))); }
Example #19
Source File: Demo_ReplaySubject.java From Reactive-Programming-With-Java-9 with MIT License | 4 votes |
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(); }
Example #20
Source File: FlowableFetchPagesByRequest.java From rxjava2-extras with Apache License 2.0 | 4 votes |
public static <T> Flowable<T> create(final BiFunction<? super Long, ? super Long, ? extends Flowable<T>> fetch, final long start, final int maxConcurrency) { return Flowable.defer(new Callable<Flowable<T>>() { @Override public Flowable<T> call() throws Exception { // need a ReplaySubject because multiple requests can come // through before concatEager has established subscriptions to // the subject final ReplaySubject<Flowable<T>> subject = ReplaySubject.create(); final AtomicLong position = new AtomicLong(start); LongConsumer request = new LongConsumer() { @Override public void accept(final long n) throws Exception { final long pos = position.getAndAdd(n); if (SubscriptionHelper.validate(n)) { Flowable<T> flowable; try { flowable = fetch.apply(pos, n); } catch (Throwable e) { Exceptions.throwIfFatal(e); subject.onError(e); return; } // reduce allocations by incorporating the onNext // and onComplete actions into the mutable count // object final Count count = new Count(subject, n); flowable = flowable // .doOnNext(count) // .doOnComplete(count); subject.onNext(flowable); } } }; return Flowable // .concatEager(subject.serialize() // .toFlowable(BackpressureStrategy.BUFFER), maxConcurrency, 128) // .doOnRequest(request); } }); }
Example #21
Source File: RxBleClientMock.java From RxAndroidBle with Apache License 2.0 | 4 votes |
/** * Build a new {@link RxBleClientMock}. */ public Builder() { this.discoverableDevicesSubject = ReplaySubject.create(); this.bondedDevices = new HashSet<>(); }
Example #22
Source File: RxJava2Proxies.java From RHub with Apache License 2.0 | 4 votes |
public static RxJava2SubjProxy safeSerializedReplaySubjectProxy() { return new RxJava2SubjProxy(ReplaySubject.create().toSerialized(), Roxy.TePolicy.WRAP); }
Example #23
Source File: RxJava2Proxies.java From RHub with Apache License 2.0 | 4 votes |
public static RxJava2SubjProxy safeReplaySubjectProxy() { return new RxJava2SubjProxy(ReplaySubject.create(), Roxy.TePolicy.WRAP); }
Example #24
Source File: Pageload.java From AndroidGodEye with Apache License 2.0 | 4 votes |
@Override protected Subject<PageLifecycleEventInfo> createSubject() { return ReplaySubject.create(); }
Example #25
Source File: RxJava2Proxies.java From RHub with Apache License 2.0 | 4 votes |
public static RxJava2SubjProxy replaySubjectProxy() { return new RxJava2SubjProxy(ReplaySubject.create(), Roxy.TePolicy.PASS); }
Example #26
Source File: Data.java From scava with Eclipse Public License 2.0 | 4 votes |
public Data(){ this.status = Status.CREATED; this.subject = ReplaySubject.create(); }
Example #27
Source File: Network.java From AndroidGodEye with Apache License 2.0 | 4 votes |
@Override protected Subject<NetworkInfo> createSubject() { return ReplaySubject.create(); }
Example #28
Source File: ViewCanary.java From AndroidGodEye with Apache License 2.0 | 4 votes |
@Override protected Subject<ViewIssueInfo> createSubject() { return ReplaySubject.create(); }
Example #29
Source File: Leak.java From AndroidGodEye with Apache License 2.0 | 4 votes |
@Override protected Subject<LeakInfo> createSubject() { return ReplaySubject.create(); }
Example #30
Source File: LeakDetector.java From AndroidGodEye with Apache License 2.0 | 4 votes |
@Override protected Subject<LeakQueue.LeakMemoryInfo> createSubject() { return ReplaySubject.create(); }