Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

events: remove type check for event type #4915

Merged
merged 2 commits into from Mar 4, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 8 additions & 17 deletions lib/events.js
Expand Up @@ -84,7 +84,7 @@ EventEmitter.prototype.emit = function(type) {
if (this.domain && this !== process)
this.domain.enter();

if (typeof handler == 'function') {
if (typeof handler === 'function') {
switch (arguments.length) {
// fast cases
case 1:
Expand Down Expand Up @@ -125,8 +125,6 @@ EventEmitter.prototype.emit = function(type) {
EventEmitter.prototype.addListener = function(type, listener) {
var m;

if (typeof type !== 'string')
throw TypeError('type must be a string');
if (typeof listener !== 'function')
throw TypeError('listener must be a function');

Expand Down Expand Up @@ -168,8 +166,6 @@ EventEmitter.prototype.addListener = function(type, listener) {
EventEmitter.prototype.on = EventEmitter.prototype.addListener;

EventEmitter.prototype.once = function(type, listener) {
if (typeof type !== 'string')
throw TypeError('type must be a string');
if (typeof listener !== 'function')
throw TypeError('listener must be a function');

Expand All @@ -188,8 +184,6 @@ EventEmitter.prototype.once = function(type, listener) {
EventEmitter.prototype.removeListener = function(type, listener) {
var list, position, length, i;

if (typeof type !== 'string')
throw TypeError('type must be a string');
if (typeof listener !== 'function')
throw TypeError('listener must be a function');

Expand Down Expand Up @@ -235,9 +229,6 @@ EventEmitter.prototype.removeListener = function(type, listener) {
EventEmitter.prototype.removeAllListeners = function(type) {
var key, listeners;

if (arguments.length > 0 && typeof type !== 'string')
throw TypeError('type must not be set or must be a string');

if (!this._events)
return this;

Expand Down Expand Up @@ -276,14 +267,14 @@ EventEmitter.prototype.removeAllListeners = function(type) {
};

EventEmitter.prototype.listeners = function(type) {
if (typeof type !== 'string')
throw TypeError('event type must be a string');

var ret;
if (!this._events || !this._events[type])
return [];
if (typeof this._events[type] === 'function')
return [this._events[type]];
return this._events[type].slice();
ret = [];
else if (typeof this._events[type] === 'function')
ret = [this._events[type]];
else
ret = this._events[type].slice();
return ret;
};

EventEmitter.listenerCount = function(emitter, type) {
Expand Down
7 changes: 4 additions & 3 deletions test/simple/test-event-emitter-subclass.js
Expand Up @@ -27,9 +27,9 @@ var util = require('util');
util.inherits(MyEE, EventEmitter);

function MyEE(cb) {
this.emit('bar');
this.on('foo', cb);
process.nextTick(this.emit.bind(this, 'foo'));
this.once(1, cb);
this.emit(1);
this.removeAllListeners();
EventEmitter.call(this);
}

Expand All @@ -50,5 +50,6 @@ assert.throws(function() {

process.on('exit', function() {
assert(called);
assert.deepEqual(myee._events, {});
console.log('ok');
});