From df471161a79bba9ccc889d290087c3ca72784365 Mon Sep 17 00:00:00 2001 From: Samuel Elliott Date: Wed, 18 Jul 2018 20:55:57 +0100 Subject: [PATCH] Fix leaks in keybind settings --- client/src/structs/settings/types/keybind.js | 26 ++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/client/src/structs/settings/types/keybind.js b/client/src/structs/settings/types/keybind.js index 70ce66c5..8114f21c 100644 --- a/client/src/structs/settings/types/keybind.js +++ b/client/src/structs/settings/types/keybind.js @@ -8,10 +8,12 @@ * LICENSE file in the root directory of this source tree. */ -import Setting from './basesetting'; +import { IterableWeakSet } from 'common'; import Combokeys from 'combokeys'; import CombokeysGlobalBind from 'combokeys/plugins/global-bind'; +import Setting from './basesetting'; +const instances = new IterableWeakSet(); let keybindsPaused = false; export default class KeybindSetting extends Setting { @@ -19,9 +21,11 @@ export default class KeybindSetting extends Setting { constructor(args, ...merge) { super(args, ...merge); + instances.add(this); + this.__keybind_activated = this.__keybind_activated.bind(this); - this.combokeys = new Combokeys(document); + this.combokeys = new Combokeys(this); CombokeysGlobalBind(this.combokeys); this.combokeys.bindGlobal(this.value, this.__keybind_activated); } @@ -43,6 +47,22 @@ export default class KeybindSetting extends Setting { this.emit('keybind-activated', event); } + // Event function aliases for Combokeys + get addEventListener() { return this.on } + get removeEventListener() { return this.removeListener } + + static _setup() { + document.addEventListener('keydown', this.__event_handler.bind(this, 'keydown')); + document.addEventListener('keyup', this.__event_handler.bind(this, 'keyup')); + document.addEventListener('keypress', this.__event_handler.bind(this, 'keypress')); + } + + static __event_handler(event, data) { + for (let keybindSetting of instances) { + keybindSetting.emit(event, data); + } + } + static get paused() { return keybindsPaused; } @@ -52,3 +72,5 @@ export default class KeybindSetting extends Setting { } } + +KeybindSetting._setup();