Java Code Examples for io.reactivex.subjects.ReplaySubject#create()

The following examples show how to use io.reactivex.subjects.ReplaySubject#create() . 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: SubscriptionProxyTest.java    From RxGroups with Apache License 2.0 6 votes vote down vote up
@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 2
Source File: SubscriptionProxyTest.java    From RxGroups with Apache License 2.0 6 votes vote down vote up
@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 3
Source File: ReplaySubjectExampleActivity.java    From RxJava2-Android-Samples with Apache License 2.0 6 votes vote down vote up
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 4
Source File: ExampleUnitTest.java    From RxAndroid-Sample with Apache License 2.0 5 votes vote down vote up
@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 5
Source File: AbstractDataSet.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
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 6
Source File: JobManagerTest.java    From jobson with Apache License 2.0 5 votes vote down vote up
@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 7
Source File: AbstractDataSet.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
public AbstractDataSet() {
	this.status = Status.CREATED;
	this.count = 0;
	this.tester = new LoggerUtil();
	this.max = null;
	this.subject = ReplaySubject.create();
}
 
Example 8
Source File: AbstractDataSet.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
public AbstractDataSet(Integer max) {
	this.status = Status.CREATED;
	this.count = 0;
	this.tester = new LoggerUtil();
	this.max = max;
	this.subject = ReplaySubject.create(max);
}
 
Example 9
Source File: ObservableTest.java    From Java-programming-methodology-Rxjava-articles with Apache License 2.0 5 votes vote down vote up
@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 10
Source File: Pageload.java    From AndroidGodEye with Apache License 2.0 4 votes vote down vote up
@Override
protected Subject<PageLifecycleEventInfo> createSubject() {
    return ReplaySubject.create();
}
 
Example 11
Source File: Data.java    From scava with Eclipse Public License 2.0 4 votes vote down vote up
public Data(){
	this.status = Status.CREATED;
	this.subject = ReplaySubject.create();
}
 
Example 12
Source File: Leak.java    From AndroidGodEye with Apache License 2.0 4 votes vote down vote up
@Override
protected Subject<LeakInfo> createSubject() {
    return ReplaySubject.create();
}
 
Example 13
Source File: LeakDetector.java    From AndroidGodEye with Apache License 2.0 4 votes vote down vote up
@Override
protected Subject<LeakQueue.LeakMemoryInfo> createSubject() {
    return ReplaySubject.create();
}
 
Example 14
Source File: Crash.java    From AndroidGodEye with Apache License 2.0 4 votes vote down vote up
@Override
protected Subject<List<CrashInfo>> createSubject() {
    return ReplaySubject.create();
}
 
Example 15
Source File: RxJava2Proxies.java    From RHub with Apache License 2.0 4 votes vote down vote up
public static RxJava2SubjProxy replaySubjectProxy() {
    return new RxJava2SubjProxy(ReplaySubject.create(), Roxy.TePolicy.PASS);
}
 
Example 16
Source File: RxBleClientMock.java    From RxAndroidBle with Apache License 2.0 4 votes vote down vote up
/**
 * Build a new {@link RxBleClientMock}.
 */
public Builder() {
    this.discoverableDevicesSubject = ReplaySubject.create();
    this.bondedDevices = new HashSet<>();
}
 
Example 17
Source File: Network.java    From AndroidGodEye with Apache License 2.0 4 votes vote down vote up
@Override
protected Subject<NetworkInfo> createSubject() {
    return ReplaySubject.create();
}
 
Example 18
Source File: Demo_ReplaySubject.java    From Reactive-Programming-With-Java-9 with MIT License 4 votes vote down vote up
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 19
Source File: RxShellTest.java    From RxShell with Apache License 2.0 4 votes vote down vote up
@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 20
Source File: RxJava2Proxies.java    From RHub with Apache License 2.0 4 votes vote down vote up
public static RxJava2SubjProxy safeReplaySubjectProxy() {
    return new RxJava2SubjProxy(ReplaySubject.create(), Roxy.TePolicy.WRAP);
}