Use a Set instead of an array in a KeyboardHandler's member

The variable `triggers` is only used to check if in contains a particular
value. Given that the number of keyboard shortcuts is starting to be
significant, let's future-proof the performances and use a `Set` instead of an
`Array` instead.
This commit is contained in:
jvoisin 2024-03-10 23:55:56 +01:00 committed by Frédéric Guillot
parent eaaeb68474
commit d9d17f0d69

View file

@ -2,12 +2,12 @@ class KeyboardHandler {
constructor() { constructor() {
this.queue = []; this.queue = [];
this.shortcuts = {}; this.shortcuts = {};
this.triggers = []; this.triggers = new Set();
} }
on(combination, callback) { on(combination, callback) {
this.shortcuts[combination] = callback; this.shortcuts[combination] = callback;
this.triggers.push(combination.split(" ")[0]); this.triggers.add(combination.split(" ")[0]);
} }
listen() { listen() {
@ -48,7 +48,7 @@ class KeyboardHandler {
isEventIgnored(event, key) { isEventIgnored(event, key) {
return event.target.tagName === "INPUT" || return event.target.tagName === "INPUT" ||
event.target.tagName === "TEXTAREA" || event.target.tagName === "TEXTAREA" ||
(this.queue.length < 1 && !this.triggers.includes(key)); (this.queue.length < 1 && !this.triggers.has(key));
} }
isModifierKeyDown(event) { isModifierKeyDown(event) {