Skip to content

Commit

Permalink
Add types (#32)
Browse files Browse the repository at this point in the history
* Add types

* Add return type and fix type
  • Loading branch information
after-finitude committed Sep 12, 2020
1 parent ad89ac2 commit fb277f8
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/tinykeys.ts
Expand Up @@ -103,15 +103,15 @@ function match(event: KeyboardEvent, press: KeyBindingPress): boolean {
export default function keybindings(
target: Window | HTMLElement,
keyBindingMap: KeyBindingMap,
) {
): () => void {
let keyBindings = Object.keys(keyBindingMap).map(key => {
return [parse(key), keyBindingMap[key]] as const
})

let possibleMatches = new Map<KeyBindingPress[], KeyBindingPress[]>()
let timer: any = null
let timer: NodeJS.Timeout | null = null

let onKeyDown = (event: KeyboardEvent) => {
let onKeyDown: EventListener = event => {
// Ensure and stop any event that isn't a full keyboard event.
// Autocomplete option navigation and selection would fire a instanceof Event,
// instead of the expected KeyboardEvent
Expand Down Expand Up @@ -146,12 +146,16 @@ export default function keybindings(
}
})

clearTimeout(timer)
if (timer) {
clearTimeout(timer)
}

timer = setTimeout(possibleMatches.clear.bind(possibleMatches), TIMEOUT)
}

target.addEventListener("keydown", onKeyDown as any)
target.addEventListener("keydown", onKeyDown)

return () => {
target.removeEventListener("keydown", onKeyDown as any)
target.removeEventListener("keydown", onKeyDown)
}
}

0 comments on commit fb277f8

Please sign in to comment.