1 | type FixedSizeArray<T extends number, U> = T extends 0
|
---|
2 | ? void[]
|
---|
3 | : ReadonlyArray<U> & {
|
---|
4 | 0: U;
|
---|
5 | length: T;
|
---|
6 | };
|
---|
7 | type Measure<T extends number> = T extends 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8
|
---|
8 | ? T
|
---|
9 | : never;
|
---|
10 | type Append<T extends any[], U> = {
|
---|
11 | 0: [U];
|
---|
12 | 1: [T[0], U];
|
---|
13 | 2: [T[0], T[1], U];
|
---|
14 | 3: [T[0], T[1], T[2], U];
|
---|
15 | 4: [T[0], T[1], T[2], T[3], U];
|
---|
16 | 5: [T[0], T[1], T[2], T[3], T[4], U];
|
---|
17 | 6: [T[0], T[1], T[2], T[3], T[4], T[5], U];
|
---|
18 | 7: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], U];
|
---|
19 | 8: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], T[7], U];
|
---|
20 | }[Measure<T["length"]>];
|
---|
21 | type AsArray<T> = T extends any[] ? T : [T];
|
---|
22 |
|
---|
23 | declare class UnsetAdditionalOptions {
|
---|
24 | _UnsetAdditionalOptions: true
|
---|
25 | }
|
---|
26 | type IfSet<X> = X extends UnsetAdditionalOptions ? {} : X;
|
---|
27 |
|
---|
28 | type Callback<E, T> = (error: E | null, result?: T) => void;
|
---|
29 | type InnerCallback<E, T> = (error?: E | null | false, result?: T) => void;
|
---|
30 |
|
---|
31 | type FullTap = Tap & {
|
---|
32 | type: "sync" | "async" | "promise",
|
---|
33 | fn: Function
|
---|
34 | }
|
---|
35 |
|
---|
36 | type Tap = TapOptions & {
|
---|
37 | name: string;
|
---|
38 | };
|
---|
39 |
|
---|
40 | type TapOptions = {
|
---|
41 | before?: string;
|
---|
42 | stage?: number;
|
---|
43 | };
|
---|
44 |
|
---|
45 | interface HookInterceptor<T, R, AdditionalOptions = UnsetAdditionalOptions> {
|
---|
46 | name?: string;
|
---|
47 | tap?: (tap: FullTap & IfSet<AdditionalOptions>) => void;
|
---|
48 | call?: (...args: any[]) => void;
|
---|
49 | loop?: (...args: any[]) => void;
|
---|
50 | error?: (err: Error) => void;
|
---|
51 | result?: (result: R) => void;
|
---|
52 | done?: () => void;
|
---|
53 | register?: (tap: FullTap & IfSet<AdditionalOptions>) => FullTap & IfSet<AdditionalOptions>;
|
---|
54 | }
|
---|
55 |
|
---|
56 | type ArgumentNames<T extends any[]> = FixedSizeArray<T["length"], string>;
|
---|
57 |
|
---|
58 | declare class Hook<T, R, AdditionalOptions = UnsetAdditionalOptions> {
|
---|
59 | constructor(args?: ArgumentNames<AsArray<T>>, name?: string);
|
---|
60 | name: string | undefined;
|
---|
61 | taps: FullTap[];
|
---|
62 | intercept(interceptor: HookInterceptor<T, R, AdditionalOptions>): void;
|
---|
63 | isUsed(): boolean;
|
---|
64 | callAsync(...args: Append<AsArray<T>, Callback<Error, R>>): void;
|
---|
65 | promise(...args: AsArray<T>): Promise<R>;
|
---|
66 | tap(options: string | Tap & IfSet<AdditionalOptions>, fn: (...args: AsArray<T>) => R): void;
|
---|
67 | withOptions(options: TapOptions & IfSet<AdditionalOptions>): Omit<this, "call" | "callAsync" | "promise">;
|
---|
68 | }
|
---|
69 |
|
---|
70 | export class SyncHook<T, R = void, AdditionalOptions = UnsetAdditionalOptions> extends Hook<T, R, AdditionalOptions> {
|
---|
71 | call(...args: AsArray<T>): R;
|
---|
72 | }
|
---|
73 |
|
---|
74 | export class SyncBailHook<T, R, AdditionalOptions = UnsetAdditionalOptions> extends SyncHook<T, R, AdditionalOptions> {}
|
---|
75 | export class SyncLoopHook<T, AdditionalOptions = UnsetAdditionalOptions> extends SyncHook<T, void, AdditionalOptions> {}
|
---|
76 | export class SyncWaterfallHook<T, AdditionalOptions = UnsetAdditionalOptions> extends SyncHook<T, AsArray<T>[0], AdditionalOptions> {}
|
---|
77 |
|
---|
78 | declare class AsyncHook<T, R, AdditionalOptions = UnsetAdditionalOptions> extends Hook<T, R, AdditionalOptions> {
|
---|
79 | tapAsync(
|
---|
80 | options: string | Tap & IfSet<AdditionalOptions>,
|
---|
81 | fn: (...args: Append<AsArray<T>, InnerCallback<Error, R>>) => void
|
---|
82 | ): void;
|
---|
83 | tapPromise(
|
---|
84 | options: string | Tap & IfSet<AdditionalOptions>,
|
---|
85 | fn: (...args: AsArray<T>) => Promise<R>
|
---|
86 | ): void;
|
---|
87 | }
|
---|
88 |
|
---|
89 | export class AsyncParallelHook<T, AdditionalOptions = UnsetAdditionalOptions> extends AsyncHook<T, void, AdditionalOptions> {}
|
---|
90 | export class AsyncParallelBailHook<T, R, AdditionalOptions = UnsetAdditionalOptions> extends AsyncHook<T, R, AdditionalOptions> {}
|
---|
91 | export class AsyncSeriesHook<T, AdditionalOptions = UnsetAdditionalOptions> extends AsyncHook<T, void, AdditionalOptions> {}
|
---|
92 | export class AsyncSeriesBailHook<T, R, AdditionalOptions = UnsetAdditionalOptions> extends AsyncHook<T, R, AdditionalOptions> {}
|
---|
93 | export class AsyncSeriesLoopHook<T, AdditionalOptions = UnsetAdditionalOptions> extends AsyncHook<T, void, AdditionalOptions> {}
|
---|
94 | export class AsyncSeriesWaterfallHook<T, AdditionalOptions = UnsetAdditionalOptions> extends AsyncHook<T, AsArray<T>[0], AdditionalOptions> {}
|
---|
95 |
|
---|
96 | type HookFactory<H> = (key: any, hook?: H) => H;
|
---|
97 |
|
---|
98 | interface HookMapInterceptor<H> {
|
---|
99 | factory?: HookFactory<H>;
|
---|
100 | }
|
---|
101 |
|
---|
102 | export class HookMap<H> {
|
---|
103 | constructor(factory: HookFactory<H>, name?: string);
|
---|
104 | name: string | undefined;
|
---|
105 | get(key: any): H | undefined;
|
---|
106 | for(key: any): H;
|
---|
107 | intercept(interceptor: HookMapInterceptor<H>): void;
|
---|
108 | }
|
---|
109 |
|
---|
110 | export class MultiHook<H> {
|
---|
111 | constructor(hooks: H[], name?: string);
|
---|
112 | name: string | undefined;
|
---|
113 | tap(options: string | Tap, fn?: Function): void;
|
---|
114 | tapAsync(options: string | Tap, fn?: Function): void;
|
---|
115 | tapPromise(options: string | Tap, fn?: Function): void;
|
---|
116 | }
|
---|