Make hotkeys a function not a singleton class that has side effects when you construct it

This also sets us up for being able to unbind things
This commit is contained in:
Jordan Eldredge 2018-09-19 21:43:55 -07:00
parent b42d5fb91c
commit 72875f3367
2 changed files with 12 additions and 5 deletions

View file

@ -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);
};
}

View file

@ -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));
}
}