| 3 | type Callback = () => void; |
| 4 | |
| 5 | class EventEmitter { |
| 6 | private static instance: EventEmitter; |
| 7 | private events: { [eventName: string]: Callback[] } = {}; |
| 8 | |
| 9 | private constructor() {} |
| 10 | |
| 11 | public static getInstance(): EventEmitter { |
| 12 | if (!EventEmitter.instance) { |
| 13 | EventEmitter.instance = new EventEmitter(); |
| 14 | } |
| 15 | return EventEmitter.instance; |
| 16 | } |
| 17 | |
| 18 | on(eventName: EventType, callback: Callback) { |
| 19 | if (!this.events[eventName]) { |
| 20 | this.events[eventName] = []; |
| 21 | } |
| 22 | this.events[eventName].push(callback); |
| 23 | } |
| 24 | |
| 25 | emit(eventName: EventType) { |
| 26 | const callbacks = this.events[eventName]; |
| 27 | if (callbacks) { |
| 28 | callbacks.forEach((callback) => callback()); |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | const getEventEmitter = () => { |
| 34 | return EventEmitter.getInstance(); |
nothing calls this directly
no outgoing calls
no test coverage detected