n-api-article | https : //medium.com/ @
kandi X-RAY | n-api-article Summary
kandi X-RAY | n-api-article Summary
https://medium.com/@schahriar/n-api-and-getting-started-with-writing-c-addons-for-node-js-cf061b3eae75
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of n-api-article
n-api-article Key Features
n-api-article Examples and Code Snippets
Community Discussions
Trending Discussions on n-api-article
QUESTION
I have an N-API C++ addon that I would like to use with an Electron GUI. Currently the C++ addon has a simple function that sleeps for 10 seconds and then performs a computation of 8*2, and returns the value to the Javascript code. The Javascript code runs the C++ addon every 10 seconds.
...ANSWER
Answered 2019-Mar-12 at 11:40The reason it blocks Electron is the sleep(10)
. That call doesn't return for 10 seconds.
So yes, there is a way to offload heavy computation to another thread. The biggest complication from doing so is that the thread can't make a callback to JavaScript without taking extra precautions nor can the thread access JavaScript data structures, so it must be provided with all the data necessary.
Here's the example the C++ abstraction provides for N-API using a thread to calculate Pi.
And here are C++ wrappers to create thread-safe callbacks.
It's not trivial but these two examples should get you through it.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install n-api-article
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page