RxJS - debounce event after throttle?
here is updated solution due to rxjs changes
import { fromEvent } from 'rxjs';
import { debounceTime, map, throttleTime } from 'rxjs/operators';
const observable = fromEvent(window, 'scroll');
const subscriber = observable
.pipe(
throttleTime(300 /* ms */),
map((data) => {
console.log('throttle');
console.log(window.pageYOffset);
return data;
}),
debounceTime(350)
)
.subscribe(
(x) => {
console.log(window.pageYOffset);
},
(err) => {
console.log('Error: %s', err);
},
() => {
console.log('Completed');
}
);
You could merge two streams: throttled and debounced into one using merge
. Demo.
const observable = Rx.Observable.fromEvent(window, 'scroll');
const subscriber = observable
.throttleTime(300 /* ms */)
.map(() => 'throttle')
.merge(observable
.debounceTime(350)
.map(() => 'debounce')
)
.subscribe(
(x) => {
console.log('Next: event!', x);
},
(err) => {
console.log('Error: %s', err);
},
() => {
console.log('Completed');
});