mitt

Tiny 200b functional event emitter / pubsub.

mitt
Mitt: tiny 200b functional event emitter / pubsub.
npm travis

Why Mitt?

  • Microscopic: weighs less than 200 bytes gzipped
  • Useful: a wildcard "*" event type listens to all events
  • Famliar: same names & ideas as Node’s EventEmitter
  • Functional: methods don’t rely on this
  • Great Name: somehow mitt wasn’t taken

Mitt was made for the browser, but works in any JavaScript runtime. It has no dependencies and only uses basic language features. It probably works in Internet Explorer 5.


Usage

After installing via npm install --save mitt:

import mitt from 'mitt'

let emitter = mitt()

// listen to an event
emitter.on('foo', e => console.log('foo', e) )

// listen to all events
emitter.on('*', (type, e) => console.log(type, e) )

// fire an event
emitter.emit('foo', { a: 'b' })

// working with handler references:
function onFoo() {}
emitter.on('foo', onFoo)   // listen
emitter.off('foo', onFoo)  // unlisten

API

mitt

Mitt: Tiny (~200b) functional event emitter / pubsub.

Returns Mitt

on

Register an event handler for the given type.

Parameters

  • type String Type of event to listen for, or "*" for all events
  • handler Function Function to call in response to the given event

off

Remove an event handler for the given type.

Parameters

  • type String Type of event to unregister handler from, or "*"
  • handler Function Handler function to remove

emit

Invoke all handlers for the given type. If present, "*" handlers are invoked prior to type-matched handlers.

Parameters

  • type String The event type to invoke
  • event [Any] An event object, passed to each handler

Related Repositories

mitt

mitt

Tiny 200b functional event emitter / pubsub. ...

smitty

smitty

Tiny flux implementation built on mitt ...

homemirror

homemirror

Web based home mirror, inspired by Hannah Mitt's project. ...

postmark-mitt

postmark-mitt

Little library for composing JSON from Postmark's incoming email api back into an object ...

Mitt-Saldo

Mitt-Saldo

Mitt Saldo is a multi-banking application for iPhone and iPad. ...