-
Notifications
You must be signed in to change notification settings - Fork 461
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
Question about ThreadSafeFunction #536
Comments
Hi @marco-ms, I think an AsyncWorker might work for you? Take a look at the example. |
AsyncWorker doesn't seem to fit, as I need to call my c++ code in the main thread and then it'll callback to the worker thread. |
Actually seems my request to have it optional has been implemented already on N-API, but looks like made it only to Node 12.6.0 and yet to be released 10.17.0? Too bad it'll never get to Node 8.x I guess. Anyhow, is there a way to support this directly under node-addon-api and create a bogus callback? I tried passing Napi::Function() to ThreadSafe, but it errors out. Suggestions welcome. cc @mhdawson |
@marco-ms can you show the code and the resulting error. You should be able to define dummy function that you can pass in. Likely, a global static one so that you can use the same one in all cases as well. |
Code looks like this, from the JS this function is called directly passing a Promise and getting back some result.
It throws:
If instead I modify |
Calling Function::New(env, [](const CallbackInfo& cbinfo){ return cbinfo.Env().Undefined(); } ) or a global static Value Noop( const CallbackInfo& info ) {
return info.Env().Undefined();
}
Function::New(env, Noop) // pass this to ThreadSafeFunction::New If you will be creating many TSFNs, you could create one function, store it in a reference, and pass that reference's value: Napi::FunctionReference ref = Napi::Persistent(Function::New(.../* from above */));
ref.Value() // pass this to ThreadSafeFunction::New The optional |
Adding a question here as I have been following this thread. I'm doing something similar, and using
|
You want the Well, I guess you could pass a function that was bound to a specific |
@KevinEady I'm just extending EventEmitter, I want to call |
@CraigMason sooo I would think, you are passing either the complete
What are you expecting to call |
I should probably explain the goal in a more abstract sense, rather than querying a specific implementation - as I'm new to native modules and I could be taking the wrong approach here. I have a device which has an SDK, which dispatches events via a callback on new threads. I ultimately want to encapsulate that object as a JS Object that emits events, so I can deal with remaining logic in Node. So having read through some example and the docs here, my conclusion was to extend EventEmitter (
Then create a Thank you, |
@CraigMason I see... One thing is that if you pass I'm not 100% sure if it would work, but try Napi::Function emit = info.This().As<Napi::Object>().Get("emit").As<Napi::Function>();
Napi::Function bound = emit.Get("bind").As<Function>().Call(emit, { info.This() }).As<Function>();
this->_emitRef = Napi::Reference<Napi::Function>::New(bound, 1); |
@KevinEady Thank you :) Calling Regarding single parameter.. hmm, I'm not sure yet, I need to consider the remaining architecture first. |
Thanks, your fix works for me. I wonder if node-addon-api can anticipate this optional argument by creating a noop function itself. That way would work also on older Node 8.x that won't probably get the fix backported. But I understand this may go out of scope. |
@marco-ms I think given that 8.x only has < 4 months before it goes EOL and there is a work around we'll probably just close this as we have lots of other work to do. Please let me know if you think that is not the right thing to do and we'll reopen. |
Hi,
I am doing some c++ native code that returns some values via a callback to the add-on on a separate thread. I am using ThreadSafeFunction to go back to the main thread and post result to the JavaScript, however to do that I am using Promises, so I don't really need a function callback.
https://github.com/nodejs/node-addon-api/blob/master/doc/threadsafe_function.md#new
But in all Napi::ThreadSafeFunction::New() there is no way to NOT pass a Function, which I don't really need. Am I using the right tool here to go from a worker thread back to the main thread or is there an alternative that doesn't require a Function?
I think this likely should be called ThreadSafeCallback and accept either a Function or a Promise.
The text was updated successfully, but these errors were encountered: