title: How can I get last item of Flux without collapsing it with reduce() or last()
body: <p>How can I get last item of Flux without collapsing it with reduce() or last() ? Here is my use-case: </p>
  
  <p>1) I have generator that produces <code>Flux&lt;T&gt;</code> based on state.
  2) When inner <code>Flux</code> completes it alters the state that affect next <code>Flux</code> objects I emit in generator.</p>
  
  <p>Schematicaly it looks like this</p>
  
  <pre><code>static class State {
      int secret = 2;
      int iteration = 0;
  }
  
  Random rand = new Random(1024);
  Flux&lt;Integer&gt; stream = Flux.&lt;Flux&lt;Integer&gt;, State&gt;generate(State::new, (state, sink) -&gt; {
  
      System.out.println(String.format("Generate: %d", state.secret));
      Flux&lt;Integer&gt; inner = Flux.range(1, rand.nextInt(10));
  
      sink.next(inner.doOnComplete(() -&gt; {
          // How do I get last item of `inner` here ?
          // For example I'd like to decrement `state.secret` by last value of `inner`
      }));
  
      return state;
  }).flatMap(Function.identity());
  </code></pre>
  
  <p>UPD: I unmarked my answer because the hack turned out to be unreliable. It's possible that <code>.generate()</code> will be invoked before previous <code>Flux</code> is fully consumed therefore making value in <code>last</code> incorrect.</p>