From ef737c3be1dfd14e2db2fa40c707bf92cfd6bdf0 Mon Sep 17 00:00:00 2001 From: PartMan <47669599+PartMan7@users.noreply.github.com> Date: Thu, 29 Aug 2024 22:23:32 +0530 Subject: [PATCH] Teambuilder: Debounce searching teams (#2270) Only debounce for 500+ teams --- .../js/client-teambuilder.js | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/play.pokemonshowdown.com/js/client-teambuilder.js b/play.pokemonshowdown.com/js/client-teambuilder.js index 1f0031d84a..be22d105f3 100644 --- a/play.pokemonshowdown.com/js/client-teambuilder.js +++ b/play.pokemonshowdown.com/js/client-teambuilder.js @@ -138,6 +138,8 @@ curFolder: '', curFolderKeep: '', curSearchVal: '', + // Debounce value for searching in the teambuilder + searchTimeout: null, exportMode: false, formatResources: {}, @@ -3243,11 +3245,24 @@ this.chartSet(val, selectNext); }, searchChange: function (e) { - // 91 for right CMD / 93 for left CMD / 17 for CTL - if (e.keyCode !== 91 && e.keyCode !== 93 && e.keyCode !== 17) { - this.curSearchVal = e.currentTarget.value; + var DEBOUNCE_THRESHOLD_TEAMS = 500; + function updateTeamList() { + // 91 for right CMD / 93 for left CMD / 17 for CTL + if (e.keyCode !== 91 && e.keyCode !== 93 && e.keyCode !== 17) { + this.curSearchVal = searchVal; + } this.updateTeamList(); } + + // If the user has a lot of teams, search is debounced to + // ensure this isn't called too frequently while typing + if (Storage.teams.length > DEBOUNCE_THRESHOLD_TEAMS) { + if (this.searchTimeout) clearTimeout(this.searchTimeout); + + var searchVal = e.currentTarget.value; + this.searchTimeout = setTimeout(updateTeamList.bind(this), 400); + } else updateTeamList(); + }, chartSetCustom: function (val) { val = toID(val);