forked from ultraworkers/claw-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemitter.ts
More file actions
39 lines (31 loc) · 1.1 KB
/
Copy pathemitter.ts
File metadata and controls
39 lines (31 loc) · 1.1 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
import { EventEmitter as NodeEventEmitter } from 'events'
import { Event } from './event.js'
// Similar to node's builtin EventEmitter, but is also aware of our `Event`
// class, and so `emit` respects `stopImmediatePropagation()`.
export class EventEmitter extends NodeEventEmitter {
constructor() {
super()
// Disable the default maxListeners warning. In React, many components
// can legitimately listen to the same event (e.g., useInput hooks).
// The default limit of 10 causes spurious warnings.
this.setMaxListeners(0)
}
override emit(type: string | symbol, ...args: unknown[]): boolean {
// Delegate to node for `error`, since it's not treated like a normal event
if (type === 'error') {
return super.emit(type, ...args)
}
const listeners = this.rawListeners(type)
if (listeners.length === 0) {
return false
}
const ccEvent = args[0] instanceof Event ? args[0] : null
for (const listener of listeners) {
listener.apply(this, args)
if (ccEvent?.didStopImmediatePropagation()) {
break
}
}
return true
}
}