GithubHelp home page GithubHelp logo

emmett's Introduction

Emmett

A custom event emitter for Node.js and the browser.

Its aim is to provide its user with a lot of event emitting sugar while remaining lightweight and fast.

Installation

You can install Emmett through npm:

npm install --save emmett

Usage

Creating an emitter

var Emitter = require('emmett');

var emitter = new Emitter();

Extending the emitter

Node.js

var util = require('util'),
    Emitter = require('emmett');

function MyObject() {
	Emitter.call(this);
}

util.inherits(MyObject, Emitter);

ES6 class

import Emitter from 'emmett';

class MyObject extends Emitter {
	/* ... */
}

Listening to events

// Basic
emitter.on('eventName', callback);

// Once
emitter.once('eventName', callback);

// Using ES6 symbol as event name
const sym = Symbol();
emitter.on(sym, callback);

// Matching event names with a regex
emitter.on(/^event/, callback);

// Options
emitter.on('eventName', callback, {scope: customScope, once: true});

// Polymorphisms
emitter.on(['event1', 'event2'], callback);

emitter.on({
	event1: callback1,
	event2: callback2
});

// Listening to every events
emitter.on(callback);

Event data

Events are objects having the following keys:

  • data: the data attached to the event.
  • type: the event type.
  • target: the event emitter.
emitter.on('myEvent', function(e) {
	console.log(e.data);
});

emitter.emit('myEvent', 'Hello World!');

// Will print "Hello World!" in the console

Removing listeners

// Basic
emitter.off('eventName', callback);

// Removing every listeners attached to the given event
emitter.off('eventName');

// Removing the callback from any event
emitter.off(callback);

// Polymorphisms
emitter.off(['event1', 'event2'], callback);

emitter.off({
	event1: callback1,
	event2: callback2
});

// Removing every listeners
emitter.unbindAll();

Emitting

// Basic
emitter.emit('eventName');

// With data
emitter.emit('eventName', {hello: 'world'});

// Polymorphisms
emitter.emit(['event1', 'event2']);
emitter.emit(['event1', 'event2'], {hello: 'world'});

emitter.emit({
	event1: 'hey',
	event2: 'ho'
});

Retrieving listeners

// Return every matching handlers for a given event name
emitter.listeners('eventName');

Disabling an emitter

While disabled, emitting events won't produce nothing.

emitter.disable();
emitter.enable();

Killing an emitter

Killing an emitter will remove all its listeners and make it inoperant in the future.

emitter.kill();

Contribution

Do not hesitate to contribute to the library. Be sure to add and pass any relevant unit test before submitting any code.

# Installing the dev version
git clone http://github.com/jacomyal/emmett
cd emmett

# Installing dependencies
npm install

# Running unit tests
npm test

# Lint the code
npm run lint

emmett's People

Contributors

ayalpani avatar christianalfoni avatar codyray avatar iaspin92 avatar jacomyal avatar matthewwithanm avatar mjhasbach avatar yomguithereal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

emmett's Issues

Event names enumerator

Can we add some option to enumerate all possible event names?

This is valuable because:

  1. users will have an official place to check for possible emit options
  2. we get errors when event name is mistyped

Something like:

import Emmett from "emmett";

let emitter = new Emmett([
  "scale.mouseDown"
]);

emitter.on("scale.mouseDown", (data) => {
  console.log("scale.mouseDown!");
});

emitter.emit("scale.mouseDown"); // CONSOLE.LOG
emitter.emit("scale.mouseUp"); // ERROR!

I don't know how it's better to adopt this to custom emitters (inheritance) but there are some ways...

Symbol-keys are ignored when using pair events

Hi there,

just noticed you've added Symbol support. There is one bug though when using pair events like this:

myEmitter.on({
  myEvent1: function(e) {  },
  myEvent2: function(e) {  }
});

Since Symbols can not be enumerated with for ... in or Object.keys(events).on they are just being ignored in such cases.

Sorry, I don't have time for a proper PR, but here is a possible solution

// instead of using `for ... in` you can process each key type individually

// Symbol-keys
Object.getOwnPropertySymbols(events).forEach(function(event){
     // this.on/off/etc....
});

// String-keys
Object.getOwnPropertyNames(events).forEach(function(event){
     // this.on/off/etc....
});

#off(handler) fails with symbols

const Emitter = require("emmett");
const emitter = new Emitter();
const event = Symbol("myEvent");

let count = 0;
const handler = function() { count++; }

emitter.on(event, handler);
emitter.off(handler);
emitter.emit(event);

console.log("Count:", count);

Expected result:

Should log Count: 0

Actual result:

Should log Count: 1

#emit breaks with symbols and complex handlers

const Emitter = require("emmett");
const emitter = new Emitter();

emitter.on(/some pattern/, function() {});
emitter.emit(Symbol("myEvent"));

console.log("All good.");

Expected result:

Should log All good.

Actual result:

Throws TypeError: event.search is not a function

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.