@wooksjs/event-core 0.4.35 → 0.4.36

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
@@ -1,8 +1,25 @@
1
1
  'use strict';
2
2
 
3
3
  var crypto = require('crypto');
4
+ var hookable = require('hookable');
4
5
  var logger = require('@prostojs/logger');
5
6
 
7
+ class EventContextHooks extends hookable.Hookable {
8
+ fireStartEvent(eventType) {
9
+ this.callHook('start-event', eventType);
10
+ }
11
+ fireEndEvent(eventType, abortReason) {
12
+ this.callHook('end-event', eventType, abortReason);
13
+ }
14
+ onStartEvent(cb) {
15
+ this.hook('start-event', cb);
16
+ }
17
+ onEndEvent(cb) {
18
+ this.hook('end-event', cb);
19
+ }
20
+ }
21
+ const eventContextHooks = new EventContextHooks();
22
+
6
23
  function attachHook(target, opts, name) {
7
24
  Object.defineProperty(target, name || 'value', {
8
25
  get: opts.get,
@@ -15,6 +32,7 @@ let currentContext = null;
15
32
  function createEventContext(data) {
16
33
  const newContext = { ...data };
17
34
  currentContext = newContext;
35
+ eventContextHooks.fireStartEvent(data.event.type);
18
36
  return _getCtxHelpers(newContext);
19
37
  }
20
38
  function useEventContext(expectedTypes) {
@@ -110,10 +128,15 @@ function _getCtxHelpers(cc) {
110
128
  function set(key, v) {
111
129
  getCtx()[key] = v;
112
130
  }
131
+ const clearCtx = () => (cc === currentContext ? (currentContext = null) : null);
113
132
  return {
114
133
  getCtx,
115
134
  restoreCtx: () => (currentContext = cc),
116
- clearCtx: () => (cc === currentContext ? (currentContext = null) : null),
135
+ clearCtx,
136
+ endEvent: (abortReason) => {
137
+ eventContextHooks.fireEndEvent(cc.event.type, abortReason);
138
+ clearCtx();
139
+ },
117
140
  store,
118
141
  getStore: get,
119
142
  setStore: set,
@@ -177,6 +200,7 @@ function useRouteParams() {
177
200
  exports.EventLogger = EventLogger;
178
201
  exports.attachHook = attachHook;
179
202
  exports.createEventContext = createEventContext;
203
+ exports.eventContextHooks = eventContextHooks;
180
204
  exports.useEventContext = useEventContext;
181
205
  exports.useEventId = useEventId;
182
206
  exports.useEventLogger = useEventLogger;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as _prostojs_logger from '@prostojs/logger';
2
2
  import { TProstoLoggerOptions, ProstoLogger } from '@prostojs/logger';
3
+ import { Hookable } from 'hookable';
3
4
 
4
5
  declare function useEventId(): {
5
6
  getId: () => string;
@@ -34,6 +35,7 @@ declare function createEventContext<S = TEmpty, EventTypeToCreate = TEmpty>(data
34
35
  getCtx: () => S & TGenericContextStore<EventTypeToCreate>;
35
36
  restoreCtx: () => TGenericContextStore<TEmpty>;
36
37
  clearCtx: () => null;
38
+ endEvent: (abortReason?: string | undefined) => void;
37
39
  store: <K extends keyof S | keyof TGenericContextStore<EventTypeToCreate>>(key: K) => {
38
40
  value: (S & TGenericContextStore<EventTypeToCreate>)[K];
39
41
  hook: <K2 extends keyof Required<S & TGenericContextStore<EventTypeToCreate>>[K]>(key2: K2) => {
@@ -64,6 +66,7 @@ declare function useEventContext<S = TEmpty, EventType = TEmpty>(expectedTypes?:
64
66
  getCtx: () => S & TGenericContextStore<EventType>;
65
67
  restoreCtx: () => TGenericContextStore<TEmpty>;
66
68
  clearCtx: () => null;
69
+ endEvent: (abortReason?: string | undefined) => void;
67
70
  store: <K extends keyof S | keyof TGenericContextStore<EventType>>(key: K) => {
68
71
  value: (S & TGenericContextStore<EventType>)[K];
69
72
  hook: <K2 extends keyof Required<S & TGenericContextStore<EventType>>[K]>(key2: K2) => {
@@ -98,6 +101,14 @@ declare function useRouteParams<T extends object = Record<string, string | strin
98
101
  get: <K extends keyof T>(name: K) => T[K];
99
102
  };
100
103
 
104
+ declare class EventContextHooks extends Hookable {
105
+ fireStartEvent(eventType: string): void;
106
+ fireEndEvent(eventType: string, abortReason?: string): void;
107
+ onStartEvent(cb: (eventType: string) => void): void;
108
+ onEndEvent(cb: (eventType: string, abortReason?: string) => void): void;
109
+ }
110
+ declare const eventContextHooks: EventContextHooks;
111
+
101
112
  declare function attachHook<V = unknown, T extends object | Function = object, P extends PropertyKey = 'value'>(target: T, opts: {
102
113
  get: () => V | undefined;
103
114
  set?: (value: V) => void;
@@ -106,4 +117,4 @@ type THook<T = string, K extends PropertyKey = 'value'> = {
106
117
  [key in K]: T;
107
118
  };
108
119
 
109
- export { EventLogger, type TEmpty, type TEventLoggerData, type TEventOptions, type TGenericContextStore, type TGenericEvent, type THook, attachHook, createEventContext, useEventContext, useEventId, useEventLogger, useRouteParams };
120
+ export { EventLogger, type TEmpty, type TEventLoggerData, type TEventOptions, type TGenericContextStore, type TGenericEvent, type THook, attachHook, createEventContext, eventContextHooks, useEventContext, useEventId, useEventLogger, useRouteParams };
package/dist/index.mjs CHANGED
@@ -1,6 +1,23 @@
1
1
  import { randomUUID } from 'crypto';
2
+ import { Hookable } from 'hookable';
2
3
  import { ProstoLogger, createConsoleTransort, coloredConsole } from '@prostojs/logger';
3
4
 
5
+ class EventContextHooks extends Hookable {
6
+ fireStartEvent(eventType) {
7
+ this.callHook('start-event', eventType);
8
+ }
9
+ fireEndEvent(eventType, abortReason) {
10
+ this.callHook('end-event', eventType, abortReason);
11
+ }
12
+ onStartEvent(cb) {
13
+ this.hook('start-event', cb);
14
+ }
15
+ onEndEvent(cb) {
16
+ this.hook('end-event', cb);
17
+ }
18
+ }
19
+ const eventContextHooks = new EventContextHooks();
20
+
4
21
  function attachHook(target, opts, name) {
5
22
  Object.defineProperty(target, name || 'value', {
6
23
  get: opts.get,
@@ -13,6 +30,7 @@ let currentContext = null;
13
30
  function createEventContext(data) {
14
31
  const newContext = { ...data };
15
32
  currentContext = newContext;
33
+ eventContextHooks.fireStartEvent(data.event.type);
16
34
  return _getCtxHelpers(newContext);
17
35
  }
18
36
  function useEventContext(expectedTypes) {
@@ -108,10 +126,15 @@ function _getCtxHelpers(cc) {
108
126
  function set(key, v) {
109
127
  getCtx()[key] = v;
110
128
  }
129
+ const clearCtx = () => (cc === currentContext ? (currentContext = null) : null);
111
130
  return {
112
131
  getCtx,
113
132
  restoreCtx: () => (currentContext = cc),
114
- clearCtx: () => (cc === currentContext ? (currentContext = null) : null),
133
+ clearCtx,
134
+ endEvent: (abortReason) => {
135
+ eventContextHooks.fireEndEvent(cc.event.type, abortReason);
136
+ clearCtx();
137
+ },
115
138
  store,
116
139
  getStore: get,
117
140
  setStore: set,
@@ -172,4 +195,4 @@ function useRouteParams() {
172
195
  };
173
196
  }
174
197
 
175
- export { EventLogger, attachHook, createEventContext, useEventContext, useEventId, useEventLogger, useRouteParams };
198
+ export { EventLogger, attachHook, createEventContext, eventContextHooks, useEventContext, useEventId, useEventLogger, useRouteParams };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wooksjs/event-core",
3
- "version": "0.4.35",
3
+ "version": "0.4.36",
4
4
  "description": "@wooksjs/event-core",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -39,6 +39,7 @@
39
39
  "url": "https://github.com/wooksjs/wooksjs/issues"
40
40
  },
41
41
  "dependencies": {
42
+ "hookable": "^5.5.3",
42
43
  "@prostojs/logger": "^0.4.0"
43
44
  },
44
45
  "homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/event-core#readme"