-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-basic.ts
More file actions
48 lines (36 loc) Β· 1.63 KB
/
01-basic.ts
File metadata and controls
48 lines (36 loc) Β· 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Basic PatternEmitter Example
*
* Shows the fundamental difference between string events and regex patterns
*/
import {PatternEmitter} from '../src/index';
const emitter = new PatternEmitter();
console.log('=== BASIC PATTERN EMITTER EXAMPLE ===\n');
// 1. String event listeners - exact match only
console.log('1οΈβ£ String Event Listener (exact match):');
emitter.on('user:login', (username: string) => {
console.log(` β String listener: ${username} logged in`);
});
// 2. Regex pattern listeners - match multiple events
console.log('\n2οΈβ£ Regex Pattern Listener (matches multiple):');
emitter.on(/^user:/, (username: string, action: string) => {
console.log(` β Regex listener: User action detected - ${action}`);
});
// 3. Multiple patterns can match the same event
console.log('\n3οΈβ£ Multiple Patterns Matching:');
emitter.on(/login$/, (username: string) => {
console.log(` β Login pattern: Someone logged in`);
});
// Emit events and see which listeners fire
console.log('\n--- Emitting Events ---\n');
console.log('Emit: "user:login" with "Alice"');
emitter.emit('user:login', 'Alice', 'login');
console.log('\nEmit: "user:logout" with "Bob"');
emitter.emit('user:logout', 'Bob', 'logout');
console.log('\nEmit: "admin:login" with "Charlie"');
emitter.emit('admin:login', 'Charlie', 'login');
// 4. Listener counts and introspection
console.log('\n--- Introspection ---\n');
console.log(`Listeners for "user:login": ${emitter.listenerCount('user:login')}`);
console.log(`Event names registered: ${emitter.eventNames().join(', ')}`);
console.log(`Regex patterns registered: ${emitter.eventPatterns().join(', ')}`);