mittee

A super tiny event bus

Usage no npm install needed!

<script type="module">
  import mittee from 'https://cdn.skypack.dev/mittee';
</script>

README

Mittee

English | 中文文档

Frame-independent event manager, no more than 400 bytes after Gzip.

Import

  1. Install
npm install --save mittee
  1. 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 events
  • handler 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 events
  • handler Function Specifies the callback function for the event

Returns void

off

Remove the event

Parameters

  • evtName (string | symbol) Event name, '*' means all events
  • handler 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

  • evtName (string | symbol) Event name
  • params Any? The argument passed to the callback function

Returns void