-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathindex.ts
More file actions
294 lines (235 loc) · 9.12 KB
/
index.ts
File metadata and controls
294 lines (235 loc) · 9.12 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
const API_NAME = "lifecycle-hooks";
import { getGlobal, registerGlobal, unregisterGlobal } from "../utils/globals.js";
import { NoopLifecycleHooksManager } from "./manager.js";
import {
AnyOnCatchErrorHookFunction,
AnyOnCleanupHookFunction,
AnyOnCompleteHookFunction,
AnyOnFailureHookFunction,
AnyOnInitHookFunction,
AnyOnMiddlewareHookFunction,
AnyOnResumeHookFunction,
AnyOnStartHookFunction,
AnyOnSuccessHookFunction,
AnyOnWaitHookFunction,
AnyOnCancelHookFunction,
RegisteredHookFunction,
RegisterHookFunctionParams,
TaskWait,
type LifecycleHooksManager,
} from "./types.js";
const NOOP_LIFECYCLE_HOOKS_MANAGER = new NoopLifecycleHooksManager();
export class LifecycleHooksAPI {
private static _instance?: LifecycleHooksAPI;
private constructor() {}
public static getInstance(): LifecycleHooksAPI {
if (!this._instance) {
this._instance = new LifecycleHooksAPI();
}
return this._instance;
}
public setGlobalLifecycleHooksManager(lifecycleHooksManager: LifecycleHooksManager): boolean {
return registerGlobal(API_NAME, lifecycleHooksManager);
}
public disable() {
unregisterGlobal(API_NAME);
}
public registerGlobalInitHook(hook: RegisterHookFunctionParams<AnyOnInitHookFunction>): void {
this.#getManager().registerGlobalInitHook(hook);
}
public registerTaskInitHook(
taskId: string,
hook: RegisterHookFunctionParams<AnyOnInitHookFunction>
): void {
this.#getManager().registerTaskInitHook(taskId, hook);
}
public getTaskInitHook(taskId: string): AnyOnInitHookFunction | undefined {
return this.#getManager().getTaskInitHook(taskId);
}
public getGlobalInitHooks(): RegisteredHookFunction<AnyOnInitHookFunction>[] {
return this.#getManager().getGlobalInitHooks();
}
public registerTaskStartHook(
taskId: string,
hook: RegisterHookFunctionParams<AnyOnStartHookFunction>
): void {
this.#getManager().registerTaskStartHook(taskId, hook);
}
public registerGlobalStartHook(hook: RegisterHookFunctionParams<AnyOnStartHookFunction>): void {
this.#getManager().registerGlobalStartHook(hook);
}
public getTaskStartHook(taskId: string): AnyOnStartHookFunction | undefined {
return this.#getManager().getTaskStartHook(taskId);
}
public getGlobalStartHooks(): RegisteredHookFunction<AnyOnStartHookFunction>[] {
return this.#getManager().getGlobalStartHooks();
}
public registerGlobalFailureHook(
hook: RegisterHookFunctionParams<AnyOnFailureHookFunction>
): void {
this.#getManager().registerGlobalFailureHook(hook);
}
public registerTaskFailureHook(
taskId: string,
hook: RegisterHookFunctionParams<AnyOnFailureHookFunction>
): void {
this.#getManager().registerTaskFailureHook(taskId, hook);
}
public getTaskFailureHook(taskId: string): AnyOnFailureHookFunction | undefined {
return this.#getManager().getTaskFailureHook(taskId);
}
public getGlobalFailureHooks(): RegisteredHookFunction<AnyOnFailureHookFunction>[] {
return this.#getManager().getGlobalFailureHooks();
}
public registerGlobalSuccessHook(
hook: RegisterHookFunctionParams<AnyOnSuccessHookFunction>
): void {
this.#getManager().registerGlobalSuccessHook(hook);
}
public registerTaskSuccessHook(
taskId: string,
hook: RegisterHookFunctionParams<AnyOnSuccessHookFunction>
): void {
this.#getManager().registerTaskSuccessHook(taskId, hook);
}
public getTaskSuccessHook(taskId: string): AnyOnSuccessHookFunction | undefined {
return this.#getManager().getTaskSuccessHook(taskId);
}
public getGlobalSuccessHooks(): RegisteredHookFunction<AnyOnSuccessHookFunction>[] {
return this.#getManager().getGlobalSuccessHooks();
}
public registerGlobalCompleteHook(
hook: RegisterHookFunctionParams<AnyOnCompleteHookFunction>
): void {
this.#getManager().registerGlobalCompleteHook(hook);
}
public registerTaskCompleteHook(
taskId: string,
hook: RegisterHookFunctionParams<AnyOnCompleteHookFunction>
): void {
this.#getManager().registerTaskCompleteHook(taskId, hook);
}
public getTaskCompleteHook(taskId: string): AnyOnCompleteHookFunction | undefined {
return this.#getManager().getTaskCompleteHook(taskId);
}
public getGlobalCompleteHooks(): RegisteredHookFunction<AnyOnCompleteHookFunction>[] {
return this.#getManager().getGlobalCompleteHooks();
}
public registerGlobalWaitHook(hook: RegisterHookFunctionParams<AnyOnWaitHookFunction>): void {
this.#getManager().registerGlobalWaitHook(hook);
}
public registerTaskWaitHook(
taskId: string,
hook: RegisterHookFunctionParams<AnyOnWaitHookFunction>
): void {
this.#getManager().registerTaskWaitHook(taskId, hook);
}
public getTaskWaitHook(taskId: string): AnyOnWaitHookFunction | undefined {
return this.#getManager().getTaskWaitHook(taskId);
}
public getGlobalWaitHooks(): RegisteredHookFunction<AnyOnWaitHookFunction>[] {
return this.#getManager().getGlobalWaitHooks();
}
public registerGlobalResumeHook(hook: RegisterHookFunctionParams<AnyOnResumeHookFunction>): void {
this.#getManager().registerGlobalResumeHook(hook);
}
public registerTaskResumeHook(
taskId: string,
hook: RegisterHookFunctionParams<AnyOnResumeHookFunction>
): void {
this.#getManager().registerTaskResumeHook(taskId, hook);
}
public getTaskResumeHook(taskId: string): AnyOnResumeHookFunction | undefined {
return this.#getManager().getTaskResumeHook(taskId);
}
public getGlobalResumeHooks(): RegisteredHookFunction<AnyOnResumeHookFunction>[] {
return this.#getManager().getGlobalResumeHooks();
}
public registerGlobalCatchErrorHook(
hook: RegisterHookFunctionParams<AnyOnCatchErrorHookFunction>
): void {
this.#getManager().registerGlobalCatchErrorHook(hook);
}
public registerTaskCatchErrorHook(
taskId: string,
hook: RegisterHookFunctionParams<AnyOnCatchErrorHookFunction>
): void {
this.#getManager().registerTaskCatchErrorHook(taskId, hook);
}
public getTaskCatchErrorHook(taskId: string): AnyOnCatchErrorHookFunction | undefined {
return this.#getManager().getTaskCatchErrorHook(taskId);
}
public getGlobalCatchErrorHooks(): RegisteredHookFunction<AnyOnCatchErrorHookFunction>[] {
return this.#getManager().getGlobalCatchErrorHooks();
}
public registerGlobalMiddlewareHook(
hook: RegisterHookFunctionParams<AnyOnMiddlewareHookFunction>
): void {
this.#getManager().registerGlobalMiddlewareHook(hook);
}
public registerTaskMiddlewareHook(
taskId: string,
hook: RegisterHookFunctionParams<AnyOnMiddlewareHookFunction>
): void {
this.#getManager().registerTaskMiddlewareHook(taskId, hook);
}
public getTaskMiddlewareHook(taskId: string): AnyOnMiddlewareHookFunction | undefined {
return this.#getManager().getTaskMiddlewareHook(taskId);
}
public getGlobalMiddlewareHooks(): RegisteredHookFunction<AnyOnMiddlewareHookFunction>[] {
return this.#getManager().getGlobalMiddlewareHooks();
}
public registerGlobalCleanupHook(
hook: RegisterHookFunctionParams<AnyOnCleanupHookFunction>
): void {
this.#getManager().registerGlobalCleanupHook(hook);
}
public registerTaskCleanupHook(
taskId: string,
hook: RegisterHookFunctionParams<AnyOnCleanupHookFunction>
): void {
this.#getManager().registerTaskCleanupHook(taskId, hook);
}
public getTaskCleanupHook(taskId: string): AnyOnCleanupHookFunction | undefined {
return this.#getManager().getTaskCleanupHook(taskId);
}
public getGlobalCleanupHooks(): RegisteredHookFunction<AnyOnCleanupHookFunction>[] {
return this.#getManager().getGlobalCleanupHooks();
}
public callOnWaitHookListeners(wait: TaskWait): Promise<void> {
return this.#getManager().callOnWaitHookListeners(wait);
}
public callOnResumeHookListeners(wait: TaskWait): Promise<void> {
return this.#getManager().callOnResumeHookListeners(wait);
}
public registerOnWaitHookListener(listener: (wait: TaskWait) => Promise<void>): void {
this.#getManager().registerOnWaitHookListener(listener);
}
public registerOnResumeHookListener(listener: (wait: TaskWait) => Promise<void>): void {
this.#getManager().registerOnResumeHookListener(listener);
}
public registerGlobalCancelHook(hook: RegisterHookFunctionParams<AnyOnCancelHookFunction>): void {
this.#getManager().registerGlobalCancelHook(hook);
}
public registerTaskCancelHook(
taskId: string,
hook: RegisterHookFunctionParams<AnyOnCancelHookFunction>
): void {
this.#getManager().registerTaskCancelHook(taskId, hook);
}
public getTaskCancelHook(taskId: string): AnyOnCancelHookFunction | undefined {
return this.#getManager().getTaskCancelHook(taskId);
}
public getGlobalCancelHooks(): RegisteredHookFunction<AnyOnCancelHookFunction>[] {
return this.#getManager().getGlobalCancelHooks();
}
public callOnCancelHookListeners(): Promise<void> {
return this.#getManager().callOnCancelHookListeners();
}
public registerOnCancelHookListener(listener: () => Promise<void>): void {
this.#getManager().registerOnCancelHookListener(listener);
}
#getManager(): LifecycleHooksManager {
return getGlobal(API_NAME) ?? NOOP_LIFECYCLE_HOOKS_MANAGER;
}
}