-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.d.ts
106 lines (88 loc) · 2.68 KB
/
index.d.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { Tail, Reverse } from "typescript-tuple";
type Fn = (...args: any) => any;
type RemoveLast<T extends any[]> = Reverse<Tail<Reverse<T>>>;
type Last<T extends any[]> = T[Exclude<keyof T, keyof Tail<T>>];
type LastParameter<F extends (...args: any) => any> = Last<Parameters<F>>;
export interface BeforeSync<T extends Fn> {
(...args: Parameters<T>): void;
bail: (returnValue: ReturnType<T>) => void;
}
export interface BeforeAsync<T extends Fn, Cb extends Fn> {
(...args: RemoveLast<Parameters<T>>): void;
bail: (...args: Parameters<Cb>) => void;
}
export interface BeforeAsyncNoCallback<T extends Fn> {
(...args: Parameters<T>): void;
}
export interface AfterSync<T extends Fn> {
(returnValue: ReturnType<T>): void;
bail: (returnValue: ReturnType<T>) => void;
}
export interface AfterAsync<T extends Fn> {
(...args: Parameters<T>): void;
bail: (...args: Parameters<T>) => void;
}
export interface HookGetter {
getHooks(
matcher?:
| {
type: "before" | "after";
}
| {
hook: Fn;
}
): Array<Fn> & {
remove(): void;
};
removeAll(): void;
}
export type SyncHook<T extends Fn> = T &
HookGetter & {
before: (
hook: (next: BeforeSync<T>, ...args: Parameters<T>) => void,
priority?: number
) => SyncHook<T>;
after: (
hook: (next: AfterSync<T>, returnValue: ReturnType<T>) => void,
priority?: number
) => SyncHook<T>;
};
export type AsyncHook<T extends Fn> = LastParameter<T> extends Fn
? AsyncHookCallback<T, LastParameter<T>>
: AsyncHookNoCallback<T>;
export type AsyncHookCallback<T extends Fn, Cb extends Fn> = T &
HookGetter & {
before: (
hook: (
next: BeforeAsync<T, Cb>,
...args: RemoveLast<Parameters<T>>
) => void,
priority?: number
) => AsyncHookCallback<T, Cb>;
after: (
hook: (next: AfterAsync<Cb>, ...args: Parameters<Cb>) => void,
priority?: number
) => SyncHook<T>;
};
export type AsyncHookNoCallback<T extends Fn> = T &
HookGetter & {
before: (
hook: (next: BeforeAsyncNoCallback<T>, ...args: Parameters<T>) => void,
priority?: number
) => AsyncHookNoCallback<T>;
};
export interface CreateHook {
<T extends Fn>(fn: T): SyncHook<T>;
<T extends Fn>(type: "sync", fn: T, name?: string): SyncHook<T>;
<T extends Fn>(type: "async", fn: T, name?: string): AsyncHook<T>;
<Hooks extends {[key: string]: any}, T extends object = object>(obj: T, props: string[], name?: string): Hooks;
get<T>(name: string): T;
}
export interface HooksFactory {
(config?: { ready?: number }): CreateHook;
SYNC: 1;
ASYNC: 2;
QUEUE: 4;
}
declare const funHooks: HooksFactory;
export default funHooks;