Giter VIP home page Giter VIP logo

emitter's Introduction

EventEmitter

Facebook's EventEmitter is a simple emitter implementation that prioritizes speed and simplicity. It is conceptually similar to other emitters like Node's EventEmitter, but the precise APIs differ. More complex abstractions like the event systems used on facebook.com and m.facebook.com can be built on top of EventEmitter as well DOM event systems.

API Concepts

EventEmitter's API shares many concepts with other emitter APIs. When events are emitted through an emitter instance, all listeners for the given event type are invoked.

var emitter = new EventEmitter();
emitter.addListener('event', function(x, y) { console.log(x, y); });
emitter.emit('event', 5, 10);  // Listener prints "5 10".

EventEmitters return a subscription for each added listener. Subscriptions provide a convenient way to remove listeners that ensures they are removed from the correct emitter instance.

var subscription = emitter.addListener('event', listener);
subscription.remove();

Usage

First install the fbemitter package via npm, then you can require or import it.

var {EventEmitter} = require('fbemitter');
var emitter = new EventEmitter();

Building from source

Once you have the repository cloned, building a copy of fbemitter is easy, just run gulp build. This assumes you've installed gulp globally with npm install -g gulp.

gulp build

API

constructor()

Create a new emitter using the class' constructor. It accepts no arguments.

var {EventEmitter} = require('fbemitter');
var emitter = new EventEmitter();

addListener(eventType, callback)

Register a specific callback to be called on a particular event. A token is returned that can be used to remove the listener.

var token = emitter.addListener('change', (...args) => {
  console.log(...args);
});

emitter.emit('change', 10); // 10 is logged
token.remove();
emitter.emit('change', 10); // nothing is logged

once(eventType, callback)

Similar to addListener() but the callback is removed after it is invoked once. A token is returned that can be used to remove the listener.

var token = emitter.once('change', (...args) => {
  console.log(...args);
});

emitter.emit('change', 10); // 10 is logged
emitter.emit('change', 10); // nothing is logged

removeAllListeners(eventType)

Removes all of the registered listeners. eventType is optional, if provided only listeners for that event type are removed.

var token = emitter.addListener('change', (...args) => {
  console.log(...args);
});

emitter.removeAllListeners();
emitter.emit('change', 10); // nothing is logged

listeners(eventType)

Return an array of listeners that are currently registered for the given event type.

emit(eventType, ...args)

Emits an event of the given type with the given data. All callbacks that are listening to the particular event type will be notified.

var token = emitter.addListener('change', (...args) => {
  console.log(...args);
});

emitter.emit('change', 10); // 10 is logged

__emitToSubscription(subscription, eventType, ...args)

It is reasonable to extend EventEmitter in order to inject some custom logic that you want to do on every callback that is called during an emit, such as logging, or setting up error boundaries. __emitToSubscription() is exposed to make this possible.

class MyEventEmitter extends EventEmitter {
  __emitToSubscription(subscription, eventType) {
    var args = Array.prototype.slice.call(arguments, 2);
    var start = Date.now();
    subscription.listener.apply(subscription.context, args);
    var time = Date.now() - start;
    MyLoggingUtility.log('callback-time', {eventType, time});
  }
}

And then you can create instances of MyEventEmitter and use it like a standard EventEmitter. If you just want to log on each emit and not on each callback called during an emit you can override emit() instead of this method.

emitter's People

Contributors

bob76828 avatar davidxi avatar deepak1556 avatar fisherwebdev avatar flarnie avatar ide avatar kyldvs avatar mroch avatar rosskevin avatar shinnn avatar syotfs avatar yangshun avatar zpao 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  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  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

emitter's Issues

Update build infra

What we have is basically copied from React 2 years ago. We have some upgrades there that should make their way here.

Handle all events

Hi, can you add API for handling all events like a

emmiterNamespace.addListener('*', () => {...});

No more validation?

Hi, this is really a "just curious" question! I remember reading about this project in the spring and one thing that really stood out was "event validation", the way you could whitelist events upfront to cause loud failures if the emitter was misused later on.

But, it seems like that feature has been removed. Why did you decide to remove it? Was it less helpful in practice than on paper? Or did it have unintended consequences?

Package installs various fetch libraries

Steps to reproduce

  1. npm init
  2. npm install fbemitter
  3. See isomorphic-fetch, node-fetch, whatwg-fetch in the node_modules folder
    node_modules

This looks smelly to me. Why does the emitter needs fetch libraries?

Add some check on EmitterSubscription's listener

https://github.com/facebook/emitter/blob/master/src/EmitterSubscription.js#L32
there is no validation on the param listener

  constructor(subscriber: EventSubscriptionVendor, listener, context: ?Object) {
    super(subscriber);
    this.listener = listener;
    this.context = context;
  }

and no appliable check when apply the listener.
https://github.com/facebook/emitter/blob/master/src/BaseEventEmitter.js#L182

  __emitToSubscription(subscription, eventType) {
    var args = Array.prototype.slice.call(arguments, 2);
    subscription.listener.apply(subscription.context, args);
  }

can we add some checks or make some noises when create a new EmitterSubscription?

Can I use multiple `once` to register the same event?

Suppose I have the following code:

electron_1.app.once('open-file', function (e, p) {
    process.argv.push(p);
    e.preventDefault();
});

electron_1.app.once('open-file', function (e, p) {
    // log the event
   console.log('open ' + p);
    e.preventDefault();
});

Only one of the events will be received. Is it the expected behavior?

ES6 module loading: Cannot find module ./lib/BaseEventEmitter

I'm trying to load the module using ES6 with babelify.

I get the error:

Cannot find module './lib/BaseEventEmitter' from ...

I install the module using npm

I load the module like this:

import {EventEmitter} from 'fbemitter';

I see in package.js, that it points main to index.js. And index.js require('./lib/BaseEventEmitter').

The lib directory is not present. May be it should say src?

Ship to npm

We don't right now but we probably should. That way Flux can make use of this instead of the node emitter.

  • don't ship any bins
  • clean up dependencies
  • figure out public API (require('fbemitter') gives you what?)

Naming collision detected

react packager

Error: Naming collision detected: /Users/roku/Documents/r/on/node_modules/react-native/node_modules/fbjs/lib/camelize.js collides with /Users/roku/Documents/r/on/node_modules/fbemitter/node_modules/fbjs/lib/camelize.js
    at HasteMap._updateHasteMap (HasteMap.js:132:13)
    at HasteMap.js:103:28
    at tryCallOne (/Users/roku/Documents/r/on/node_modules/react-native/node_modules/promise/lib/core.js:37:12)
    at /Users/roku/Documents/r/on/node_modules/react-native/node_modules/promise/lib/core.js:123:15
    at flush (/Users/roku/Documents/r/on/node_modules/react-native/node_modules/promise/node_modules/asap/raw.js:50:29)
    at doNTCallback0 (node.js:407:9)
    at process._tickCallback (node.js:336:13)
~

i am using

    "react-native": "^0.18.0-rc",
    "fbemitter": "2.0.1",

Typo in README.md

This example has a typo in it:

var emitter = new EventEmitter();
emitter.addListener('event', function(x, y) { console.log(x, y); }
emitter.emit('event', 5, 10);  // Listener prints "5 10".

The second line should read

emitter.addListener('event', function(x, y) { console.log(x, y); });

Document full API in readme

Since we don't have a website (nor do I think we need to have one) we need to document the full API somewhere. Readme is an easy place. You could also use the wiki.

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.