Node.js native EventEmitter. Allows you to implement Native C++ code that behaves like an EventEmitter. Users of your native module will be able to write code like
foo = ;foo
And inside your native code, you will be able to emit events as they happen, which will trigger the appropriate callbacks in javascript. The emit occurs in a separate thread from your AsyncWorker, and so will not appreciably block the work being done.
Examples of using The EventEmitter, the first is the non-reentrant non-threadsafe version, the second is the threadsafe and reentrant, but requires you pass the emitter object around
; ;
And then for the object that behaves like an EventEmitter, you add the 'on' method to register callbacks on events, and have some sort of "run" methods that do asynchronous work
;
By default, this module uses a multiple-producer, multiple-consumer ringbuffer using a mutex for synchronization. With EventEmitter, there can really only ever be a single consumer, and that's the thread running the main loop; however there could be multiple producers. IF you can guarantee that you will only ever have a single producer thread emitting at a time via some external synchronization method, and you have boost available, you can define "HAVE_BOOST" to enable the use of boost::lockfree::spsc_queue, which will provide much lower latency, which could be particularly important in your worker thread if emitting occurs at a high rate. As an example of where you might benefit from this, if you have an object which has a single emitter per instance, and you only permit a single asynchronous method to be invoked on that instance at any one time, then you can safely use the higher-performing boost::lockfree::spsc_queue