Skip to content

Commit 8f47402

Browse files
committed
feat(plugin): add support for compound events
1 parent aa7412e commit 8f47402

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

src/base/api/utils.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import some from '../../utils/some.js';
1212
import TabManager from '../../utils/tabbedView.js';
1313
import rand from '../../utils/rand.js';
1414
import SettingType from '../../utils/settings/types/setting.js';
15-
import compound from '../../utils/compoundEvent.js';
1615

1716
const utils = api.mod.utils;
1817

@@ -39,4 +38,3 @@ utils.tabManager = TabManager;
3938
utils.VarStore = VarStore;
4039
utils.rand = rand;
4140
utils.SettingType = SettingType;
42-
utils.compoundEvent = compound;

src/base/plugin/events.js

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,49 @@ import eventManager from '../../utils/eventManager.js';
22
import wrap from '../../utils/2.pokemon.js';
33
import { registerModule } from '../../utils/plugin.js';
44
import { capturePluginError } from '../../utils/sentry.js';
5+
import compoundEvent from '../../utils/compoundEvent.js';
56

67
wrap(() => {
78
const options = ['cancelable', 'canceled', 'singleton', 'async'];
89

910
const name = 'events';
1011
function mod(plugin) {
12+
function wrapper(fn, event) {
13+
function listener(...args) {
14+
try {
15+
fn.call(this, ...args);
16+
} catch (e) {
17+
capturePluginError(plugin, e, {
18+
args,
19+
...this,
20+
});
21+
plugin.logger.error(`Event error (${event}):\n`, e, '\n', JSON.stringify({
22+
args,
23+
event: this,
24+
}));
25+
}
26+
}
27+
listener.plugin = plugin;
28+
return listener;
29+
}
1130
const obj = {
1231
...eventManager,
13-
on(event = '', fn) {
32+
compound(...events) {
33+
const fn = events.pop();
1434
if (typeof fn !== 'function') throw new Error('Must pass a function');
35+
if (!events.length) throw new Error('Must pass events');
36+
if (events.length === 1) throw new Error('Use `events.on` for single events');
1537

16-
function pluginListener(...args) {
17-
try {
18-
fn.call(this, ...args);
19-
} catch (e) {
20-
capturePluginError(plugin, e, {
21-
args,
22-
...this,
23-
});
24-
plugin.logger.error(`Event error (${event}):\n`, e, '\n', JSON.stringify({
25-
args,
26-
event: this,
27-
}));
28-
}
29-
}
30-
pluginListener.plugin = plugin;
38+
compoundEvent(...events, wrapper(fn, `Compound[${events.join(';')}]`));
39+
},
40+
on(event, fn) {
41+
if (typeof fn !== 'function') throw new Error('Must pass a function');
3142

3243
if (event.split(' ').includes(':loaded')) {
3344
plugin.logger.warn('Event manager: `:loaded` is deprecated, ask author to update to `:preload`!');
3445
}
3546

36-
eventManager.on.call(obj, event, pluginListener);
47+
eventManager.on.call(obj, event, wrapper(fn, event));
3748
},
3849
emit(...args) {
3950
return eventManager.emit(...args);

src/utils/compoundEvent.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ export default function compound(...events) {
88
let triggered = 0;
99
// TODO: cache data
1010
function trigger(event, ...data) {
11-
if (cache[event].triggered) return;
12-
cache[event].triggered = this.singleton ? 'singleton' : true;
13-
triggered += 1;
11+
if (!cache[event].triggered) {
12+
cache[event].triggered = this.singleton ? 'singleton' : true;
13+
triggered += 1;
14+
}
1415

15-
if (triggered === events.length) {
16+
if (triggered >= events.length) {
17+
console.log('triggered');
1618
events.forEach((ev) => {
1719
const e = cache[ev];
1820
// Only reset if not singleton

0 commit comments

Comments
 (0)