Skip to content

Commit

Permalink
close #41
Browse files Browse the repository at this point in the history
  • Loading branch information
smart--petea committed Dec 16, 2014
1 parent 45a1abb commit 0b7823b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/eventemitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ EventEmitter.prototype._on = function(event, listeners, listenersStorage, maxLis


EventEmitter.prototype.emit = function(event) {
if( ! isString(event) ) { throw "event name is not string";}
if( ! isString(event) ) {throw new Error("event name is not string");}

var evListeners = this._events[event] = this._events[event] || [];
var onceEvListeners = this._onceEvents[event] = this._onceEvents[event] || [];
var hadListeners = (evListeners.length + onceEvListeners) > 0;
var key;

for(key in evListeners) evListeners[key].apply(null, [].slice.call(arguments, 1));
Expand All @@ -88,7 +89,7 @@ EventEmitter.prototype.emit = function(event) {
this.removeListener(event, onceEvListeners[key]);
}

return (evListeners.length + onceEvListeners.length) > 0;
return hadListeners;
}

EventEmitter.prototype.removeListener = function(event, listener) {
Expand Down
69 changes: 69 additions & 0 deletions test/eventemitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,75 @@ var should = require('should');

describe('EventEmitter', function() {
describe('methods', function() {
describe('#emit', function(){
it('() - throw error "event name is not string"', function() {
try {
(new EE).emit();
} catch (e) {
e.message.should.equal("event name is not string");
}
});

it('({}) - throw error "event name is not string"', function() {
try {
(new EE).emit({});
} catch (e) {
e.message.should.equal("event name is not string");
}
});

it('("event", "arg1", 1, {a: "a"}, [2, 2]) - on', function(done) {
var ev = "event";
var ag1 = "arg1";
var ag2 = 1;
var ag3 = {a: "a"};
var ag4 = [2, 2];

function eventClbk(arg1, arg2, arg3, arg4) {
arg1.should.equal(ag1);
arg2.should.equal(ag2);
arg3.should.equal(ag3);
arg4.should.equal(ag4);
done();
}

(new EE)
.on(ev, eventClbk)
.emit(ev, ag1, ag2, ag3, ag4);
});

it('("event", "arg1", 1, {a: "a"}, [2, 2]) - once', function(done) {
var ev = "event";
var ag1 = "arg1";
var ag2 = 1;
var ag3 = {a: "a"};
var ag4 = [2, 2];

function eventClbk(arg1, arg2, arg3, arg4) {
arg1.should.equal(ag1);
arg2.should.equal(ag2);
arg3.should.equal(ag3);
arg4.should.equal(ag4);
done();
}

(new EE)
.once(ev, eventClbk)
.emit(ev, ag1, ag2, ag3, ag4);
});

it('("event") - return false if no listeners', function() {
(new EE).emit("event").should.be.false;
});

it('("event") - return true if exist listeners', function() {
(new EE)
.on("event", function(){})
.emit("event")
.should.be.true;
});
});

describe('#listeners', function() {
it('() - return []', function() {
(new EE).listeners().should.length(0);
Expand Down

0 comments on commit 0b7823b

Please sign in to comment.