safe-event-emitter
Advanced tools
Comparing version 1.0.0 to 1.0.1
90
index.js
@@ -0,1 +1,2 @@ | ||
const util = require('util') | ||
const EventEmitter = require('events/') | ||
@@ -10,50 +11,57 @@ | ||
class SafeEventEmitter extends EventEmitter { | ||
emit (type) { | ||
// copied from https://github.com/Gozala/events/blob/master/events.js | ||
// modified lines are commented with "edited:" | ||
var args = []; | ||
for (var i = 1; i < arguments.length; i++) args.push(arguments[i]); | ||
var doError = (type === 'error'); | ||
module.exports = SafeEventEmitter | ||
var events = this._events; | ||
if (events !== undefined) | ||
doError = (doError && events.error === undefined); | ||
else if (!doError) | ||
return false; | ||
// If there is no 'error' event listener then throw. | ||
if (doError) { | ||
var er; | ||
if (args.length > 0) | ||
er = args[0]; | ||
if (er instanceof Error) { | ||
// Note: The comments on the `throw` lines are intentional, they show | ||
// up in Node's output if this results in an unhandled exception. | ||
throw er; // Unhandled 'error' event | ||
} | ||
// At least give some kind of context to the user | ||
var err = new Error('Unhandled error.' + (er ? ' (' + er.message + ')' : '')); | ||
err.context = er; | ||
throw err; // Unhandled 'error' event | ||
function SafeEventEmitter() { | ||
EventEmitter.call(this) | ||
} | ||
util.inherits(SafeEventEmitter, EventEmitter) | ||
SafeEventEmitter.prototype.emit = function (type) { | ||
// copied from https://github.com/Gozala/events/blob/master/events.js | ||
// modified lines are commented with "edited:" | ||
var args = []; | ||
for (var i = 1; i < arguments.length; i++) args.push(arguments[i]); | ||
var doError = (type === 'error'); | ||
var events = this._events; | ||
if (events !== undefined) | ||
doError = (doError && events.error === undefined); | ||
else if (!doError) | ||
return false; | ||
// If there is no 'error' event listener then throw. | ||
if (doError) { | ||
var er; | ||
if (args.length > 0) | ||
er = args[0]; | ||
if (er instanceof Error) { | ||
// Note: The comments on the `throw` lines are intentional, they show | ||
// up in Node's output if this results in an unhandled exception. | ||
throw er; // Unhandled 'error' event | ||
} | ||
// At least give some kind of context to the user | ||
var err = new Error('Unhandled error.' + (er ? ' (' + er.message + ')' : '')); | ||
err.context = er; | ||
throw err; // Unhandled 'error' event | ||
} | ||
var handler = events[type]; | ||
var handler = events[type]; | ||
if (handler === undefined) | ||
return false; | ||
if (handler === undefined) | ||
return false; | ||
if (typeof handler === 'function') { | ||
if (typeof handler === 'function') { | ||
// edited: using safeApply | ||
safeApply(handler, this, args); | ||
} else { | ||
var len = handler.length; | ||
var listeners = arrayClone(handler, len); | ||
for (var i = 0; i < len; ++i) | ||
// edited: using safeApply | ||
safeApply(handler, this, args); | ||
} else { | ||
var len = handler.length; | ||
var listeners = arrayClone(handler, len); | ||
for (var i = 0; i < len; ++i) | ||
// edited: using safeApply | ||
safeApply(listeners[i], this, args); | ||
} | ||
safeApply(listeners[i], this, args); | ||
} | ||
return true; | ||
} | ||
return true; | ||
} | ||
@@ -78,3 +86,1 @@ | ||
} | ||
module.exports = SafeEventEmitter |
{ | ||
"name": "safe-event-emitter", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2973
70