Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: thomasnordquist <7721625+thomasnordquist@users.noreply.github.com> Co-authored-by: Thomas Nordquist <thomasnordquist@users.noreply.github.com>
44 lines
960 B
TypeScript
44 lines
960 B
TypeScript
import { EventEmitter } from 'events'
|
|
|
|
interface CallbackStore {
|
|
wrappedCallback: any
|
|
callback: any
|
|
}
|
|
|
|
export class EventDispatcher<Message> {
|
|
private emitter = new EventEmitter()
|
|
|
|
private callbacks: Array<CallbackStore> = []
|
|
|
|
public dispatch(msg: Message) {
|
|
this.emitter.emit('event', msg)
|
|
}
|
|
|
|
public subscribe(callback: (msg: Message) => void) {
|
|
const wrappedCallback = (msg: Message) => {
|
|
callback(msg)
|
|
}
|
|
this.emitter.on('event', wrappedCallback)
|
|
|
|
this.callbacks.push({
|
|
callback,
|
|
wrappedCallback,
|
|
})
|
|
}
|
|
|
|
public unsubscribe(callback: (msg: Message) => void) {
|
|
const item = this.callbacks.find(store => store.callback === callback)
|
|
if (!item) {
|
|
return
|
|
}
|
|
|
|
this.emitter.removeListener('event', item.wrappedCallback)
|
|
this.callbacks = this.callbacks.filter(a => a !== item)
|
|
}
|
|
|
|
public removeAllListeners() {
|
|
this.emitter.removeAllListeners()
|
|
this.callbacks = []
|
|
}
|
|
}
|