Optimize hotkey registration (#1264)

This commit is contained in:
toasted-nutbread 2021-01-17 17:05:06 -05:00 committed by GitHub
parent 14b4aee07d
commit 04d53e5642
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View File

@ -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() {

View File

@ -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.