0

So i am working with electron and i have a NAPI file where i have my backend code. On my interface, when i click on a certain button, a function is triggered and it calls the function from NAPI ( electron is the bridge between NAPI and my javascript file ) and when the "SimpleFunction" from NAPI is called, i have another function inside it called "SendToElectron" and i want it to send something to electron instead of returning a value from SimpleFunction.(It probably doesn't make sense now because i used a simple template to describe the problem, but i need it for my original code). I tried to do it with napi callbacks, but didn't work (because i probably didn't use them properly, i'd like a solution with callbacks as well if you have one), i am out of ideas and i don't know what to do.

scripts.js (where i call the function on my frontend)

async function ClickButtonEvent(currentID)
{
  data = await api.SimpleFunction(parseInt(currentID));
}
  

main.js (my main electron file)

const { app, BrowserWindow, Menu, ipcMain, dialog } = require('electron');
const path = require('path');
const getPcapData = require('./NAPI/build/Release/operations');

function createWindow() {
  const win = new BrowserWindow({
    width: 1920,
    height: 1080,
    minWidth: 500,
    minHeight: 500,
    maxWidth: 1920,
    maxHeight: 1080,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
    },
  });

  win.loadFile('src/HTML/index.html');
}

app.whenReady().then(() => {
  createWindow();

  ipcMain.handle('SimpleFunction', async (event, index) => {
    const result = await getPcapData.SimpleFunction(index);

    return result;
  });
});

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') app.quit();
});

preload.js(renderer)

const { contextBridge, ipcRenderer } = require('electron');

contextBridge.exposeInMainWorld('api', {

  SimpleFunction: async (index) => {
    return await ipcRenderer.invoke('SimpleFunction', index);
  }
});

operations.cc(my napi file)

#include <node_api.h>
#include <iostream>

#define NAPI_CALL(env, call)                                      \
  do                                                              \
  {                                                               \
    napi_status status = (call);                                  \
    if (status != napi_ok)                                        \
    {                                                             \
      const napi_extended_error_info *error_info = NULL;          \
      napi_get_last_error_info((env), &error_info);               \
      bool is_pending;                                            \
      napi_is_exception_pending((env), &is_pending);              \
      if (!is_pending)                                            \
      {                                                           \
        const char *message = (error_info->error_message == NULL) \
                                  ? "empty error message"         \
                                  : error_info->error_message;    \
        napi_throw_error((env), NULL, message);                   \
        return NULL;                                              \
      }                                                           \
    }                                                             \
  } while (0)

void SendToElectron(int index) {

// send something to electron

}

napi_value SimpleFunction(napi_env env, napi_callback_info info) {
  // Send a message to Electron
    size_t argc = 1;
    napi_value args[1];
    int index=0;

    // Get the values of the arguments passed to the function
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

    // Convert the values to C++ integers
    NAPI_CALL(env, napi_get_value_int32(env, args[0], &index));

    SendToElectron(index);

  return nullptr;
}

napi_value init(napi_env env, napi_value exports) {
  napi_value simpleFunction;
  napi_create_function(env, nullptr, 0, SimpleFunction, nullptr, &simpleFunction);

  napi_set_named_property(env, exports, "SimpleFunction", simpleFunction);

  return exports;
}

NAPI_MODULE(NODE_GYP_MODULE_NAME, init);

alx
  • 1

1 Answers1

0

Everything that uses a Promise can be made to use a callback, what is your problem with returning a value from C++? This has been made simple on purpose?

Just change:

async function ClickButtonEvent(currentID)
{
  data = await api.SimpleFunction(parseInt(currentID));
}

to

function ClickButtonEvent(currentID)
{
  api.SimpleFunction(parseInt(currentID)).then((data) => callback(data));
}
mmomtchev
  • 2,497
  • 1
  • 8
  • 23