-
Notifications
You must be signed in to change notification settings - Fork 169
Add debounce to @mentions remote filter for improved performance #350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Add debounce function to improve performance of @mentions remote filter - Integrated debounce function to throttle the frequency of remoteFilter calls. - Enhanced user experience by reducing the number of AJAX requests made during typing. - Updated the remoteFilter callback in mentions.js to use debounce. - Adjusted debounce delay to 300ms to balance responsiveness and performance. This change aims to optimize the performance of the @mentions feature by minimizing unnecessary AJAX requests and improving overall responsiveness.
src/bp-activity/js/mentions.js
Outdated
| * @param {number} wait The number of milliseconds to wait before calling `func`. | ||
| * @returns {Function} A debounced version of the provided function. | ||
| */ | ||
| function debounce(func, wait) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We actually have some prior art. Could we add this in a way that can be reshared?
buddypress/src/bp-templates/bp-nouveau/js/buddypress-priority-menu.js
Lines 17 to 44 in 151f093
| function debounce(func, wait, immediate) { | |
| 'use strict'; | |
| var timeout; | |
| wait = (typeof wait !== 'undefined') ? wait : 20; | |
| immediate = (typeof immediate !== 'undefined') ? immediate : true; | |
| return function() { | |
| var context = this, args = arguments; | |
| var later = function() { | |
| timeout = null; | |
| if (!immediate) { | |
| func.apply(context, args); | |
| } | |
| }; | |
| var callNow = immediate && !timeout; | |
| clearTimeout(timeout); | |
| timeout = setTimeout(later, wait); | |
| if (callNow) { | |
| func.apply(context, args); | |
| } | |
| }; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've implemented the changes:
- Updated the debounce function to ensure consistency with existing code.
- Added functionality to skip AJAX requests if the query is empty.
Please review the latest commit and let me know if there are any further adjustments needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually meant that we could reuse the same code in both places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the clarification. I understand your suggestion to reuse the same debounce function across different parts of the project. To achieve this, I propose extracting the debounce function from buddypress/src/bp-templates/bp-nouveau/js/buddypress-priority-menu.js and placing it in a shared location that can be imported where needed.
Specifically, we can create a common utility file, utils.js, where we define the debounce function. Then, we can update both mentions.js and buddypress-priority-menu.js to import and use the debounce function from utils.js. This approach ensures consistency and reduces redundancy, as any updates to the debounce function will only need to be made in one place.
Would you agree with this approach? If so, could you please advise on the best location to place this shared function? I'll go ahead and implement these changes once I have your approval.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@imath Do you have suggestions for the location of this shared util file?
|
A trac ticket would be great too. |
- Modified the debounce function to include an option for immediate execution of the function. - Updated the remoteFilter to skip AJAX requests when the query is empty, ensuring that no unnecessary network calls are made.
src/bp-activity/js/mentions.js
Outdated
| var timeout; | ||
| wait = (typeof wait !== 'undefined') ? wait : 20; | ||
| immediate = (typeof immediate !== 'undefined') ? immediate : true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like there is some spacing issues here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the spacing issues.
src/bp-activity/js/mentions.js
Outdated
| if (!query.trim()) { | ||
| render_view([]); // Return an empty array to indicate no results | ||
| return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here too.
|
@Praveenkroot Let's not try to reuse this functionality. It might be harder than I expected. And might require a large conversation. We can fix the issue here with the current implementation. Can you create a ticket https://buddypress.trac.wordpress.org/newticket related to this issue? |
…alization * Standardized indentation and spacing in the variable initialization section. * Corrected inconsistent indentation and spacing in the AJAX request handling code.
|
@renatonascalves Thank you for the feedback. I understand that reusing the functionality might be complex and could require extensive discussion. I’ll focus on addressing the issue with the current implementation as you suggested. I will create a ticket on BuddyPress Trac regarding this issue and provide a link to it here. Thanks for your guidance! |
|
@Praveenkroot I think this pr is only pending a ticket with more information and possibly some testing steps. |
|
Hi @renatonascalves, I've created the Trac ticket as requested, which includes additional details and testing steps for the issue we discussed. You can find the ticket here: https://buddypress.trac.wordpress.org/ticket/9239#ticket Please let me know if there’s anything else you'd like to see added or adjusted. Thanks again for your guidance and feedback! |
|
Thanks @Praveenkroot 🤟🏽 |
…ormance. This change aims to optimize the performance of the `@mentions` feature by minimizing unnecessary AJAX requests and improving overall responsiveness. Props praveenkumar98. Fixes #9239 Closes buddypress/buddypress#350 git-svn-id: http://buddypress.svn.wordpress.org/trunk@14098 cdf35c40-ae34-48e0-9cc9-0c9da1808c22
…ormance. This change aims to optimize the performance of the `@mentions` feature by minimizing unnecessary AJAX requests and improving overall responsiveness. Props praveenkumar98. Fixes #9239 Closes buddypress/buddypress#350 git-svn-id: https://buddypress.svn.wordpress.org/trunk@14098 cdf35c40-ae34-48e0-9cc9-0c9da1808c22
…ormance. This change aims to optimize the performance of the `@mentions` feature by minimizing unnecessary AJAX requests and improving overall responsiveness. Props praveenkumar98. Fixes #9239 Closes buddypress/buddypress#350 git-svn-id: https://buddypress.svn.wordpress.org/trunk@14098 cdf35c40-ae34-48e0-9cc9-0c9da1808c22
Add debounce function to improve performance of @mentions remote filter
This change aims to optimize the performance of the @mentions feature by minimizing unnecessary AJAX requests and improving overall responsiveness.
Trac ticket: https://buddypress.trac.wordpress.org/ticket/9239
This Pull Request is for code review only. Please keep all other discussion in the BuddyPress Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the WordPress Core Handbook for more details.