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

on-object

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

on-object

Register many EventEmitter listeners at once using objects

  • 1.0.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
14
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

on-object

npm version Build Status Dependency Status

Register many EventEmitter listeners at once using objects

Usage

npm install on-object

var EventEmitter = require('on-object')
var emitter = new EventEmitter()

emitter.on({
  foo: function () {
    console.log('foo event emitted')
  },
  bar: function () {
    console.log('bar event emitted')
  }
})
emitter.once({ ... })
emitter.removeListener({ ... })

Instantiation

You can just use this module as a replacement for the EventEmitter class in the events module:

var EventEmitter = require('on-object')
var emitter = new EventEmitter()

You can also use it to wrap the methods of a pre-existing EventEmitter:

var EventEmitter = require('events')
var onObject = require('on-object')

var emitter = onObject(new EventEmitter())

Rationale

This module is useful when you find yourself registering many event listeners at once and want the syntax to look a little less noisy.

For example, you might write something like this using the original API:

emitter.on('foo', function () {
  console.log('foo')
})
emitter.on('bar', function () {
  console.log('bar')
})
emitter.on('bar2', function () {
  console.log('bar2')
})

This module will let you do this:

emitter.on({
  foo: function () {
    console.log('bar')
  },
  bar: function () {
    console.log('bar')
  },
  bar2: function () {
    console.log('bar2')
  }
})

Also, you can make it look especially nice with the ES6 object method syntax:

emitter.on({
  foo () {
    console.log('bar')
  },
  bar () {
    console.log('bar')
  },
  bar2 () {
    console.log('bar2')
  }
})

FAQs

Package last updated on 13 Sep 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