fix maxListeners warning for once listeners

This commit is contained in:
Luis Blanco 2018-03-07 20:28:26 +03:00
parent 54189d4047
commit 14d66d28fe
2 changed files with 21 additions and 0 deletions

View File

@ -6,6 +6,7 @@
This is a set of helpers for simplification and standardization of addons and This is a set of helpers for simplification and standardization of addons and
dependency packages. dependency packages.
* EventEmitter C++ implementation.
* Contains helpers of following types: GYP, C++, JS, BAT (Windows). * Contains helpers of following types: GYP, C++, JS, BAT (Windows).
* Platforms: win x32/x64, linux x32/x64, mac x64. * Platforms: win x32/x64, linux x32/x64, mac x64.
* Useful links: [V8 Ref](https://v8docs.nodesource.com/node-0.8/d2/dc3/namespacev8.html), * Useful links: [V8 Ref](https://v8docs.nodesource.com/node-0.8/d2/dc3/namespacev8.html),

View File

@ -292,6 +292,26 @@ protected:
emitter->_wrappedIds[nextId] = cb; emitter->_wrappedIds[nextId] = cb;
emitter->_rawIds[nextId] = raw; emitter->_rawIds[nextId] = raw;
int count = emitter->_raw[name].size();
if (emitter->_maxListeners > 0 && count > emitter->_maxListeners) {
std::cout << "EventEmitter Warning: too many listeners (";
std::cout << count << " > " << emitter->_maxListeners << ") on '";
std::cout << name << "' event, possible memory leak." << std::endl;
// Some JS magic to retrieve the call stack
v8::Local<v8::String> code = JS_STR(
"(new Error()).stack.split('\\n').slice(1).join('\\n')"
);
v8::Local<v8::String> stack = v8::Local<v8::String>::Cast(
v8::Script::Compile(code)->Run()
);
Nan::Utf8String stackStr(stack);
std::cout << *stackStr << std::endl;
}
} }