Skip to content
event emitter for native addons
Branch: master
Clone or download
Latest commit bf4a9cc Aug 25, 2015
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
test
README.md install Aug 25, 2015
emitter.h first Aug 25, 2015
include_dirs.js first Aug 25, 2015
package.json first Aug 25, 2015

README.md

addon-emitter

event emitter class for native addons.

usage

using a native addon (here foo) with event emitter:

var Foo = require('foo')
var foo = new Foo()

foo.on('sus', function (a, b) {
  console.log('Received:', a, b);
})

the Foo class inherits the public interface of the Emitter class:

// foo.h

#include <nan.h>
#include <emitter.h>

// inherit from the Emitter class
class Foo : public Emitter {
public:
  static void Init(v8::Local<v8::Object> exports);

private:
  explicit Foo ();
  ~Foo ();

  void Bar ();
};

set the method for the event listening (usually the on method):

// foo.cc

void Foo::Init(v8::Local<v8::Object> exports) {
  v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);

  // setting the prototype method for the event listening
  Nan::SetPrototypeMethod(tpl, "on", Emitter::On);
}

to emit an event from Foo:

void Foo::Bar(const Nan::FunctionCallbackInfo<v8::Value>& info) {
  // the first parameter is the name of the event,
  // after that you can write as many parameters as needed,
  // but they have to convertible to v8's `Local<Value>` type
  Emit(Nan::New("sus").ToLocalChecked()
      , Nan::New("a string").ToLocalChecked(), 123);
}

the example above is just to show what you have to do to use the module. it's not a fully functioning program. for a working example check the test folder.

install

npm i addon-emitter

in binding.gyp:

{
  'include_dirs': [ "<!(node -e \"require('addon-emitter')\")" ]
}
You can’t perform that action at this time.