@bernierllc/event-emitter 1.4.0 → 1.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bernierllc/event-emitter",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "description": "Type-safe event bus for inter-package communication",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -19,7 +19,7 @@
19
19
  "author": "Bernier LLC",
20
20
  "license": "SEE LICENSE IN LICENSE",
21
21
  "dependencies": {
22
- "@bernierllc/logger": "1.7.0"
22
+ "@bernierllc/logger": "2.0.0"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@types/jest": "^29.5.0",
package/dist/errors.d.ts DELETED
@@ -1,16 +0,0 @@
1
- export type EventEmitterErrorCode = 'EVENT_EMITTER_ERROR' | 'EVENT_MAX_LISTENERS_EXCEEDED';
2
- /**
3
- * Error thrown by event-emitter when invariants are violated (e.g. exceeding
4
- * the configured max-listeners ceiling). Uses ES2022 `Error.cause` so upstream
5
- * failures from handler execution can be wrapped and inspected via
6
- * `getErrorChain` from `@bernierllc/logger`.
7
- */
8
- export declare class EventEmitterError extends Error {
9
- readonly code: EventEmitterErrorCode | string;
10
- readonly context: Record<string, unknown> | undefined;
11
- constructor(message: string, options?: {
12
- cause?: Error | undefined;
13
- code?: EventEmitterErrorCode | string | undefined;
14
- context?: Record<string, unknown> | undefined;
15
- });
16
- }
package/dist/errors.js DELETED
@@ -1,28 +0,0 @@
1
- "use strict";
2
- /*
3
- Copyright (c) 2025 Bernier LLC
4
-
5
- This file is licensed to the client under a limited-use license.
6
- The client may use and modify this code *only within the scope of the project it was delivered for*.
7
- Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.EventEmitterError = void 0;
11
- /**
12
- * Error thrown by event-emitter when invariants are violated (e.g. exceeding
13
- * the configured max-listeners ceiling). Uses ES2022 `Error.cause` so upstream
14
- * failures from handler execution can be wrapped and inspected via
15
- * `getErrorChain` from `@bernierllc/logger`.
16
- */
17
- class EventEmitterError extends Error {
18
- code;
19
- context;
20
- constructor(message, options) {
21
- super(message, options?.cause ? { cause: options.cause } : undefined);
22
- this.name = 'EventEmitterError';
23
- this.code = options?.code ?? 'EVENT_EMITTER_ERROR';
24
- this.context = options?.context;
25
- Error.captureStackTrace?.(this, this.constructor);
26
- }
27
- }
28
- exports.EventEmitterError = EventEmitterError;
@@ -1,39 +0,0 @@
1
- import { EventHandler, EventSubscription, EventEmitterOptions } from './types';
2
- /**
3
- * Event bus for inter-package communication
4
- */
5
- export declare class EventEmitter {
6
- private listeners;
7
- private wildcardListeners;
8
- private options;
9
- private logger?;
10
- constructor(options?: EventEmitterOptions);
11
- /**
12
- * Subscribe to event
13
- */
14
- on<T = any>(eventType: string, handler: EventHandler<T>): EventSubscription;
15
- /**
16
- * Subscribe to event once
17
- */
18
- once<T = any>(eventType: string, handler: EventHandler<T>): EventSubscription;
19
- /**
20
- * Unsubscribe from event
21
- */
22
- off(subscription: EventSubscription): void;
23
- /**
24
- * Publish event
25
- */
26
- emit<T = any>(eventType: string, data: T): Promise<void>;
27
- /**
28
- * Remove all listeners for event type
29
- */
30
- removeAllListeners(eventType?: string): void;
31
- /**
32
- * Get listener count for event type
33
- */
34
- listenerCount(eventType: string): number;
35
- /**
36
- * Get all event types with listeners
37
- */
38
- eventNames(): string[];
39
- }
@@ -1,184 +0,0 @@
1
- "use strict";
2
- /*
3
- Copyright (c) 2025 Bernier LLC
4
-
5
- This file is licensed to the client under a limited-use license.
6
- The client may use and modify this code *only within the scope of the project it was delivered for*.
7
- Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.EventEmitter = void 0;
11
- const errors_1 = require("./errors");
12
- /**
13
- * Event bus for inter-package communication
14
- */
15
- class EventEmitter {
16
- listeners;
17
- wildcardListeners;
18
- options;
19
- logger;
20
- constructor(options = {}) {
21
- this.listeners = new Map();
22
- this.wildcardListeners = new Set();
23
- this.options = {
24
- maxListeners: options.maxListeners || 0,
25
- logEvents: options.logEvents || false,
26
- captureErrors: options.captureErrors !== false // Default to true
27
- };
28
- // Try to load logger if available
29
- try {
30
- // eslint-disable-next-line @typescript-eslint/no-var-requires
31
- const { createLogger } = require('@bernierllc/logger');
32
- this.logger = createLogger({ level: 'debug' });
33
- }
34
- catch {
35
- // Logger not available, continue without it
36
- }
37
- }
38
- /**
39
- * Subscribe to event
40
- */
41
- on(eventType, handler) {
42
- // Check max listeners
43
- if (this.options.maxListeners > 0) {
44
- const currentCount = this.listenerCount(eventType);
45
- if (currentCount >= this.options.maxListeners) {
46
- throw new errors_1.EventEmitterError(`Max listeners (${this.options.maxListeners}) exceeded for event type: ${eventType}`, {
47
- code: 'EVENT_MAX_LISTENERS_EXCEEDED',
48
- context: { eventType, maxListeners: this.options.maxListeners, currentCount },
49
- });
50
- }
51
- }
52
- const subscription = {
53
- id: `${eventType}-${Date.now()}-${Math.random()}`,
54
- eventType,
55
- handler,
56
- once: false,
57
- unsubscribe: () => this.off(subscription)
58
- };
59
- if (eventType === '*') {
60
- this.wildcardListeners.add(subscription);
61
- }
62
- else {
63
- if (!this.listeners.has(eventType)) {
64
- this.listeners.set(eventType, new Set());
65
- }
66
- this.listeners.get(eventType).add(subscription);
67
- }
68
- if (this.options.logEvents && this.logger) {
69
- this.logger.debug('Event listener added', { eventType, subscriptionId: subscription.id });
70
- }
71
- return subscription;
72
- }
73
- /**
74
- * Subscribe to event once
75
- */
76
- once(eventType, handler) {
77
- const subscription = this.on(eventType, handler);
78
- subscription.once = true;
79
- return subscription;
80
- }
81
- /**
82
- * Unsubscribe from event
83
- */
84
- off(subscription) {
85
- if (subscription.eventType === '*') {
86
- this.wildcardListeners.delete(subscription);
87
- }
88
- else {
89
- const listeners = this.listeners.get(subscription.eventType);
90
- if (listeners) {
91
- listeners.delete(subscription);
92
- // Clean up empty sets
93
- if (listeners.size === 0) {
94
- this.listeners.delete(subscription.eventType);
95
- }
96
- }
97
- }
98
- if (this.options.logEvents && this.logger) {
99
- this.logger.debug('Event listener removed', {
100
- eventType: subscription.eventType,
101
- subscriptionId: subscription.id
102
- });
103
- }
104
- }
105
- /**
106
- * Publish event
107
- */
108
- async emit(eventType, data) {
109
- if (this.options.logEvents && this.logger) {
110
- this.logger.debug('Event emitted', { eventType, data });
111
- }
112
- // Get specific listeners
113
- const specificListeners = this.listeners.get(eventType) || new Set();
114
- // Combine with wildcard listeners
115
- const allListeners = [...specificListeners, ...this.wildcardListeners];
116
- // Execute handlers
117
- const promises = allListeners.map(async (subscription) => {
118
- try {
119
- await subscription.handler(data);
120
- }
121
- catch (error) {
122
- if (this.options.captureErrors) {
123
- if (this.logger) {
124
- this.logger.error('Event handler error', {
125
- eventType,
126
- subscriptionId: subscription.id,
127
- error: error instanceof Error ? error.message : String(error)
128
- });
129
- }
130
- }
131
- else {
132
- throw error;
133
- }
134
- }
135
- finally {
136
- // Remove if once subscription (always happens, even if error occurred)
137
- if (subscription.once) {
138
- this.off(subscription);
139
- }
140
- }
141
- });
142
- await Promise.all(promises);
143
- }
144
- /**
145
- * Remove all listeners for event type
146
- */
147
- removeAllListeners(eventType) {
148
- if (eventType) {
149
- if (eventType === '*') {
150
- this.wildcardListeners.clear();
151
- }
152
- else {
153
- this.listeners.delete(eventType);
154
- }
155
- }
156
- else {
157
- this.listeners.clear();
158
- this.wildcardListeners.clear();
159
- }
160
- if (this.options.logEvents && this.logger) {
161
- this.logger.debug('Listeners removed', { eventType: eventType || 'all' });
162
- }
163
- }
164
- /**
165
- * Get listener count for event type
166
- */
167
- listenerCount(eventType) {
168
- if (eventType === '*') {
169
- return this.wildcardListeners.size;
170
- }
171
- return this.listeners.get(eventType)?.size || 0;
172
- }
173
- /**
174
- * Get all event types with listeners
175
- */
176
- eventNames() {
177
- const names = Array.from(this.listeners.keys());
178
- if (this.wildcardListeners.size > 0) {
179
- names.push('*');
180
- }
181
- return names;
182
- }
183
- }
184
- exports.EventEmitter = EventEmitter;
package/dist/factory.d.ts DELETED
@@ -1,6 +0,0 @@
1
- import { EventEmitter } from './event-emitter';
2
- import { EventEmitterOptions } from './types';
3
- /**
4
- * Factory function for creating event emitters
5
- */
6
- export declare function createEventEmitter(options?: EventEmitterOptions): EventEmitter;
package/dist/factory.js DELETED
@@ -1,17 +0,0 @@
1
- "use strict";
2
- /*
3
- Copyright (c) 2025 Bernier LLC
4
-
5
- This file is licensed to the client under a limited-use license.
6
- The client may use and modify this code *only within the scope of the project it was delivered for*.
7
- Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.createEventEmitter = createEventEmitter;
11
- const event_emitter_1 = require("./event-emitter");
12
- /**
13
- * Factory function for creating event emitters
14
- */
15
- function createEventEmitter(options) {
16
- return new event_emitter_1.EventEmitter(options);
17
- }
package/dist/index.d.ts DELETED
@@ -1,5 +0,0 @@
1
- export { EventEmitter } from './event-emitter';
2
- export { createEventEmitter } from './factory';
3
- export type { EventHandler, EventSubscription, EventEmitterOptions } from './types';
4
- export { EventEmitterError } from './errors';
5
- export type { EventEmitterErrorCode } from './errors';
package/dist/index.js DELETED
@@ -1,16 +0,0 @@
1
- "use strict";
2
- /*
3
- Copyright (c) 2025 Bernier LLC
4
-
5
- This file is licensed to the client under a limited-use license.
6
- The client may use and modify this code *only within the scope of the project it was delivered for*.
7
- Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.EventEmitterError = exports.createEventEmitter = exports.EventEmitter = void 0;
11
- var event_emitter_1 = require("./event-emitter");
12
- Object.defineProperty(exports, "EventEmitter", { enumerable: true, get: function () { return event_emitter_1.EventEmitter; } });
13
- var factory_1 = require("./factory");
14
- Object.defineProperty(exports, "createEventEmitter", { enumerable: true, get: function () { return factory_1.createEventEmitter; } });
15
- var errors_1 = require("./errors");
16
- Object.defineProperty(exports, "EventEmitterError", { enumerable: true, get: function () { return errors_1.EventEmitterError; } });
package/dist/types.d.ts DELETED
@@ -1,30 +0,0 @@
1
- /**
2
- * Event handler function
3
- */
4
- export type EventHandler<T = any> = (data: T) => void | Promise<void>;
5
- /**
6
- * Event subscription
7
- */
8
- export interface EventSubscription {
9
- /** Unique subscription ID */
10
- id: string;
11
- /** Event type */
12
- eventType: string;
13
- /** Handler function */
14
- handler: EventHandler;
15
- /** Whether this is a one-time subscription */
16
- once: boolean;
17
- /** Unsubscribe function */
18
- unsubscribe: () => void;
19
- }
20
- /**
21
- * Event emitter options
22
- */
23
- export interface EventEmitterOptions {
24
- /** Maximum listeners per event (0 = unlimited) */
25
- maxListeners?: number;
26
- /** Whether to log events */
27
- logEvents?: boolean;
28
- /** Whether to capture errors */
29
- captureErrors?: boolean;
30
- }
package/dist/types.js DELETED
@@ -1,9 +0,0 @@
1
- "use strict";
2
- /*
3
- Copyright (c) 2025 Bernier LLC
4
-
5
- This file is licensed to the client under a limited-use license.
6
- The client may use and modify this code *only within the scope of the project it was delivered for*.
7
- Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });