Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

emit.js

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

emit.js

Efficient minimalist event emitter in JavaScript.

  • 0.5.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

emit.js Build Status

Efficient minimalist event emitter in JavaScript.

Installation

It is available with bower or npm:

bower install emit.js
npm install emit.js

Include emit.min.js to the HTML, and the emit object is now available in the global scope:

<script type="text/javascript" src="/path/to/bower_components/emit.js/dist/emit.min.js"></script>

Alternately, you can use a module manager to avoid global scoping:

var emit = require('emit.js');
// or es6 import
import emit from 'emit.js';

Usage

Create an event emitter

var emitter = emit();

Register listener on it

You can register a listener with on and once method. Listeners registered with once will be triggered only once.

emitter.on('event', function(...args) {
    // the args depends on the emit call
    
    console.log('I am a listener');
});

// you can also register an event with a regex
emitter.on(/^event(foo)?$/, function(...args) {
    // the args depends on the emit call
    
    console.log('I am a listener');
});

// you can also register an event with a callback for powerful listener
emitter.on(function(event) {
    return event.substr(0, 5) === 'hello'; // We want to match all event beginning with `hello`.
}, function(...args) {
    // the args depends on the emit call
    
    console.log('I am a listener');
});

emitter.once('event', function(...args) {
    console.log('I am triggered only once');
});

Unregister a listener

When you register a listener, you get in return a callback to unregister it:

var unregister = emitter.on('event', function(...args) {
    // the args depends on the emit call
    if (...) {
        unregister(); // unregistering this listener
    }

    console.log('I am a listener');
});

Emit an event

To emit an event, just use the emit method:

emitter.emit('event', arg1, arg2, ...); // we emit an event with as many parameters as we want

emitter.emit('event').then(function() {
    // all listeners have been called
}, function(error) {
    // an error occured
});

Removing all listeners

You can remove all listeners by calling the removeAllListeners method.

Development

Installation

make install

Build

make build or make build-dev (unminified version)

Watch

make watch

Test

make test

Contributing

All contributions are welcome and must pass the tests. If you add a new feature, please write tests for it.

License

This application is available under the MIT License.

FAQs

Package last updated on 28 Jun 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc