title: rxcpp: nested while loop or similar "classic" imperative structure for program
body: <p>I have a device that streams some events. I want to use reactive extensions to model the following behavior:</p>
  
  <ol>
  <li>Detect when a user connects a dongle (my program checks for events for dongle connected).</li>
  <li>Start to capture a stream of data from the dongle <em>after</em> the dongle is connected.</li>
  <li>Be able to detect when the dongle got disconnected and come back to 1., later if user connects the dongle again, I want to go to 2. In the state where I stream data if keyboard is hit, then the program finishes.</li>
  </ol>
  
  <p>I know how to wait for dongle connection (1.):</p>
  
  <pre><code>auto waitForDongle = events.take_while([](auto const &amp; event) {
        return event == DongleConnected
        }).subscribe([](auto) {});
  </code></pre>
  
  <p>And I know how capture the stream (2.):</p>
  
  <pre><code>auto streamMotionData = events.take_while([](auto const &amp;) { return !keyboardPressed(); })
      .map([](auto const &amp; evt) -&gt; boost::optional&lt;std::vector&lt;double&gt;&gt; {
              ...
              return data;
          }).subscribe([](vector&lt;double&gt; const &amp;) { ...});
  </code></pre>
  
  <p>My problem is that I do not know how to combine streams in order to come back to 1. and later 2. I just know how to do this once and sequentially. But I want the behavior described above.</p>