Events
Simplified and TypeScripted version of EventEmitter3@4.0.7
(no support for Symbols)
Installation
npm install @yandeu/events
CDN
// ES2015+
https://unpkg.com/@yandeu/events/umd/events.min.js
// ES5
https://unpkg.com/@yandeu/events/umd/events.es5.min.js
Usage
const { Events } = require('@yandeu/events')
const events = new Events()
events.on('message', msg => {
console.log(`Message: ${msg}`)
})
events.emit('message', 'Hello there!')
console.log('Events VERSION: ', Events.VERSION)
TypeScript
import { Events } from '@yandeu/events'
interface EventMap {
signal: () => void
error: (err: string) => void
something: (a: number, b: { color?: string }, c: [number, number, string]) => void
}
const events = new Events<EventMap>()
events.on('something', (a, b, c) => {
console.log(a, b.color, c)
})
events.emit('something', 1, { color: 'blue' }, [2, 2, 'k'])
import type { EventListener } from '@yandeu/events'
const listener: EventListener<EventMap, 'error'> = err => {
console.log('err:', err)
}
events.on('error', listener)
events.removeListener('error', listener)
License
MIT