1.0.5 • Published 3 years ago

mitt-emitter v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

mitt-emitter

This is a redesigned package to add the concept of topics like RabbitMQ. You can now use the special characters "*" and "#" to get a topic.

The original package you can find at: https://github.com/developit/mitt - © Jason Miller

Table of Contents

Install

This project uses node and npm. Go check them out if you don't have them locally installed.

$ npm install --save mitt-emitter

Then with a module bundler like rollup or webpack, use as you would anything else:

// using ES6 modules
import MittEmitter from 'mitt-emitter'

// using CommonJS modules
var MittEmitter = require('mitt-emitter')

The UMD build is also available on unpkg:

<script src="https://unpkg.com/mitt-emitter/dist/mitt-emitter.umd.js"></script>

Usage

import MittEmitter from 'mitt-emitter'

const emitter = new MittEmitter();

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

// listen topic event
emitter.on('organization.*.device.1', (type, e) => console.log(type, e) )
emitter.emit('organization.1.device.1', { a: 'b' })

// listen topic event
emitter.on('organization.*.device.*', (type, e) => console.log(type, e) )
emitter.emit('organization.2.device.2', { a: 'b' })

// listen topic event
emitter.on('organization.*.device.#', (type, e) => console.log(type, e) )
emitter.emit('organization.1.device.1.apple.linux', { a: 'b' })

// listen topic event
emitter.on('organization.#', (type, e) => console.log(type, e) )
emitter.emit('organization.1.device.1.apple.linux', { a: 'b' })

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

License

MIT License © Thiago Anselmo

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago