-
Notifications
You must be signed in to change notification settings - Fork 16
/
key.ts
61 lines (52 loc) · 1.35 KB
/
key.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import {hyper, hyperShift} from './config';
import log from './logger';
const handlers: Map<string, Key> = new Map();
function onKey(
keys: Phoenix.KeyIdentifier | Phoenix.KeyIdentifier[],
mod: Phoenix.ModifierKey[],
cb: (handler: Key, repeated: boolean) => Promise<any> | any,
) {
if (Array.isArray(keys)) {
const unbinds = keys.map((key) => onKeySingle(key, mod, cb));
return () => unbinds.forEach((u) => u());
}
return onKeySingle(keys, mod, cb);
}
function onKeySingle(
key: Phoenix.KeyIdentifier,
mod: Phoenix.ModifierKey[],
cb: (handler: Key, repeated: boolean) => Promise<any> | any,
) {
const handler = new Key(key, mod, (handler: Key, repeated: boolean) => {
const notify = (e: any) => {
log.notify(`Key: ${key} + [${mod}]:`, e);
};
try {
const ret = cb(handler, repeated);
if (ret instanceof Promise) {
return ret.catch(notify);
}
return ret;
} catch (e) {
notify(e);
}
});
const id = createID(key, mod);
handlers.set(id, handler);
return () => unbind(id);
}
function unbind(id: string) {
const handler = handlers.get(id);
if (handler) {
handler.disable();
handlers.delete(id);
}
}
function createID(key: string, mod: string[]) {
return key + mod.sort().join();
}
function getHandler(key: string, mod: string[]) {
const id = createID(key, mod);
return handlers.get(id);
}
export {onKey, getHandler};