Changeset 14098
- Timestamp:
- 02/16/2025 03:37:07 PM (5 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-activity/js/mentions.js
r12856 r14098 12 12 if ( typeof window.BP_Suggestions === 'object' ) { 13 13 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 }; 14 56 } 15 57 … … 158 200 * If there are no matches for the query in this.data, then query BuddyPress. 159 201 * 202 * @since 2.1.0 203 * @since 3.0.0. Renamed from "remote_filter" for at.js v1.5.4 support. 204 * 160 205 * @param {string} query Partial @mention to search for. 161 206 * @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 ) { 166 209 var self = $( this ), 167 210 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 } 168 218 169 219 mentionsItem = mentionsQueryCache[ query ]; … … 188 238 * Success callback for the @suggestions lookup. 189 239 * 240 * @since 2.1.0 241 * 190 242 * @param {object} response Details of users matching the query. 191 * @since 2.1.0192 243 */ 193 244 .done(function( response ) { … … 201 252 * nicename matches will appear on top. 202 253 * 254 * @since 2.1.0 255 * 203 256 * @param {array} suggestion A suggestion's original data. 204 257 * @return {array} A suggestion's new data. 205 * @since 2.1.0206 258 */ 207 259 function( suggestion ) { … … 214 266 render_view( data ); 215 267 }); 216 } 268 }, 500) // 500ms debounce time. 217 269 }, 218 270
Note: See TracChangeset
for help on using the changeset viewer.