How to replace debounceTime(0) to emit when needed?

Viewed 443

I have a similar use case to this

import { BehaviorSubject, combineLatest } from 'rxjs'
import { map, debounceTime } from 'rxjs/operators'

const items$ = new BehaviorSubject([]);
const size$ = new BehaviorSubject(10);

const visibleItems$ = combineLatest([items$, size$])
  .pipe(
    debounceTime(0),
    map(([items, size]) => items.slice(0, size))
  );

And some times I have this scenario

const onData = bigArr => {
  items$.next(bigArr);
}

Sometimes this

const changeSize = () => {
  size$.next(20);
}

And sometimes this

const onData2 = bigArr => {
  items$.next(bigArr);
  size$.next(10);
}

I don't want to trigger the visibleItems$ observable flow multiple times, so a solution that I've found is use the debounceTime operator with 0 ms to perform the onData2 method without running the pipe operators twice, but it is a bit hard to reason this operator (at least from the first glance). How can I replace it, so my code is easier to understand?

0 Answers
Related