event-emitter-object v4.0.4
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
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 🐬
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago