diff --git a/examples/node-addon/cpp/common.hpp b/examples/node-addon/cpp/common.hpp index 2ec3285..7364e3c 100644 --- a/examples/node-addon/cpp/common.hpp +++ b/examples/node-addon/cpp/common.hpp @@ -4,5 +4,7 @@ #include +#define DES_CHECK \ + if (_isDestroyed) return; #endif /* _COMMON_HPP_ */ diff --git a/examples/node-addon/cpp/example.cpp b/examples/node-addon/cpp/example.cpp index 00d2dff..dd51765 100644 --- a/examples/node-addon/cpp/example.cpp +++ b/examples/node-addon/cpp/example.cpp @@ -7,11 +7,11 @@ using namespace v8; using namespace node; using namespace std; -#define THIS_EXAMPLE \ +#define THIS_EXAMPLE \ Example *example = ObjectWrap::Unwrap(info.This()); #define THIS_CHECK \ - if (body->_isDestroyed) return; + if (example->_isDestroyed) return; Nan::Persistent Example::_constructor; @@ -60,14 +60,14 @@ Example::Example() { } -Body::~Body() { +Example::~Example() { _destroy(); } -void Body::_destroy() { DES_CHECK; +void Example::_destroy() { DES_CHECK; _isDestroyed = true; @@ -76,7 +76,7 @@ void Body::_destroy() { DES_CHECK; } -NAN_METHOD(Body::destroy) { THIS_BODY; THIS_CHECK; +NAN_METHOD(Example::destroy) { THIS_EXAMPLE; THIS_CHECK; example->_destroy(); diff --git a/examples/node-addon/cpp/example.hpp b/examples/node-addon/cpp/example.hpp index b45b5df..c2bb23a 100644 --- a/examples/node-addon/cpp/example.hpp +++ b/examples/node-addon/cpp/example.hpp @@ -25,6 +25,11 @@ protected: static NAN_METHOD(destroy); +private: + + void _destroy(); + + private: static Nan::Persistent _constructor; diff --git a/include/event-emitter.hpp b/include/event-emitter.hpp index fd22904..1da0a01 100644 --- a/include/event-emitter.hpp +++ b/include/event-emitter.hpp @@ -17,7 +17,7 @@ class EventEmitter : public Nan::ObjectWrap { typedef Nan::CopyablePersistentTraits::CopyablePersistent FN_TYPE; typedef std::deque VEC_TYPE; typedef std::map MAP_TYPE; - typedef std::map FNMAP_TYPE; + typedef std::map FNMAP_TYPE; typedef VEC_TYPE::iterator IT_TYPE; typedef MAP_TYPE::iterator MAP_IT_TYPE; @@ -25,6 +25,7 @@ public: EventEmitter () { _maxListeners = _defaultMaxListeners; + _freeId = 0; } ~EventEmitter () {} @@ -87,12 +88,12 @@ public: // Deprecated method static NAN_METHOD(jsStaticListenerCount) { - EventEmitter *emitter = ObjectWrap::Unwrap(info[0]); - REQ_UTF8_ARG(1, name); + // EventEmitter *emitter = ObjectWrap::Unwrap(info[0]); + // REQ_UTF8_ARG(1, name); - const VEC_TYPE &list = emitter->_listeners[*name]; + // const VEC_TYPE &list = emitter->_listeners[*name]; - RET_VALUE(JS_INT(list.size())); + // RET_VALUE(JS_NUM(list.size())); } @@ -166,7 +167,7 @@ public: const VEC_TYPE &list = emitter->_listeners[*name]; - RET_VALUE(JS_INT(list.size())); + RET_VALUE(JS_INT(static_cast(list.size()))); } @@ -245,7 +246,10 @@ public: emitter->_listeners[name].push_back(cb); emitter->_raw[name].push_back(raw); } - emitter->_wrapped[raw] = cb; + + int nextId = _freeId++; + emitter->_wrappedIds[nextId] = cb; + emitter->_rawIds[nextId] = raw; } @@ -292,11 +296,11 @@ public: static NAN_METHOD(jsRemoveAllListeners) { THIS_EMITTER; - if (info->Length > 0 && info[0]->IsString()) { + if (info.Length() > 0 && info[0]->IsString()) { - _listeners->clear(); - _raw->clear(); - _wrapped->clear(); + emitter->_listeners.clear(); + emitter->_raw.clear(); + emitter->_wrapped.clear(); return; @@ -313,12 +317,12 @@ public: for (IT_TYPE it = list.begin(); it != list.end(); ++it) { - emitter->_wrapped.erase(*it) + emitter->_wrapped.erase(*it); } - emitter->_listeners[*name].clear(); - emitter->_raw[*name].clear(); + emitter->_listeners[name].clear(); + emitter->_raw[name].clear(); } @@ -328,8 +332,8 @@ public: REQ_UTF8_ARG(0, n); REQ_FUN_ARG(1, raw); - Nan::Persistent persistentCb; - persistentCb.Reset(raw); + Nan::Persistent persistentRaw; + persistentRaw.Reset(raw); std::string name = std::string(*n); @@ -342,7 +346,7 @@ public: for (IT_TYPE it = rawList.begin(); it != rawList.end(); ++it) { - if (*it === persistentCb) { + if (*it == persistentRaw) { rawList.erase(it); break; } @@ -352,11 +356,11 @@ public: VEC_TYPE &wrapList = emitter->_listeners[name]; - if (emitter->_wrapped.count(persistentCb) == 0) { + if (emitter->_wrapped.count(persistentRaw) == 0) { for (IT_TYPE it = wrapList.begin(); it != wrapList.end(); ++it) { - if (*it === persistentCb) { + if (*it == persistentRaw) { wrapList.erase(it); break; } @@ -368,18 +372,18 @@ public: } - const FN_TYPE &wrapped = _wrapped[persistentCb]; + const FN_TYPE &wrapped = emitter->_wrapped[persistentRaw]; for (IT_TYPE it = wrapList.begin(); it != wrapList.end(); ++it) { - if (*it === wrapped) { + if (*it == wrapped) { wrapList.erase(it); break; } } - emitter->_wrapped.erase(persistentCb); + emitter->_wrapped.erase(persistentRaw); } @@ -399,7 +403,7 @@ public: VEC_TYPE &list = emitter->_raw[*name]; - Local jsListeners = Nan::New(list.size()); + v8::Local jsListeners = Nan::New(list.size()); if (list.empty()) { RET_VALUE(jsListeners); @@ -429,7 +433,10 @@ private: MAP_TYPE _listeners; MAP_TYPE _raw; - FNMAP_TYPE _wrapped; + + int _freeId; + FNMAP_TYPE _wrappedIds; + FNMAP_TYPE _rawIds; };