@wooksjs/event-core 0.5.4 → 0.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +7 -13
- package/dist/index.d.ts +15 -15
- package/dist/index.mjs +8 -15
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -4,9 +4,8 @@ var crypto = require('crypto');
|
|
|
4
4
|
var node_async_hooks = require('node:async_hooks');
|
|
5
5
|
var logger = require('@prostojs/logger');
|
|
6
6
|
|
|
7
|
-
class Hookable
|
|
7
|
+
class Hookable {
|
|
8
8
|
constructor() {
|
|
9
|
-
super('hookable');
|
|
10
9
|
this.hooks = {};
|
|
11
10
|
}
|
|
12
11
|
hook(name, cb) {
|
|
@@ -22,7 +21,7 @@ class Hookable extends node_async_hooks.AsyncResource {
|
|
|
22
21
|
if (this.hooks[name]) {
|
|
23
22
|
for (const cb of this.hooks[name]) {
|
|
24
23
|
try {
|
|
25
|
-
|
|
24
|
+
cb(...args);
|
|
26
25
|
}
|
|
27
26
|
catch (error) {
|
|
28
27
|
console.error(`Error in hook ${name}:`, error);
|
|
@@ -72,17 +71,17 @@ function createAsyncEventContext(data) {
|
|
|
72
71
|
if (result instanceof Promise) {
|
|
73
72
|
result
|
|
74
73
|
.then(r => {
|
|
75
|
-
|
|
74
|
+
fireEndEvent(r);
|
|
76
75
|
return r;
|
|
77
76
|
})
|
|
78
77
|
.catch(error => {
|
|
79
|
-
|
|
78
|
+
fireEndEvent(error);
|
|
80
79
|
});
|
|
81
80
|
}
|
|
82
81
|
else {
|
|
83
|
-
|
|
82
|
+
fireEndEvent(result);
|
|
84
83
|
}
|
|
85
|
-
function
|
|
84
|
+
function fireEndEvent(output) {
|
|
86
85
|
if (!newContext._ended) {
|
|
87
86
|
if (output instanceof Error) {
|
|
88
87
|
asyncStorage.run(newContext, () => {
|
|
@@ -195,12 +194,6 @@ function _getCtxHelpers(cc) {
|
|
|
195
194
|
const hasParentCtx = () => !!cc.parentCtx;
|
|
196
195
|
return {
|
|
197
196
|
getCtx,
|
|
198
|
-
endEvent: (abortReason) => {
|
|
199
|
-
if (cc && !cc._ended) {
|
|
200
|
-
cc._ended = true;
|
|
201
|
-
eventContextHooks.fireEndEvent(cc.event.type, abortReason);
|
|
202
|
-
}
|
|
203
|
-
},
|
|
204
197
|
store,
|
|
205
198
|
getStore: get,
|
|
206
199
|
setStore: set,
|
|
@@ -268,6 +261,7 @@ function useRouteParams() {
|
|
|
268
261
|
}
|
|
269
262
|
|
|
270
263
|
exports.EventLogger = EventLogger;
|
|
264
|
+
exports.Hookable = Hookable;
|
|
271
265
|
exports.asyncStorage = asyncStorage;
|
|
272
266
|
exports.attachHook = attachHook;
|
|
273
267
|
exports.createAsyncEventContext = createAsyncEventContext;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as _prostojs_logger from '@prostojs/logger';
|
|
2
2
|
import { TProstoLoggerOptions, ProstoLogger } from '@prostojs/logger';
|
|
3
|
-
import { AsyncLocalStorage
|
|
3
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
4
4
|
|
|
5
5
|
declare function useEventId(): {
|
|
6
6
|
getId: () => string;
|
|
@@ -35,7 +35,6 @@ declare const asyncStorage: AsyncLocalStorage<unknown>;
|
|
|
35
35
|
declare function createAsyncEventContext<S = TEmpty, EventTypeToCreate = TEmpty>(data: S & TGenericContextStore<EventTypeToCreate>): <T>(cb: (...a: any[]) => T) => T;
|
|
36
36
|
declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTypes?: string | string[]): {
|
|
37
37
|
getCtx: () => S & TGenericContextStore<EventType>;
|
|
38
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
39
38
|
store: <K extends keyof S | keyof TGenericContextStore<EventType>>(key: K) => {
|
|
40
39
|
value: (S & TGenericContextStore<EventType>)[K];
|
|
41
40
|
hook: <K2 extends keyof Required<S & TGenericContextStore<EventType>>[K]>(key2: K2) => {
|
|
@@ -56,7 +55,6 @@ declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTy
|
|
|
56
55
|
hasParentCtx: () => boolean;
|
|
57
56
|
getParentCtx: <T2 = S & TGenericContextStore<EventType>>() => {
|
|
58
57
|
getCtx: () => T2;
|
|
59
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
60
58
|
store: <K_3 extends keyof T2>(key: K_3) => {
|
|
61
59
|
value: T2[K_3];
|
|
62
60
|
hook: <K2_6 extends keyof Required<T2>[K_3]>(key2: K2_6) => {
|
|
@@ -77,7 +75,6 @@ declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTy
|
|
|
77
75
|
hasParentCtx: () => boolean;
|
|
78
76
|
getParentCtx: <T2_1 = T2>() => {
|
|
79
77
|
getCtx: () => T2_1;
|
|
80
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
81
78
|
store: <K_6 extends keyof T2_1>(key: K_6) => {
|
|
82
79
|
value: T2_1[K_6];
|
|
83
80
|
hook: <K2_12 extends keyof Required<T2_1>[K_6]>(key2: K2_12) => {
|
|
@@ -98,7 +95,6 @@ declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTy
|
|
|
98
95
|
hasParentCtx: () => boolean;
|
|
99
96
|
getParentCtx: <T2_2 = T2_1>() => {
|
|
100
97
|
getCtx: () => T2_2;
|
|
101
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
102
98
|
store: <K_9 extends keyof T2_2>(key: K_9) => {
|
|
103
99
|
value: T2_2[K_9];
|
|
104
100
|
hook: <K2_18 extends keyof Required<T2_2>[K_9]>(key2: K2_18) => {
|
|
@@ -119,7 +115,6 @@ declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTy
|
|
|
119
115
|
hasParentCtx: () => boolean;
|
|
120
116
|
getParentCtx: <T2_3 = T2_2>() => {
|
|
121
117
|
getCtx: () => T2_3;
|
|
122
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
123
118
|
store: <K_12 extends keyof T2_3>(key: K_12) => {
|
|
124
119
|
value: T2_3[K_12];
|
|
125
120
|
hook: <K2_24 extends keyof Required<T2_3>[K_12]>(key2: K2_24) => {
|
|
@@ -140,7 +135,6 @@ declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTy
|
|
|
140
135
|
hasParentCtx: () => boolean;
|
|
141
136
|
getParentCtx: <T2_4 = T2_3>() => {
|
|
142
137
|
getCtx: () => T2_4;
|
|
143
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
144
138
|
store: <K_15 extends keyof T2_4>(key: K_15) => {
|
|
145
139
|
value: T2_4[K_15];
|
|
146
140
|
hook: <K2_30 extends keyof Required<T2_4>[K_15]>(key2: K2_30) => {
|
|
@@ -161,7 +155,6 @@ declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTy
|
|
|
161
155
|
hasParentCtx: () => boolean;
|
|
162
156
|
getParentCtx: <T2_5 = T2_4>() => {
|
|
163
157
|
getCtx: () => T2_5;
|
|
164
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
165
158
|
store: <K_18 extends keyof T2_5>(key: K_18) => {
|
|
166
159
|
value: T2_5[K_18];
|
|
167
160
|
hook: <K2_36 extends keyof Required<T2_5>[K_18]>(key2: K2_36) => {
|
|
@@ -182,7 +175,6 @@ declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTy
|
|
|
182
175
|
hasParentCtx: () => boolean;
|
|
183
176
|
getParentCtx: <T2_6 = T2_5>() => {
|
|
184
177
|
getCtx: () => T2_6;
|
|
185
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
186
178
|
store: <K_21 extends keyof T2_6>(key: K_21) => {
|
|
187
179
|
value: T2_6[K_21];
|
|
188
180
|
hook: <K2_42 extends keyof Required<T2_6>[K_21]>(key2: K2_42) => {
|
|
@@ -203,7 +195,6 @@ declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTy
|
|
|
203
195
|
hasParentCtx: () => boolean;
|
|
204
196
|
getParentCtx: <T2_7 = T2_6>() => {
|
|
205
197
|
getCtx: () => T2_7;
|
|
206
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
207
198
|
store: <K_24 extends keyof T2_7>(key: K_24) => {
|
|
208
199
|
value: T2_7[K_24];
|
|
209
200
|
hook: <K2_48 extends keyof Required<T2_7>[K_24]>(key2: K2_48) => {
|
|
@@ -224,7 +215,6 @@ declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTy
|
|
|
224
215
|
hasParentCtx: () => boolean;
|
|
225
216
|
getParentCtx: <T2_8 = T2_7>() => {
|
|
226
217
|
getCtx: () => T2_8;
|
|
227
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
228
218
|
store: <K_27 extends keyof T2_8>(key: K_27) => {
|
|
229
219
|
value: T2_8[K_27];
|
|
230
220
|
hook: <K2_54 extends keyof Required<T2_8>[K_27]>(key2: K2_54) => {
|
|
@@ -245,7 +235,6 @@ declare function useAsyncEventContext<S = TEmpty, EventType = TEmpty>(expectedTy
|
|
|
245
235
|
hasParentCtx: () => boolean;
|
|
246
236
|
getParentCtx: <T2_9 = T2_8>() => {
|
|
247
237
|
getCtx: () => T2_9;
|
|
248
|
-
endEvent: (abortReason?: string | undefined) => void;
|
|
249
238
|
store: <K_30 extends keyof T2_9>(key: K_30) => {
|
|
250
239
|
value: T2_9[K_30];
|
|
251
240
|
hook: <K2_60 extends keyof Required<T2_9>[K_30]>(key2: K2_60) => {
|
|
@@ -292,8 +281,7 @@ declare function useRouteParams<T extends object = Record<string, string | strin
|
|
|
292
281
|
};
|
|
293
282
|
|
|
294
283
|
type HookCallback = (...args: any[]) => any;
|
|
295
|
-
declare class Hookable
|
|
296
|
-
constructor();
|
|
284
|
+
declare class Hookable {
|
|
297
285
|
private hooks;
|
|
298
286
|
/**
|
|
299
287
|
* Registers a callback to a specific hook name.
|
|
@@ -316,9 +304,21 @@ declare class Hookable extends AsyncResource {
|
|
|
316
304
|
}
|
|
317
305
|
|
|
318
306
|
declare class EventContextHooks extends Hookable {
|
|
307
|
+
/**
|
|
308
|
+
* SHould be fired right after a new context is created
|
|
309
|
+
*/
|
|
319
310
|
fireStartEvent(eventType: string): void;
|
|
311
|
+
/**
|
|
312
|
+
* Should be fired at the very end of event processing
|
|
313
|
+
*/
|
|
320
314
|
fireEndEvent(eventType: string, abortReason?: string): void;
|
|
315
|
+
/**
|
|
316
|
+
* Fires when a new context was just created
|
|
317
|
+
*/
|
|
321
318
|
onStartEvent(cb: (eventType: string) => void): () => void;
|
|
319
|
+
/**
|
|
320
|
+
* Fires when event was processed
|
|
321
|
+
*/
|
|
322
322
|
onEndEvent(cb: (eventType: string, abortReason?: string) => void): () => void;
|
|
323
323
|
}
|
|
324
324
|
declare const eventContextHooks: EventContextHooks;
|
|
@@ -331,4 +331,4 @@ type THook<T = string, K extends PropertyKey = 'value'> = {
|
|
|
331
331
|
[key in K]: T;
|
|
332
332
|
};
|
|
333
333
|
|
|
334
|
-
export { EventLogger, type TEmpty, type TEventLoggerData, type TEventOptions, type TGenericContextStore, type TGenericEvent, type THook, asyncStorage, attachHook, createAsyncEventContext, eventContextHooks, useAsyncEventContext, useEventId, useEventLogger, useRouteParams };
|
|
334
|
+
export { EventLogger, Hookable, type TEmpty, type TEventLoggerData, type TEventOptions, type TGenericContextStore, type TGenericEvent, type THook, asyncStorage, attachHook, createAsyncEventContext, eventContextHooks, useAsyncEventContext, useEventId, useEventLogger, useRouteParams };
|
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { randomUUID } from 'crypto';
|
|
2
|
-
import {
|
|
2
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
3
|
import { ProstoLogger, createConsoleTransort, coloredConsole } from '@prostojs/logger';
|
|
4
4
|
|
|
5
|
-
class Hookable
|
|
5
|
+
class Hookable {
|
|
6
6
|
constructor() {
|
|
7
|
-
super('hookable');
|
|
8
7
|
this.hooks = {};
|
|
9
8
|
}
|
|
10
9
|
hook(name, cb) {
|
|
@@ -20,7 +19,7 @@ class Hookable extends AsyncResource {
|
|
|
20
19
|
if (this.hooks[name]) {
|
|
21
20
|
for (const cb of this.hooks[name]) {
|
|
22
21
|
try {
|
|
23
|
-
|
|
22
|
+
cb(...args);
|
|
24
23
|
}
|
|
25
24
|
catch (error) {
|
|
26
25
|
console.error(`Error in hook ${name}:`, error);
|
|
@@ -70,17 +69,17 @@ function createAsyncEventContext(data) {
|
|
|
70
69
|
if (result instanceof Promise) {
|
|
71
70
|
result
|
|
72
71
|
.then(r => {
|
|
73
|
-
|
|
72
|
+
fireEndEvent(r);
|
|
74
73
|
return r;
|
|
75
74
|
})
|
|
76
75
|
.catch(error => {
|
|
77
|
-
|
|
76
|
+
fireEndEvent(error);
|
|
78
77
|
});
|
|
79
78
|
}
|
|
80
79
|
else {
|
|
81
|
-
|
|
80
|
+
fireEndEvent(result);
|
|
82
81
|
}
|
|
83
|
-
function
|
|
82
|
+
function fireEndEvent(output) {
|
|
84
83
|
if (!newContext._ended) {
|
|
85
84
|
if (output instanceof Error) {
|
|
86
85
|
asyncStorage.run(newContext, () => {
|
|
@@ -193,12 +192,6 @@ function _getCtxHelpers(cc) {
|
|
|
193
192
|
const hasParentCtx = () => !!cc.parentCtx;
|
|
194
193
|
return {
|
|
195
194
|
getCtx,
|
|
196
|
-
endEvent: (abortReason) => {
|
|
197
|
-
if (cc && !cc._ended) {
|
|
198
|
-
cc._ended = true;
|
|
199
|
-
eventContextHooks.fireEndEvent(cc.event.type, abortReason);
|
|
200
|
-
}
|
|
201
|
-
},
|
|
202
195
|
store,
|
|
203
196
|
getStore: get,
|
|
204
197
|
setStore: set,
|
|
@@ -265,4 +258,4 @@ function useRouteParams() {
|
|
|
265
258
|
};
|
|
266
259
|
}
|
|
267
260
|
|
|
268
|
-
export { EventLogger, asyncStorage, attachHook, createAsyncEventContext, eventContextHooks, useAsyncEventContext, useEventId, useEventLogger, useRouteParams };
|
|
261
|
+
export { EventLogger, Hookable, asyncStorage, attachHook, createAsyncEventContext, eventContextHooks, useAsyncEventContext, useEventId, useEventLogger, useRouteParams };
|