Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including addon-emitter with all npm packages installed. Try it out:

var addonEmitter = require("addon-emitter")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

addon-emitter v0.0.2

event emitter class for native addons

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')\")" ]
}

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free