tags: electron
ThreadSafeFunctionCreate emptyNapi::ThreadSafeFunctionInstance
Napi::Function::ThreadSafeFunction();
Create a new Napi::ThreadSafeFunction object instance
Napi::ThreadSafeFunction::ThreadSafeFunction(napi_threadsafe_function tsfn);
New(napi_env env,
const Function& callback,
const Object& resource,
ResourceString resourceName,
size_t maxQueueSize,
size_t initialThreadCount,
ContextType* context,
Finalizer finalizeCallback,
FinalizzerDataType* data);
env:structureNapi::ThreadSafeFunctionObject environmentcallback: A function called by another thread#include <chrono>
#include <napi.h>
#include <windows.h>
using namespace Napi;
std::string res;
ThreadSafeFunction tsfn;
Napi::Value Debug(Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
return Napi::String::New(env, res);
}
std::string dllcbk(std::string out)
{
auto callback = [](Napi::Env env, Function jsCallback, std::string *res)
{
jsCallback.Call({String::New(env, *res)});
delete res;
};
std::string *dllout = new std::string(out);
tsfn.BlockingCall(dllout, callback);
return "a";
}
Value Start( const CallbackInfo& info )
{
Napi::Env env = info.Env();
if ( !info[0].IsFunction() )
{
throw TypeError::New( env, "Expected first arg to be function" );
}
HMODULE module = LoadLibrary("DLL.dll");
typedef std::string (*pCallback)(std::string out);
typedef std::string (*InitFunc)(pCallback dllcallback);
InitFunc dllfunc = (InitFunc)GetProcAddress(module, "Background_DLL_Initialise");
// Create a ThreadSafeFunction
tsfn = ThreadSafeFunction::New(
env,
info[0].As<Function>(), // JavaScript function called asynchronously
"Resource Name", // Name
0, // Unlimited queue
1, // Only one thread will use this initially
[](Napi::Env){}
/*[]( Napi::Env ) { // Finalizer used to clean threads up
nativeThread.join();
} */);
return String::New(env, dllfunc(dllcbk));
}
Napi::Value Ver(Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
return Napi::Number::New(env, Napi::VersionManagement::GetNapiVersion(env));
}
Napi::Object Init( Napi::Env env, Object exports )
{
exports.Set( "start", Function::New( env, Start ) );
exports.Set("ver", Napi::Function::New(env, Ver));
exports.Set("Debug", Napi::Function::New(env, Debug));
return exports;
}
NODE_API_MODULE( clock, Init )
#include <napi.h>
#include <windows.h>
#include "async.h"
typedef std::string (*pCallBack)(std::string out);
typedef std::string (*DllCBK)(pCallBack callback);
// Inherited from AsyncWorker, where asynchronous calls are implemented
class PiWorker : public Napi::AsyncWorker {
public:
PiWorker(Napi::Function& callback)
: Napi::AsyncWorker(callback), estimate(0) {}
~PiWorker() {}
// Executed inside the worker-thread.
// It is not safe to access JS engine data structure
// here, so everything we need for input and output
// should go on `this`.
// The executive body is here
void Execute () {
volatile int i = 0;
while(--i < 0);
}
// Executed when the async work is complete
// this function will be run inside the main event loop
// so it is safe to use JS engine data again
// This will be called after execution
void OnOK() {
Napi::HandleScope scope(Env());
Callback().Call({Napi::String::New(Env(), "Async Call")});
}
void Notify(std::string res) {
Napi::HandleScope scope(Env());
Callback().Call({Napi::String::New(Env(),res)});
}
private:
int points;
double estimate;
};
PiWorker* piWorker;
std::string DllCallBack(std::string out)
{
piWorker->Notify(out);
return "";
}
Napi::Value calltst(const Napi::CallbackInfo& info)
{
piWorker->Notify("testasync");
return info.Env().Null();
}
// Asynchronous access to the `Estimate()` function
Napi::Value CalculateAsync(const Napi::CallbackInfo& info) {
Napi::Function callback = info[0].As<Napi::Function>();
PiWorker* piWorker = new PiWorker(callback);
piWorker->Queue();
// HMODULE module = LoadLibrary("DLL.dll");
// DllCBK cbk = (DllCBK)GetProcAddress(module, "Background_DLL_Initialise");
// cbk(DllCallBack);
return info.Env().Undefined();
}
Napi::Object Init(Napi::Env env, Napi::Object exports)
{
exports.Set(Napi::String::New(env, "Call"), Napi::Function::New(env, CalculateAsync));
exports.Set(Napi::String::New(env, "Test"), Napi::Function::New(env, calltst));
return exports;
}
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
Recently the company asked to write a C++ plugin for Node.js. I haven't touched v8 before, but I also accepted the task with a hard scalp. The specific content is not mentioned here, so as to open the...
NODE.JS NAPI greatly facilitates the writing of C ++ ADDON, so that users no longer need to face complex V8. This article uses an example to analyze what NAPI's usage and NAPI did. 1 Deport to the fun...
2019 Unicorn Enterprise Heavy Glour Recruitment Python Engineer Standard >>> The previous introduction article: Node.js C ++ Addon Computer War (1) Node-GYP Node.js C ++ ADDON Computer Common...
Installing somenpmWhen the module is used, it often encounters packages that need to be compiled locally. inLinuxThere are very few problems under the system. But whenWindowsThere will be a lot of ine...
CentOS environment realizes Node.js calling C++ method through C++ Addon 1. C++ Addon 2. Basic configuration 2.1 Build node.js environment and configure npm 2.1.1 wget 2.1.2 nodejs&npm 2.2 Install...
Today, I summed up the previous C++ Addon project for node.js. After the exchange, I rejected the previous plan to package MacDataTransfer.lib and dll into a node.node file available, because this con...
Because of the needs of the project, write some C++ plugins for node.js calls, so build the environment for developing node addon in vs. Because the video is all in English, and there are some details...
How to build a C++Addon environment that can write node.js in VS2015 Due to the needs of the project, some C++ implemented plug-ins are written for node.js to call, so the environment for devel...
I. Clone Electron-Quick-Start II. Import the Addon project my blogc++addon CategoryBuild a C ++ Addon project with Node-Addon-API (packaging class object) Three. Use Addon's module in the main program...
Thrift uses Nodejs as the server and C# as the client. 1. The processing of callbacks in Thrift in nodejs, I didn't read the document carefully. Depressed for a while. 2. Nodejs supports relatively fe...