From ff43f3ca620a61347ac1fea6600771055489c861 Mon Sep 17 00:00:00 2001 From: SaadY Date: Sun, 22 Nov 2015 16:15:29 +0000 Subject: [PATCH 1/2] first implementation --- src/multi-event-es6.js | 6 ++++++ test.js | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/multi-event-es6.js b/src/multi-event-es6.js index 1acc869..22da6ac 100644 --- a/src/multi-event-es6.js +++ b/src/multi-event-es6.js @@ -93,6 +93,7 @@ class MultiEvent { let multiNamesMatches = allMultiNames.filter(helpers.matches(infos.splited)); + // get multiCallBacks let multiCallBacks = []; for (let multiNamesMatch of multiNamesMatches) { Array.prototype.push.apply(multiCallBacks, [...this._mapMulti.get(multiNamesMatch)]); @@ -110,6 +111,11 @@ class MultiEvent { return this; } + pipe(subEventPatternAsString, eventEmitter){ + this.on(subEventPatternAsString, function (...args){ + eventEmitter.emit(this.eventName, ...args); + }); + } } export default MultiEvent; diff --git a/test.js b/test.js index 5030fab..7a648fe 100644 --- a/test.js +++ b/test.js @@ -63,9 +63,22 @@ describe('EventEmitter', () => { }); }); - }); +describe('Pipe EventsSubSet', ()=>{ + let mtOne = new EventEmitter(); + let mtTwo = new EventEmitter(); + + mtOne.pipe('subSet.*', mtTwo); + it('#on event in the sub set (basic test)', (done)=>{ + mtTwo.on('subSet.pipeEventOk', (...arg)=>{ + if(arg.length === 2 && arg[0] === 'an argument' && arg[1] === 'another'){ + done(); + } + }); + mtOne.emit('subSet.pipeEventOk', 'an argument', 'another'); + }); +}); describe('MultiEventEmitter', () => { it('#on() and #emit() multi', (done) => { From 07ca8409f35dbd3ad84861dcc709cfbab22ca4f9 Mon Sep 17 00:00:00 2001 From: SaadY Date: Sun, 22 Nov 2015 16:35:10 +0000 Subject: [PATCH 2/2] another test --- test.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test.js b/test.js index 7a648fe..aa19491 100644 --- a/test.js +++ b/test.js @@ -70,6 +70,7 @@ describe('Pipe EventsSubSet', ()=>{ let mtTwo = new EventEmitter(); mtOne.pipe('subSet.*', mtTwo); + it('#on event in the sub set (basic test)', (done)=>{ mtTwo.on('subSet.pipeEventOk', (...arg)=>{ if(arg.length === 2 && arg[0] === 'an argument' && arg[1] === 'another'){ @@ -78,6 +79,21 @@ describe('Pipe EventsSubSet', ()=>{ }); mtOne.emit('subSet.pipeEventOk', 'an argument', 'another'); }); + + it('#on event in the wrong sub set', (done)=>{ + mtTwo.on('subSet.pipeEventOk2', (...arg)=>{ + if(arg.length === 2 && arg[0] === 'an argument' && arg[1] === 'another'){ + done(); + } + }); + mtTwo.on('subSet2.pipeEventOk2', (...arg)=>{ + if(arg.length === 2 && arg[0] === 'an argument' && arg[1] === 'another'){ + done("error"); + } + }); + mtOne.emit('subSet2.pipeEventOk2', 'an argument', 'another'); + mtOne.emit('subSet.pipeEventOk2', 'an argument', 'another'); + }); }); describe('MultiEventEmitter', () => {