io.reactivex.Observable.firstElement()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(8.0k)|赞(0)|评价(0)|浏览(106)

本文整理了Java中io.reactivex.Observable.firstElement()方法的一些代码示例,展示了Observable.firstElement()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable.firstElement()方法的具体详情如下:
包路径:io.reactivex.Observable
类名称:Observable
方法名:firstElement

Observable.firstElement介绍

[英]Returns a Maybe that emits only the very first item emitted by the source ObservableSource, or completes if the source ObservableSource is empty.

Scheduler: firstElement does not operate by default on a particular Scheduler.
[中]返回一个Maybe,该Maybe只发出源ObservableSource发出的第一个项,如果源ObservableSource为空,则返回complete。
Scheduler:firstElement默认情况下不会在特定的计划程序上运行。

代码示例

代码示例来源:origin: trello/RxLifecycle

@Override
public MaybeSource<T> apply(Maybe<T> upstream) {
  return upstream.takeUntil(observable.firstElement());
}

代码示例来源:origin: alibaba/Tangram-Android

@Override
public MaybeSource<T> apply(Maybe<T> upstream) {
  return upstream.takeUntil(mObservable.firstElement());
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testFirstWithPredicateOfNoneMatchingThePredicate() {
  Observable<Integer> o = Observable.just(1, 3, 5, 7, 9, 7, 5, 3, 1);
  o.filter(IS_EVEN).firstElement().subscribe(wm);
  verify(wm, never()).onSuccess(anyInt());
  verify(wm).onComplete();
  verify(wm, never()).onError(any(Throwable.class));
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testFirst() {
  Maybe<Integer> o = Observable.just(1, 2, 3).firstElement();
  o.subscribe(wm);
  InOrder inOrder = inOrder(wm);
  inOrder.verify(wm, times(1)).onSuccess(1);
  inOrder.verifyNoMoreInteractions();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testFirstOfNone() {
  Observable<Integer> o = Observable.empty();
  o.firstElement().subscribe(wm);
  verify(wm, never()).onSuccess(anyInt());
  verify(wm).onComplete();
  verify(wm, never()).onError(any(Throwable.class));
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testFirstWithOneElement() {
  Maybe<Integer> o = Observable.just(1).firstElement();
  o.subscribe(wm);
  InOrder inOrder = inOrder(wm);
  inOrder.verify(wm, times(1)).onSuccess(1);
  inOrder.verifyNoMoreInteractions();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testFirstWithOneElementObservable() {
  Observable<Integer> o = Observable.just(1).firstElement().toObservable();
  Observer<Integer> observer = TestHelper.mockObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onNext(1);
  inOrder.verify(observer, times(1)).onComplete();
  inOrder.verifyNoMoreInteractions();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testFirstObservable() {
  Observable<Integer> o = Observable.just(1, 2, 3).firstElement().toObservable();
  Observer<Integer> observer = TestHelper.mockObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onNext(1);
  inOrder.verify(observer, times(1)).onComplete();
  inOrder.verifyNoMoreInteractions();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testFirstWithEmpty() {
  Maybe<Integer> o = Observable.<Integer> empty().firstElement();
  o.subscribe(wm);
  InOrder inOrder = inOrder(wm);
  inOrder.verify(wm, times(1)).onComplete();
  inOrder.verify(wm, never()).onError(any(Throwable.class));
  inOrder.verifyNoMoreInteractions();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testFirstWithEmptyObservable() {
  Observable<Integer> o = Observable.<Integer> empty().firstElement().toObservable();
  Observer<Integer> observer = TestHelper.mockObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer).onComplete();
  inOrder.verify(observer, never()).onError(any(Throwable.class));
  inOrder.verifyNoMoreInteractions();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testFirstWithPredicateAndEmpty() {
  Maybe<Integer> o = Observable.just(1)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .firstElement();
  o.subscribe(wm);
  InOrder inOrder = inOrder(wm);
  inOrder.verify(wm, times(1)).onComplete();
  inOrder.verify(wm, never()).onError(any(Throwable.class));
  inOrder.verifyNoMoreInteractions();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testFirstWithPredicateAndOneElement() {
  Maybe<Integer> o = Observable.just(1, 2)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .firstElement();
  o.subscribe(wm);
  InOrder inOrder = inOrder(wm);
  inOrder.verify(wm, times(1)).onSuccess(2);
  inOrder.verifyNoMoreInteractions();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testFirstWithPredicate() {
  Maybe<Integer> o = Observable.just(1, 2, 3, 4, 5, 6)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .firstElement();
  o.subscribe(wm);
  InOrder inOrder = inOrder(wm);
  inOrder.verify(wm, times(1)).onSuccess(2);
  inOrder.verifyNoMoreInteractions();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testFirstWithPredicateAndEmptyObservable() {
  Observable<Integer> o = Observable.just(1)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .firstElement().toObservable();
  Observer<Integer> observer = TestHelper.mockObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer).onComplete();
  inOrder.verify(observer, never()).onError(any(Throwable.class));
  inOrder.verifyNoMoreInteractions();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testFirstWithPredicateObservable() {
  Observable<Integer> o = Observable.just(1, 2, 3, 4, 5, 6)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .firstElement().toObservable();
  Observer<Integer> observer = TestHelper.mockObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onNext(2);
  inOrder.verify(observer, times(1)).onComplete();
  inOrder.verifyNoMoreInteractions();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testFirstWithPredicateAndOneElementObservable() {
  Observable<Integer> o = Observable.just(1, 2)
      .filter(new Predicate<Integer>() {
        @Override
        public boolean test(Integer t1) {
          return t1 % 2 == 0;
        }
      })
      .firstElement().toObservable();
  Observer<Integer> observer = TestHelper.mockObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onNext(2);
  inOrder.verify(observer, times(1)).onComplete();
  inOrder.verifyNoMoreInteractions();
}

代码示例来源:origin: ReactiveX/RxJava

@SuppressWarnings("unchecked")
@Test
public void noSubsequentSubscriptionIterable() {
  final int[] calls = { 0 };
  Observable<Integer> source = Observable.create(new ObservableOnSubscribe<Integer>() {
    @Override
    public void subscribe(ObservableEmitter<Integer> s) throws Exception {
      calls[0]++;
      s.onNext(1);
      s.onComplete();
    }
  });
  Observable.concat(Arrays.asList(source, source)).firstElement()
  .test()
  .assertResult(1);
  assertEquals(1, calls[0]);
}

代码示例来源:origin: ReactiveX/RxJava

@SuppressWarnings("unchecked")
@Test
public void noSubsequentSubscriptionDelayErrorIterable() {
  final int[] calls = { 0 };
  Observable<Integer> source = Observable.create(new ObservableOnSubscribe<Integer>() {
    @Override
    public void subscribe(ObservableEmitter<Integer> s) throws Exception {
      calls[0]++;
      s.onNext(1);
      s.onComplete();
    }
  });
  Observable.concatDelayError(Arrays.asList(source, source)).firstElement()
  .test()
  .assertResult(1);
  assertEquals(1, calls[0]);
}

代码示例来源:origin: ReactiveX/RxJava

@SuppressWarnings("unchecked")
@Test
public void noSubsequentSubscription() {
  final int[] calls = { 0 };
  Observable<Integer> source = Observable.create(new ObservableOnSubscribe<Integer>() {
    @Override
    public void subscribe(ObservableEmitter<Integer> s) throws Exception {
      calls[0]++;
      s.onNext(1);
      s.onComplete();
    }
  });
  Observable.concatArray(source, source).firstElement()
  .test()
  .assertResult(1);
  assertEquals(1, calls[0]);
}

代码示例来源:origin: ReactiveX/RxJava

@SuppressWarnings("unchecked")
@Test
public void noSubsequentSubscriptionDelayError() {
  final int[] calls = { 0 };
  Observable<Integer> source = Observable.create(new ObservableOnSubscribe<Integer>() {
    @Override
    public void subscribe(ObservableEmitter<Integer> s) throws Exception {
      calls[0]++;
      s.onNext(1);
      s.onComplete();
    }
  });
  Observable.concatArrayDelayError(source, source).firstElement()
  .test()
  .assertResult(1);
  assertEquals(1, calls[0]);
}

相关文章

Observable类方法