0

I have two behaviorSubjects

offset = new behaviorSubject<sring>(null)
filter = new behaviorSubject<string>(null)

I do the following..

combineLatest(
    this.offset, // last item in the list, ID for example
    this.filter //  can be any string
).pipe(
    mergeMap(([offset, filter]) => this.getChunkOfTen(offset, filter)),
    scan((acc, chunkOfTen) => {
        return { ...acc, ...chunkOfTen }
    }, {}),
    map(results => Object.values(results)) // to be able to loop on the frontend
)

When the page loads I get 10 records from DB, onScroll I get 10 more and 10 more.. that's what mergeMap and scan are in charge of (as expected).

onFilter all the existing records are gone (as expected) and I get NEW 10 filtered records from DB, NOW when I scroll I DO NOT get more records and I have them in DB.

as far as I know the problem lays.. in scan operator

is there an if else operator? or some other operators I can use before scan?

user12163165
  • 555
  • 8
  • 12

2 Answers2

0

Nothing stops you from climbing one-abstraction-level-up and deal with so called "higher order observables" - those, which are of the type... Observable<Observable<T>>. Also documentation states:

Be aware that combineLatest will not emit an initial value until each observable emits at least one value. This is the same behavior as withLatestFrom and can be a gotcha as there will be no output and no error but one (or more) of your inner observables is likely not functioning as intended, or a subscription is late.

If you feel like my answer has nothing to do with you problem, than please provide more details and I'll update myself.


Nope, scan most probably isn't a cause here. combineLatest is. First of all, it can't be seen when both filtering and scrolling call next; one of them might even complete and if that is the case, here you go. Secondly, scan is reduce with extra feature: it nexts not only the final result of reduction, but intermediate results as well, that's it, nothing else. Last, but not the least, you might decide to plug tap(console.log) (or rely on the debugger) all over your pipes to quickly inspect what's going on.

Zazaeil
  • 3,900
  • 2
  • 14
  • 31
  • @Sereje, I had this issue when using `filter` and `offset` just as normal variables of type `string`. I manged to solve this issue with a simple `if else` inside `scan` so I know for sure that the problem lays in `scan`, all I'm looking for is a way to solve this reactively. – user12163165 Oct 17 '19 at 12:46
  • @Sereje, I'm not `filtering` and `scrolling` on the same time. first I `filter` to get the records then I `scroll`. – user12163165 Oct 17 '19 at 15:24
  • @user12163165, just append all the code, including scrolling/filtering handling. – Zazaeil Oct 17 '19 at 20:11
0

found where the problem was.. it is not in combineLatest nor scan
after filtering all I needed to do is to reset offset observable back to null so it can get a new offset from the filtered items in the list.

user12163165
  • 555
  • 8
  • 12