I have two streams. One is a flow of data (could be any type), the other is a boolean stream acting as a gate. I need to combine these into a stream that has the following behaviour:
- When the gate is open (most recent value was true) then the data
should flow straight through
- When the gate is closed (most recent value was false) then the data
should be buffered to be released as individual elements when the
gate is next open
- The solution should preserve all elements of the data and preserve
order
I am not really sure how to put this together. The inputs I have been testing with are like this:
// 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);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…