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

cocktail-trait-eventable

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cocktail-trait-eventable

EventEmitter as delegate trait

  • 0.0.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
increased by100%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status NPM version

cocktail-trait-eventable

A CocktailJS Trait Extension

A trait to use an Emitter as a delegate.

Install

npm install cocktail --save
npm install cocktail-trait-eventable --save

Trait requires (glue code)

A getEmitter method is required as a glue code for this trait.

getEmitter(): should return an EventEmitter instance or any other object with the same api as node events.EventEmitter class.

Usage

MyClass.js

var cocktail     = require('cocktail'),
    Eventable    = require('cocktail-trait-eventable'),
    EventEmitter = require('events').EventEmitter;

cocktail.mix({
    '@exports': module,
    '@as'     : 'class',

    '@traits' : [Eventable],

    constructor: function() {
        this._emitter = new EventEmitter();
    },

    // glue code for Eventable Trait
    getEmitter: function() {
        return this._emitter;
    },

    doSomethingAndFireEvent: function(){
        // ... do something here
        // and
        // now fire an event 'firing'
        this.emit('firing', this);
    }
});

And then in your index you can do:

index.js


var MyClass = require('./MyClass'),
    obj;

obj = new MyClass();

//we can call `on`  or `addListener` here since our MyClass is Eventable
obj.on('firing', function(){ console.log('Event Fired!'); });

obj.doSomethingAndFireEvent();

API

The following methods will be publicly available on the host class:

  • addListener(eventName, handler): Adds a listener to the end of the listeners array for the specified event.
    • eventName: {String} the event name to be listened to.
    • handler: {Function} the event handler.
    myObj.addListener('event', function(){/*handler*/});
  • addListener(options): Adds a listener to the end of the listeners array for the specified event.
    • options: {Object} an object containing value-pairs of event name and handler and an optional scope.
    myObj.addListener({event: myObj.onEvent, scope: myObj});
  • emit(eventName, arg1, arg2, ..., argN): Execute each of the listeners in order with the supplied arguments.
    • eventName: {String} the event name to be listened to.
    • arg1..N: {Any} arguments to be passed as parameters in the event handler.
    myObj.emit('event', myObj, true, 1, 'another param');
  • on(eventName, handler): Idem addListener method.
    myObj.on('event', function(){/*handler*/});
  • on(options): Idem addListener method.
    myObj.on({event: myObj.onEvent, scope: myObj});
  • once(eventName, handler): Adds a one time event listener.
    • eventName: {String} the event name to be listened to.
    • handler: {Function} the event handler.
    myObj.once('event', function(){/*handler*/});
  • once(options): Adds a one time event listener.
    • options: {Object} an object containing value-pairs of event name and handler and an optional scope.
    myObj.once({event: myObj.onEvent, scope: myObj});
  • removeListener(eventName, handler): Removes the given handler for the event.
    • eventName: {String} the event name.
    • handler: {Function} the event handler.
    myObj.removeListener('event', funcHandler);
  • removeListener(options): Removes the given handler for the event.
    • options: {Object} an object containing value-pairs of event name and handler.
    myObj.removeListener({event: myObj.onEvent});
  • removeAllListeners(eventName): Remove all listeners for the given event.
    • eventName: {String} the event name.
    myObj.removeAllListeners();

Keywords

FAQs

Package last updated on 24 Mar 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