mitt
deno module gzip size

Mitt

Tiny 200b functional event emitter / pubsub.

  • Zero dependencies: doesn't rely on any other package or polyfill
  • Microscopic: weighs less than 200 bytes gzipped
  • Useful: a wildcard "*" event type listens to all events
  • Familiar: same names & ideas as Node's EventEmitter
  • Functional: methods don't rely on this

Table of Contents

Usage

import mitt from "mitt"


const 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" })

// clearing all events
emitter.all.clear()

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

Examples & Demos

Example in Replit

API


Table of Contents

mitt

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

Returns Mitt

all

A Map of event names to registered handler functions.

on

Register an event handler for the given type.

Parameters

  • type (string | symbol) Type of event to listen for, or '*' for all events
  • handler Function Function to call in response to given event

off

Remove an event handler for the given type. If handler is omitted, all handlers of the given type are removed.

Parameters

  • type (string | symbol) 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 after type-matched handlers.

Note: Manually firing '*' handlers is not supported.

Table of Contents

mitt

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

Returns Mitt

all

A Map of event names to registered handler functions.

on

Register an event handler for the given type.

Parameters

  • type (string | symbol) Type of event to listen for, or "*" for all events
  • handler Function Function to call in response to given event

off

Remove an event handler for the given type. If handler is omitted, all handlers of the given type are removed.

Parameters

  • type (string | symbol) 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 after type-matched handlers.

Note: Manually firing "*" handlers is not supported.

Parameters

  • type (string | symbol) The event type to invoke
  • evt Any? Any value (object is recommended and powerful), passed to each handler

License

MIT License © Jason Miller, Petr Kolonicz