From 04d53e5642ddf4fe9a15f22df0c3892e53074739 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 17 Jan 2021 17:05:06 -0500 Subject: [PATCH] Optimize hotkey registration (#1264) --- ext/mixed/js/display.js | 4 +--- ext/mixed/js/hotkey-handler.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index c3e118c6..c017a2f1 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -1880,9 +1880,7 @@ class Display extends EventDispatcher { } _updateHotkeys(options) { - const scope = this._pageType; - this._hotkeyHandler.clearHotkeys(scope); - this._hotkeyHandler.registerHotkeys(scope, options.inputs.hotkeys); + this._hotkeyHandler.setHotkeys(this._pageType, options.inputs.hotkeys); } async _closeTab() { diff --git a/ext/mixed/js/hotkey-handler.js b/ext/mixed/js/hotkey-handler.js index 81ec950e..2de2a8d2 100644 --- a/ext/mixed/js/hotkey-handler.js +++ b/ext/mixed/js/hotkey-handler.js @@ -105,6 +105,23 @@ class HotkeyHandler extends EventDispatcher { this._updateHotkeyRegistrations(); } + /** + * Assigns a set of hotkeys for a given scope. This is an optimized shorthand for calling + * `clearHotkeys`, then calling `registerHotkeys`. + * @see registerHotkeys for argument information. + */ + setHotkeys(scope, hotkeys) { + let registrations = this._hotkeyRegistrations.get(scope); + if (typeof registrations === 'undefined') { + registrations = []; + this._hotkeyRegistrations.set(scope, registrations); + } else { + registrations.length = 0; + } + registrations.push(...hotkeys); + this._updateHotkeyRegistrations(); + } + /** * Adds a single event listener to a specific event. * @param eventName The string representing the event's name.