@ad-execute-manager/event 2.0.1 → 2.0.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/README.md +351 -351
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,351 +1,351 @@
|
|
|
1
|
-
# @ad-execute-manager/event
|
|
2
|
-
|
|
3
|
-
A collection of event-related utilities for JavaScript applications including EventEmitter and PubSub implementations.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install @ad-execute-manager/event
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Features
|
|
12
|
-
|
|
13
|
-
- **EventEmitter**: A powerful event emitter with support for once events, max listeners, and event queuing
|
|
14
|
-
- **PubSub**: A simple publish-subscribe pattern implementation with automatic unsubscribe function
|
|
15
|
-
- **Event Queuing**: Automatically queue events when no listeners are present
|
|
16
|
-
- **Listener Limits**: Set maximum listeners per event to prevent memory leaks
|
|
17
|
-
- **Once Events**: Support for events that only fire once
|
|
18
|
-
- **Error Handling**: Built-in error handling for event listeners
|
|
19
|
-
- **TypeScript Support**: Includes TypeScript type definitions
|
|
20
|
-
- **Singleton Support**: EventEmitter includes static getInstance method for singleton pattern
|
|
21
|
-
|
|
22
|
-
## Usage
|
|
23
|
-
|
|
24
|
-
### EventEmitter
|
|
25
|
-
|
|
26
|
-
```javascript
|
|
27
|
-
import { EventEmitter } from '@ad-execute-manager/event';
|
|
28
|
-
|
|
29
|
-
// Create an event emitter instance
|
|
30
|
-
const emitter = new EventEmitter({
|
|
31
|
-
maxListeners: 10,
|
|
32
|
-
maxQueueSize: 5,
|
|
33
|
-
maxOnceEvents: 5
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
// Register event listener
|
|
37
|
-
emitter.on('userLoggedIn', (userData) => {
|
|
38
|
-
console.log('User logged in:', userData);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
// Register once event listener
|
|
42
|
-
emitter.once('appStarted', (appData) => {
|
|
43
|
-
console.log('App started:', appData);
|
|
44
|
-
// This listener will be automatically removed after first execution
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
// Emit events
|
|
48
|
-
emitter.emit('userLoggedIn', { id: 1, name: 'John' });
|
|
49
|
-
emitter.emit('appStarted', { version: '1.0.0' });
|
|
50
|
-
|
|
51
|
-
// Remove event listener
|
|
52
|
-
function onDataReceived(data) {
|
|
53
|
-
console.log('Data received:', data);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
emitter.on('dataReceived', onDataReceived);
|
|
57
|
-
emitter.off('dataReceived', onDataReceived);
|
|
58
|
-
|
|
59
|
-
// Remove all listeners for an event
|
|
60
|
-
emitter.removeAllListeners('userLoggedIn');
|
|
61
|
-
|
|
62
|
-
// Remove all listeners
|
|
63
|
-
// emitter.removeAllListeners();
|
|
64
|
-
|
|
65
|
-
// Using singleton pattern
|
|
66
|
-
const singletonEmitter = EventEmitter.getInstance({
|
|
67
|
-
maxListeners: 15
|
|
68
|
-
});
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
### PubSub
|
|
72
|
-
|
|
73
|
-
```javascript
|
|
74
|
-
import { PubSub } from '@ad-execute-manager/event';
|
|
75
|
-
|
|
76
|
-
// Create a PubSub instance
|
|
77
|
-
const pubsub = new PubSub();
|
|
78
|
-
|
|
79
|
-
// Subscribe to an event
|
|
80
|
-
const unsubscribe = pubsub.on('message', (data) => {
|
|
81
|
-
console.log('Message received:', data);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
// Publish an event
|
|
85
|
-
pubsub.emit('message', { text: 'Hello, world!' });
|
|
86
|
-
|
|
87
|
-
// Unsubscribe using the returned function
|
|
88
|
-
unsubscribe();
|
|
89
|
-
|
|
90
|
-
// Subscribe to an event only once
|
|
91
|
-
pubsub.once('notification', (notification) => {
|
|
92
|
-
console.log('Notification:', notification);
|
|
93
|
-
// This listener will be automatically removed after first execution
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
// Publish the notification event
|
|
97
|
-
pubsub.emit('notification', { title: 'New Message', body: 'You have a new message' });
|
|
98
|
-
|
|
99
|
-
// Check listener count
|
|
100
|
-
const listenerCount = pubsub.listenerCount('message');
|
|
101
|
-
console.log('Message listeners:', listenerCount);
|
|
102
|
-
|
|
103
|
-
// Remove all listeners
|
|
104
|
-
// pubsub.removeAllListeners();
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
## API
|
|
108
|
-
|
|
109
|
-
### EventEmitter
|
|
110
|
-
|
|
111
|
-
#### Constructor
|
|
112
|
-
|
|
113
|
-
```javascript
|
|
114
|
-
new EventEmitter(options)
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
- **options** (Object): Configuration options
|
|
118
|
-
- **maxListeners** (Number): Maximum listeners per event, default 5
|
|
119
|
-
- **maxQueueSize** (Number): Maximum queue size for events with no listeners, default 1
|
|
120
|
-
- **maxOnceEvents** (Number): Maximum once events per event, default 5
|
|
121
|
-
|
|
122
|
-
#### Methods
|
|
123
|
-
|
|
124
|
-
- **on(event, listener)**: Register an event listener
|
|
125
|
-
- **event** (String): Event name
|
|
126
|
-
- **listener** (Function): Event listener function
|
|
127
|
-
|
|
128
|
-
- **once(event, listener)**: Register a one-time event listener
|
|
129
|
-
- **event** (String): Event name
|
|
130
|
-
- **listener** (Function): Event listener function
|
|
131
|
-
|
|
132
|
-
- **emit(event, ...args)**: Emit an event
|
|
133
|
-
- **event** (String): Event name
|
|
134
|
-
- **...args** (Any): Arguments to pass to listeners
|
|
135
|
-
|
|
136
|
-
- **off(event, listenerToRemove)**: Remove an event listener
|
|
137
|
-
- **event** (String): Event name
|
|
138
|
-
- **listenerToRemove** (Function): Listener function to remove
|
|
139
|
-
|
|
140
|
-
- **removeAllListeners(event)**: Remove all listeners for an event
|
|
141
|
-
- **event** (String, optional): Event name, omit to remove all listeners
|
|
142
|
-
|
|
143
|
-
- **static getInstance(args)**: Get singleton instance
|
|
144
|
-
- **args** (Object): Constructor options
|
|
145
|
-
- **returns** (EventEmitter): Singleton instance
|
|
146
|
-
|
|
147
|
-
### PubSub
|
|
148
|
-
|
|
149
|
-
#### Constructor
|
|
150
|
-
|
|
151
|
-
```javascript
|
|
152
|
-
new PubSub()
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
#### Methods
|
|
156
|
-
|
|
157
|
-
- **on(eventName, callback)**: Subscribe to an event
|
|
158
|
-
- **eventName** (String): Event name
|
|
159
|
-
- **callback** (Function): Event callback function
|
|
160
|
-
- **returns** (Function): Unsubscribe function
|
|
161
|
-
|
|
162
|
-
- **off(eventName, callback)**: Unsubscribe from an event
|
|
163
|
-
- **eventName** (String): Event name
|
|
164
|
-
- **callback** (Function): Callback function to remove
|
|
165
|
-
|
|
166
|
-
- **emit(eventName, ...args)**: Publish an event
|
|
167
|
-
- **eventName** (String): Event name
|
|
168
|
-
- **...args** (Any): Arguments to pass to callbacks
|
|
169
|
-
|
|
170
|
-
- **once(eventName, callback)**: Subscribe to an event once
|
|
171
|
-
- **eventName** (String): Event name
|
|
172
|
-
- **callback** (Function): Event callback function
|
|
173
|
-
|
|
174
|
-
- **listenerCount(eventName)**: Get listener count for an event
|
|
175
|
-
- **eventName** (String): Event name
|
|
176
|
-
- **returns** (Number): Listener count
|
|
177
|
-
|
|
178
|
-
- **removeAllListeners()**: Remove all listeners
|
|
179
|
-
|
|
180
|
-
## Examples
|
|
181
|
-
|
|
182
|
-
### Example 1: Application Event Bus
|
|
183
|
-
|
|
184
|
-
```javascript
|
|
185
|
-
import { EventEmitter } from '@ad-execute-manager/event';
|
|
186
|
-
|
|
187
|
-
// Create global event bus
|
|
188
|
-
const eventBus = EventEmitter.getInstance({
|
|
189
|
-
maxListeners: 20
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
// User service
|
|
193
|
-
function loginUser(userData) {
|
|
194
|
-
// Login logic
|
|
195
|
-
console.log('Logging in user:', userData.username);
|
|
196
|
-
|
|
197
|
-
// Emit login event
|
|
198
|
-
eventBus.emit('userLoggedIn', {
|
|
199
|
-
user: userData,
|
|
200
|
-
timestamp: new Date().toISOString()
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// Notification service
|
|
205
|
-
function setupNotifications() {
|
|
206
|
-
eventBus.on('userLoggedIn', (loginData) => {
|
|
207
|
-
console.log('Sending welcome notification to:', loginData.user.username);
|
|
208
|
-
// Send welcome notification
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
eventBus.on('userLoggedOut', (logoutData) => {
|
|
212
|
-
console.log('Updating user status to offline:', logoutData.userId);
|
|
213
|
-
// Update user status
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// Analytics service
|
|
218
|
-
function setupAnalytics() {
|
|
219
|
-
eventBus.once('appStarted', (appData) => {
|
|
220
|
-
console.log('Recording app start event:', appData.version);
|
|
221
|
-
// Record app start analytics
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
eventBus.on('userLoggedIn', (loginData) => {
|
|
225
|
-
console.log('Recording login event:', loginData.user.username);
|
|
226
|
-
// Record login analytics
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// Setup services
|
|
231
|
-
setupNotifications();
|
|
232
|
-
setupAnalytics();
|
|
233
|
-
|
|
234
|
-
// Start app
|
|
235
|
-
eventBus.emit('appStarted', { version: '1.0.0' });
|
|
236
|
-
|
|
237
|
-
// Login user
|
|
238
|
-
loginUser({ id: 1, username: 'john_doe', email: 'john@example.com' });
|
|
239
|
-
```
|
|
240
|
-
|
|
241
|
-
### Example 2: Component Communication
|
|
242
|
-
|
|
243
|
-
```javascript
|
|
244
|
-
import { PubSub } from '@ad-execute-manager/event';
|
|
245
|
-
|
|
246
|
-
// Create pubsub instance for component communication
|
|
247
|
-
const componentBus = new PubSub();
|
|
248
|
-
|
|
249
|
-
// Header component
|
|
250
|
-
function Header() {
|
|
251
|
-
// Subscribe to user changes
|
|
252
|
-
const unsubscribe = componentBus.on('userChanged', (user) => {
|
|
253
|
-
console.log('Header: Updating user display:', user.name);
|
|
254
|
-
// Update header with user info
|
|
255
|
-
});
|
|
256
|
-
|
|
257
|
-
// Subscribe to theme changes
|
|
258
|
-
componentBus.on('themeChanged', (theme) => {
|
|
259
|
-
console.log('Header: Updating theme:', theme);
|
|
260
|
-
// Update header theme
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
// Cleanup function
|
|
264
|
-
return () => {
|
|
265
|
-
unsubscribe();
|
|
266
|
-
console.log('Header: Unsubscribed from events');
|
|
267
|
-
};
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
// User profile component
|
|
271
|
-
function UserProfile() {
|
|
272
|
-
// Subscribe to user changes
|
|
273
|
-
const unsubscribe = componentBus.on('userChanged', (user) => {
|
|
274
|
-
console.log('UserProfile: Updating user profile:', user.name);
|
|
275
|
-
// Update user profile
|
|
276
|
-
});
|
|
277
|
-
|
|
278
|
-
// Cleanup function
|
|
279
|
-
return () => {
|
|
280
|
-
unsubscribe();
|
|
281
|
-
console.log('UserProfile: Unsubscribed from events');
|
|
282
|
-
};
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
// Settings component
|
|
286
|
-
function Settings() {
|
|
287
|
-
// Update user
|
|
288
|
-
function updateUser(userData) {
|
|
289
|
-
console.log('Settings: Updating user');
|
|
290
|
-
componentBus.emit('userChanged', userData);
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
// Change theme
|
|
294
|
-
function changeTheme(theme) {
|
|
295
|
-
console.log('Settings: Changing theme to:', theme);
|
|
296
|
-
componentBus.emit('themeChanged', theme);
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
return {
|
|
300
|
-
updateUser,
|
|
301
|
-
changeTheme
|
|
302
|
-
};
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
// Initialize components
|
|
306
|
-
const cleanupHeader = Header();
|
|
307
|
-
const cleanupUserProfile = UserProfile();
|
|
308
|
-
const settings = Settings();
|
|
309
|
-
|
|
310
|
-
// Simulate user update
|
|
311
|
-
settings.updateUser({ id: 1, name: 'John Doe', email: 'john@example.com' });
|
|
312
|
-
|
|
313
|
-
// Simulate theme change
|
|
314
|
-
settings.changeTheme('dark');
|
|
315
|
-
|
|
316
|
-
// Cleanup components
|
|
317
|
-
// cleanupHeader();
|
|
318
|
-
// cleanupUserProfile();
|
|
319
|
-
```
|
|
320
|
-
|
|
321
|
-
### Example 3: Event Queueing
|
|
322
|
-
|
|
323
|
-
```javascript
|
|
324
|
-
import { EventEmitter } from '@ad-execute-manager/event';
|
|
325
|
-
|
|
326
|
-
// Create event emitter with queueing
|
|
327
|
-
const emitter = new EventEmitter({
|
|
328
|
-
maxQueueSize: 10
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
// Emit events before listeners are registered
|
|
332
|
-
console.log('Emitting events before listeners...');
|
|
333
|
-
emitter.emit('data', { value: 1 });
|
|
334
|
-
emitter.emit('data', { value: 2 });
|
|
335
|
-
emitter.emit('data', { value: 3 });
|
|
336
|
-
|
|
337
|
-
// Register listener - will receive queued events
|
|
338
|
-
console.log('Registering listener...');
|
|
339
|
-
emitter.on('data', (data) => {
|
|
340
|
-
console.log('Received data:', data);
|
|
341
|
-
});
|
|
342
|
-
|
|
343
|
-
// Emit more events
|
|
344
|
-
console.log('Emitting more events...');
|
|
345
|
-
emitter.emit('data', { value: 4 });
|
|
346
|
-
emitter.emit('data', { value: 5 });
|
|
347
|
-
```
|
|
348
|
-
|
|
349
|
-
## License
|
|
350
|
-
|
|
351
|
-
MIT
|
|
1
|
+
# @ad-execute-manager/event
|
|
2
|
+
|
|
3
|
+
A collection of event-related utilities for JavaScript applications including EventEmitter and PubSub implementations.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @ad-execute-manager/event
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **EventEmitter**: A powerful event emitter with support for once events, max listeners, and event queuing
|
|
14
|
+
- **PubSub**: A simple publish-subscribe pattern implementation with automatic unsubscribe function
|
|
15
|
+
- **Event Queuing**: Automatically queue events when no listeners are present
|
|
16
|
+
- **Listener Limits**: Set maximum listeners per event to prevent memory leaks
|
|
17
|
+
- **Once Events**: Support for events that only fire once
|
|
18
|
+
- **Error Handling**: Built-in error handling for event listeners
|
|
19
|
+
- **TypeScript Support**: Includes TypeScript type definitions
|
|
20
|
+
- **Singleton Support**: EventEmitter includes static getInstance method for singleton pattern
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### EventEmitter
|
|
25
|
+
|
|
26
|
+
```javascript
|
|
27
|
+
import { EventEmitter } from '@ad-execute-manager/event';
|
|
28
|
+
|
|
29
|
+
// Create an event emitter instance
|
|
30
|
+
const emitter = new EventEmitter({
|
|
31
|
+
maxListeners: 10,
|
|
32
|
+
maxQueueSize: 5,
|
|
33
|
+
maxOnceEvents: 5
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Register event listener
|
|
37
|
+
emitter.on('userLoggedIn', (userData) => {
|
|
38
|
+
console.log('User logged in:', userData);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Register once event listener
|
|
42
|
+
emitter.once('appStarted', (appData) => {
|
|
43
|
+
console.log('App started:', appData);
|
|
44
|
+
// This listener will be automatically removed after first execution
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// Emit events
|
|
48
|
+
emitter.emit('userLoggedIn', { id: 1, name: 'John' });
|
|
49
|
+
emitter.emit('appStarted', { version: '1.0.0' });
|
|
50
|
+
|
|
51
|
+
// Remove event listener
|
|
52
|
+
function onDataReceived(data) {
|
|
53
|
+
console.log('Data received:', data);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
emitter.on('dataReceived', onDataReceived);
|
|
57
|
+
emitter.off('dataReceived', onDataReceived);
|
|
58
|
+
|
|
59
|
+
// Remove all listeners for an event
|
|
60
|
+
emitter.removeAllListeners('userLoggedIn');
|
|
61
|
+
|
|
62
|
+
// Remove all listeners
|
|
63
|
+
// emitter.removeAllListeners();
|
|
64
|
+
|
|
65
|
+
// Using singleton pattern
|
|
66
|
+
const singletonEmitter = EventEmitter.getInstance({
|
|
67
|
+
maxListeners: 15
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### PubSub
|
|
72
|
+
|
|
73
|
+
```javascript
|
|
74
|
+
import { PubSub } from '@ad-execute-manager/event';
|
|
75
|
+
|
|
76
|
+
// Create a PubSub instance
|
|
77
|
+
const pubsub = new PubSub();
|
|
78
|
+
|
|
79
|
+
// Subscribe to an event
|
|
80
|
+
const unsubscribe = pubsub.on('message', (data) => {
|
|
81
|
+
console.log('Message received:', data);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// Publish an event
|
|
85
|
+
pubsub.emit('message', { text: 'Hello, world!' });
|
|
86
|
+
|
|
87
|
+
// Unsubscribe using the returned function
|
|
88
|
+
unsubscribe();
|
|
89
|
+
|
|
90
|
+
// Subscribe to an event only once
|
|
91
|
+
pubsub.once('notification', (notification) => {
|
|
92
|
+
console.log('Notification:', notification);
|
|
93
|
+
// This listener will be automatically removed after first execution
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// Publish the notification event
|
|
97
|
+
pubsub.emit('notification', { title: 'New Message', body: 'You have a new message' });
|
|
98
|
+
|
|
99
|
+
// Check listener count
|
|
100
|
+
const listenerCount = pubsub.listenerCount('message');
|
|
101
|
+
console.log('Message listeners:', listenerCount);
|
|
102
|
+
|
|
103
|
+
// Remove all listeners
|
|
104
|
+
// pubsub.removeAllListeners();
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## API
|
|
108
|
+
|
|
109
|
+
### EventEmitter
|
|
110
|
+
|
|
111
|
+
#### Constructor
|
|
112
|
+
|
|
113
|
+
```javascript
|
|
114
|
+
new EventEmitter(options)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
- **options** (Object): Configuration options
|
|
118
|
+
- **maxListeners** (Number): Maximum listeners per event, default 5
|
|
119
|
+
- **maxQueueSize** (Number): Maximum queue size for events with no listeners, default 1
|
|
120
|
+
- **maxOnceEvents** (Number): Maximum once events per event, default 5
|
|
121
|
+
|
|
122
|
+
#### Methods
|
|
123
|
+
|
|
124
|
+
- **on(event, listener)**: Register an event listener
|
|
125
|
+
- **event** (String): Event name
|
|
126
|
+
- **listener** (Function): Event listener function
|
|
127
|
+
|
|
128
|
+
- **once(event, listener)**: Register a one-time event listener
|
|
129
|
+
- **event** (String): Event name
|
|
130
|
+
- **listener** (Function): Event listener function
|
|
131
|
+
|
|
132
|
+
- **emit(event, ...args)**: Emit an event
|
|
133
|
+
- **event** (String): Event name
|
|
134
|
+
- **...args** (Any): Arguments to pass to listeners
|
|
135
|
+
|
|
136
|
+
- **off(event, listenerToRemove)**: Remove an event listener
|
|
137
|
+
- **event** (String): Event name
|
|
138
|
+
- **listenerToRemove** (Function): Listener function to remove
|
|
139
|
+
|
|
140
|
+
- **removeAllListeners(event)**: Remove all listeners for an event
|
|
141
|
+
- **event** (String, optional): Event name, omit to remove all listeners
|
|
142
|
+
|
|
143
|
+
- **static getInstance(args)**: Get singleton instance
|
|
144
|
+
- **args** (Object): Constructor options
|
|
145
|
+
- **returns** (EventEmitter): Singleton instance
|
|
146
|
+
|
|
147
|
+
### PubSub
|
|
148
|
+
|
|
149
|
+
#### Constructor
|
|
150
|
+
|
|
151
|
+
```javascript
|
|
152
|
+
new PubSub()
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
#### Methods
|
|
156
|
+
|
|
157
|
+
- **on(eventName, callback)**: Subscribe to an event
|
|
158
|
+
- **eventName** (String): Event name
|
|
159
|
+
- **callback** (Function): Event callback function
|
|
160
|
+
- **returns** (Function): Unsubscribe function
|
|
161
|
+
|
|
162
|
+
- **off(eventName, callback)**: Unsubscribe from an event
|
|
163
|
+
- **eventName** (String): Event name
|
|
164
|
+
- **callback** (Function): Callback function to remove
|
|
165
|
+
|
|
166
|
+
- **emit(eventName, ...args)**: Publish an event
|
|
167
|
+
- **eventName** (String): Event name
|
|
168
|
+
- **...args** (Any): Arguments to pass to callbacks
|
|
169
|
+
|
|
170
|
+
- **once(eventName, callback)**: Subscribe to an event once
|
|
171
|
+
- **eventName** (String): Event name
|
|
172
|
+
- **callback** (Function): Event callback function
|
|
173
|
+
|
|
174
|
+
- **listenerCount(eventName)**: Get listener count for an event
|
|
175
|
+
- **eventName** (String): Event name
|
|
176
|
+
- **returns** (Number): Listener count
|
|
177
|
+
|
|
178
|
+
- **removeAllListeners()**: Remove all listeners
|
|
179
|
+
|
|
180
|
+
## Examples
|
|
181
|
+
|
|
182
|
+
### Example 1: Application Event Bus
|
|
183
|
+
|
|
184
|
+
```javascript
|
|
185
|
+
import { EventEmitter } from '@ad-execute-manager/event';
|
|
186
|
+
|
|
187
|
+
// Create global event bus
|
|
188
|
+
const eventBus = EventEmitter.getInstance({
|
|
189
|
+
maxListeners: 20
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// User service
|
|
193
|
+
function loginUser(userData) {
|
|
194
|
+
// Login logic
|
|
195
|
+
console.log('Logging in user:', userData.username);
|
|
196
|
+
|
|
197
|
+
// Emit login event
|
|
198
|
+
eventBus.emit('userLoggedIn', {
|
|
199
|
+
user: userData,
|
|
200
|
+
timestamp: new Date().toISOString()
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Notification service
|
|
205
|
+
function setupNotifications() {
|
|
206
|
+
eventBus.on('userLoggedIn', (loginData) => {
|
|
207
|
+
console.log('Sending welcome notification to:', loginData.user.username);
|
|
208
|
+
// Send welcome notification
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
eventBus.on('userLoggedOut', (logoutData) => {
|
|
212
|
+
console.log('Updating user status to offline:', logoutData.userId);
|
|
213
|
+
// Update user status
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Analytics service
|
|
218
|
+
function setupAnalytics() {
|
|
219
|
+
eventBus.once('appStarted', (appData) => {
|
|
220
|
+
console.log('Recording app start event:', appData.version);
|
|
221
|
+
// Record app start analytics
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
eventBus.on('userLoggedIn', (loginData) => {
|
|
225
|
+
console.log('Recording login event:', loginData.user.username);
|
|
226
|
+
// Record login analytics
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Setup services
|
|
231
|
+
setupNotifications();
|
|
232
|
+
setupAnalytics();
|
|
233
|
+
|
|
234
|
+
// Start app
|
|
235
|
+
eventBus.emit('appStarted', { version: '1.0.0' });
|
|
236
|
+
|
|
237
|
+
// Login user
|
|
238
|
+
loginUser({ id: 1, username: 'john_doe', email: 'john@example.com' });
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### Example 2: Component Communication
|
|
242
|
+
|
|
243
|
+
```javascript
|
|
244
|
+
import { PubSub } from '@ad-execute-manager/event';
|
|
245
|
+
|
|
246
|
+
// Create pubsub instance for component communication
|
|
247
|
+
const componentBus = new PubSub();
|
|
248
|
+
|
|
249
|
+
// Header component
|
|
250
|
+
function Header() {
|
|
251
|
+
// Subscribe to user changes
|
|
252
|
+
const unsubscribe = componentBus.on('userChanged', (user) => {
|
|
253
|
+
console.log('Header: Updating user display:', user.name);
|
|
254
|
+
// Update header with user info
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
// Subscribe to theme changes
|
|
258
|
+
componentBus.on('themeChanged', (theme) => {
|
|
259
|
+
console.log('Header: Updating theme:', theme);
|
|
260
|
+
// Update header theme
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
// Cleanup function
|
|
264
|
+
return () => {
|
|
265
|
+
unsubscribe();
|
|
266
|
+
console.log('Header: Unsubscribed from events');
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// User profile component
|
|
271
|
+
function UserProfile() {
|
|
272
|
+
// Subscribe to user changes
|
|
273
|
+
const unsubscribe = componentBus.on('userChanged', (user) => {
|
|
274
|
+
console.log('UserProfile: Updating user profile:', user.name);
|
|
275
|
+
// Update user profile
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
// Cleanup function
|
|
279
|
+
return () => {
|
|
280
|
+
unsubscribe();
|
|
281
|
+
console.log('UserProfile: Unsubscribed from events');
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Settings component
|
|
286
|
+
function Settings() {
|
|
287
|
+
// Update user
|
|
288
|
+
function updateUser(userData) {
|
|
289
|
+
console.log('Settings: Updating user');
|
|
290
|
+
componentBus.emit('userChanged', userData);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Change theme
|
|
294
|
+
function changeTheme(theme) {
|
|
295
|
+
console.log('Settings: Changing theme to:', theme);
|
|
296
|
+
componentBus.emit('themeChanged', theme);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
return {
|
|
300
|
+
updateUser,
|
|
301
|
+
changeTheme
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// Initialize components
|
|
306
|
+
const cleanupHeader = Header();
|
|
307
|
+
const cleanupUserProfile = UserProfile();
|
|
308
|
+
const settings = Settings();
|
|
309
|
+
|
|
310
|
+
// Simulate user update
|
|
311
|
+
settings.updateUser({ id: 1, name: 'John Doe', email: 'john@example.com' });
|
|
312
|
+
|
|
313
|
+
// Simulate theme change
|
|
314
|
+
settings.changeTheme('dark');
|
|
315
|
+
|
|
316
|
+
// Cleanup components
|
|
317
|
+
// cleanupHeader();
|
|
318
|
+
// cleanupUserProfile();
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
### Example 3: Event Queueing
|
|
322
|
+
|
|
323
|
+
```javascript
|
|
324
|
+
import { EventEmitter } from '@ad-execute-manager/event';
|
|
325
|
+
|
|
326
|
+
// Create event emitter with queueing
|
|
327
|
+
const emitter = new EventEmitter({
|
|
328
|
+
maxQueueSize: 10
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
// Emit events before listeners are registered
|
|
332
|
+
console.log('Emitting events before listeners...');
|
|
333
|
+
emitter.emit('data', { value: 1 });
|
|
334
|
+
emitter.emit('data', { value: 2 });
|
|
335
|
+
emitter.emit('data', { value: 3 });
|
|
336
|
+
|
|
337
|
+
// Register listener - will receive queued events
|
|
338
|
+
console.log('Registering listener...');
|
|
339
|
+
emitter.on('data', (data) => {
|
|
340
|
+
console.log('Received data:', data);
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
// Emit more events
|
|
344
|
+
console.log('Emitting more events...');
|
|
345
|
+
emitter.emit('data', { value: 4 });
|
|
346
|
+
emitter.emit('data', { value: 5 });
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
## License
|
|
350
|
+
|
|
351
|
+
MIT
|
package/package.json
CHANGED