title: How can I "stop" an IObservable from generating notifcations while another IObservable yields specific value?
body: <p>I want to model the network connection state with Rx:</p>
  
  <ul>
  <li>The network connection my go up or down at any moment.</li>
  <li>The network connection is used to send data to a server.</li>
  <li>While the network connection is down, I don't even want to try sending data, rather buffer it until the connection comes up again.</li>
  </ul>
  
  <p>I have two observables:</p>
  
  <ul>
  <li><code>connection</code> generates <code>true</code>/<code>false</code> notifications for connection state changes</li>
  <li><code>data</code> generates notifications for data to be sent</li>
  </ul>
  
  <p>While <code>connection</code> is up (latest value == <code>true</code>) I want <code>data</code> notifications to be sent without delay. When the <code>connection</code> goes down, I want notifications from <code>data</code> to queue up until it goes up again.</p>
  
  <p>Here's a marble diagram of the desired resulting sequence:</p>
  
  <pre><code>D = down
  U = up
  
  connection D  U    D      U
  data       1 2   3  4 5 6
  result        12 3        456
  </code></pre>
  
  <p>I've found a solution that feels not right as is saves state in a local variable (<a href="https://gist.github.com/agross/c53f5c54fd8c6e163daf" rel="nofollow">Runnable Gist</a>).</p>
  
  <pre><code>var data = Observable.Interval(TimeSpan.FromSeconds(1));
  var connection = new Subject&lt;bool&gt;();
  
  var connected = false;
  connection.Do(x =&gt; connected = x).Subscribe();
  
  data
    .Window(() =&gt; connection)
    .Subscribe(window =&gt;
    {
      Console.Write("Starting new window: ");
      if (connected == false)
      {
        Console.WriteLine("Buffering until online");
        window
          .Buffer(() =&gt; connection.Where(x =&gt; x))
          .Subscribe(buffered =&gt; buffered
                                   .ToObservable()
                                   .Subscribe(Console.WriteLine));
      }
      else
      {
        Console.WriteLine("Forwarding notifications");
        window.Subscribe(Console.WriteLine);
      }
    });
  </code></pre>
  
  <p>Thanks!</p>