diff --git a/README.md b/README.md index fa795a3..9da06a5 100644 --- a/README.md +++ b/README.md @@ -570,9 +570,9 @@ A C++ implementation of [Events API](https://nodejs.org/api/events.html). NOTE: This implementation has some minor deviations from the above standard. Specifically there is no static `EventEmitter.defaultMaxListeners` property. -However the dynamic one persists. +However the dynamic one persists and is infinite (`0`) by default. -An example of it's usage can be found in **examples/node-addon** directory. +An example can be found in **examples/node-addon** directory. There is `Example` class, implemented in **cpp/example.cpp**, that inherits EventEmitter behavior and is exported to JS. @@ -595,7 +595,7 @@ Be sure to add the include directory in **binding.gyp**: ], ``` -Include the **event-emitter.hpp**, it already includes **addon-tools.hpp**. +Include the **event-emitter.hpp**, it also includes **addon-tools.hpp**. Inherit from `EventEmitter`, it already inherits from `Nan::ObjectWrap`: ``` diff --git a/examples/node-addon/index.js b/examples/node-addon/index.js index 9aedc36..1c7c614 100644 --- a/examples/node-addon/index.js +++ b/examples/node-addon/index.js @@ -52,7 +52,9 @@ example.emit('evt2', 112, '222'); console.log('example 1', example); -Example.defaultMaxListeners = -1; - +example.setMaxListeners(2); +example.on('max1', () => {}); +example.on('max1', () => {}); +example.on('max1', () => {}); module.exports = Example; diff --git a/include/event-emitter.hpp b/include/event-emitter.hpp index 1302fe3..c84d468 100644 --- a/include/event-emitter.hpp +++ b/include/event-emitter.hpp @@ -230,14 +230,21 @@ protected: emitter->_raw[name].push_back(cb); } - if (emitter->_maxListeners > 0 && emitter->_raw.size() > emitter->_maxListeners) { + int count = emitter->_raw[name].size(); + + if (emitter->_maxListeners > 0 && count > emitter->_maxListeners) { std::cout << "EventEmitter Warning: too many listeners ("; - std::cout << emitter->_raw.size() << " > " << emitter->_maxListeners; - std::cout << ") possible memory leak." << std::endl; + std::cout << count << " > " << emitter->_maxListeners << ") on '"; + std::cout << name << "' event, possible memory leak." << std::endl; - v8::Local code = JS_STR("(new Error()).stack"); - v8::Local stack = v8::Local::Cast(v8::Script::Compile(code)->Run()); + // Some JS magic to retrieve the call stack + v8::Local code = JS_STR( + "(new Error()).stack.split('\\n').slice(1).join('\\n')" + ); + v8::Local stack = v8::Local::Cast( + v8::Script::Compile(code)->Run() + ); Nan::Utf8String stackStr(stack); std::cout << *stackStr << std::endl;