io.reactivex.Single.compose()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(182)

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

Single.compose介绍

[英]Transform a Single by applying a particular Transformer function to it.

This method operates on the Single itself whereas #lift operates on the Single's SingleObservers.

If the operator you are creating is designed to act on the individual item emitted by a Single, use #lift. If your operator is designed to transform the source Single as a whole (for instance, by applying a particular set of existing RxJava operators to it) use compose. Scheduler: compose does not operate by default on a particular Scheduler.
[中]将特定的变换器函数应用于单个变换器。
这种方法对单曲本身起作用,而#lift对单曲的单曲起作用。
如果您正在创建的操作符旨在作用于单个控件发出的单个项目,请使用#lift。如果操作符被设计为将源代码作为一个整体进行转换(例如,通过对其应用一组特定的现有RxJava操作符),请使用compose。调度器:默认情况下,compose不会在特定的调度器上运行。

代码示例

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

@Test(expected = NullPointerException.class)
public void composeNull() {
  just1.compose(null);
}

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

@Test
public void singleGenericsSignatureTest() {
  A<String, Integer> a = new A<String, Integer>() { };
  Single.just(a).compose(TransformerTest.<String>testSingleTransformerCreator());
}

代码示例来源:origin: pockethub/PocketHub

/**
 * Edit comment.
 *
 * @param commentRequest
 */
protected void editComment(CommentRequest commentRequest) {
  ServiceGenerator.createService(this, GistCommentService.class)
      .editGistComment(gist.id(), comment.id(), commentRequest)
      .subscribeOn(Schedulers.io())
      .observeOn(AndroidSchedulers.mainThread())
      .compose(RxProgress.bindToLifecycle(this, R.string.editing_comment))
      .as(AutoDisposeUtils.bindToLifecycle(this))
      .subscribe(response -> finish(response.body()), e -> {
        Log.d(TAG, "Exception editing comment on gist", e);
        ToastUtils.show(this, e.getMessage());
      });
}

代码示例来源:origin: pockethub/PocketHub

@Override
public void onActivityResult(final int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == REQUEST_CODE_SELECT_PHOTO && resultCode == Activity.RESULT_OK) {
    ImageBinPoster.post(getActivity(), data.getData())
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(RxProgress.bindToLifecycle(getActivity(), R.string.loading))
        .as(AutoDisposeUtils.bindToLifecycle(this))
        .subscribe(response -> {
          if (response.isSuccessful()) {
            insertImage(ImageBinPoster.getUrl(response.body().string()));
          } else {
            showImageError();
          }
        }, throwable -> showImageError());
  }
}

代码示例来源:origin: pockethub/PocketHub

@Override
public void onItemClick(@NonNull Item item, @NonNull View view) {
  if (item instanceof RepositoryItem) {
    final Repository result = ((RepositoryItem) item).getRepo();
    ServiceGenerator.createService(getContext(), RepositoryService.class)
        .getRepository(result.owner().login(), result.name())
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(RxProgress.bindToLifecycle(getActivity(),
            MessageFormat.format(getString(R.string.opening_repository),
                InfoUtils.createRepoId(result))))
        .as(AutoDisposeUtils.bindToLifecycle(this))
        .subscribe(response ->
            startActivity(RepositoryViewActivity.createIntent(response.body())));
  }
}

代码示例来源:origin: pockethub/PocketHub

/**
   * Edit issue to have given milestone
   *
   * @param milestone
   * @return this task
   */
  public EditMilestoneTask edit(Milestone milestone) {
    if (milestone != null) {
      IssueRequest editedIssue = IssueRequest.builder().milestone(milestone.number().longValue()).build();

      store.editIssue(repositoryId, issueNumber, editedIssue)
          .subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread())
          .compose(RxProgress.bindToLifecycle(activity, R.string.updating_milestone))
          .as(AutoDisposeUtils.bindToLifecycle(activity))
          .subscribe(observer);
    }

    return this;
  }
}

代码示例来源:origin: pockethub/PocketHub

/**
   * Edit issue to have given assignee.
   *
   * @param assignee The user the assign
   * @return this task
   */
  public EditAssigneeTask edit(User assignee) {
    String assigneeLogin = assignee != null ? assignee.login() : "";

    IssueRequest edit = IssueRequest.builder()
        .assignees(Collections.singletonList(assigneeLogin))
        .build();

    store.editIssue(repositoryId, issueNumber, edit)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(RxProgress.bindToLifecycle(activity, R.string.updating_assignee))
        .as(AutoDisposeUtils.bindToLifecycle(activity))
        .subscribe(observer);

    return this;
  }
}

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

@Test
public void compose() {
  Single.just(1)
  .compose(new SingleTransformer<Integer, Object>() {
    @Override
    public SingleSource<Object> apply(Single<Integer> f) {
      return f.map(new Function<Integer, Object>() {
        @Override
        public Object apply(Integer v) throws Exception {
          return v + 1;
        }
      });
    }
  })
  .test()
  .assertResult(2);
}

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

@Test
public void singleTransformerThrows() {
  try {
    Single.just(1).compose(new SingleTransformer<Integer, Integer>() {
      @Override
      public Single<Integer> apply(Single<Integer> v) {
        throw new TestException("Forced failure");
      }
    });
    fail("Should have thrown!");
  } catch (TestException ex) {
    assertEquals("Forced failure", ex.getMessage());
  }
}

代码示例来源:origin: pockethub/PocketHub

@Override
protected void createComment(final String comment) {
  CreateCommitComment.Builder commitCommentBuilder = CreateCommitComment.builder()
      .body(comment);
  if(isLineComment(path, position)) {
    commitCommentBuilder.path(path).position(position);
  }
  ServiceGenerator.createService(this, RepositoryCommentService.class)
      .createCommitComment(repository.owner().login(), repository.name(), commit, commitCommentBuilder.build())
      .subscribeOn(Schedulers.io())
      .observeOn(AndroidSchedulers.mainThread())
      .compose(RxProgress.bindToLifecycle(this, R.string.creating_comment))
      .as(AutoDisposeUtils.bindToLifecycle(this))
      .subscribe(response -> finish(response.body()),
          e -> ToastUtils.show(this, e.getMessage()));
}

代码示例来源:origin: pockethub/PocketHub

/**
 * Create dialog helper to display labels
 *
 * @param activity
 * @param requestCode
 * @param repository
 */
public LabelsDialog(final BaseActivity activity,
    final int requestCode, final Repository repository) {
  this.activity = activity;
  this.requestCode = requestCode;
  GitHubRequest<Response<Page<Label>>> gitHubRequest = page -> ServiceGenerator
      .createService(activity, IssueLabelService.class)
      .getRepositoryLabels(repository.owner().login(), repository.name(), page);
  labelsSingle = RxPageUtil.getAllPages(gitHubRequest, 1)
      .flatMap(page -> Observable.fromIterable(page.items()))
      .toSortedList((o1, o2) -> CASE_INSENSITIVE_ORDER.compare(o1.name(), o2.name()))
      .compose(RxProgress.bindToLifecycle(activity, R.string.loading_labels))
      .cache();
}

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

@Test
public void noEvents() {
  TestObserver<String> testObserver = Single.just("1")
    .delay(1, TimeUnit.MILLISECONDS, testScheduler)
    .compose(RxLifecycle.<String, String>bind(lifecycle, CORRESPONDING_EVENTS))
    .test();
  testObserver.assertNoValues();
  testScheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS);
  testObserver.assertValue("1");
  testObserver.assertComplete();
}

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

@Test
public void noEvent() {
  TestObserver<String> testObserver = Single.just("1")
    .delay(1, TimeUnit.MILLISECONDS, testScheduler)
    .compose(RxLifecycle.<String, String>bind(lifecycle))
    .test();
  testObserver.assertNoValues();
  testScheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS);
  testObserver.assertValue("1");
  testObserver.assertComplete();
}

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

@Test
public void noEvents() {
  TestObserver<String> testObserver = Single.just("1")
    .delay(1, TimeUnit.MILLISECONDS, testScheduler)
    .compose(RxLifecycle.<String, String>bindUntilEvent(lifecycle, "stop"))
    .test();
  testObserver.assertNoValues();
  testScheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS);
  testObserver.assertValue("1");
  testObserver.assertComplete();
}

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

@Test
public void oneStartEvent() {
  TestObserver<String> testObserver = Single.just("1")
    .delay(1, TimeUnit.MILLISECONDS, testScheduler)
    .compose(RxLifecycle.<String, String>bind(lifecycle, CORRESPONDING_EVENTS))
    .test();
  testObserver.assertNoValues();
  lifecycle.onNext("create");
  testScheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS);
  testObserver.assertValue("1");
  testObserver.assertComplete();
}

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

@Test
public void oneWrongEvent() {
  TestObserver<String> testObserver = Single.just("1")
    .delay(1, TimeUnit.MILLISECONDS, testScheduler)
    .compose(RxLifecycle.<String, String>bindUntilEvent(lifecycle, "stop"))
    .test();
  testObserver.assertNoValues();
  lifecycle.onNext("keep going");
  testScheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS);
  testObserver.assertValue("1");
  testObserver.assertComplete();
}

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

@Test
public void twoOpenEvents() {
  TestObserver<String> testObserver = Single.just("1")
    .delay(1, TimeUnit.MILLISECONDS, testScheduler)
    .compose(RxLifecycle.<String, String>bind(lifecycle, CORRESPONDING_EVENTS))
    .test();
  testObserver.assertNoValues();
  lifecycle.onNext("create");
  lifecycle.onNext("start");
  testScheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS);
  testObserver.assertValue("1");
  testObserver.assertComplete();
}

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

@Test
public void twoEvents() {
  TestObserver<String> testObserver = Single.just("1")
    .delay(1, TimeUnit.MILLISECONDS, testScheduler)
    .compose(RxLifecycle.<String, String>bindUntilEvent(lifecycle, "stop"))
    .test();
  lifecycle.onNext("keep going");
  testObserver.assertNoErrors();
  lifecycle.onNext("stop");
  testScheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS);
  testObserver.assertNoValues();
  testObserver.assertError(CancellationException.class);
}

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

@Test
public void openAndCloseEvent() {
  TestObserver<String> testObserver = Single.just("1")
    .delay(1, TimeUnit.MILLISECONDS, testScheduler)
    .compose(RxLifecycle.<String, String>bind(lifecycle, CORRESPONDING_EVENTS))
    .test();
  lifecycle.onNext("create");
  testObserver.assertNoErrors();
  lifecycle.onNext("destroy");
  testScheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS);
  testObserver.assertNoValues();
  testObserver.assertError(CancellationException.class);
}

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

@Test
  public void oneEvent() {
    TestObserver<String> testObserver = Single.just("1")
      .delay(1, TimeUnit.MILLISECONDS, testScheduler)
      .compose(RxLifecycle.<String, String>bind(lifecycle))
      .test();

    testObserver.assertNoValues();
    testObserver.assertNoErrors();

    lifecycle.onNext("stop");
    testScheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS);

    testObserver.assertNoValues();
    testObserver.assertError(CancellationException.class);
  }
}

相关文章

微信公众号

最新文章

更多