diff --git a/src/index.js b/src/index.js index fc3eae7..dfef518 100644 --- a/src/index.js +++ b/src/index.js @@ -1,57 +1,63 @@ export default class Eventhub { - constructor() { - this.listeners = []; - } - - on(string, handler) { - this.testHandler(handler); - this.listeners = this.listeners.concat({ string, handler }); - return this; - } - - onAll(handler) { - this.testHandler(handler); - this.listeners = this.listeners.concat({ string: -1, handler }); - return this; - } - - once(string, handler) { - this.testHandler(handler); - this.on(string, () => { - this.exec(handler); - this.remove(string); - }); - return this; - } - - remove(string, handler) { - const noHandler = handler === undefined; - this.listeners = this.listeners.filter(listener => { - const sameString = listener.string === string || listener.string === -1; - const sameHandler = handler === listener.handler; - return !(sameString && noHandler || sameHandler); - }); - return this; - } - - emit(string, ...args) { - this.listeners - .filter(listener => listener.string === string || listener.string === -1) - .forEach(listener => this.exec(listener.handler, args)); - return this; - } - - exec(handler, args) { - handler.apply(null, args); - } - - testHandler(handler) { - if (typeof handler !== 'function') { - throw new Error(`Event handler can't be of type '${typeof handler}'`); - } - } - - get isListening() { - return this.listeners.length > 0; - } + constructor() { + this.listeners = []; + } + + on(string, handler) { + this.testHandler(handler); + this.listeners = this.listeners.concat({ + string, + handler + }); + return this; + } + + onAll(handler) { + this.testHandler(handler); + this.listeners = this.listeners.concat({ + string: -1, + handler + }); + return this; + } + + once(string, handler) { + this.testHandler(handler); + this.on(string, () => { + this.exec(handler); + this.remove(string); + }); + return this; + } + + remove(string, handler) { + const noHandler = handler === undefined; + this.listeners = this.listeners.filter(listener => { + const sameString = listener.string === string || listener.string === -1; + const sameHandler = handler === listener.handler; + return !(sameString && noHandler || sameHandler); + }); + return this; + } + + emit(string, ...args) { + this.listeners + .filter(listener => listener.string === string || listener.string === -1) + .forEach(listener => this.exec(listener.handler, args)); + return this; + } + + exec(handler, args) { + handler.apply(null, args); + } + + testHandler(handler) { + if (typeof handler !== 'function') { + throw new Error(`Event handler can't be of type '${typeof handler}'`); + } + } + + get isListening() { + return this.listeners.length > 0; + } } \ No newline at end of file diff --git a/src/index.test.js b/src/index.test.js index 37b1730..f27048f 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -1,336 +1,318 @@ import Eventhub from './index'; test('Single event without arguments', () => { - const eventhub = new Eventhub(); - let hasFired = false; + const eventhub = new Eventhub(); + let hasFired = false; - eventhub.on('eventname', () => { - hasFired = true; - }); + eventhub.on('eventname', () => { + hasFired = true; + }); - eventhub.emit('eventname'); + eventhub.emit('eventname'); - expect(hasFired).toBeTruthy(); + expect(hasFired).toBeTruthy(); }); test('Single event with arguments', () => { - const eventhub = new Eventhub(); - const args = [1, 2, 3, 'hello']; + const eventhub = new Eventhub(); + const args = [1, 2, 3, 'hello']; - eventhub.on('singleW', (a, b, c, d) => { - expect(a, b, c, d).toEqual(...args); - }); + eventhub.on('singleW', (a, b, c, d) => { + expect(a, b, c, d).toEqual(...args); + }); - eventhub.emit('singleW', ...args); + eventhub.emit('singleW', ...args); }); test('Multiple events without arguments', () => { - const eventhub = new Eventhub(); - let timesCalled = 0; + const eventhub = new Eventhub(); + let timesCalled = 0; - eventhub.on('eventname', () => { - timesCalled++; - }); + eventhub.on('eventname', () => { + timesCalled++; + }); - eventhub - .emit('eventname') - .emit('eventname') - .emit('eventname') - .emit('eventname'); + eventhub + .emit('eventname') + .emit('eventname') + .emit('eventname') + .emit('eventname'); - expect(timesCalled).toEqual(4); + expect(timesCalled).toEqual(4); }); test('Multiple events with arguments', () => { - const eventhub = new Eventhub(); - const arg1 = 0; - const arg2 = 'hey'; - let timesCalled = 0; - - eventhub.on('eventname', (a1, a2) => { - if (arg1 === a1 && arg2 === a2) { - timesCalled++; - } - }); + const eventhub = new Eventhub(); + const arg1 = 0; + const arg2 = 'hey'; + let timesCalled = 0; + + eventhub.on('eventname', (a1, a2) => { + if (arg1 === a1 && arg2 === a2) { + timesCalled++; + } + }); - eventhub - .emit('eventname', arg1, arg2) - .emit('eventname', arg1, arg2) - .emit('eventname', arg1, arg2) - .emit('eventname', arg1, arg2); + eventhub + .emit('eventname', arg1, arg2) + .emit('eventname', arg1, arg2) + .emit('eventname', arg1, arg2) + .emit('eventname', arg1, arg2); - expect(timesCalled).toEqual(4); + expect(timesCalled).toEqual(4); }); test('Fire event once without arguments', () => { - const eventhub = new Eventhub(); - let timesCalled = 0; + const eventhub = new Eventhub(); + let timesCalled = 0; - eventhub.once('eventname', () => { - timesCalled++; - }); + eventhub.once('eventname', () => { + timesCalled++; + }); - eventhub - .emit('eventname') - .emit('eventname') - .emit('eventname') - .emit('eventname'); - + eventhub + .emit('eventname') + .emit('eventname') + .emit('eventname') + .emit('eventname'); - expect(timesCalled).toEqual(1); + expect(timesCalled).toEqual(1); }); test('Fire event once with arguments', () => { - const eventhub = new Eventhub(); - const arg1 = 0; - const arg2 = 'hey'; - let timesCalled = 0; - - eventhub.once('eventname', () => { - timesCalled++; - }); - - eventhub - .emit('eventname', arg1, arg2) - .emit('eventname', arg1, arg2) - .emit('eventname', arg1, arg2) - .emit('eventname', arg1, arg2); - - expect(timesCalled).toEqual(1); + const eventhub = new Eventhub(); + const arg1 = 0; + const arg2 = 'hey'; + let timesCalled = 0; + + eventhub.once('eventname', () => { + timesCalled++; + }); + + eventhub + .emit('eventname', arg1, arg2) + .emit('eventname', arg1, arg2) + .emit('eventname', arg1, arg2) + .emit('eventname', arg1, arg2); + + expect(timesCalled).toEqual(1); }); test('Event on any', () => { - const eventhub = new Eventhub(); - let timesCalled = 0; + const eventhub = new Eventhub(); + let timesCalled = 0; - eventhub.onAll(() => { - timesCalled++; - }); + eventhub.onAll(() => { + timesCalled++; + }); - eventhub - .emit('eventname') - .emit('') - .emit(123) - .emit('fire'); - + eventhub + .emit('eventname') + .emit('') + .emit(123) + .emit('fire'); - expect(timesCalled).toEqual(4); + expect(timesCalled).toEqual(4); }); test('Remove listener without arguments', () => { - const eventhub = new Eventhub(); - const arg1 = 0; - const arg2 = 'hey'; - let timesCalled = 0; + const eventhub = new Eventhub(); + const arg1 = 0; + const arg2 = 'hey'; + let timesCalled = 0; - eventhub.on('eventname', () => { - timesCalled++; - }); + eventhub.on('eventname', () => { + timesCalled++; + }); - eventhub - .emit('eventname') - .emit('eventname'); + eventhub.emit('eventname').emit('eventname'); - eventhub.remove('eventname'); + eventhub.remove('eventname'); - eventhub - .emit('eventname') - .emit('eventname'); + eventhub.emit('eventname').emit('eventname'); - expect(timesCalled).toEqual(2); + expect(timesCalled).toEqual(2); }); test('Remove listener with arguments', () => { - const eventhub = new Eventhub(); - const arg1 = 0; - const arg2 = 'hey'; - let timesCalled = 0; - - eventhub.on('eventname', (a1, a2) => { - if (a1 === arg1 && a2 === arg2) { - timesCalled++; - } - }); + const eventhub = new Eventhub(); + const arg1 = 0; + const arg2 = 'hey'; + let timesCalled = 0; + + eventhub.on('eventname', (a1, a2) => { + if (a1 === arg1 && a2 === arg2) { + timesCalled++; + } + }); - eventhub - .emit('eventname', arg1, arg2) - .emit('eventname', arg1, arg2); + eventhub.emit('eventname', arg1, arg2).emit('eventname', arg1, arg2); - eventhub.remove('eventname'); + eventhub.remove('eventname'); - eventhub - .emit('eventname', arg1, arg2) - .emit('eventname', arg1, arg2); + eventhub.emit('eventname', arg1, arg2).emit('eventname', arg1, arg2); - expect(timesCalled).toEqual(2); + expect(timesCalled).toEqual(2); }); test('Remove listener with handler', () => { - const eventhub = new Eventhub(); - const arg1 = 0; - const arg2 = 'hey'; - let timesCalled = 0; - - function handler(a1, a2) { - if (a1 === arg1 && a2 === arg2) { - timesCalled++; - } + const eventhub = new Eventhub(); + const arg1 = 0; + const arg2 = 'hey'; + let timesCalled = 0; + + function handler(a1, a2) { + if (a1 === arg1 && a2 === arg2) { + timesCalled++; } + } - eventhub.on('eventname', handler); + eventhub.on('eventname', handler); - eventhub - .emit('eventname', arg1, arg2) - .emit('eventname', arg1, arg2); + eventhub.emit('eventname', arg1, arg2).emit('eventname', arg1, arg2); - eventhub.remove('eventname', handler); + eventhub.remove('eventname', handler); - eventhub - .emit('eventname', arg1, arg2) - .emit('eventname', arg1, arg2); + eventhub.emit('eventname', arg1, arg2).emit('eventname', arg1, arg2); - expect(timesCalled).toEqual(2); + expect(timesCalled).toEqual(2); }); test('Remove listener with wrong handler', () => { - const eventhub = new Eventhub(); - const arg1 = 0; - const arg2 = 'hey'; - let timesCalled = 0; - - function handler(a1, a2) { - if (a1 === arg1 && a2 === arg2) { - timesCalled++; - } + const eventhub = new Eventhub(); + const arg1 = 0; + const arg2 = 'hey'; + let timesCalled = 0; + + function handler(a1, a2) { + if (a1 === arg1 && a2 === arg2) { + timesCalled++; } + } - eventhub.on('eventname', handler); + eventhub.on('eventname', handler); - eventhub - .emit('eventname', arg1, arg2) - .emit('eventname', arg1, arg2); + eventhub.emit('eventname', arg1, arg2).emit('eventname', arg1, arg2); - eventhub.remove('eventname', () => { - alert('test'); - }); + eventhub.remove('eventname', () => { + alert('test'); + }); - eventhub - .emit('eventname', arg1, arg2) - .emit('eventname', arg1, arg2); + eventhub.emit('eventname', arg1, arg2).emit('eventname', arg1, arg2); - expect(timesCalled).toEqual(4); + expect(timesCalled).toEqual(4); }); test('Event listener outside context', () => { - const eventhub = new Eventhub(); - let hasFired = false; - - (function() { - const data = 123; - eventhub.on('eventname', (argument) => { - if (argument === data) { - hasFired = true; - } - }); - })(); - - (function() { - const data = 123; - eventhub.emit('eventname', 123); - })(); - - expect(hasFired).toBeTruthy(); + const eventhub = new Eventhub(); + let hasFired = false; + + (function() { + const data = 123; + eventhub.on('eventname', argument => { + if (argument === data) { + hasFired = true; + } + }); + })(); + + (function() { + const data = 123; + eventhub.emit('eventname', 123); + })(); + + expect(hasFired).toBeTruthy(); }); test('Multiple event listeners', () => { - const eventhub = new Eventhub(); - let timesCalled = 0; - - eventhub - .on('eventname', () => { - timesCalled++; - }) - .on('eventname', () => { - timesCalled++; - }) - .on('eventname', () => { - timesCalled++; - }); - - eventhub.emit('eventname'); - - expect(timesCalled).toEqual(3); + const eventhub = new Eventhub(); + let timesCalled = 0; + + eventhub + .on('eventname', () => { + timesCalled++; + }) + .on('eventname', () => { + timesCalled++; + }) + .on('eventname', () => { + timesCalled++; + }); + + eventhub.emit('eventname'); + + expect(timesCalled).toEqual(3); }); test('Remove multiple event listeners and add the listeners again', () => { - const eventhub = new Eventhub(); - let timesCalled = 0; - - eventhub - .on('eventname', () => { - timesCalled++; - }) - .on('eventname', () => { - timesCalled++; - }) - .on('eventname', () => { - timesCalled++; - }); - - eventhub.remove('eventname'); - - eventhub - .on('eventname', () => { - timesCalled++; - }) - .on('eventname', () => { - timesCalled++; - }); - - eventhub.emit('eventname'); - - expect(timesCalled).toEqual(2); + const eventhub = new Eventhub(); + let timesCalled = 0; + + eventhub + .on('eventname', () => { + timesCalled++; + }) + .on('eventname', () => { + timesCalled++; + }) + .on('eventname', () => { + timesCalled++; + }); + + eventhub.remove('eventname'); + + eventhub + .on('eventname', () => { + timesCalled++; + }) + .on('eventname', () => { + timesCalled++; + }); + + eventhub.emit('eventname'); + + expect(timesCalled).toEqual(2); }); test('Event listener on without handler', () => { - const eventhub = new Eventhub(); + const eventhub = new Eventhub(); - expect(() => { - eventhub.once('eventname'); - }).toThrow(); + expect(() => { + eventhub.once('eventname'); + }).toThrow(); }); test('Event listener once without handler', () => { - const eventhub = new Eventhub(); + const eventhub = new Eventhub(); - expect(() => { - eventhub.once('eventname', 'string'); - }).toThrow(); + expect(() => { + eventhub.once('eventname', 'string'); + }).toThrow(); }); test('Is eventhub listening', () => { - const eventhub = new Eventhub(); - eventhub.on('eventname', () => { - alert('event'); - }); + const eventhub = new Eventhub(); + eventhub.on('eventname', () => { + alert('event'); + }); - expect(eventhub.isListening).toEqual(true); + expect(eventhub.isListening).toEqual(true); }); test('Is eventhub not listening', () => { - const eventhub = new Eventhub(); + const eventhub = new Eventhub(); - expect(eventhub.isListening).toEqual(false); + expect(eventhub.isListening).toEqual(false); }); test('Is eventhub not listening with remove', () => { - const eventhub = new Eventhub(); + const eventhub = new Eventhub(); - eventhub.on('eventname', () => { - alert('test'); - }); + eventhub.on('eventname', () => { + alert('test'); + }); - eventhub.remove('eventname'); + eventhub.remove('eventname'); - expect(eventhub.isListening).toEqual(false); -}); \ No newline at end of file + expect(eventhub.isListening).toEqual(false); +});