Code Monkey home page Code Monkey logo

emittery's Introduction

Simple and modern async event emitter

Build Status codecov

It's only ~200 bytes minified and gzipped. I'm not fanatic about keeping the size at this level though.

Emitting events asynchronously is important for production code where you want the least amount of synchronous operations.

Install

$ npm install emittery

Usage

const Emittery = require('emittery');
const emitter = new Emittery();

emitter.on('๐Ÿฆ„', data => {
	console.log(data);
	// '๐ŸŒˆ'
});

emitter.emit('๐Ÿฆ„', '๐ŸŒˆ');

Node.js 4 and 6

The above only works in Node.js 8 or newer. For older Node.js versions you can use require('emittery/legacy').

API

emitter = new Emittery()

on(eventName, listener)

Subscribe to an event.

Returns an unsubscribe method.

Using the same listener multiple times for the same event will result in only one method call per emitted event.

listener(data)

off(eventName, [listener])

Unsubscribe to an event.

If you don't pass in a listener, it will remove all listeners for that event.

listener(data)

once(eventName)

Subscribe to an event only once. It will be unsubscribed after the first event.

Returns a promise for the event data when eventName is emitted.

emitter.once('๐Ÿฆ„').then(data => {
	console.log(data);
	//=> '๐ŸŒˆ'
});

emitter.emit('๐Ÿฆ„', '๐ŸŒˆ');

emit(eventName, [data])

Trigger an event asynchronously, optionally with some data. Listeners are called in the order they were added, but execute concurrently.

Returns a promise for when all the event listeners are done. Done meaning executed if synchronous or resolved when an async/promise-returning function. You usually wouldn't want to wait for this, but you could for example catch possible errors. If any of the listeners throw/reject, the returned promise will be rejected with the error, but the other listeners will not be affected.

emitSerial(eventName, [data])

Same as above, but it waits for each listener to resolve before triggering the next one. This can be useful if your events depend on each other. Although ideally they should not. Prefer emit() whenever possible.

If any of the listeners throw/reject, the returned promise will be rejected with the error and the remaining listeners will not be called.

onAny(listener)

Subscribe to be notified about any event.

Returns a method to unsubscribe.

listener(eventName, data)

offAny([listener])

Unsubscribe an onAny listener.

If you don't pass in a listener, it will remove all onAny listeners.

clear()

Clear all event listeners on the instance.

listenerCount([eventName])

The number of listeners for the eventName or all events if not specified.

FAQ

How is this different than the built-in EventEmitter in Node.js?

There are many things to not like about EventEmitter: its huge API surface, synchronous event emitting, magic error event, flawed memory leak detection. Emittery has none of that.

Isn't EventEmitter synchronous for a reason?

Mostly backwards compatibility reasons. The Node.js team can't break the whole ecosystem.

It also allows silly code like this:

let unicorn = false;

emitter.on('๐Ÿฆ„', () => {
	unicorn = true;
});

emitter.emit('๐Ÿฆ„');

console.log(unicorn);
//=> true

But I would argue doing that shows a deeper lack of Node.js and async comprehension and is not something we should optimize for. The benefit of async emitting is much greater.

Can you support multiple arguments for emit()?

No, just use destructuring:

emitter.on('๐Ÿฆ„', ([foo, bar]) => {
	console.log(foo, bar);
});

emitter.emit('๐Ÿฆ„', [foo, bar]);

Related

  • p-event - Promisify an event by waiting for it to be emitted

License

MIT ยฉ Sindre Sorhus

emittery's People

Contributors

dangh avatar dinoboff avatar fregante avatar linusu avatar novemberborn avatar sindresorhus avatar

Watchers

 avatar  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.