Skip to content

Commit

Permalink
feat: change storing order of handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
monochromer committed Oct 23, 2019
1 parent 1759898 commit 69da423
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 10 deletions.
15 changes: 8 additions & 7 deletions src/event-emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ class EventEmitter {
*/
on(type, fn) {
const events = this._events[type] || (this._events[type] = []);
if (events && events.some(handler => handler === fn)) {
return;
if (!events.some(handler => handler === fn)) {
events.unshift(fn);
};
events.push(fn);
return this;
}

Expand Down Expand Up @@ -65,14 +64,16 @@ class EventEmitter {
* @param {any[]} args event arguments
*/
emit(type, ...args) {
if (this._events[type]) {
this._events[type].forEach(fn => {
const events = this._events[type];
if (events) {
let index = events.length;
while (index--) {
try {
fn(...args);
events[index](...args);
} catch(error) {
this.emit('error', type, error);
}
});
}
};
return this;
}
Expand Down
69 changes: 66 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('Event Emitter', () => {
it('should exists with empty events', () => {
assert.isObject(ee);
assert.isObject(ee._events);
assert.equal(Object.keys(ee._events), 0);
assert.lengthOf(Object.keys(ee._events), 0);
});

describe('subscribe handlers', () => {
Expand All @@ -36,16 +36,21 @@ describe('Event Emitter', () => {
ee.on(eventName, fn1);
ee.on(eventName, fn2);
assert.property(ee._events, eventName);
assert.deepEqual(ee._events[eventName], [fn1, fn2]);
assert.deepEqual(ee._events[eventName], [fn2, fn1]);
});

it('should add the same handler only once', () => {
const fn = () => {};
const fn2 = () => {};
const eventName = 'event';
ee.on(eventName, fn);
ee.on(eventName, fn);
assert.property(ee._events, eventName);
assert.deepEqual(ee._events[eventName], [fn]);
ee.on(eventName, fn);
ee.on(eventName, fn2);
ee.on(eventName, fn);
assert.deepEqual(ee._events[eventName], [fn2, fn]);
});
});

Expand Down Expand Up @@ -84,7 +89,7 @@ describe('Event Emitter', () => {
assert.typeOf(ee.emit, 'function');
});

it('should emit handler for event type with arguments', () => {
it('should emit handler for event type with arguments', (done) => {
const eventName = 'emit-event-name';
const arg1 = 'some-arg';
const arg2 = {
Expand All @@ -93,10 +98,68 @@ describe('Event Emitter', () => {
};
const fn = (...args) => {
assert.deepEqual(args, [arg1, arg2]);
done();
};
ee.on(eventName, fn);
ee.emit(eventName, arg1, arg2);
});

it('should handle error', (done) => {
const eventName = 'emit-event-name';
const error = new Error();
error.name = 'test-error';
error.message = 'some error description';
const fn = () => {
throw error;
}
ee.on(eventName, fn);
ee.on('error', (type, ex) => {
assert.deepEqual(ex, error);
assert.equal(type, eventName);
done();
});

ee.emit(eventName);
});

it('should continue handle events, if error happens', (done) => {
let count = 0;
const end = () => {
count++;
if (count === 2) {
done();
}
}

const eventName = 'emit-event-name';

const error = new Error();
error.name = 'test-error';
error.message = 'some error description';
const handlerWithError = () => {
throw error;
}

const arg1 = 'some-arg';
const arg2 = {
a: 'a',
props: ['1', 2, 'x']
};
const handler = (...args) => {
assert.deepEqual(args, [arg1, arg2]);
end();
};

ee.on(eventName, handlerWithError);
ee.on(eventName, handler);
ee.on('error', (type, ex) => {
assert.deepEqual(ex, error);
assert.equal(type, eventName);
end();
});

ee.emit(eventName, arg1, arg2);
});
});

describe('once subscribe handler', () => {
Expand Down

0 comments on commit 69da423

Please sign in to comment.