English | 中文文档
Frame-independent event manager, no more than 400 bytes after Gzip.
- Install
npm install --save mittee
- Import
// ES6 modules
import mittee from 'mittee'
// CommonJS modules
const mittee = require('mittee')
import mittee from 'mittee'
const mit = mittee()
// Listen for events
mit.on('event', params => console.log('event', params) )
// Listen for all events
mit.on('*', (eventName, params) => console.log(eventName, params) )
// Listen for an event once
mit.once('once', params => console.log('once', params))
// Trigger an event
mit.emit('event', { name: 'Mittee' }) // event { name: 'Mittee' }
mit.emit('once', { name: 'Mittee' }) // once { name: 'Mittee' }
mit.emit('once', { name: 'Mittee' }) // no console
// Clear all events
mit.all.clear()
// Using the de-listening function, pass in the function reference
function onFoo() {}
mit.on('foo', onFoo) // on
mit.off('foo', onFoo) // off
Support for TypeScript
import mittee, { Mittee } from 'mittee'
const mit: Mittee = mittee()
Mittee: A super tiny event bus.
Returns Mittee
The mapping table of the event name and the callback function array
Register the event
evtName
(string | symbol) Event name, '*' means all eventshandler
Function Specifies the callback function for the event
Returns void
Register an event and trigger it only once
evtName
(string | symbol) Event name, '*' means all eventshandler
Function Specifies the callback function for the event
Returns void
Remove the event
evtName
(string | symbol) Event name, '*' means all eventshandler
Function Specify the callback function
Returns void
Trigger an event If a function exists under the event '*', it is called after the callback function for the specified event has been executed
Note: Callback functions that manually trigger the event '*' are not supported
Returns void