@wooksjs/event-core 0.5.3 → 0.5.5
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 +11 -6
- package/dist/index.d.ts +2 -3
- package/dist/index.mjs +12 -7
- 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);
|
|
@@ -65,7 +64,9 @@ const asyncStorage = new node_async_hooks.AsyncLocalStorage();
|
|
|
65
64
|
function createAsyncEventContext(data) {
|
|
66
65
|
const newContext = { ...data };
|
|
67
66
|
return (cb) => {
|
|
68
|
-
|
|
67
|
+
asyncStorage.run(newContext, () => {
|
|
68
|
+
eventContextHooks.fireStartEvent(newContext.event.type);
|
|
69
|
+
});
|
|
69
70
|
const result = asyncStorage.run(newContext, cb);
|
|
70
71
|
if (result instanceof Promise) {
|
|
71
72
|
result
|
|
@@ -83,10 +84,14 @@ function createAsyncEventContext(data) {
|
|
|
83
84
|
function endEvent(output) {
|
|
84
85
|
if (!newContext._ended) {
|
|
85
86
|
if (output instanceof Error) {
|
|
86
|
-
|
|
87
|
+
asyncStorage.run(newContext, () => {
|
|
88
|
+
eventContextHooks.fireEndEvent(newContext.event.type, output.message);
|
|
89
|
+
});
|
|
87
90
|
}
|
|
88
91
|
else {
|
|
89
|
-
|
|
92
|
+
asyncStorage.run(newContext, () => {
|
|
93
|
+
eventContextHooks.fireEndEvent(newContext.event.type);
|
|
94
|
+
});
|
|
90
95
|
}
|
|
91
96
|
}
|
|
92
97
|
}
|
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;
|
|
@@ -292,8 +292,7 @@ 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
|
|
296
|
-
constructor();
|
|
295
|
+
declare class Hookable {
|
|
297
296
|
private hooks;
|
|
298
297
|
/**
|
|
299
298
|
* Registers a callback to a specific hook name.
|
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);
|
|
@@ -63,7 +62,9 @@ const asyncStorage = new AsyncLocalStorage();
|
|
|
63
62
|
function createAsyncEventContext(data) {
|
|
64
63
|
const newContext = { ...data };
|
|
65
64
|
return (cb) => {
|
|
66
|
-
|
|
65
|
+
asyncStorage.run(newContext, () => {
|
|
66
|
+
eventContextHooks.fireStartEvent(newContext.event.type);
|
|
67
|
+
});
|
|
67
68
|
const result = asyncStorage.run(newContext, cb);
|
|
68
69
|
if (result instanceof Promise) {
|
|
69
70
|
result
|
|
@@ -81,10 +82,14 @@ function createAsyncEventContext(data) {
|
|
|
81
82
|
function endEvent(output) {
|
|
82
83
|
if (!newContext._ended) {
|
|
83
84
|
if (output instanceof Error) {
|
|
84
|
-
|
|
85
|
+
asyncStorage.run(newContext, () => {
|
|
86
|
+
eventContextHooks.fireEndEvent(newContext.event.type, output.message);
|
|
87
|
+
});
|
|
85
88
|
}
|
|
86
89
|
else {
|
|
87
|
-
|
|
90
|
+
asyncStorage.run(newContext, () => {
|
|
91
|
+
eventContextHooks.fireEndEvent(newContext.event.type);
|
|
92
|
+
});
|
|
88
93
|
}
|
|
89
94
|
}
|
|
90
95
|
}
|