Package Exports
- @byungi/event-emitter
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@byungi/event-emitter) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@byungi/event-emitter
Small (< 600Byte), type-safe event emitter that support old browsers
Install
npm i @byungi/event-emitterUMD
<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