4.0.4 • Published 4 years ago

event-emitter-object v4.0.4

Weekly downloads
19
License
MIT
Repository
github
Last release
4 years ago

Javascript Event Emitter Object

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

NPM npm version npm bundle size npm

API is very similar to the node.js events.

Install

npm install event-emitter-object

Import

There are different types of distributions depending on your use case. Essentially, the package can be imported via require:

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

or via script tag:

<script src="https://cdn.jsdelivr.net/npm/event-emitter-object@4/dist/event-emitter-object.iife.js" crossorigin type="text/javascript"></script>

but there are lots of other options. See distribution report below.

Usage

Simple logger:

const {create} = require('event-emitter-object')

const logger = create()

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

// ... when an error happens:
logger.emit('criticalError', new Error('resourceNotFound'))

When arguments and return values matter:

const emailMessages = create()

emailMessages.on('emailMessage', function(recipient, subject, message) {
  // send an email and return the id of the sent message
  return {id: 1}
})

const results = emailMessages.emit('emailMessage', ['sample@example.com', 'Hey', 'Lorem ipsum.'])
// results == [{ok: 1}]

Return values are always array because multiple event functions can be attached to the same event:

const emailMessages = create()

emailMessages.on('emailMessage', function(recipient, subject, message) {
  // send an email and return the id of the sent message
  return {id: 1}
})

emailMessages.on('emailMessage', function(recipient, subject, message) {
  return 'ok'
})

const results = emailMessages.emit('emailMessage', ['sample@example.com', 'Hey', 'Lorem ipsum.'])
// results == [{ok: 1}, 'ok']

An event that will be invoked by the emitter just once:

const analytics = create()

analytics.on('view', function(id, title) {
  // save visitor and landing page info
  return 'done'
}, {once: true})

analytics.on('view', function(id, title) {
  // send page view to the analytics server
  return 'ok'
})

const results = analytics.emit('view', [1, 'Homepage'])
// results == ['done', 'ok']

const results2 = analytics.emit('view', [2, 'Contact'])
// results2 == ['ok']

Distributions Report

This is an auto-generated report that shows the type, name and size of the bundles available to use individually.

[
  "event-emitter-object.amd.js (1.08 KB)",
  "event-emitter-object.amd.polyfilled.js (10.61 KB)",
  "event-emitter-object.cjs.js (1.08 KB)",
  "event-emitter-object.cjs.polyfilled.js (10.64 KB)",
  "event-emitter-object.es.js (1.06 KB)",
  "event-emitter-object.es.polyfilled.js (10.63 KB)",
  "event-emitter-object.iife.js (1.10 KB)",
  "event-emitter-object.iife.polyfilled.js (10.63 KB)",
  "event-emitter-object.umd.js (1.30 KB)",
  "event-emitter-object.umd.polyfilled.js (10.82 KB)"
]

Babel Polyfills Report

This is an auto-generated report that shows the pollyfils added by core-js based on the targets configuration described below.

// polyfills:
[]
// based on the targets:
{
  "android": "4.4.3",
  "chrome": "49",
  "edge": "18",
  "firefox": "78",
  "ie": "10",
  "ios": "6",
  "opera": "73",
  "safari": "5.1",
  "samsung": "4"
}

Thanks for watching 🐬

ko-fi

4.0.4

4 years ago

4.0.3

4 years ago

4.0.2

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

1.1.4

4 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.5

5 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago