@wooksjs/event-core 0.5.1 → 0.5.3
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 +36 -4
- package/dist/index.d.ts +27 -4
- package/dist/index.mjs +36 -4
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -2,10 +2,42 @@
|
|
|
2
2
|
|
|
3
3
|
var crypto = require('crypto');
|
|
4
4
|
var node_async_hooks = require('node:async_hooks');
|
|
5
|
-
var hookable = require('hookable');
|
|
6
5
|
var logger = require('@prostojs/logger');
|
|
7
6
|
|
|
8
|
-
class
|
|
7
|
+
class Hookable extends node_async_hooks.AsyncResource {
|
|
8
|
+
constructor() {
|
|
9
|
+
super('hookable');
|
|
10
|
+
this.hooks = {};
|
|
11
|
+
}
|
|
12
|
+
hook(name, cb) {
|
|
13
|
+
if (!this.hooks[name]) {
|
|
14
|
+
this.hooks[name] = [];
|
|
15
|
+
}
|
|
16
|
+
this.hooks[name].push(cb);
|
|
17
|
+
return () => {
|
|
18
|
+
this.unhook(name, cb);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
callHook(name, ...args) {
|
|
22
|
+
if (this.hooks[name]) {
|
|
23
|
+
for (const cb of this.hooks[name]) {
|
|
24
|
+
try {
|
|
25
|
+
this.runInAsyncScope(cb, null, ...args);
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
console.error(`Error in hook ${name}:`, error);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
unhook(name, cb) {
|
|
34
|
+
if (this.hooks[name]) {
|
|
35
|
+
this.hooks[name] = this.hooks[name].filter(hookCb => hookCb !== cb);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
class EventContextHooks extends Hookable {
|
|
9
41
|
fireStartEvent(eventType) {
|
|
10
42
|
this.callHook('start-event', eventType);
|
|
11
43
|
}
|
|
@@ -13,10 +45,10 @@ class EventContextHooks extends hookable.Hookable {
|
|
|
13
45
|
this.callHook('end-event', eventType, abortReason);
|
|
14
46
|
}
|
|
15
47
|
onStartEvent(cb) {
|
|
16
|
-
this.hook('start-event', cb);
|
|
48
|
+
return this.hook('start-event', cb);
|
|
17
49
|
}
|
|
18
50
|
onEndEvent(cb) {
|
|
19
|
-
this.hook('end-event', cb);
|
|
51
|
+
return this.hook('end-event', cb);
|
|
20
52
|
}
|
|
21
53
|
}
|
|
22
54
|
const eventContextHooks = new EventContextHooks();
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import * as _prostojs_logger from '@prostojs/logger';
|
|
2
2
|
import { TProstoLoggerOptions, ProstoLogger } from '@prostojs/logger';
|
|
3
|
-
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
4
|
-
import { Hookable } from 'hookable';
|
|
3
|
+
import { AsyncLocalStorage, AsyncResource } from 'node:async_hooks';
|
|
5
4
|
|
|
6
5
|
declare function useEventId(): {
|
|
7
6
|
getId: () => string;
|
|
@@ -292,11 +291,35 @@ declare function useRouteParams<T extends object = Record<string, string | strin
|
|
|
292
291
|
get: <K extends keyof T>(name: K) => T[K];
|
|
293
292
|
};
|
|
294
293
|
|
|
294
|
+
type HookCallback = (...args: any[]) => any;
|
|
295
|
+
declare class Hookable extends AsyncResource {
|
|
296
|
+
constructor();
|
|
297
|
+
private hooks;
|
|
298
|
+
/**
|
|
299
|
+
* Registers a callback to a specific hook name.
|
|
300
|
+
* @param name - The name of the hook.
|
|
301
|
+
* @param cb - The callback to register.
|
|
302
|
+
*/
|
|
303
|
+
hook(name: string, cb: HookCallback): () => void;
|
|
304
|
+
/**
|
|
305
|
+
* Calls all callbacks registered to the specified hook name.
|
|
306
|
+
* @param name - The name of the hook.
|
|
307
|
+
* @param args - The arguments to pass to each callback.
|
|
308
|
+
*/
|
|
309
|
+
callHook(name: string, ...args: any[]): void;
|
|
310
|
+
/**
|
|
311
|
+
* Unregisters a specific callback from a hook name.
|
|
312
|
+
* @param name - The name of the hook.
|
|
313
|
+
* @param cb - The callback to unregister.
|
|
314
|
+
*/
|
|
315
|
+
unhook(name: string, cb: HookCallback): void;
|
|
316
|
+
}
|
|
317
|
+
|
|
295
318
|
declare class EventContextHooks extends Hookable {
|
|
296
319
|
fireStartEvent(eventType: string): void;
|
|
297
320
|
fireEndEvent(eventType: string, abortReason?: string): void;
|
|
298
|
-
onStartEvent(cb: (eventType: string) => void): void;
|
|
299
|
-
onEndEvent(cb: (eventType: string, abortReason?: string) => void): void;
|
|
321
|
+
onStartEvent(cb: (eventType: string) => void): () => void;
|
|
322
|
+
onEndEvent(cb: (eventType: string, abortReason?: string) => void): () => void;
|
|
300
323
|
}
|
|
301
324
|
declare const eventContextHooks: EventContextHooks;
|
|
302
325
|
|
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,40 @@
|
|
|
1
1
|
import { randomUUID } from 'crypto';
|
|
2
|
-
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
|
-
import { Hookable } from 'hookable';
|
|
2
|
+
import { AsyncResource, AsyncLocalStorage } from 'node:async_hooks';
|
|
4
3
|
import { ProstoLogger, createConsoleTransort, coloredConsole } from '@prostojs/logger';
|
|
5
4
|
|
|
5
|
+
class Hookable extends AsyncResource {
|
|
6
|
+
constructor() {
|
|
7
|
+
super('hookable');
|
|
8
|
+
this.hooks = {};
|
|
9
|
+
}
|
|
10
|
+
hook(name, cb) {
|
|
11
|
+
if (!this.hooks[name]) {
|
|
12
|
+
this.hooks[name] = [];
|
|
13
|
+
}
|
|
14
|
+
this.hooks[name].push(cb);
|
|
15
|
+
return () => {
|
|
16
|
+
this.unhook(name, cb);
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
callHook(name, ...args) {
|
|
20
|
+
if (this.hooks[name]) {
|
|
21
|
+
for (const cb of this.hooks[name]) {
|
|
22
|
+
try {
|
|
23
|
+
this.runInAsyncScope(cb, null, ...args);
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
console.error(`Error in hook ${name}:`, error);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
unhook(name, cb) {
|
|
32
|
+
if (this.hooks[name]) {
|
|
33
|
+
this.hooks[name] = this.hooks[name].filter(hookCb => hookCb !== cb);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
6
38
|
class EventContextHooks extends Hookable {
|
|
7
39
|
fireStartEvent(eventType) {
|
|
8
40
|
this.callHook('start-event', eventType);
|
|
@@ -11,10 +43,10 @@ class EventContextHooks extends Hookable {
|
|
|
11
43
|
this.callHook('end-event', eventType, abortReason);
|
|
12
44
|
}
|
|
13
45
|
onStartEvent(cb) {
|
|
14
|
-
this.hook('start-event', cb);
|
|
46
|
+
return this.hook('start-event', cb);
|
|
15
47
|
}
|
|
16
48
|
onEndEvent(cb) {
|
|
17
|
-
this.hook('end-event', cb);
|
|
49
|
+
return this.hook('end-event', cb);
|
|
18
50
|
}
|
|
19
51
|
}
|
|
20
52
|
const eventContextHooks = new EventContextHooks();
|