c++node.jsnode-addon-apin-api

node js callback from native C++ code is throwing errors


I am trying to implement event listeners. Node js will subscribe to the events (using C++ function). When the event occurs, C++ should inform node js.

JS code:

pal.subscribeEvent("ONSTATECHANGE", function(a) { console.log("Event received for ONSTATECHAN";});
function kcb (a)  {
    console.log("KCB .....ONSTATECHAN"+JSON.stringify(a));
}
pal.subscribeEvent("ONSTATECHANGE", kcb);

C++ code:

struct deviceNapi {
    Napi::Env env;   // to store JS Function Env
    Napi::Function jsCallback;   // to Store JS function
};

//std::multimap<int, Napi::Function> deviceEventMgmt;
std::multimap<int, struct deviceNapi> deviceEventMgmt;

Napi::Value PAL::subscribeEvent(const Napi::CallbackInfo& info) {
    Napi::Env env  = info.Env();
    int eventID = info[0].As<Napi::Number>().Int32Value();

    deviceNapi obj;
    obj.env = env;
    obj.jsCallback = info[1].As<Napi::Function>();

    deviceEventMgmt.insert(std::make_pair(eventID, obj));
    /*Napi::Function jsCallback = info[1].As<Napi::Function>();
    deviceEventMgmt.insert(std::make_pair(eventID, jsCallback));*/
}

void ProcessDeviceEvent(int evt, DeviceEvtData data) {
    switch(evt) {
        case ONSTATECHANGE:
            for(auto it = deviceEventMgmt.begin(); it != deviceEventMgmt.end(); it ++) {
                if(it->first == ENUM_VERIZON_DEVICE_PENDING_RESET) {
                    Napi::Env env = it.second.env;
                    Napi::Object ret = Napi::Object::New(env);

                    ret.Set("reason", Napi::String::New(env, data.resetReason));
                    ret.Set("time", Napi::Number::New(env, data.seconds));

                    it->second.jsCallback.Call(ret);
                }
            }

            /*for(auto it = deviceEventMgmt.begin(); it != deviceEventMgmt.end(); it ++) {
                if(it->first == ENUM_VERIZON_DEVICE_PENDING_RESET) {
                    it->second.Call();
                }
            }*/

            break;

        default:
            std::cout << "ProcessDeviceEvent : Currently not handling evt : " << evt << std::endl;
            break;
    }
}

C++ code is throwing below errors while compiling

../PAL.cpp: In member function 'Napi::Value PAL:: subscribeEvent(const Napi::CallbackInfo&)':
../PAL.cpp:1915:13: error: use of deleted function 'deviceNapi::deviceNapi()'
  deviceNapi obj;
             ^~~
../PAL.cpp:1903:8: note: 'deviceNapi::deviceNapi()' is implicitly deleted because the default definition would be ill-formed:
 struct deviceNapi {
        ^~~~~~~~~~
../PAL.cpp:1903:8: error: no matching function for call to 'Napi::Env::Env()'

Can any one please help me to how to fix this problem of storing Node function env into C++ map. I need to save the env so that I can send the response when calling Node callback.


Solution

  • Napi::Env doesn't have a default constructor so your deviceNapi struct's default constructor cannot initialise the env member so the compiler didn't generate it, but you try to call it on this line:

    deviceNapi obj;
    

    You can fix this by initialising it like this:

    deviceNapi obj = {env, info[1].As<Napi::Function>()};