Skip to content

Conversation

@saisankhe
Copy link

@saisankhe saisankhe commented Jan 23, 2026

What the file does
The file implements the client-side infinite scrolling behaviour in NodeBB. It keeps a track of the window scroll events, calculates how far the user has scrolled relative to the container and then triggers a callback to load more content.

The scope of the refactoring
Refactored was done only on the onScroll() function. No changes were made to the behaviours of the function. I created a few helper functions to keep everything clean and easy to read.

Qlty Smell Addressed
Function with high complexity (count = 11): onScroll in public/src/client/infinitescroll.js from line 40. The following was the output of the Qlty-reported issue
public/src/client/infinitescroll.js 40 Function with high complexity (count = 11): onScroll

REFACTORING
in onScroll, the high complexity (count = 11) reduced adaptability since the logic was condensed into one function. This made it harder to understand and modify the code.

I refactored onScroll() by adding helper functions that did one job each.
- skipScroll() was made to have all the early-exit conditions
- getMetrics() was created to compute currentScrollTop, viewportHeight and scrollPercent
- getDirection(currentScrollTop) was created to compute scroll direction in relation to previousScrollTop
- getTrigger(metrics) contained trigger rules for when to call the callback

The changes made improved adaptability. The multiple responsibilities condensed into the onScroll() function were converted to a function each for easier reading. An alternative I considered was to make changes in the existing function but I was running into the same problem of the responsibilities being condensed into one function.

VALIDATION
I ran NodBB locally, opened the site in the browser, created a few posts myself by creating an account to trigger the infinite scroll function, opened the Console from the DevTools, scrolled down the posts page to trigger the infinite scrolling. I inserted a console.log('Sai Sankhe') statement which was shown as belows:

Screenshot 2026-01-23 at 2 37 30 AM Screenshot 2026-01-23 at 3 03 25 AM

qlty smells after changes
Screenshot 2026-01-23 at 3 01 41 AM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant