🌳 Simple CustomEvent listener and emitter
$ npm install @emoji-gen/ptero --save # for npm users
$ yarn add @emoji-gen/ptero # for yarn users
import { Ptero } from '@emoji-gen/ptero'
const ptero = new Ptero()
ptero.on('eventname', e => {
console.log(e.detail) // => { foo: 1 }
})
ptero.emit('eventname', { foo: 1 })
- Supports TypeScript
- Supports both CommonJS and ESModules
Create new Ptero
instance.
// Default target is `window`
const ptero = new Ptero()
// Set `document.body` for an event target
const ptero = new Ptero(document.body)
Listen a single custom event.
ptero.addListener('eventname', e => {
console.log(e.detail)
})
Listen a single custom event or multi custom events.
ptero.on('eventname', e => {
console.log(e.detail)
})
ptero.on(['eventname1', 'eventname2'], e => {
console.log(e.detail)
})
Stop listening a single custom event.
const listener = e => {
console.log(e.detail)
}
ptero.addListener('eventname', listener)
ptero.removeListener('eventname', listener)
Stop listening a single custom event or multi custom events.
const listener = e => {
console.log(e.detail)
}
ptero.on('eventname1', listener)
ptero.on(['eventname2', 'eventname3'], listener)
ptero.off('eventname1', listener)
ptero.off(['eventname2', 'eventname3'], listener)
Dispatch a single custom event.
ptero.emit('eventname', { foo: 1 })
$ yarn start # for development
$ yarn run build # for production
$ yarn test # run tests
MIT © Emoji Generator