I use the two helper classes:
1, AsyncProgressQueueWorker
2, ObjectWrap
The first class, I used to do some long calculation
The second class, used to wrap the first class, so, in js invoked, can use a new way of creating
Interface exposed way is as follows:
For a: : Value Echo (const for a: : CallbackInfo & amp; Info)
{
If (info. Length ()!=8)
{
For a: : TypeError: : the New (info. Env (), "insufficient parameters"). The ThrowAsJavaScriptException ();
Return for a: : Boolean: : the New (info. The Env (), false);
}
STD: : string strCom=info. [0] As(a);
Int nBaundRate=info [1]. AsInt32Value () ();
STD: : string strParity=info. [2] As(a);
Unsigned char nDataSize=info. [3] AsInt32Value () ();
STD: : string strStopBits=info. [4] As(a);
For a: : Function okcb=info. [5] As(a);
For a: : Function errcb=info. [6] As(a);
For a: : Function progresscb=info. [7] As(a);
CZLCom * zlcom=new CZLCom (okcb errcb, progresscb);
Zlcom - & gt; The Queue ();
If (zlcom - & gt; Open (strCom nBaundRate, strParity nDataSize, strStopBits))
{
Code/* */
Return for a: : Boolean: : the New (info. The Env (), false);
}
The else
{
Return for a: : Boolean: : the New (info. The Env (), true);
}
}
,,,,,,,,
For a: : Object CZLComWrapper: : Init (for a: : Env Env, for a: : Object exports)
{
For a: : Function func=DefineClass (
Env, "ZLComWrapper,"
{InstanceMethod (" open ", & amp; CZLComWrapper: : Open),
InstanceMethod (" close ", & amp; CZLComWrapper: : Close),
InstanceMethod (" getValue ", & amp; CZLComWrapper: : GetValue),
StaticMethod<& CZLComWrapper: : CreateNewItem> (" CreateNewItem ")});
For a: : FunctionReference * constructor=new for a: : FunctionReference ();
* constructor=for a: : Persistent (func);
Env. SetInstanceData (constructor);
Exports. The Set (ZLComWrapper, func);
Return exports;
}
,,,,,,,,
Value CZLComWrapper for a: : : : Open (const for a: : CallbackInfo & amp; Info)
{
If (info. Length ()!=8)
{
For a: : TypeError: : the New (info. Env (), "insufficient parameters"). The ThrowAsJavaScriptException ();
Return for a: : Boolean: : the New (info. The Env (), false);
}
STD: : string strCom=info. [0] As(a);
Int nBaundRate=info [1]. AsInt32Value () ();
STD: : string strParity=info. [2] As(a);
Unsigned char nDataSize=info. [3] AsInt32Value () ();
STD: : string strStopBits=info. [4] As(a);
For a: : Function okcb=info. [5] As(a);
For a: : Function errcb=info. [6] As(a);
For a: : Function progresscb=info. [7] As(a);
M_pCom=new CZLCom (okcb errcb, progresscb);
M_pCom - & gt; The Queue ();
If (m_pCom - & gt; Open (strCom nBaundRate, strParity nDataSize, strStopBits))
{
The return info. Env (). Undefined ();
}
Return for a: : Boolean: : the New (info. The Env (), false);
}
,,,,,,,,
For a: : Object Init (for a: : Env Env, for a: : Object exports)
{
Exports. Set (
For a: : String: : the New (env, "echo"),
For a: : Function: : the New (env, Echo));
Return CZLComWrapper: : Init (env, exports).
}
Js invoked as follows:
Const addon=the require ("./build/Debug/greet. Node ')
Const onOk=(MSG)=& gt; {
console.log(msg);
};
Const onErr=(MSG)=& gt; {
console.log(msg);
};
Const onProgress=(MSG)=& gt; {
console.log(msg);
};
//class way
Var com=new addon. ZLComWrapper ();
Var ret=com. Open (" com3, "9600," n ", 8 "1", the onOk and onErr, onProgress);
The console. The log (ret);
//function way
//var ret=addon. Echo (" com3, "9600," n ", 8 "1", the onOk and onErr, onProgress);
//the console. The log (ret);
Then I found that using class, a short time later, can be received by CZLComWrapper internal recycling, and won't function way,
But if I'm in command line test (node inde. Js), instead of under the vscode debugging, won't appear afore-mentioned problems,
Does anyone know what reason is this excuse me?