@wooksjs/event-core 0.5.2 → 0.5.4
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 +12 -5
- package/dist/index.d.ts +3 -2
- package/dist/index.mjs +13 -6
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -4,8 +4,9 @@ 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 extends node_async_hooks.AsyncResource {
|
|
8
8
|
constructor() {
|
|
9
|
+
super('hookable');
|
|
9
10
|
this.hooks = {};
|
|
10
11
|
}
|
|
11
12
|
hook(name, cb) {
|
|
@@ -21,7 +22,7 @@ class Hookable {
|
|
|
21
22
|
if (this.hooks[name]) {
|
|
22
23
|
for (const cb of this.hooks[name]) {
|
|
23
24
|
try {
|
|
24
|
-
cb
|
|
25
|
+
this.runInAsyncScope(cb, null, ...args);
|
|
25
26
|
}
|
|
26
27
|
catch (error) {
|
|
27
28
|
console.error(`Error in hook ${name}:`, error);
|
|
@@ -64,7 +65,9 @@ const asyncStorage = new node_async_hooks.AsyncLocalStorage();
|
|
|
64
65
|
function createAsyncEventContext(data) {
|
|
65
66
|
const newContext = { ...data };
|
|
66
67
|
return (cb) => {
|
|
67
|
-
|
|
68
|
+
asyncStorage.run(newContext, () => {
|
|
69
|
+
eventContextHooks.fireStartEvent(newContext.event.type);
|
|
70
|
+
});
|
|
68
71
|
const result = asyncStorage.run(newContext, cb);
|
|
69
72
|
if (result instanceof Promise) {
|
|
70
73
|
result
|
|
@@ -82,10 +85,14 @@ function createAsyncEventContext(data) {
|
|
|
82
85
|
function endEvent(output) {
|
|
83
86
|
if (!newContext._ended) {
|
|
84
87
|
if (output instanceof Error) {
|
|
85
|
-
|
|
88
|
+
asyncStorage.run(newContext, () => {
|
|
89
|
+
eventContextHooks.fireEndEvent(newContext.event.type, output.message);
|
|
90
|
+
});
|
|
86
91
|
}
|
|
87
92
|
else {
|
|
88
|
-
|
|
93
|
+
asyncStorage.run(newContext, () => {
|
|
94
|
+
eventContextHooks.fireEndEvent(newContext.event.type);
|
|
95
|
+
});
|
|
89
96
|
}
|
|
90
97
|
}
|
|
91
98
|
}
|
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 } from 'node:async_hooks';
|
|
3
|
+
import { AsyncLocalStorage, AsyncResource } from 'node:async_hooks';
|
|
4
4
|
|
|
5
5
|
declare function useEventId(): {
|
|
6
6
|
getId: () => string;
|
|
@@ -292,7 +292,8 @@ declare function useRouteParams<T extends object = Record<string, string | strin
|
|
|
292
292
|
};
|
|
293
293
|
|
|
294
294
|
type HookCallback = (...args: any[]) => any;
|
|
295
|
-
declare class Hookable {
|
|
295
|
+
declare class Hookable extends AsyncResource {
|
|
296
|
+
constructor();
|
|
296
297
|
private hooks;
|
|
297
298
|
/**
|
|
298
299
|
* Registers a callback to a specific hook name.
|
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { randomUUID } from 'crypto';
|
|
2
|
-
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import { AsyncResource, AsyncLocalStorage } from 'node:async_hooks';
|
|
3
3
|
import { ProstoLogger, createConsoleTransort, coloredConsole } from '@prostojs/logger';
|
|
4
4
|
|
|
5
|
-
class Hookable {
|
|
5
|
+
class Hookable extends AsyncResource {
|
|
6
6
|
constructor() {
|
|
7
|
+
super('hookable');
|
|
7
8
|
this.hooks = {};
|
|
8
9
|
}
|
|
9
10
|
hook(name, cb) {
|
|
@@ -19,7 +20,7 @@ class Hookable {
|
|
|
19
20
|
if (this.hooks[name]) {
|
|
20
21
|
for (const cb of this.hooks[name]) {
|
|
21
22
|
try {
|
|
22
|
-
cb
|
|
23
|
+
this.runInAsyncScope(cb, null, ...args);
|
|
23
24
|
}
|
|
24
25
|
catch (error) {
|
|
25
26
|
console.error(`Error in hook ${name}:`, error);
|
|
@@ -62,7 +63,9 @@ const asyncStorage = new AsyncLocalStorage();
|
|
|
62
63
|
function createAsyncEventContext(data) {
|
|
63
64
|
const newContext = { ...data };
|
|
64
65
|
return (cb) => {
|
|
65
|
-
|
|
66
|
+
asyncStorage.run(newContext, () => {
|
|
67
|
+
eventContextHooks.fireStartEvent(newContext.event.type);
|
|
68
|
+
});
|
|
66
69
|
const result = asyncStorage.run(newContext, cb);
|
|
67
70
|
if (result instanceof Promise) {
|
|
68
71
|
result
|
|
@@ -80,10 +83,14 @@ function createAsyncEventContext(data) {
|
|
|
80
83
|
function endEvent(output) {
|
|
81
84
|
if (!newContext._ended) {
|
|
82
85
|
if (output instanceof Error) {
|
|
83
|
-
|
|
86
|
+
asyncStorage.run(newContext, () => {
|
|
87
|
+
eventContextHooks.fireEndEvent(newContext.event.type, output.message);
|
|
88
|
+
});
|
|
84
89
|
}
|
|
85
90
|
else {
|
|
86
|
-
|
|
91
|
+
asyncStorage.run(newContext, () => {
|
|
92
|
+
eventContextHooks.fireEndEvent(newContext.event.type);
|
|
93
|
+
});
|
|
87
94
|
}
|
|
88
95
|
}
|
|
89
96
|
}
|