Skip to content

Commit

Permalink
plugin: don't call process.emitWarning in cordova
Browse files Browse the repository at this point in the history
Removes call to process.emitWarning in the cordova events code.
Logs with console.warn() instead of throwing an exception due to
the process object not existing in the cordova runtime.
Also adds test to avoid a regression.
  • Loading branch information
jaimecbernardo committed Apr 18, 2019
1 parent 41689bc commit 50b33d3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
15 changes: 15 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,21 @@ exports.defineAutoTests = function () {
nodejs.channel.post('test-echo', 'bar');
});

it( 'should not throw error when going over MaxListeners' , function(done) {
let listenerNumberToTest = nodejs.channel.getMaxListeners()+1;
let numberCalled = 0;
for(let i=0;i<listenerNumberToTest;i++) {
nodejs.channel.addListener('echo', function(replyObj) {
numberCalled++;
if(numberCalled>=listenerNumberToTest) {
done();
}
});
}
expect(nodejs.channel.listenerCount('echo')).toBe(listenerNumberToTest);
nodejs.channel.post('test-echo', 'foo');
});

afterEach( function(done) {
nodejs.channel.removeAllListeners('echo');
generalCleanUp('echo', done);
Expand Down
10 changes: 3 additions & 7 deletions www/nodejs_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,11 @@ function _addListener(target, type, listener, prepend) {
m = $getMaxListeners(target);
if (m && m > 0 && existing.length > m) {
existing.warned = true;
const w = new Error('Possible EventEmitter memory leak detected. ' +
const warnMsg = 'Possible EventEmitter memory leak detected. ' +
`${existing.length} ${String(type)} listeners ` +
'added. Use emitter.setMaxListeners() to ' +
'increase limit');
w.name = 'MaxListenersExceededWarning';
w.emitter = target;
w.type = type;
w.count = existing.length;
process.emitWarning(w);
'increase limit';
console.warn(warnMsg);
}
}
}
Expand Down

0 comments on commit 50b33d3

Please sign in to comment.