Git Product home page Git Product logo

async-mitt's Introduction

mitt
npm build status gzip size

async-mitt

Tiny functional Event Emitter / pubsub with async support.

This is a fork of developit/mitt with added support for async event handlers.

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 async-mitt

$ yarn add async-mitt

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

// using ES6 modules
import asyncMitt from 'async-mitt';

// using CommonJS modules
var asyncMitt = require('async-mitt');

The UMD build is also available on unpkg:

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

You can find the library on window.mitt.

Usage

import asyncMitt from 'async-mitt';

const emitter = asyncMitt();

// listen to an async event
emitter.onAsync('foo', async (e) => await doSomethingAsync(e));

// listen to all async events
emitter.onAsync('*', async (type, e) => await doSomethingAsync(type, e));

// fire an event
await emitter.emitAsync('foo', data);

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

// working with handler references:
async function onFoo() {}
emitter.onAsync('foo', onFoo); // listen
emitter.offAsync('foo', onFoo); // unlisten

Typescript

Set "strict": true in your tsconfig.json to get improved type inference for mitt instance methods.

import asyncMitt from 'async-mitt';

type Events = {
  foo: string;
  bar?: number;
};

type AsyncEvents = {
  foo: number;
  bar?: string;
};

const emitter = asyncMitt<Events, AsyncEvents>(); // inferred as Emitter<Events, AsyncEvents>

emitter.on('foo', (e) => {}); // 'e' has inferred type 'string'
emitter.onAsync('foo', async (e) => {}); // 'e' has inferred type 'number'

emitter.emit('foo', 42); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. (2345)
await emitter.emitAsync('foo', 'bar'); // Error: Argument of type 'string' is not assignable to parameter of type 'number'. (2345)

API

Table of Contents

asyncMitt

async-mitt: Tiny functional event emitter / pubsub with support for async events.

Returns asyncMitt

all

A Map of event names to registered handler functions.

allAsync

A Map of async 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 ('*' to remove a wildcard handler)
  • 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

onAsync

Registers an asynchronous event handler for the specified event type.

Parameters

  • type (string | symbol) The async event type to listen for.
  • handler Function? The asynchronous event handler function.

offAsync

Removes an asynchronous event handler for the specified event type.

Parameters

  • type Key The async event type to remove the handler for.
  • handler Function? The handler function to remove. If not provided, all handlers for the specified event type will be removed.

emitAsync

Asynchronously emits an event of the specified type and invokes all registered handlers.

Parameters

  • type (string | symbol) The type of the event.
  • evt Any The event data.

Returns Promise<void> A promise that resolves when all handlers have been invoked.

Contribute

First off, thanks for taking the time to contribute! Now, take a moment to be sure your contributions make sense to everyone else.

Reporting Issues

Found a problem? Want a new feature? First of all see if your issue or idea has already been reported. If don't, just open a new clear and descriptive issue.

License

MIT License ยฉ Moritz Reisinger

async-mitt's People

Contributors

developit avatar thiamsantos avatar sealice avatar jackfranklin avatar tunnckocore avatar rvikmanis avatar skellock avatar itoxiq avatar jaylinski avatar lukechilds avatar lukeed avatar andarist avatar mathiasbynens avatar mister-hope avatar pmg103 avatar seveves avatar dotproto avatar styfle avatar tungv avatar zbuttram avatar iyegoroff avatar oksas avatar jesperzach avatar dhaxor avatar floriangosse avatar esebastian avatar gamtiq avatar davidkpiano avatar danielruf avatar bwendt-mylo avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.