@wooksjs/event-core 0.5.1 → 0.5.2

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