New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

mittt

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mittt

npmnpm
Version
1.0.0
Version published
Weekly downloads
131
122.03%
Maintainers
1
Weekly downloads
 
Created
Source

npm gzip size


Mittt

Tiny functional event emitter / pubsub.
Forked from https://github.com/developit/mitt
New project created from TSDX CLI.

  • Microscopic: weighs ~200 bytes gzipped size.
  • Functional: methods don't rely on this

Mittt was made for the browser, but works in any JavaScript runtime. It has no dependencies and supports IE9+.

Table of Contents

Install

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

$ npm install mittt
# or
# yarn add mittt

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

// using ES6 modules
import mittt from 'mittt'

// using CommonJS modules
var mittt = require('mittt')

The ESM build is also available on unpkg:

<script type="module" src="https://unpkg.com/mittt/dist/mittt.esm.js"></script>

Usage

import mittt from 'mittt'

const emitter = mittt()

function onEvent(eventType, payload) {
  console.log(eventType, payload)
}

// Listen to an event
emitter.on('foo', onEvent)

// Fire an event
emitter.emit('foo')

// Fire an event with payload
const payload = { a: 'b' }
emitter.emit('bar', payload)

// Working with handler references:
emitter.on('foo', onEvent) // listen
emitter.off('foo', onEvent) // unlisten

// Initiate emitter with on event setup
const emitter = mittt({
  foo: [
    (eventType, payload) => {
      console.log(eventType, payload) // foo, undefined
    },
    (eventType, payload) => {
      console.log(eventType, payload) // foo, undefined
    },
  ],
})

emitter.emit('foo') // all handlers for foo are invoked

TypeScript

import mittt, { Emitter, EventHandler } from 'mittt'
const emitter: Emitter = mittt()

let foo: EventHandler = (eventType, payload) => {}

emitter.on('foo', foo)

FAQs

Package last updated on 03 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts