-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathon.ts
53 lines (47 loc) · 1.27 KB
/
on.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
import { emitterEventNames } from "../generated/webhook-names";
import {
EmitterWebhookEvent,
EmitterWebhookEventName,
State,
WebhookEventHandlerError,
} from "../types";
function handleEventHandlers(
state: State,
webhookName: EmitterWebhookEventName | "error" | "*",
handler: Function
) {
if (!state.hooks[webhookName]) {
state.hooks[webhookName] = [];
}
state.hooks[webhookName].push(handler);
}
export function receiverOn(
state: State,
webhookNameOrNames: EmitterWebhookEventName | EmitterWebhookEventName[],
handler: Function
) {
if (Array.isArray(webhookNameOrNames)) {
webhookNameOrNames.forEach((webhookName) =>
receiverOn(state, webhookName, handler)
);
return;
}
if (emitterEventNames.indexOf(webhookNameOrNames) === -1) {
console.warn(
`"${webhookNameOrNames}" is not a known webhook name (https://developer.github.com/v3/activity/events/types/)`
);
}
handleEventHandlers(state, webhookNameOrNames, handler);
}
export function receiverOnAny(
state: State,
handler: (event: EmitterWebhookEvent) => any
) {
handleEventHandlers(state, "*", handler);
}
export function receiverOnError(
state: State,
handler: (event: WebhookEventHandlerError) => any
) {
handleEventHandlers(state, "error", handler);
}