-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathevents.js
More file actions
77 lines (69 loc) · 2.26 KB
/
events.js
File metadata and controls
77 lines (69 loc) · 2.26 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
67
68
69
70
71
72
73
74
75
76
77
import eventManager from 'src/utils/eventManager.js';
import wrap from 'src/utils/2.pokemon.js';
import { registerModule } from 'src/utils/plugin.js';
import { capturePluginError } from 'src/utils/sentry.js';
import compoundEvent from 'src/utils/compoundEvent.js';
wrap(() => {
const options = ['cancelable', 'canceled', 'singleton', 'async'];
const name = 'events';
function mod(plugin) {
function log(error, event, args, meta) {
capturePluginError(plugin, error, {
args,
...meta,
});
plugin.logger.error(`Event error (${event}):\n`, error, '\n', JSON.stringify({
args,
event: meta,
}));
}
function wrapper(fn, event) {
function listener(...args) {
try {
const val = fn.call(this, ...args);
if (val instanceof Promise) {
return val.catch((error) => log(error, event, args, this));
}
return val;
} catch (e) {
log(e, event, args, this);
}
return undefined;
}
listener.plugin = plugin;
return listener;
}
const obj = {
...eventManager,
compound(...events) {
const fn = events.pop();
if (typeof fn !== 'function') throw new Error('Must pass a function');
if (!events.length) throw new Error('Must pass events');
if (events.length === 1) throw new Error('Use `events.on` for single events');
compoundEvent(...events, wrapper(fn, `Compound[${events.join(';')}]`));
},
on(event, fn) {
if (typeof fn !== 'function') throw new Error('Must pass a function');
if (event.split(' ').includes(':loaded')) {
plugin.logger.warn('Event manager: `:loaded` is deprecated, ask author to update to `:preload`!');
}
eventManager.on.call(obj, event, wrapper(fn, event));
},
emit(...args) {
return eventManager.emit(...args);
},
};
options.forEach((key) => {
Object.defineProperty(obj.emit, key, {
get: () => {
// Toggle the event manager
eventManager[key]; // eslint-disable-line no-unused-expressions
// Return our object
return obj.emit;
},
});
});
return Object.freeze(obj);
}
registerModule(name, mod);
});