基于Single的条件流

时间:2018-08-22 01:58:44

标签: reactive-programming rx-java2

如何基于Single的值产生条件流?例如,在以下示例中,如果getNextAction(nodeId)导致空操作,则我希望链结束,否则继续执行。 每个方法都返回Single,而不是Observable。显然,过滤器仅适用于Observables。

          getNextAction(nodeId)
           .flatMap(action -> {
               if (action.isEmpty()) {
                   sendResponse("");
               }
               return Single.just(action);
           })
           //.filter(action -> !action.isEmpty())
           .flatMap(action -> prepareAction(action, nodeId)
            .subscribe(response -> sendResponse(transform(response)));

1 个答案:

答案 0 :(得分:0)

一种方法可能是:

  1. Sub foo() Dim Source As Workbook Dim wsTarget As Worksheet, wsSource As Worksheet Dim sourceRange As Range Dim targetLastRow As Long, sourceFirstRow As Long Set Source = ThisWorkbook ' ### MODIFY AS NEEDED Set wsSource = Source.Worksheets("Sheet1") '#Source.Worksheets("SUMMARY DATA SHEET") Set sourceRange = wsSource.Range("A8:A18") sourceFirstRow = sourceRange.Find("*", After:=wsSource.Range("A8")).Row Set sourceRange = wsSource.Range("A" & sourceFirstRow & ":A18") Set wsTarget = ThisWorkbook.Worksheets("Sheet2") targetLastRow = wsTarget.Cells(Rows.Count, 2).End(xlUp).Row + 1 wsTarget.Range("B" & targetLastRow).Resize(sourceRange.Rows.Count, 1).Value = _ sourceRange.Value wsTarget.Range("D" & targetLastRow).Resize(sourceRange.Rows.Count, 1).Value = _ sourceRange.Offset(, 3).Value 'etc... End Sub flatMap转换成getNextAction()
  2. 检查操作以立即完成流,或编写一组要完成的操作(例如,准备操作并发送响应)

可能看起来像这样:

Completable

(以上假设getNextAction(nodeId) .flatMapCompletable(action -> { if(action.isEmpty()) { return Completable.complete(); } else { return Completable.fromAction(() -> { // prepareAction()... // sendResponse()... }); } }) .subscribe( () -> System.out.println("## onComplete()"), error -> System.out.println("## onError(" + error.getMessage() + ")") ); 返回了getNextAction()

如果没有别的,我希望它能激发一些思想。