-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhotkey.js
More file actions
66 lines (58 loc) · 1.53 KB
/
hotkey.js
File metadata and controls
66 lines (58 loc) · 1.53 KB
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
62
63
64
65
66
import wrap from 'src/utils/2.pokemon.js';
import { registerModule } from 'src/utils/plugin.js';
import Hotkey from 'src/utils/hotkey.class.js';
import { hotkeys } from 'src/utils/1.variables.js';
import { capturePluginError } from 'src/utils/sentry.js';
class PluginHotkey extends Hotkey {
constructor(plugin, hotkey) {
super();
this.plugin = plugin;
this.hotkey = hotkey;
}
get name() {
return `[${this.hotkey.name}]`;
}
get keys() {
return this.hotkey.keys;
}
get clicks() {
return this.hotkey.clicks;
}
run(...args) {
try {
return this.hotkey.run(...args);
} catch (e) {
capturePluginError(this.plugin, e, {
hotkey: this.name,
});
this.plugin.logger.error(`Hotkey${this.name || ''}${e instanceof Error ? '' : ' Error:'}`, e);
}
return undefined;
}
}
wrap(() => {
const name = 'hotkey';
function mod(plugin) {
const registry = new Map();
return {
register(hotkey) {
validate(hotkey);
if (registry.has(hotkey)) return;
const wrapper = new PluginHotkey(plugin, hotkey);
registry.set(hotkey, wrapper);
hotkeys.push(wrapper);
},
unregister(hotkey) {
validate(hotkey);
const wrapper = registry.get(hotkey);
if (!wrapper) return;
registry.delete(hotkey);
hotkeys.splice(hotkeys.indexOf(wrapper), 1);
},
};
}
registerModule(name, mod);
});
function validate(hotkey) {
if (!(hotkey instanceof Hotkey)) throw new Error('Not valid hotkey');
}