branchseer / DeskGap

A cross-platform desktop app framework based on Node.js and the system webview
https://deskgap.com/
MIT License
1.83k stars 75 forks source link

How to use asyncNode #34

Open TemporalAgent7 opened 5 years ago

TemporalAgent7 commented 5 years ago

Hi, I'm trying to figure out how asyncNode is supposed to be used. I'd like to call node code (that returns a result) from the webview side. I guess one option would be to use the ipc mechanism and build some sort of map of request ids on top of that (similar to electron-promise-ipc), but it looks like asyncNode may have been designed to solve this exact problem, except I can't figure out how to use it. Any tips / examples?

What I'm looking for is something like this: On the node.js side

function coolNodeFunction(argument: string): Promise<string> {
  return Promise.resolve('from node');
}

On the webview side

let result = await magic.invoke('coolNodeFunction', 'argument from web');

I've tried various combinations of deskgap.asyncNode.getGlobal().invoke() to no avail.

Thank you!

nicck commented 4 years ago

I had to move 'node' code to separate module to invoke it from renderer:

// ./node.js
exports.coolNodeFunction = async function (argument) {
  return 'from node';
};
// ./browser.js
const something = await deskgap.asyncNode.require(`./node`);
const result = await something.invoke('coolNodeFunction', 'argument from web').value();