Created
May 3, 2014 10:55
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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