fix maxListeners

This commit is contained in:
raub 2018-03-07 20:00:44 +03:00
parent be1f79e4fc
commit 54189d4047
3 changed files with 19 additions and 10 deletions

View File

@ -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. NOTE: This implementation has some minor deviations from the above standard.
Specifically there is no static `EventEmitter.defaultMaxListeners` property. 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 There is `Example` class, implemented in **cpp/example.cpp**, that inherits
EventEmitter behavior and is exported to JS. 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`: Inherit from `EventEmitter`, it already inherits from `Nan::ObjectWrap`:
``` ```

View File

@ -52,7 +52,9 @@ example.emit('evt2', 112, '222');
console.log('example 1', example); console.log('example 1', example);
Example.defaultMaxListeners = -1; example.setMaxListeners(2);
example.on('max1', () => {});
example.on('max1', () => {});
example.on('max1', () => {});
module.exports = Example; module.exports = Example;

View File

@ -230,14 +230,21 @@ protected:
emitter->_raw[name].push_back(cb); 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 << "EventEmitter Warning: too many listeners (";
std::cout << emitter->_raw.size() << " > " << emitter->_maxListeners; std::cout << count << " > " << emitter->_maxListeners << ") on '";
std::cout << ") possible memory leak." << std::endl; std::cout << name << "' event, possible memory leak." << std::endl;
v8::Local<v8::String> code = JS_STR("(new Error()).stack"); // Some JS magic to retrieve the call stack
v8::Local<v8::String> stack = v8::Local<v8::String>::Cast(v8::Script::Compile(code)->Run()); 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); Nan::Utf8String stackStr(stack);
std::cout << *stackStr << std::endl; std::cout << *stackStr << std::endl;