@byungi/event-emitter
v1.2.2
Published
Small (< 600Byte), type-safe event emitter that support old browsers
Downloads
105
Readme
@byungi/event-emitter
Small (< 600Byte), type-safe event emitter that support old browsers
Install
npm i @byungi/event-emitter
UMD
<script src="https://unpkg.com/@byungi/event-emitter"></script>
<script>
const emitter = new EventEmitter.default();
</script>
Browser ESM
<script type="module">
import EventEmitter from 'https://unpkg.com/@byungi/event-emitter/dist/index.esm.js'
const emitter = new EventEmitter();
</script>
Example
interface Events {
a(): void
b(a: number, b: string): void
}
const emitter = new EventEmitter<Events>()
// ✔️ Compiled.
emitter.on('a', () => { /.../ })
emitter.on('b', (a, b) => { /.../ })
emitter.emit('a')
emitter.emit('b', 100, 'test')
// ❌ Compile error.
emitter.on('a', (a, b) => { /.../ })
emitter.on('b', (other:boolean) => { /.../ })
emitter.on('c', () => { /.../ })
emitter.emit('a', 100, 'test')
emitter.emit('b')
emitter.emit('c')
Browser compatibility
IE6+
API
new EventEmitter()
Create an event emitter instance.
emitter.on(name, listener)
Add an event listener. Returns function to off.
const off = emitter.on('a', listener)
off() // == emitter.off('a', listener)
ee.off(name[, listener])
Remove an event listener(s).
ee.has(name[, listener])
Returns whether there is an event listener.
ee.emit(name, ...params)
Emit event to listeners.
ee.once(name, listener)
Add an event listener that runs once.
License
MIT