Skip to:
Content

BuddyPress.org

Changeset 14098


Ignore:
Timestamp:
02/16/2025 03:37:07 PM (5 months ago)
Author:
espellcaste
Message:

Activity: Add debounce to @mentions remote filter for improved performance.

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 https://github.com/buddypress/buddypress/pull/350

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-activity/js/mentions.js

    r12856 r14098  
    1212    if ( typeof window.BP_Suggestions === 'object' ) {
    1313        bp.mentions.users = window.BP_Suggestions.friends || bp.mentions.users;
     14    }
     15
     16    /**
     17     * Define a debounce function to optimize performance by limiting the rate
     18     * of AJAX requests for user mentions.
     19     *
     20     * This function ensures that the `func` is only called after `wait` milliseconds
     21     * have passed since the last invocation, with an optional immediate execution.
     22     * This helps reduce the frequency of AJAX requests and improves the efficiency of the @mentions feature.
     23     *
     24     * @param {Function} func The function to debounce.
     25     * @param {number} wait The number of milliseconds to wait before calling `func`.
     26     * @param {boolean} [immediate=true] Whether to call the function immediately.
     27     * @returns {Function} A debounced version of the provided function.
     28     */
     29    function debounce(func, wait, immediate) {
     30        'use strict';
     31
     32        var timeout;
     33        wait = (typeof wait !== 'undefined') ? wait : 20;
     34        immediate = (typeof immediate !== 'undefined') ? immediate : true;
     35
     36        return function() {
     37
     38            var context = this, args = arguments;
     39            var later = function() {
     40                timeout = null;
     41
     42                if (!immediate) {
     43                    func.apply(context, args);
     44                }
     45            };
     46
     47            var callNow = immediate && !timeout;
     48
     49            clearTimeout(timeout);
     50            timeout = setTimeout(later, wait);
     51
     52            if (callNow) {
     53                func.apply(context, args);
     54            }
     55        };
    1456    }
    1557
     
    158200                 * If there are no matches for the query in this.data, then query BuddyPress.
    159201                 *
     202                 * @since 2.1.0
     203                 * @since 3.0.0. Renamed from "remote_filter" for at.js v1.5.4 support.
     204                 *
    160205                 * @param {string} query Partial @mention to search for.
    161206                 * @param {function} render_view Render page callback function.
    162                  * @since 2.1.0
    163                  * @since 3.0.0. Renamed from "remote_filter" for at.js v1.5.4 support.
    164                  */
    165                 remoteFilter: function( query, render_view ) {
     207                 */
     208                remoteFilter: debounce(function( query, render_view ) {
    166209                    var self = $( this ),
    167210                        params = {};
     211
     212                    // Skip AJAX request if query is empty.
     213                    if (!query.trim()) {
     214                        // Return an empty array to indicate no results.
     215                        render_view([]);
     216                        return;
     217                    }
    168218
    169219                    mentionsItem = mentionsQueryCache[ query ];
     
    188238                         * Success callback for the @suggestions lookup.
    189239                         *
     240                         * @since 2.1.0
     241                         *
    190242                         * @param {object} response Details of users matching the query.
    191                          * @since 2.1.0
    192243                         */
    193244                        .done(function( response ) {
     
    201252                                 * nicename matches will appear on top.
    202253                                 *
     254                                 * @since 2.1.0
     255                                 *
    203256                                 * @param {array} suggestion A suggestion's original data.
    204257                                 * @return {array} A suggestion's new data.
    205                                  * @since 2.1.0
    206258                                 */
    207259                                function( suggestion ) {
     
    214266                            render_view( data );
    215267                        });
    216                 }
     268                }, 500) // 500ms debounce time.
    217269            },
    218270
Note: See TracChangeset for help on using the changeset viewer.