mittee
v0.0.2
Published
A super tiny event bus
Downloads
9
Readme
Mittee
English | 中文文档
Frame-independent event manager, no more than 400 bytes after Gzip.
Import
- Install
npm install --save mittee
- Import
// ES6 modules
import mittee from 'mittee'
// CommonJS modules
const mittee = require('mittee')
Usage
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()
APIs
mittee
Mittee: A super tiny event bus.
Returns Mittee
all
The mapping table of the event name and the callback function array
on
Register the event
Parameters
evtName
(string | symbol) Event name, '*' means all eventshandler
Function Specifies the callback function for the event
Returns void
once
Register an event and trigger it only once
Parameters
evtName
(string | symbol) Event name, '*' means all eventshandler
Function Specifies the callback function for the event
Returns void
off
Remove the event
Parameters
evtName
(string | symbol) Event name, '*' means all eventshandler
Function Specify the callback function
Returns void
emit
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
Parameters
Returns void