tridactyl/src/keydown_background.ts
2018-04-13 19:28:03 +01:00

28 lines
744 B
TypeScript

// Interface: onKeydown.addListener(func)
//
import * as Messaging from "./messaging"
import { MsgSafeKeyboardEvent } from "./msgsafe"
type KeydownCallback = (keyevent: MsgSafeKeyboardEvent) => void
const listeners = new Set<KeydownCallback>()
function addListener(cb: KeydownCallback) {
listeners.add(cb)
return () => {
listeners.delete(cb)
}
}
export const onKeydown = { addListener }
// Receive events from content and pass to listeners
export function recvEvent(event: MsgSafeKeyboardEvent) {
for (let listener of listeners) {
listener(event)
}
}
// Get messages from content
import * as SELF from "./keydown_background"
Messaging.addListener("keydown_background", Messaging.attributeCaller(SELF))