event emitters
npm install event-emitters


Typesafe event emitters.
Similar to node's EventEmitter but:
- Only emits a single event of type T.
- Throw an exception when a client registers the same listener more than once.
- Throw an exception when a client tries to remove a listener that is not listening.
- Support async iteration.
``typescript
const emitter = new EventEmitter
const listener = (n: number): void => {
console.log(n)
}
emitter.subscribe(listener)
emitter.emit(42)
try {
emitter.subscribe(listener)
} catch (e) {
console.log('same listener')
}
emitter.unsubscribe(listener)
try {
emitter.unsubscribe(() => {})
} catch (e) {
console.log('listener not found')
}
`
The above will log:
``
42
same listener
listener not found
`typescript
const emitter = new EventEmitter
async function logEmitterValues(emitter: EventEmitter
for await (const val of emitter) {
console.log(val)
}
}
logEmitterValues(emitter)
emitter.emit(42)
emitter.emit(43)
`
The above will log:
``
42
43
`typescript`
const emitter = new EventEmitter
const listener = emitter.subscribe((val: number) => {
console.log(val)
})
emitter.unsubscribe(listener)
subscribe returns the callback passed to it which can be useful for unsubscribing later.
Provides the same API as EventEmitter but:
- Is initialized with the current message.
- Emits the current message to each listener as soon as it subscribes.
`typescript`
const emitter = new EventEmitterWithCurrent
emitter.subscribe((n: number): void => {
console.log(n)
})
emitter.emit(43)
The above will log:
``
42
43
Provides the same API as EventEmitterWithCurrent but:
- Can optionally be initialized with the current message.
- Emits the current message to each listener as soon as it subscribes only when the current message is available.
`typescript`
const emitter = new EventEmitterWithCurrent
emitter.subscribe((n: number): void => {
console.log(n)
})
emitter.emit(43)
The above will log:
``
43
Provides the same API as EventEmitter but:
- Queues messages when there are no subscribers.
- Delivers queued message to the first subscriber, then drains the queue.
`typescript`
const emitter = new EventEmitterWithCurrent
emitter.emit(44)
emitter.emit(45)
emitter.subscribe((n: number): void => {
console.log(n)
})
emitter.emit(46)
The above will log:
```
44
45
46