addon-tools-raub
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -5,3 +5,3 @@ { | ||
"description": "A set of extra tools for Node.js addons", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"main": "index.js", | ||
@@ -8,0 +8,0 @@ "keywords": [ |
@@ -9,2 +9,3 @@ # Addon Tools | ||
* EventEmitter C++ implementation. | ||
* Contains helpers of following types: GYP, C++, JS, BAT (Windows). | ||
@@ -34,2 +35,3 @@ * Platforms: win x32/x64, linux x32/x64, mac x64. | ||
[Class EventEmitter](#class-eventemitter) | ||
@@ -564,1 +566,87 @@ --- | ||
``` | ||
--- | ||
## class EventEmitter | ||
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 and is infinite (`0`) by default. | ||
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. | ||
For the C++ side `EventEmitter` has following public methods: | ||
* `void emit(const std::string &name, int argc = 0, v8::Local<v8::Value> *argv = NULL)` | ||
emits an event with the given `name` and, optionally, some additional arguments where | ||
`argc` is the number of arguments and `argv` is a pointer to the arguments array. | ||
* `void on(const std::string &name, v8::Local<v8::Value> that, const std::string &method)` | ||
subscribes `that[method]` to receive `name` events from this emitter, basically | ||
`emitter.on(name, that[method])`. | ||
Be sure to add the include directory in **binding.gyp**: | ||
``` | ||
'include_dirs': [ | ||
'<!@(node -e "require(\'addon-tools-raub\').include()")', | ||
], | ||
``` | ||
Include the **event-emitter.hpp**, it also includes **addon-tools.hpp**. | ||
Inherit from `EventEmitter`, it already inherits from `Nan::ObjectWrap`: | ||
``` | ||
#include <event-emitter.hpp> | ||
class Example : public EventEmitter { | ||
... | ||
} | ||
``` | ||
First add EventEmitter dynamic methods to the prototype via | ||
`static void extendPrototype(v8::Local<v8::FunctionTemplate> &proto)` and then, | ||
after constructor function instance is created, | ||
`static void extendConstructor(v8::Local<v8::Function> &ctorFn)`: | ||
``` | ||
void Example::init(Handle<Object> target) { | ||
Local<FunctionTemplate> proto = Nan::New<FunctionTemplate>(newCtor); | ||
proto->InstanceTemplate()->SetInternalFieldCount(1); | ||
proto->SetClassName(JS_STR("Example")); | ||
// -------- dynamic | ||
// Add EventEmitter methods | ||
extendPrototype(proto); | ||
Nan::SetPrototypeMethod(proto, "destroy", destroy); | ||
// -------- static | ||
Local<Function> ctor = Nan::GetFunction(proto).ToLocalChecked(); | ||
extendConstructor(ctor); | ||
_constructor.Reset(ctor); | ||
Nan::Set(target, JS_STR("Example"), ctor); | ||
} | ||
``` | ||
NOTE: after a `v8::Function` is created from the `v8::FunctionTemplate`, no | ||
additional methods can be added to the prototype. Also static members can only | ||
be added to the created `v8::Function` representing the constructor. This is why | ||
there are 2 extend methods: `extendPrototype` and `extendConstructor`. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51958
26
100
650
2