JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 194
  • Score
    100M100P100Q18829F
  • License MIT

A simple event emitter object that you can plug into your javascript libraries.

Package Exports

  • event-emitter-object
  • event-emitter-object/source

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (event-emitter-object) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Javascript Event Emitter Object

A simple event emitter object that you can plug into your javascript libraries.

API is very similar to the node.js events.

Install

npm install event-emitter-object

Import

This package exports multiple builds. Developer has the responsibility to choose the one that best fits to her/his needs.

  1. Minified (default)

This is basically the source code itself but minified. There are no compilation and polyfills inside. Use it if you already have a compiler (like babel) and polyfills in your project. (which is mostly is the case.)

const EventEmitter = require('event-emitter-object')
  1. UMD Bundle

The source code bundled with browserify to generate a UMD bundle. This bundle can also be imported by html script tag. No compilation and polyfills. The variable attached to the browser's window object is EventEmitterObject.

const EventEmitterObject = require('event-emitter-object/dist/umd')

or

<script src="https://unpkg.com/event-emitter-object@1/dist/umd.js" crossorigin type="text/javascript"></script>
  1. Polyfilled UMD bundle

The source code compiled with babel and bundled with browserify to generate a UMD bundle. This bundle can also be imported by html script tag. The variable attached to the browser's window object is EventEmitterObject.

<script src="https://unpkg.com/event-emitter-object@1/dist/umd.polyfilled.js" crossorigin type="text/javascript"></script>

Use

This is a pure javascript object which can be used as a super class of another objects or as a standalone.

Using it as a standalone library

const EventEmitter = require('event-emitter-object')

const logger = new EventEmitter()

logger.on('criticalError', function(err) {
  // send log to the server
})

if (resourceNotFound) {
  logger.emit('criticalError', [new Error('resourceNotFound')])
}

Using as a super class

const EventEmitter = require('event-emitter-object')

// A Logger object which inherits the methods of EventEmitter
function Logger() {
  EventEmitter.call(this)
}
Logger.prototype = Object.create(EventEmitter.prototype)
Logger.prototype.constructor = Logger

Logger.prototype.propertyYouWant = function() {}

// Initiate a logger for a particular type of logs
const errorLogger = new Logger()
errorLogger.on('error', function(err) {
  // send log to the server
})
if (resourceNotFound) {
  errorLogger.emit('error', [new Error('resourceNotFound')])
}

API

.on(eventName, callback, opts)

Registers a new listener for a certain event.

emitter.on('someEvent', function() {
  console.log('Some event happened in some emitter.')
}, {
  once: true // default is false
})

.once(eventName, callback)

This is a shortcut for the on method with {once: true}:

// instead of doin this:
emitter.on('someEvent', function(){}, {once: true})
// you can do this:
emitter.once('someEvent', function(){})

.emit(eventName, args)

Emits a certain event with arguments.

emitter.emit('someEvent')

// or with arguments
emitter.on('anotherEvent', function(arg1, arg2) {
  console.log('Another event emitted with arguments: ', arg1, arg2)
})
emitter.emit('anotherEvent', ['hello', 'world'])

.removeListeners(eventName)

Removes all listeners that belong to a certain event.

emitter.removeListeners('someEvent')

.flushEventEmitter()

Removes all events and listeners.

emitter.flushEventEmitter()

Babel Polyfills Report

This module uses the following polyfills in its polyfilled builds.

  1. es.array.splice
  2. es.date.to-string
  3. es.object.to-string
  4. es.regexp.to-string