Reputation: 51283
How can I create a pausableBuffered
observable where it only buffers the latest item?
Currently I've got an ugly workaround:
source.pauseableBuffered(pauser).debounce(0)
Upvotes: 0
Views: 80
Reputation: 10783
You could have two observable sequences
1. The source
sequence
2. The pauser
sequence IObservable<bool>
then you could just combine latest them
Observable.CombineLatest(
source,
pauser,
(s,p)=>Tuple.Create(s,p))
.Where(t=>!t.Item2)
This now allows you to ignore values while pauser
has pushed a 'true' value.
You then can simply stick replay(1) on the end of that.
Observable.CombineLatest(
source,
pauser,
(s,p)=>Tuple.Create(s,p))
.Where(t=>!t.Item2)
.Replay(1)
//.Publish().RefCount(); //If required.
EDIT
The Replay(1)
is just noise, and doesn't focus on the OP.
This code
var source = new Subject<int>();
var pauser = new Subject<bool>();
var query = Observable.CombineLatest(source, pauser, (s,p)=>Tuple.Create(s,p))
.Where(t=>!t.Item2);
query.Dump();
pauser.OnNext(false);
source.OnNext(1);
source.OnNext(2);
source.OnNext(3);
pauser.OnNext(true);
source.OnNext(4);
source.OnNext(5);
source.OnNext(6);
pauser.OnNext(false);
source.OnNext(7);
source.OnNext(8);
Produces this result
1
2
3
6
7
8
Upvotes: 2