-
Notifications
You must be signed in to change notification settings - Fork 462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Correct way to use EventEmitter and emit events from addon #110
Comments
Sounds like you just need to bind the
(And that is essentially what your adapter function does. But |
#bennycooly did Jason's suggestion resolve your issue ? Just wondering if we can close this issue ? |
Hi everyone, |
It might make sense to add those examples to: https://github.com/nodejs/abi-stable-node-addon-examples |
Ok in this week I will work to complete another two examples about the event emitter usage after that I can add them to the repo. |
Deleted bad examples Added examples on how to use event emitter ref. nodejs/node-addon-api#110 Removed bad examples Added examples for event emitter See: nodejs/node-addon-api#110
Hi @bennycooly did you solved the problem? May we close this issue? |
Porting asynchronous example from NAN repo. The original example is hosted here: https://github.com/nodejs/nan/tree/master/examples/async_pi_estimate. Rewrite all the logic using **node-addon-api** See the following issue https://github.com/nodejs/abi-stable-node-addon-examples/issues/10 Added examples for event emitter See: nodejs/node-addon-api#110
Porting asynchronous example from NAN repo. The original example is hosted here: https://github.com/nodejs/nan/tree/master/examples/async_pi_estimate. Rewrite all the logic using **node-addon-api** See the following issue https://github.com/nodejs/abi-stable-node-addon-examples/issues/10 Added examples for event emitter See: nodejs/node-addon-api#110
Yes, we can close this. I got mine to work using the object wrap! |
@NickNaso emit event from cpp is good but what about the same from c code? |
Hi @NickNaso, I saw your example in https://github.com/NickNaso/addon-event-emitter/blob/master/00/src/emit-from-cpp.cc and it was very useful to understand how to use event emitter (I'm pretty new to NodeJS and NAPI). However, as you mention in your comment: // All work but it's not a good practice bacause all long running task should be Assuming that my worker thread is another C++ thread, how could I send the callback from that thread? for example: As I understand this is not supported by design in NodeJS, and I saw that the NAPI implementantion of (Napi::AsyncWorker) does not support a progress callback. |
Hi @marcelman , |
Hi @NickNaso, |
Hi @NickNaso , |
This was asked for in nodejs/node-addon-api#110 (comment) and nodejs/node-addon-api#110 (comment). The solution was described in text in nodejs/node-addon-api#110 (comment) but it would be better to have a code example that folks can refer to. Signed-off-by: Darshan Sen <raisinten@gmail.com>
It's been a while but in case anyone else still needs the example, I've sent a PR to add it in - nodejs/node-addon-examples#211. |
This was asked for in nodejs/node-addon-api#110 (comment) and nodejs/node-addon-api#110 (comment). The solution was described in text in nodejs/node-addon-api#110 (comment) but it would be better to have a code example that folks can refer to. Signed-off-by: Darshan Sen <raisinten@gmail.com>
I was trying this with Napi::ThreadSafeFunction but it keeps crashing when Node encounters the function call in its event loop supposedly. The C++ itself runs through without problems.
The result is a:
I can assert that the (same) event was generated two times in the native code which I think is the reason for two Exceptions. What am I doing wrong? |
So I've been looking around and see many different ways people have tried to use EventEmitter with C++ addons. I first tried to pass the
EventEmitter.emit
method directly to a C++ ObjectWrap constructor and invoke that method usingNapi::Function::Call({args})
but the C++ code cannot see the actual EventEmitter object and thus fails sinceemit()
accesses the EventEmitter class variables. This can be trivially solved by just wrapping theemit()
call using a JS callback:And in C++:
However, I am just wondering if this is the correct way to actually emit events or not.
The text was updated successfully, but these errors were encountered: