diff --git a/js/hotkeys.ts b/js/hotkeys.ts index 9b57251c..eef797c4 100644 --- a/js/hotkeys.ts +++ b/js/hotkeys.ts @@ -21,7 +21,7 @@ import { TOGGLE_TIME_MODE, TOGGLE_LLAMA_MODE } from "./actionTypes"; import { arraysAreEqual } from "./utils"; import { Dispatch } from "./types"; -export default function(dispatch: Dispatch) { +export function bindHotkeys(dispatch: Dispatch): () => void { let keylog: number[] = []; const trigger = [ 78, // N @@ -33,7 +33,8 @@ export default function(dispatch: Dispatch) { 70, // F 84 // T ]; - document.addEventListener("keydown", e => { + + const listener = (e: KeyboardEvent) => { if (e.ctrlKey) { switch (e.keyCode) { case 68: // CTRL+D @@ -142,5 +143,10 @@ export default function(dispatch: Dispatch) { dispatch({ type: TOGGLE_LLAMA_MODE }); } } - }); + }; + document.addEventListener("keydown", listener); + + return () => { + document.removeEventListener("keydown", listener); + }; } diff --git a/js/webampLazy.js b/js/webampLazy.js index 93e24673..c7db55a4 100644 --- a/js/webampLazy.js +++ b/js/webampLazy.js @@ -4,7 +4,7 @@ import { Provider } from "react-redux"; import getStore from "./store"; import App from "./components/App"; -import Hotkeys from "./hotkeys"; +import { bindHotkeys } from "./hotkeys"; import Media from "./media"; import * as Selectors from "./selectors"; import { @@ -65,6 +65,7 @@ class Winamp { } constructor(options) { + this._subscriptions = []; this._actionEmitter = new Emitter(); this.options = options; const { @@ -161,7 +162,7 @@ class Winamp { } if (enableHotkeys) { - new Hotkeys(this.store.dispatch); + this._subscriptions.push(bindHotkeys(this.store.dispatch)); } }