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

sg-event-emitter

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sg-event-emitter

Simple service registry

  • 0.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
279
increased by84.77%
Maintainers
1
Weekly downloads
 
Created
Source

Event Emitter

Very simple class allowing for dispatch custom events in DOM-like style. It's tested in production apps on both browser and node.

Usage

All available methods are:

  • addEventListener(name: string, listener: ()=>void) - adds a new listener to given event
  • removeEventListener(name: string, listener: ()=>void) - removes registered listener for given event. You have to pass exactly the same function as passed previously to addEventListener.
  • dispatchEvent(name: string) - dispatches given event
  • settle(host: any) - links addEventListener and removeEventListener from EventEmitter instance to given host object. This allows you to create really encapsulated EventEmitter instance and expose only these two public methods.

var EventEmitter = require('sg-event-emitter').EventEmitter;
//or
import {EventEmitter} from 'sg-event-emitter';

class Foo {
    constructor () {
        this._eventEmitter = new EventEmitter();
        this._eventEmitter.settle(this); //from now this object has 
                                         //`addEventListener` and `removeEventListener` methods
    }
    
    makeSomeAction () {
        this._eventEmitter.dispatchEvent('myEvent');
    }
}

var foo = new Foo();
var listener = _ => console.log('dispatched');
foo.addEventListener('myEvent', listener);

foo.makeSomeAction();  //logs 'dispatched'

foo.removeEventListener('myEvent', listener);

foo.makeSomeAction();  //doesn't log anything

Keywords

FAQs

Package last updated on 05 Apr 2016

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