It would be useful to change the way to pass the event name so that the listener signature does not change if it starts to listener to multiple events.
it could use the context but it rules out the use of arrow function:
interface Event {
name: string;
}
class Emittery {
on(name: string|string[], listener: (this: Event, data: any) => any): () => void {}
onAny(listener: (this: Event, data: any) => any): () => void {}
}
Or the data and the event could be sent together:
interface Event {
name: string;
data: any;
}
class Emittery {
on(name: string|string[], listener: (ev: Event) => any): () => void {}
onAny(listener: (ev: Event) => any): () => void {}
// Would behave like `Promise.race` but would unsubscribe the waiting events
once(name: string|string[]): Promise<Event>
}
Alternatively, there could be an extra method:
class Emittery {
on(name: string, listener: (data: any) => any): () => void {}
onMulti(name: string[], listener: (name: string, data: any) => any): () => void {}
onAny(listener: (name: string, data: any) => any): () => void {}
}