Java Code Examples for io.reactivex.subjects.BehaviorSubject#onNext()

The following examples show how to use io.reactivex.subjects.BehaviorSubject#onNext() . 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: RxCmdShellTest.java    From RxShell with Apache License 2.0 6 votes vote down vote up
@Test
public void testClose_waitForCommands() {
    BehaviorSubject<Boolean> idler = BehaviorSubject.createDefault(false);
    when(cmdProcessor.isIdle()).thenReturn(idler);

    RxCmdShell shell = new RxCmdShell(builder, rxShell);
    shell.open().test().awaitDone(1, TimeUnit.SECONDS).assertNoTimeout().values().get(0);
    shell.isAlive().test().awaitDone(1, TimeUnit.SECONDS).assertNoTimeout().assertValue(true);

    shell.close().test().awaitDone(1, TimeUnit.SECONDS).assertTimeout();

    idler.onNext(true);

    shell.close().test().awaitDone(1, TimeUnit.SECONDS).assertNoTimeout().assertValue(0);

    verify(cmdProcessor).isIdle();
    verify(rxShellSession).close();
}
 
Example 2
Source File: ReplayRefCountSubjectTest.java    From akarnokd-misc with Apache License 2.0 6 votes vote down vote up
@Test
public void test() {
    BehaviorSubject<Integer> subject = BehaviorSubject.create();

    Observable<Integer> observable = subject
            .doOnNext(e -> { 
                System.out.println("This emits for second subscriber"); 
            })
            .doOnSubscribe(s -> System.out.println("OnSubscribe"))
            .doOnDispose(() -> System.out.println("OnDispose"))
            .replay(1)
            .refCount()
            .doOnNext(e -> { System.out.println("This does NOT emit for second subscriber"); });

    System.out.println("Subscribe-1");
    // This line causes the test to fail.
    observable.takeUntil(Observable.just(1)).test();

    subject.onNext(2);

    System.out.println("Subscribe-2");
    TestObserver<Integer> subscriber = observable.take(1).test();
    Assert.assertTrue(subscriber.awaitTerminalEvent(2, TimeUnit.SECONDS));
}
 
Example 3
Source File: ObservableTest.java    From Java-programming-methodology-Rxjava-articles with Apache License 2.0 5 votes vote down vote up
@Test
void behaviorSubject_test() {
    BehaviorSubject<Object> behaviorSubject = BehaviorSubject.create();

    behaviorSubject.onNext(1L);
    behaviorSubject.onNext(2L);
    behaviorSubject.subscribe(x -> log("一郎神: " + x),
            Throwable::printStackTrace,
            () -> System.out.println("Emission completed"),
            disposable -> System.out.println("onSubscribe")
    );
    behaviorSubject.onNext(10L);
    behaviorSubject.onComplete();
}
 
Example 4
Source File: ObservableTest.java    From Java-programming-methodology-Rxjava-articles with Apache License 2.0 5 votes vote down vote up
@Test
void behaviorSubject_error_test() {
    BehaviorSubject<Object> behaviorSubject = BehaviorSubject.create();

    behaviorSubject.onNext(1L);
    behaviorSubject.onNext(2L);
    behaviorSubject.onError(new RuntimeException("我来耍下宝"));
    behaviorSubject.subscribe(x -> log("一郎神: " + x),
            Throwable::printStackTrace,
            () -> System.out.println("Emission completed"),
            disposable -> System.out.println("onSubscribe")
    );
    behaviorSubject.onNext(10L);
    behaviorSubject.onComplete();
}
 
Example 5
Source File: RunnerRoundProcessor.java    From 2018-TowerDefence with MIT License 5 votes vote down vote up
boolean processRound(BehaviorSubject<String> addToConsoleOutput) throws Exception {
    if (roundProcessed) {
        throw new InvalidOperationException("This round has already been processed!");
    }
    boolean processed = gameRoundProcessor.processRound(gameMap, commandsToProcess);

    ArrayList<String> errorList = gameRoundProcessor.getErrorList();
    String errorListText = "Error List: " + Arrays.toString(errorList.toArray());
    addToConsoleOutput.onNext(errorListText);

    roundProcessed = true;

    return processed;
}
 
Example 6
Source File: RxJavaCharacterizationTests.java    From science-journal with Apache License 2.0 5 votes vote down vote up
@Test
public void firstElementAsBehaviorSubjectChanges() {
  BehaviorSubject<String> subject = BehaviorSubject.create();
  TestObserver<String> beforeTest = subject.firstElement().test();

  subject.onNext("A");
  TestObserver<String> firstTest = subject.firstElement().test();
  firstTest.assertValue("A").assertComplete();
  beforeTest.assertValue("A").assertComplete();

  subject.onNext("B");
  TestObserver<String> secondTest = subject.firstElement().test();
  secondTest.assertValue("B").assertComplete();
}
 
Example 7
Source File: BehaviorWithLatestTest.java    From akarnokd-misc with Apache License 2.0 5 votes vote down vote up
@Test
public void secondInit() {
    BehaviorSubject<String> s1 = BehaviorSubject.create();
    BehaviorSubject<String> s2 = BehaviorSubject.createDefault("");

    TestObserver<Boolean> to = s1.withLatestFrom(s2, (a, b) -> true)
    .test();

    to.assertEmpty();

    s1.onNext("");

    to.assertValue(true);
}
 
Example 8
Source File: RxFeaturesAdapter.java    From FeatureAdapter with Apache License 2.0 4 votes vote down vote up
/**
 * Calculates each feature's new items and diff in parallel in the computation scheduler pool,
 * then dispatches feature updates to adapter in feature order.
 *
 * @param modelObservable the stream of models
 * @return an observable of {@link FeatureUpdate} for tracking the adapter changes.
 */
public Flowable<List<FeatureUpdate>> updateFeatureItems(Observable<MODEL> modelObservable) {
  // the ticker observable is gonna emit an item every time all the
  // list of items from all the feature controllers have been computed
  // so we just process the model instances one at a time
  // this is meant to be a very fine grained back pressure mechanism.
  final Object tick = new Object();
  final BehaviorSubject<Object> tickObservable = BehaviorSubject.create();
  tickObservable.onNext(tick);
  return modelObservable
      .observeOn(mainThread())
      .zipWith(tickObservable, (model, t) -> model)
      .toFlowable(BackpressureStrategy.LATEST)
      .flatMap(
          model ->
              fromIterable(getFeatureControllers())
                  .flatMap(
                      // each feature controller receives a fork of the model observable
                      // and compute its items in parallel, and then updates the UI ASAP
                      // but we still aggregate all the list to be sure to pace the model
                      // observable
                      // correctly using the tick observable
                      feature ->
                          just(feature)
                              .observeOn(computation())
                              .map(featureController -> toFeatureUpdate(featureController, model))
                              .filter(featureUpdate -> featureUpdate.newItems != null && featureUpdate.diffResult != null)
                  )
                  // collect all observable of feature updates in a list in feature order
                  .toSortedList(featureUpdateComparator::compare)
                  .observeOn(mainThread())
                  // dispatch each feature update in order to the adapter
                  // (this also updates the internal adapter state)
                  .map(this::dispatchFeatureUpdates)
                  .map(
                      list -> {
                        tickObservable.onNext(tick);
                        if (recyclerView != null) {
                          recyclerView.setItemViewCacheSize(getItemCount());
                        }
                        return list;
                      }).toFlowable());
}
 
Example 9
Source File: Demo_BehaviorSubject.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");
			
		}
	};
	BehaviorSubject<Long> behaviorSubject=BehaviorSubject.create();
	behaviorSubject.onNext(1L);
	behaviorSubject.onNext(2L);
	behaviorSubject.onNext(10L);

	behaviorSubject.onError(new Exception("You got an Exception"));
	behaviorSubject.subscribe(observer);
	behaviorSubject.onComplete();

}
 
Example 10
Source File: BehaviorSubjectExampleActivity.java    From RxJava2-Android-Samples with Apache License 2.0 4 votes vote down vote up
private void doSomeWork() {

        BehaviorSubject<Integer> source = BehaviorSubject.create();

        source.subscribe(getFirstObserver()); // it will get 1, 2, 3, 4 and onComplete

        source.onNext(1);
        source.onNext(2);
        source.onNext(3);

        /*
         * it will emit 3(last emitted), 4 and onComplete for second observer also.
         */
        source.subscribe(getSecondObserver());

        source.onNext(4);
        source.onComplete();

    }
 
Example 11
Source File: BehaviorWithLatestTest.java    From akarnokd-misc with Apache License 2.0 4 votes vote down vote up
@Test
public void firstInit() {
    BehaviorSubject<String> s1 = BehaviorSubject.createDefault("");
    BehaviorSubject<String> s2 = BehaviorSubject.create();

    TestObserver<Boolean> to = s1.withLatestFrom(s2, (a, b) -> true)
    .test();

    to.assertEmpty();

    s2.onNext("");

    to.assertEmpty();

    s1.onNext("");

    to.assertValue(true);
}