agracio/electron-edge-js

Async functionality not working -- everything runs sync

ash47 opened this issue · 4 comments

ash47 commented

The C# code is not run async, it is running sync.

Sample code:

const edge = require('electron-edge-js');

let someCommand = edge.func(`
    async (input) => { 
        while(true){}
        return ".NET Welcomes " + input.ToString(); 
    }
`)

console.log('a');
someCommand('javascript', function() {
    console.log('c');
});
console.log('b');

I see "a" in the console, and then the entire app freezes due to the infinite loop within the C# code. According to the documentation, the C# code is meant to run async, meaning that it can be as CPU intense as it likes, take as long as it likes, and the JavaScript v8 engine code will continue to run.

I have also tried specifying "sync: false" specifically and even "sync: true", this didn't help.

How do I make the C# code run async?

I can't comment on whether or not sync is actually working, but I had to fork a process and run edge in the forked process to get it to be async. When I ran edge from the main electron process it locked up the app.

It would be nice to figure out if that is the expected standard procedure or if there is an issue with edge.

This is not an issue with edge-js but rather with Electron when module is running on main Electron process. Take a look at related issue below, this comment explains the problem in depth paranext/paranext#9 (comment)

ash47 commented

Thanks for the pro tip @togradywk -- on my end, I've gone ahead and forked the process, and written logic to control edge.js via the fork <3

This should still really be fixed, but it no longer bothers me if it is or not

As an example for someone facing this issue I'll show what worked for me:

In my case I was trying to open a pptx file asynchronously so, I used the module office-script which is based on edge-js,

So first I replaced edje-js to electron-edje-js in the module,

Next, to call my pptx file:

main.js:
const { fork } = require("child_process"); fork("../child.js", [], { env: {file: 'filename'}, })

child.js:
`
const path = require('path');
const powerpoint = require('office-script').powerpoint;
const filePath = '../../directory/';

powerpoint.open(path.join(${remotePath}${process.env.file}.pptx), function(err) {
if(err) throw err;
});
`
I hope this could help a little, this way I avoided the main process to freezeing the renderer process as well