Reputation: 342
Hellow, I'm trying to create function of observable (OBS) and subject(SUB) that stores last item from OBS, while SUB has F value, and emit it (and only it) when SUN becomes T
OBS ---a----b----c----d----e----f----g----h-----
SUB ------F----------T------------F-------T-----
OUT -----------------c--------------------h-----
I tried to solve this with
OBS.window(SUB)
.withLatestFrom(SUB)
.switchMap(([window, status]) => {
if(status === F) {
return window.combineLatest(SUB, (cmd, status) => {
if(status === T) {
return null;
};
return cmd;
}).last((e) => {
return !!e;
})
}
return Observable.empty<Command>();
}).filter((cmd) => {
return !!cmd;
})
but it doesn't work
Upvotes: 4
Views: 764
Reputation: 342
I found own solution:
OBS
.buffer(SUB)
.withLatestFrom(SUB)
.map(([buffer, t]) => {
if(!buffer || !buffer.length) {
return null;
}
if(t === T) {
return buffer[buffer.length - 1];
}
return null;
})
.filter((e) => !!e);
this variant has next behavior
---a----b----c----d----e----f----g----h-----
------F----------T------------F-------T-----
-----------------c--------------------h-----
and don't generate output if window between F and T is empty
---a--------------d----e----f----g----h-----
------F----------T------------F-------T-----
--------------------------------------h-----
Upvotes: 2
Reputation: 18663
So it seems like you want something like:
SUB
// Only emit changes in the status
.distinctUntilChanged()
// Only forward true values down stream
.filter(t => t === T)
// Only emit the latest from OBS when you get a T from SUB
// Remap it so only cmd is forwarded
.withLatestFrom(OBS, (_, cmd) => cmd)
Upvotes: 2