Converting a while loop to Reactive Java

Viewed 37

I am using Project Reactor in Java. How would I write this blocking while loop in a reactive way?

  Flux<Instant> findHistory() {
    int max = 10;
    Instant lastSeenTime = Instant.now();

    List<Instant> instants = new ArrayList<>();
    AtomicInteger pageNumber = new AtomicInteger(1);

    while (instants.size() < max) {
      List<Instant> block =
          getHistory(pageNumber.getAndIncrement())
              .filter(instant -> instant.isBefore(lastSeenTime))
              .collectList()
              .block();

      instants.addAll(block);
    }
    return Flux.fromIterable(instants);
  }

  Flux<Instant> getHistory(int pageNumber) {
    // returns the page of Instants
  }

I want to keep calling getHistory add the result after filtering to my list instants until that list has the size of max. I have tried to use repeatWhen and expand but haven't figure out how to use them to achieve what this while loop does.

0 Answers
Related