Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

feat: Add ability to pass any event arguments amount via PassthroughEmitter #917

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 11 additions & 13 deletions lib/passthrough-emitter.js
Original file line number Diff line number Diff line change
@@ -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));
}

/**
Expand All @@ -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));
}
Expand Down
12 changes: 12 additions & 0 deletions test/unit/passthrough-emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
});
});
});