當前位置: 首頁>>代碼示例>>Java>>正文


Java BehaviorSubject.create方法代碼示例

本文整理匯總了Java中io.reactivex.subjects.BehaviorSubject.create方法的典型用法代碼示例。如果您正苦於以下問題:Java BehaviorSubject.create方法的具體用法?Java BehaviorSubject.create怎麽用?Java BehaviorSubject.create使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在io.reactivex.subjects.BehaviorSubject的用法示例。


在下文中一共展示了BehaviorSubject.create方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: doSomeWork

import io.reactivex.subjects.BehaviorSubject; //導入方法依賴的package包/類
/**
 * 當觀察者訂閱BehaviorSubject時,它開始發射原始Observable最近發射的數據
 * (如果此時還 沒有收到任何數據,它會發射一個默認值),然後繼續發射其它任何來自原始Observable的數據。
 * 然而,如果原始的Observable因為發生了一個錯誤而終止,BehaviorSubject將不會發射任何數據,隻是簡單的向前傳遞這個錯誤通知。
 */
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();

}
 
開發者ID:changjiashuai,項目名稱:RxJava2-Android-Sample,代碼行數:25,代碼來源:BehaviorSubjectExampleActivity.java

示例2: testBindUntilFragmentEvent

import io.reactivex.subjects.BehaviorSubject; //導入方法依賴的package包/類
@Test
public void testBindUntilFragmentEvent() {
    BehaviorSubject<FragmentEvent> lifecycle = BehaviorSubject.create();

    TestObserver<Object> testObserver =
        observable.compose(RxLifecycle.bindUntilEvent(lifecycle, FragmentEvent.STOP)).test();

    lifecycle.onNext(FragmentEvent.ATTACH);
    testObserver.assertNotComplete();
    lifecycle.onNext(FragmentEvent.CREATE);
    testObserver.assertNotComplete();
    lifecycle.onNext(FragmentEvent.CREATE_VIEW);
    testObserver.assertNotComplete();
    lifecycle.onNext(FragmentEvent.START);
    testObserver.assertNotComplete();
    lifecycle.onNext(FragmentEvent.RESUME);
    testObserver.assertNotComplete();
    lifecycle.onNext(FragmentEvent.PAUSE);
    testObserver.assertNotComplete();
    testObserver.assertNotComplete();
    lifecycle.onNext(FragmentEvent.STOP);
    testObserver.assertComplete();
}
 
開發者ID:xufreshman,項目名稱:RxLifeCycle,代碼行數:24,代碼來源:RxLifecycleTest.java

示例3: testBindUntilActivityEvent

import io.reactivex.subjects.BehaviorSubject; //導入方法依賴的package包/類
@Test
public void testBindUntilActivityEvent() {
    BehaviorSubject<ActivityEvent> lifecycle = BehaviorSubject.create();

    TestObserver<Object> testObserver =
        observable.compose(RxLifecycle.bindUntilEvent(lifecycle, ActivityEvent.STOP)).test();

    lifecycle.onNext(ActivityEvent.CREATE);
    testObserver.assertNotComplete();
    lifecycle.onNext(ActivityEvent.START);
    testObserver.assertNotComplete();
    lifecycle.onNext(ActivityEvent.RESUME);
    testObserver.assertNotComplete();
    lifecycle.onNext(ActivityEvent.PAUSE);
    testObserver.assertNotComplete();
    lifecycle.onNext(ActivityEvent.STOP);
    testObserver.assertComplete();
}
 
開發者ID:xufreshman,項目名稱:RxLifeCycle,代碼行數:19,代碼來源:RxLifecycleTest.java

示例4: autoDispose_withProviderAndPlugin_withoutStarting_shouldFailWithExp

import io.reactivex.subjects.BehaviorSubject; //導入方法依賴的package包/類
@Test public void autoDispose_withProviderAndPlugin_withoutStarting_shouldFailWithExp() {
  AutoDisposePlugins.setOutsideLifecycleHandler(new Consumer<OutsideLifecycleException>() {
    @Override public void accept(OutsideLifecycleException e) {
      // Wrap in an IllegalStateException so we can verify this is the exception we see on the
      // other side
      throw new IllegalStateException(e);
    }
  });
  BehaviorSubject<Integer> lifecycle = BehaviorSubject.create();
  LifecycleScopeProvider<Integer> provider = TestUtil.makeLifecycleProvider(lifecycle);
  SingleSubject<Integer> source = SingleSubject.create();
  TestObserver<Integer> o = source
          .as(AutoDispose.<Integer>autoDisposable(provider))
          .test();

  o.assertNoValues();
  o.assertError(new Predicate<Throwable>() {
    @Override public boolean test(Throwable throwable) {
      return throwable instanceof IllegalStateException
          && throwable.getCause() instanceof OutsideLifecycleException;
    }
  });
}
 
開發者ID:uber,項目名稱:AutoDispose,代碼行數:24,代碼來源:AutoDisposeSingleObserverTest.java

示例5: autoDispose_withProviderAndNoOpPlugin_withoutStarting_shouldFailSilently

import io.reactivex.subjects.BehaviorSubject; //導入方法依賴的package包/類
@Test public void autoDispose_withProviderAndNoOpPlugin_withoutStarting_shouldFailSilently() {
  AutoDisposePlugins.setOutsideLifecycleHandler(new Consumer<OutsideLifecycleException>() {
    @Override public void accept(OutsideLifecycleException e) { }
  });
  BehaviorSubject<Integer> lifecycle = BehaviorSubject.create();
  LifecycleScopeProvider<Integer> provider = TestUtil.makeLifecycleProvider(lifecycle);
  PublishProcessor<Integer> source = PublishProcessor.create();
  TestSubscriber<Integer> o = source
          .as(AutoDispose.<Integer>autoDisposable(provider))
          .test();

  assertThat(source.hasSubscribers()).isFalse();
  assertThat(lifecycle.hasObservers()).isFalse();
  o.assertNoValues();
  o.assertNoErrors();
}
 
開發者ID:uber,項目名稱:AutoDispose,代碼行數:17,代碼來源:AutoDisposeSubscriberTest.java

示例6: autoDispose_withProviderAndNoOpPlugin_withoutStarting_shouldFailSilently

import io.reactivex.subjects.BehaviorSubject; //導入方法依賴的package包/類
@Test public void autoDispose_withProviderAndNoOpPlugin_withoutStarting_shouldFailSilently() {
  AutoDisposePlugins.setOutsideLifecycleHandler(new Consumer<OutsideLifecycleException>() {
    @Override public void accept(OutsideLifecycleException e) { }
  });
  BehaviorSubject<Integer> lifecycle = BehaviorSubject.create();
  LifecycleScopeProvider<Integer> provider = TestUtil.makeLifecycleProvider(lifecycle);
  MaybeSubject<Integer> source = MaybeSubject.create();
  TestObserver<Integer> o = source
          .as(AutoDispose.<Integer>autoDisposable(provider))
          .test();

  assertThat(source.hasObservers()).isFalse();
  assertThat(lifecycle.hasObservers()).isFalse();
  o.assertNoValues();
  o.assertNoErrors();
}
 
開發者ID:uber,項目名稱:AutoDispose,代碼行數:17,代碼來源:AutoDisposeMaybeObserverTest.java

示例7: autoDispose_withProviderAndPlugin_withoutStarting_shouldFailWithWrappedExp

import io.reactivex.subjects.BehaviorSubject; //導入方法依賴的package包/類
@Test public void autoDispose_withProviderAndPlugin_withoutStarting_shouldFailWithWrappedExp() {
  AutoDisposePlugins.setOutsideLifecycleHandler(new Consumer<OutsideLifecycleException>() {
    @Override public void accept(OutsideLifecycleException e) {
      // Wrap in an IllegalStateException so we can verify this is the exception we see on the
      // other side
      throw new IllegalStateException(e);
    }
  });
  BehaviorSubject<Integer> lifecycle = BehaviorSubject.create();
  LifecycleScopeProvider<Integer> provider = TestUtil.makeLifecycleProvider(lifecycle);
  MaybeSubject<Integer> source = MaybeSubject.create();
  TestObserver<Integer> o = source
          .as(AutoDispose.<Integer>autoDisposable(provider))
          .test();

  o.assertNoValues();
  o.assertError(new Predicate<Throwable>() {
    @Override public boolean test(Throwable throwable) {
      return throwable instanceof IllegalStateException
          && throwable.getCause() instanceof OutsideLifecycleException;
    }
  });
}
 
開發者ID:uber,項目名稱:AutoDispose,代碼行數:24,代碼來源:AutoDisposeMaybeObserverTest.java

示例8: autoDispose_withProviderAndPlugin_withoutStarting_shouldFailWithWrappedExp

import io.reactivex.subjects.BehaviorSubject; //導入方法依賴的package包/類
@Test public void autoDispose_withProviderAndPlugin_withoutStarting_shouldFailWithWrappedExp() {
  AutoDisposePlugins.setOutsideLifecycleHandler(new Consumer<OutsideLifecycleException>() {
    @Override public void accept(OutsideLifecycleException e) {
      // Wrap in an IllegalStateException so we can verify this is the exception we see on the
      // other side
      throw new IllegalStateException(e);
    }
  });
  BehaviorSubject<Integer> lifecycle = BehaviorSubject.create();
  LifecycleScopeProvider<Integer> provider = TestUtil.makeLifecycleProvider(lifecycle);
  TestObserver<Void> o = CompletableSubject.create()
            .as(autoDisposable(provider))
            .test();

  o.assertNoValues();
  o.assertError(new Predicate<Throwable>() {
    @Override public boolean test(Throwable throwable) {
      return throwable instanceof IllegalStateException
          && throwable.getCause() instanceof OutsideLifecycleException;
    }
  });
}
 
開發者ID:uber,項目名稱:AutoDispose,代碼行數:23,代碼來源:AutoDisposeCompletableObserverTest.java

示例9: autoDispose_withProvider_withoutStartingLifecycle_shouldFail

import io.reactivex.subjects.BehaviorSubject; //導入方法依賴的package包/類
@Test public void autoDispose_withProvider_withoutStartingLifecycle_shouldFail() {
  BehaviorSubject<Integer> lifecycle = BehaviorSubject.create();
  TestSubscriber<Integer> firstSubscriber = new TestSubscriber<>();
  TestSubscriber<Integer> secondSubscriber = new TestSubscriber<>();
  LifecycleScopeProvider<Integer> provider = TestUtil.makeLifecycleProvider(lifecycle);
  //noinspection unchecked
  Subscriber<Integer>[] subscribers = new Subscriber[] {firstSubscriber, secondSubscriber};

  Flowable.just(1, 2)
      .parallel(DEFAULT_PARALLELISM)
      .as(AutoDispose.<Integer>autoDisposable(provider))
      .subscribe(subscribers);

  List<Throwable> errors1 = firstSubscriber.errors();
  assertThat(errors1).hasSize(1);
  assertThat(errors1.get(0)).isInstanceOf(LifecycleNotStartedException.class);

  List<Throwable> errors2 = secondSubscriber.errors();
  assertThat(errors2).hasSize(1);
  assertThat(errors2.get(0)).isInstanceOf(LifecycleNotStartedException.class);
}
 
開發者ID:uber,項目名稱:AutoDispose,代碼行數:22,代碼來源:AutoDisposeParallelFlowableTest.java

示例10: autoDispose_withProviderAndNoOpPlugin_withoutStarting_shouldFailSilently

import io.reactivex.subjects.BehaviorSubject; //導入方法依賴的package包/類
@Test public void autoDispose_withProviderAndNoOpPlugin_withoutStarting_shouldFailSilently() {
  AutoDisposePlugins.setOutsideLifecycleHandler(new Consumer<OutsideLifecycleException>() {
    @Override public void accept(OutsideLifecycleException e) { }
  });
  BehaviorSubject<Integer> lifecycle = BehaviorSubject.create();
  LifecycleScopeProvider<Integer> provider = TestUtil.makeLifecycleProvider(lifecycle);
  PublishSubject<Integer> source = PublishSubject.create();
  TestObserver<Integer> o = source
          .as(AutoDispose.<Integer>autoDisposable(provider))
          .test();

  assertThat(source.hasObservers()).isFalse();
  assertThat(lifecycle.hasObservers()).isFalse();
  o.assertNoValues();
  o.assertNoErrors();
}
 
開發者ID:uber,項目名稱:AutoDispose,代碼行數:17,代碼來源:AutoDisposeObserverTest.java

示例11: autoDispose_withProviderAndPlugin_withoutStarting_shouldFailWithExp

import io.reactivex.subjects.BehaviorSubject; //導入方法依賴的package包/類
@Test public void autoDispose_withProviderAndPlugin_withoutStarting_shouldFailWithExp() {
  AutoDisposePlugins.setOutsideLifecycleHandler(new Consumer<OutsideLifecycleException>() {
    @Override public void accept(OutsideLifecycleException e) {
      // Wrap in an IllegalStateException so we can verify this is the exception we see on the
      // other side
      throw new IllegalStateException(e);
    }
  });
  BehaviorSubject<Integer> lifecycle = BehaviorSubject.create();
  LifecycleScopeProvider<Integer> provider = TestUtil.makeLifecycleProvider(lifecycle);
  PublishSubject<Integer> source = PublishSubject.create();
  TestObserver<Integer> o = source
          .as(AutoDispose.<Integer>autoDisposable(provider))
          .test();

  o.assertNoValues();
  o.assertError(new Predicate<Throwable>() {
    @Override public boolean test(Throwable throwable) {
      return throwable instanceof IllegalStateException
          && throwable.getCause() instanceof OutsideLifecycleException;
    }
  });
}
 
開發者ID:uber,項目名稱:AutoDispose,代碼行數:24,代碼來源:AutoDisposeObserverTest.java

示例12: autoDispose_withProviderAndNoOpPlugin_withoutStarting_shouldFailSilently

import io.reactivex.subjects.BehaviorSubject; //導入方法依賴的package包/類
@Test public void autoDispose_withProviderAndNoOpPlugin_withoutStarting_shouldFailSilently() {
  AutoDisposePlugins.setOutsideLifecycleHandler(new Consumer<OutsideLifecycleException>() {
    @Override public void accept(OutsideLifecycleException e) { }
  });
  BehaviorSubject<Integer> lifecycle = BehaviorSubject.create();
  LifecycleScopeProvider<Integer> provider = TestUtil.makeLifecycleProvider(lifecycle);
  SingleSubject<Integer> source = SingleSubject.create();
  TestObserver<Integer> o = source
          .as(AutoDispose.<Integer>autoDisposable(provider))
          .test();

  assertThat(source.hasObservers()).isFalse();
  assertThat(lifecycle.hasObservers()).isFalse();
  o.assertNoValues();
  o.assertNoErrors();
}
 
開發者ID:uber,項目名稱:AutoDispose,代碼行數:17,代碼來源:AutoDisposeSingleObserverTest.java

示例13: shouldEmitValueAfterViewIsAttached

import io.reactivex.subjects.BehaviorSubject; //導入方法依賴的package包/類
@Test
public void shouldEmitValueAfterViewIsAttached() {
    TestScheduler testScheduler = new TestScheduler();
    BehaviorSubject<Boolean> view = BehaviorSubject.create();
    view.onNext(true);
    WaitViewReplayTransformer<Integer> transformer = new WaitViewReplayTransformer<>(
            view.delay(EMIT_DELAY_IN_SECONDS, TimeUnit.SECONDS, testScheduler));

    TestObserver<Integer> testObserver = Observable.just(0)
            .compose(transformer)
            .test();
    testScheduler.advanceTimeBy(EMIT_DELAY_IN_SECONDS, TimeUnit.SECONDS);
    testObserver.awaitTerminalEvent();
    testObserver.assertValue(0);
    testObserver.assertComplete();
}
 
開發者ID:alapshin,項目名稱:arctor,代碼行數:17,代碼來源:WaitViewReplayTransformerTest.java

示例14: shouldReplayAllValuesAfterViewIsAttached

import io.reactivex.subjects.BehaviorSubject; //導入方法依賴的package包/類
@Test
public void shouldReplayAllValuesAfterViewIsAttached() {
    TestScheduler testScheduler = new TestScheduler();
    BehaviorSubject<Boolean> view = BehaviorSubject.create();
    view.onNext(true);
    WaitViewReplayTransformer<Integer> transformer = new WaitViewReplayTransformer<>(
            view.delay(EMIT_DELAY_IN_SECONDS, TimeUnit.SECONDS, testScheduler));

    TestObserver<Integer> testObserver = Observable.just(0, 1, 2)
            .compose(transformer)
            .test();
    testScheduler.advanceTimeBy(EMIT_DELAY_IN_SECONDS, TimeUnit.SECONDS);
    testObserver.awaitTerminalEvent();
    testObserver.assertValues(0, 1, 2);
    testObserver.assertComplete();
}
 
開發者ID:alapshin,項目名稱:arctor,代碼行數:17,代碼來源:WaitViewReplayTransformerTest.java

示例15: shouldEmitErrorAfterViewIsAttached

import io.reactivex.subjects.BehaviorSubject; //導入方法依賴的package包/類
@Test
public void shouldEmitErrorAfterViewIsAttached() {
    TestScheduler testScheduler = new TestScheduler();
    BehaviorSubject<Boolean> view = BehaviorSubject.create();
    view.onNext(true);
    WaitViewReplayTransformer<Integer> transformer = new WaitViewReplayTransformer<>(
            view.delay(EMIT_DELAY_IN_SECONDS, TimeUnit.SECONDS, testScheduler));

    Exception exception = new RuntimeException();
    TestObserver<Integer> testObserver = Observable.<Integer>error(exception)
            .compose(transformer)
            .test();
    testScheduler.advanceTimeBy(EMIT_DELAY_IN_SECONDS, TimeUnit.SECONDS);
    testObserver.awaitTerminalEvent();
    testObserver.assertError(exception);
    testObserver.assertNotComplete();
}
 
開發者ID:alapshin,項目名稱:arctor,代碼行數:18,代碼來源:WaitViewReplayTransformerTest.java


注:本文中的io.reactivex.subjects.BehaviorSubject.create方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。