From 59bd47df1b40bd6675d0a032379a1ac1a832a8bd Mon Sep 17 00:00:00 2001 From: tormozz48 Date: Sat, 2 Jun 2018 13:45:13 +0300 Subject: [PATCH 1/2] feat: Add ability to pass any event arguments via PassthroughEmitter --- lib/passthrough-emitter.js | 24 +++++++++++------------- test/unit/passthrough-emitter.js | 14 +++++++++++++- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/lib/passthrough-emitter.js b/lib/passthrough-emitter.js index 090417d85..70f4d8b49 100644 --- a/lib/passthrough-emitter.js +++ b/lib/passthrough-emitter.js @@ -1,16 +1,14 @@ 'use strict'; -const _ = require('lodash'), - AsyncEmitter = require('gemini-core').events.AsyncEmitter; +const _ = require('lodash'); +const AsyncEmitter = require('gemini-core').events.AsyncEmitter; -module.exports = class PassthroughEmitter extends AsyncEmitter { - // Allow to pass only one argument with event - emit(type, data) { - return super.emit(type, data); - } +const ASYNC_FLAG = 'async'; +const markAsAsync = (args) => !args.includes(ASYNC_FLAG) ? args.concat(ASYNC_FLAG) : args; - emitAndWait(type, data) { - return super.emitAndWait(type, data, {shouldWait: true}); +module.exports = class PassthroughEmitter extends AsyncEmitter { + emitAndWait(type, ...args) { + return super.emitAndWait(type, ...markAsAsync(args)); } /** @@ -24,11 +22,11 @@ module.exports = class PassthroughEmitter extends AsyncEmitter { return; } - emitter.on(event, function(data, opts) { - if (opts && opts.shouldWait) { - return this.emitAndWait(event, data); + emitter.on(event, function(...args) { + if (args.includes(ASYNC_FLAG)) { + return this.emitAndWait(event, ...args); } else { - this.emit(event, data); + this.emit(event, ...args); } }.bind(this)); } diff --git a/test/unit/passthrough-emitter.js b/test/unit/passthrough-emitter.js index c36bfd6b4..1a1b4936a 100644 --- a/test/unit/passthrough-emitter.js +++ b/test/unit/passthrough-emitter.js @@ -1,7 +1,7 @@ 'use strict'; const PassthroughEmitter = require('lib/passthrough-emitter'); -describe('PassthroughEmitter', () => { +describe.only('PassthroughEmitter', () => { let runner, child; @@ -46,5 +46,17 @@ describe('PassthroughEmitter', () => { assert.equal(data, 'some-data'); }); }); + + it('should be able to pass multiple event arguments', () => { + runner.passthroughEvent(child, 'some-event'); + runner.on('some-event', function(...args) { + return `some-data ${args[0]} ${args[1]}`; + }); + + return child.emitAndWait('some-event', 'foo', 'bar') + .then((data) => { + assert.equal(data, `some-data foo bar`); + }); + }); }); From 75f618e5000b1dfa5cc36e4bd8995a8bc5dc8f0b Mon Sep 17 00:00:00 2001 From: tormozz48 Date: Sat, 2 Jun 2018 13:59:14 +0300 Subject: [PATCH 2/2] chore: Remove missed .only in unit-test --- test/unit/passthrough-emitter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/passthrough-emitter.js b/test/unit/passthrough-emitter.js index 1a1b4936a..85983c09f 100644 --- a/test/unit/passthrough-emitter.js +++ b/test/unit/passthrough-emitter.js @@ -1,7 +1,7 @@ 'use strict'; const PassthroughEmitter = require('lib/passthrough-emitter'); -describe.only('PassthroughEmitter', () => { +describe('PassthroughEmitter', () => { let runner, child;