Skip to content
This repository was archived by the owner on Mar 6, 2020. It is now read-only.

Commit b9f45ee

Browse files
committed
convert api-early hook to plugin
1 parent 315cba9 commit b9f45ee

4 files changed

Lines changed: 53 additions & 44 deletions

File tree

src/ExtPlug.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import PluginsCollection from './collections/PluginsCollection';
99
import Plugin from './Plugin';
1010
import * as pluginLoader from './pluginLoader';
1111

12+
import EarlyAPIEventsPlugin from './plugins/EarlyAPIEventsPlugin';
1213
import CommandsPlugin from './plugins/CommandsPlugin';
1314
import SettingsTabPlugin from './plugins/SettingsTabPlugin';
1415
import ChatTypePlugin from './plugins/ChatTypePlugin';
@@ -87,6 +88,7 @@ const ExtPlug = Plugin.extend({
8788
this._super('extplug', this);
8889

8990
this.corePlugins = [
91+
new EarlyAPIEventsPlugin('extplug:early-api', this),
9092
new CommandsPlugin('extplug:chat-commands', this),
9193
new SettingsTabPlugin('extplug:settings-tab', this),
9294
new MoreChatEventsPlugin('extplug:more-chat-events', this),

src/hooks/api-early.js

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/hooks/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import * as apiEarlyHook from './api-early';
21
import * as playbackHook from './playback';
32
import * as popoutStyleHook from './popout-style';
43

54
export default [
6-
apiEarlyHook,
75
playbackHook,
86
popoutStyleHook,
97
];
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import meld from 'meld';
2+
import Plugin from '../Plugin';
3+
4+
function nop() { return 'Dummy handler to ensure that plug.dj actually triggers the event'; }
5+
6+
// find default plug.dj API event names
7+
const eventKeys = Object.keys(API).filter(key =>
8+
key.toUpperCase() === key && typeof API[key] === 'string'
9+
);
10+
11+
const EarlyAPIEventsPlugin = Plugin.extend({
12+
name: 'Early API Events',
13+
description:
14+
'Adds "Early" API events. Handlers for these events will always be called ' +
15+
'before other handlers.',
16+
17+
enable() {
18+
this.advice = meld.around(API, 'dispatch', this.intercept);
19+
eventKeys.forEach(key => {
20+
// add the API constants for these, too
21+
API[`BEFORE_${key}`] = `before${API[key].charAt(0).toUpperCase()}${API[key].slice(1)}`;
22+
// plug.dj checks if an event is actually attached (through the _events hash)
23+
// before dispatching. We might run into situations where there is a BEFORE_
24+
// handler, but not a normal one, and we do need to get the BEFORE_ event to
25+
// trigger there. So we just pretend like we have handlers for all the things.
26+
API.on(API[key], nop);
27+
});
28+
},
29+
30+
disable() {
31+
eventKeys.forEach(key => {
32+
delete API[`BEFORE_${key}`];
33+
API.off(key, nop);
34+
});
35+
this.advice.remove();
36+
},
37+
38+
intercept(joinpoint) {
39+
const [eventName, ...params] = joinpoint.args;
40+
41+
API.trigger.apply(
42+
API,
43+
// userLeave → beforeUserLeave
44+
[`before${eventName.charAt(0).toUpperCase()}${eventName.slice(1)}`, ...params]
45+
);
46+
47+
return joinpoint.proceed();
48+
},
49+
});
50+
51+
export default EarlyAPIEventsPlugin;

0 commit comments

Comments
 (0)