BACK
Use casesInfinite scroll — pages without duplicates
Use cases · 25

Infinite scroll — pages without duplicates

Pattern

A feed like Twitter or Instagram. Scroll to the bottom — the next page loads. Scroll more — the next one. No manual "load more" click.

The problem it solves

Scroll emits dozens of events per second. And while the request for page 2 is in flight, the user reaches the bottom again — and an isLoading flag is easy to forget resetting in every branch (error, cancel, success). The result: duplicate pages or lost items.

Operators and why they matter

  • throttleTime — cut the scroll-event rate down to something reasonable.
  • filter — pass on only the "reached the bottom" event.
  • exhaustMap — while a page request is in flight, new bottom events are IGNORED. This is the key to duplicate protection.
  • scan — accumulate items as pages load.

Gotchas

  • switchMap — every scroll cancels the current load. Pages will vanish.
  • mergeMap — parallel requests for the same page. The backend is shocked, duplicates in the UI.
  • concatMap — every bottom event queues up. Scroll 10 times and 10 pages queue, the last loading a minute later.

What you get

Each page loads exactly once. Fast scrolling doesn't create an avalanche of requests.

script.ts // TypeScript
CONSOLE · Console output
Hit Run to see the result...