Add a use case of C++ on() to addon example

This commit is contained in:
raub 2018-05-14 15:38:38 +03:00
parent efcb17a23a
commit 8718958591
3 changed files with 19 additions and 0 deletions

View File

@ -33,6 +33,17 @@ Example::~Example() {
} }
NAN_METHOD(Example::cppOn) { THIS_EXAMPLE; THIS_CHECK;
REQ_STR_ARG(0, name);
REQ_FUN_ARG(1, cb);
example->on(*name, cb);
}
// ------ System methods and props for ObjectWrap // ------ System methods and props for ObjectWrap
V8_STORE_FT Example::_protoExample; V8_STORE_FT Example::_protoExample;
@ -52,6 +63,7 @@ void Example::init(V8_VAR_OBJ target) {
// -------- dynamic // -------- dynamic
Nan::SetPrototypeMethod(proto, "destroy", destroy); Nan::SetPrototypeMethod(proto, "destroy", destroy);
Nan::SetPrototypeMethod(proto, "cppOn", cppOn);
// -------- static // -------- static
V8_VAR_FUNC ctor = Nan::GetFunction(proto).ToLocalChecked(); V8_VAR_FUNC ctor = Nan::GetFunction(proto).ToLocalChecked();

View File

@ -27,6 +27,8 @@ private:
static NAN_METHOD(destroy); static NAN_METHOD(destroy);
static NAN_METHOD(cppOn);
private: private:

View File

@ -57,4 +57,9 @@ example.on('max1', () => {});
example.on('max1', () => {}); example.on('max1', () => {});
example.on('max1', () => {}); example.on('max1', () => {});
example.on('cpp-on', (arg1, arg2) => {
console.log('CPP_ON', arg1, arg2, example.eventNames());
});
example.emit('cpp-on', 555, 'abc');
module.exports = Example; module.exports = Example;