Mitt

mitt

Tiny 200b functional event emitter / pubsub.

Sweet little implementation of pubsub patter, uses flow internally and provides the below functionality:

  • on : Register an event handler for the given type.

  • off : Remove an event handler for the given type.

  • emit: Invoke all handlers for the given type.

Get it: npm install mitt

Sample usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const mitt = require('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' })

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

GIF FTW!

mitt

Suggest a module

Comments