EventEmitter
Hi,
Is it possible to add another example showing how to emit events from the c++ land?
Here's an example done by bnoordhuis 2 years ago. Maybe you could rewrite it for v0.10 and v0.11?
The basic idea is to expose a c++ class which inherits from ObjectWrap and internally calls to the emit function and in the js code inherit from EventEmitter.
I'll look in to that, I would normally say that you should abstract those details into JS-land where it's much easier but that example from Ben looks interesting.
The example from bnoordhuis has a recent open pull request with a more modern example:
https://github.com/bnoordhuis/node-event-emitter/pull/2
Wasn't EventEmitter removed? joyent/node@4ef8f06fe62edb74fded0e817266cb6398e69f36 IIRC, you are simply not supposed to emit events from native-land. It should be done with javascript. The easiest way being to define a js callback for native-land that emits the event.
EventEmitter is alive and well. That linked example effectively does what you're describing. Do the EE stuff in JS and have simple hooks into C++. Still a worthwhile example because EE is such a common pattern.
Oh my. In that case, zmq does it quite nicely. https://github.com/JustinTulloss/zeromq.node/blob/96bd7811a41eaac8f30dfed58769752f55ab2e3d/binding.cc#L304-322
but porting the mentioned example should be very easy.
+1 on an example with nan - this would significantly reduce my learning curve.
To my eyes, the NanAsyncWorker class is built for single callback execution, whereas an event emitter will trigger callbacks multiple times - I'm not seeing a clear way to do that with nan
@davidmarkclements that's exactly what I was thinking, I'd like to run a long running thread behind the screen which can emit events. I checked on nan project @bnoordhuis suggested to use uv_thread_create for doing something long running instead of NanAsyncWorker. It would be great if nan can abstract that functionality.
+1
What you just need is to use NanAsyncProgressWorker. It's a NanAsyncQueueWorker but with progress callbacks through libuv's uv_async_send functionality. Whenever you call progress->Send(char *, size); it will be caught by your devirtualized function 'HandleProgressCallback()' that will be executed in your main event loop. This can then for example execute a callback that'll invoke something in your .js. What is asked by the author however, is how to pass these along so that we have on.<> events.