0.0.0 • Published 5 months ago

async-mitt v0.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

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\ 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