GithubHelp home page GithubHelp logo

emitter.js's Introduction

emitter.js

unmaintained

Build Status

JavaScript event-emitter.

Installation

npm: npm install emitter.js Component(1): component install necolas/emitter.js Bower: bower install emitter.js

API

Emitter(obj)

The Emitter function can be used as a Constructor or as a mixin.

As an Emitter instance:

var Emitter = require('emitter');
var foo = new Emitter;
foo.trigger('foo');

As a mixin:

var Emitter = require('emitter');
var foo = {};
Emitter(foo);

foo.trigger('bar');

As a prototype mixin:

var Emitter = require('emitter');
var Foo = function () {};
Emitter(Foo.prototype);

Emitter#on(event, callback)

Register an event handler callback. Protects against duplicate handlers being registered.

var handler = function () {
  console.log('The function `handler` has been registered for the event `foo`.');
};

foo.on('foo', handler);

Emitter#once(event, callback)

Register a one-off event handler callback, removed immediately after it is invoked the first time.

var handler = function () {
  console.log('The function `handler` has been registered as a one-off callback for the event `foo`.');
};

foo.once('foo', handler);

Emitter#off([event], [callback])

Remove the event handler callback. If no callback is specified, it will remove all callbacks for the event. If no event is specified, the entire event registry will be deleted.

var handler = function () {
  console.log('Registered for the event `foo`.');
};

foo.off('foo', handler);
foo.off('foo');
foo.off();

Emitter#trigger(event, [...])

Trigger an event with optional arguments. Alias: emit.

var data = { name: 'nicolas' };
foo.trigger('foo', data);

Emitter#getListeners(event)

Return an array of callbacks registered for the event, or an empty array. Initializes the registry if necessary.

foo.getListeners('foo');

Emitter#hasListeners(event)

Check if any callbacks are registered for the event.

foo.hasListeners('foo');

Testing

Install and run the test suite:

make

Re-run the test suite:

make test

Browser support

  • Google Chrome (latest)
  • Opera (latest)
  • Firefox 4+
  • Safari 5+
  • Internet Explorer 8+

emitter.js's People

Contributors

necolas 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

Watchers

 avatar  avatar

emitter.js's Issues

support automatic re-emit event?

let me explain the idea by taking an example:

says i have a event which called 'ready', it fires when my object is ready.

After the event is fired, that means my object will stay "ready" forever,
so i also would like to make sure, later if any callback gets added to 'ready' event, it gets called directly with the arguments previously emitted.

obj.reemit('ready',1,2,3);
obj.on('ready', function callback(){ console.log(arguments);});
// output "[1, 2, 3]"

wouldn't it be cool?

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.