Skip to content

Instantly share code, notes, and snippets.

@LeeCampbell
Created May 3, 2014 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LeeCampbell/26199ad3341d519e3838 to your computer and use it in GitHub Desktop.
Save LeeCampbell/26199ad3341d519e3838 to your computer and use it in GitHub Desktop.
Buffers values from one observable, by another 'gate' observable. Allows flows to be turned on an off without loosing intermediate values.
//From James World http://stackoverflow.com/questions/23431018/how-can-i-alternately-buffer-and-flow-a-live-data-stream-in-rx
// a demo data stream that emits every second
var dataStream = Observable.Interval(TimeSpan.FromSeconds(1));
// a demo flag stream that toggles every 5 seconds
var toggle = false;
var gateStream = Observable.Interval(TimeSpan.FromSeconds(5))
.Select(_ => toggle = !toggle);
dataStream.Window(gateStream.StartWith(false).DistinctUntilChanged())
.Select((w, i) => i % 2 == 0 ? w.ToList().SelectMany(x => x) : w)
.Concat()
.Subscribe(Console.WriteLine);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment