There are two observables that may be emiting together or separately: stream1
and stream2
.
I need my subscription to fire only if stream2
fires less then 1 second after stream1
does.
Any way to achieve that with RxJS?
You can use a timestamp and withLatestFrom
to decide which values to emit
Here I just filter such that only values that meet your condition pass through.
stream2.pipe(
timestamp(),
withLatestFrom(stream1.pipe(
timestamp(),
startWith({timestamp: 0, value: null})
)),
filter(([s2, s1]) => s2.timestamp - s1.timestamp < 1000),
map(([s2, s1]) => ({
stream1: s1.value,
stream2: s2.value
}))
);