Skip to content

Commit 91c71a4

Browse files
committed
Update sync and async file open
1 parent ffd0b75 commit 91c71a4

File tree

4 files changed

+84
-112
lines changed

4 files changed

+84
-112
lines changed

electron/guiExample.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
/>
2121
<button id="send_code">Interact with program</button>
2222
<button id="stop_code">Terminate program</button>
23-
<button id="open_file">How to open a file</button>
23+
<hr>
24+
<button id="open_file_sync">Open a file sync</button>
25+
<button id="open_file_async">Open a file async</button>
2426
<p><a href="https://github.com/keybraker">Made by Keybraker</a></p>
2527
</div>
2628
</body>

electron/guiExample.js

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
const exec = require("child_process").exec;
1+
const { exec } = require("child_process");
22
const nodeConsole = require("console");
33
const { ipcRenderer } = require("electron");
44

5+
const terminalConsole = new nodeConsole.Console(process.stdout, process.stderr);
6+
let child;
7+
58
ipcRenderer.send("run-command", "ls");
69
ipcRenderer.on("run-command-result", (event, result) => {
710
if (result.error) {
@@ -11,77 +14,76 @@ ipcRenderer.on("run-command-result", (event, result) => {
1114
}
1215
});
1316

14-
const terminalConsole = new nodeConsole.Console(process.stdout, process.stderr);
15-
let child;
17+
const printBoth = (str) => {
18+
console.log(`Javascript: ${str}`);
19+
terminalConsole.log(`Javascript: ${str}`);
20+
};
1621

17-
function print_both(str) {
18-
console.log("Javascript: " + str);
19-
terminalConsole.log("Javascript: " + str);
20-
}
21-
22-
function send_to_program(str) {
22+
const sendToProgram = (str) => {
2323
child.stdin.write(str);
24-
child.stdout.on("data", function (data) {
25-
print_both(
26-
"Following data has been piped from python program: " +
27-
data.toString("utf8")
24+
child.stdout.on("data", (data) => {
25+
printBoth(
26+
`Following data has been piped from python program: ${data.toString(
27+
"utf8"
28+
)}`
2829
);
2930
});
30-
}
31+
};
3132

32-
// starts program execution from within javascript and
33-
function start_code_function(evt) {
34-
print_both("Initiating program");
33+
const startCodeFunction = () => {
34+
printBoth("Initiating program");
3535

36-
child = exec(
37-
"python -i ./python/pythonExample.py ",
38-
function (error, stdout, stderr) {
39-
if (error !== null) {
40-
print_both("exec error: " + error);
41-
}
36+
child = exec("python -i ./python/pythonExample.py", (error) => {
37+
if (error) {
38+
printBoth(`exec error: ${error}`);
4239
}
43-
);
40+
});
4441

45-
child.stdout.on("data", function (data) {
46-
print_both(
47-
"Following data has been piped from python program: " +
48-
data.toString("utf8")
42+
child.stdout.on("data", (data) => {
43+
printBoth(
44+
`Following data has been piped from python program: ${data.toString(
45+
"utf8"
46+
)}`
4947
);
5048
});
51-
}
49+
};
5250

53-
// sends data to program
54-
function send_code_function(evt) {
55-
let string_to_send = document.getElementById("string_to_send").value;
56-
print_both('Sending "' + string_to_send + '" to program');
57-
send_to_program(string_to_send);
58-
}
51+
const sendCodeFunction = () => {
52+
const stringToSend = document.getElementById("string_to_send").value;
53+
printBoth(`Sending "${stringToSend}" to program`);
54+
sendToProgram(stringToSend);
55+
};
5956

60-
// sends termination message to python program and closed stream
61-
// that receives information from it
62-
function stop_code_function(evt) {
63-
print_both("Terminated program");
64-
send_to_program("terminate");
57+
const stopCodeFunction = () => {
58+
printBoth("Terminated program");
59+
sendToProgram("terminate");
6560
child.stdin.end();
66-
}
61+
};
6762

68-
// requests main.js to open a file from the filesystem
69-
function open_file_function(evt) {
70-
print_both("From guiExample.js sending a request to main.js via ipc");
71-
ipcRenderer.send("open_json_file");
72-
}
63+
const openFileFunctionSync = () => {
64+
printBoth("From guiExample.js sending a request to main.js via ipc");
65+
ipcRenderer.send("open_json_file_sync");
66+
};
7367

74-
document.addEventListener("DOMContentLoaded", function () {
68+
const openFileFunctionAsync = () => {
69+
printBoth("From guiExample.js sending a request to main.js via ipc");
70+
ipcRenderer.send("open_json_file_async");
71+
};
72+
73+
document.addEventListener("DOMContentLoaded", () => {
7574
document
7675
.getElementById("start_code")
77-
.addEventListener("click", start_code_function);
76+
.addEventListener("click", startCodeFunction);
7877
document
7978
.getElementById("send_code")
80-
.addEventListener("click", send_code_function);
79+
.addEventListener("click", sendCodeFunction);
8180
document
8281
.getElementById("stop_code")
83-
.addEventListener("click", stop_code_function);
82+
.addEventListener("click", stopCodeFunction);
83+
document
84+
.getElementById("open_file_sync")
85+
.addEventListener("click", openFileFunctionSync);
8486
document
85-
.getElementById("open_file")
86-
.addEventListener("click", open_file_function);
87+
.getElementById("open_file_async")
88+
.addEventListener("click", openFileFunctionAsync);
8789
});

electron/main.js

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,29 @@ const { app, BrowserWindow, ipcMain, Notification } = require("electron");
33
const exec = require("child_process").exec;
44
const path = require("path");
55

6-
var nodeConsole = require("console");
7-
var my_console = new nodeConsole.Console(process.stdout, process.stderr);
8-
var child;
6+
const nodeConsole = require("console");
7+
const myConsole = new nodeConsole.Console(process.stdout, process.stderr);
8+
let child;
99

10-
function print_both(str) {
10+
function printBoth(str) {
1111
console.log("main.js: " + str);
12-
my_console.log("main.js: " + str);
12+
myConsole.log("main.js: " + str);
1313
}
1414

15+
// Create the browser window.
1516
function createWindow() {
16-
// Create the browser window.
1717
const mainWindow = new BrowserWindow({
1818
width: 990,
1919
height: 600,
2020
resizable: true,
2121
webPreferences: {
2222
preload: path.join(__dirname, "guiExample.js"),
2323
contextIsolation: true,
24-
nodeIntegration: true
24+
nodeIntegration: true,
2525
},
2626
});
2727

28-
// and load the index.html of the app.
29-
// mainWindow.loadFile("./electron/guiExample.html");
28+
// Load the index.html of the app.
3029
mainWindow.loadFile(path.join(__dirname, "guiExample.html"));
3130

3231
// Open the DevTools.
@@ -64,32 +63,27 @@ ipcMain.on("execute", (command) => {
6463
});
6564
});
6665

67-
ipcMain.on("open_json_file", () => {
68-
var fs = require("fs");
69-
var fileName = "./config.json";
70-
var file = require(fileName);
66+
ipcMain.on("open_json_file_sync", () => {
67+
const fs = require("fs");
7168

72-
// Asynchronous read
73-
// fs.readFile('config.json', function (err, data) {
74-
// if (err) {
75-
// return console.error(err);
76-
// }
77-
// console.log("Asynchronous read: " + data.toString());
78-
// });
69+
fs.readFile("config.json", function (err, data) {
70+
if (err) {
71+
return console.error(err);
72+
}
73+
printBoth("Called through ipc.send from guiExample.js");
74+
printBoth("Asynchronous read: " + data.toString());
75+
});
76+
});
77+
78+
ipcMain.on("open_json_file_async", () => {
79+
const fs = require("fs");
7980

80-
// Synchronous read
81-
var data = fs.readFileSync(fileName);
82-
var json = JSON.parse(data);
81+
const fileName = "./config.json";
82+
const data = fs.readFileSync(fileName);
83+
const json = JSON.parse(data);
8384

84-
print_both("Called through ipc.send from guiExample.js");
85-
print_both(
86-
"Data from config.json:\nA_MODE = " +
87-
json.A_MODE +
88-
"\nB_MODE = " +
89-
json.B_MODE +
90-
"\nC_MODE = " +
91-
json.C_MODE +
92-
"\nD_MODE = " +
93-
json.D_MODE
85+
printBoth("Called through ipc.send from guiExample.js");
86+
printBoth(
87+
`Data from config.json:\nA_MODE = ${json.A_MODE}\nB_MODE = ${json.B_MODE}\nC_MODE = ${json.C_MODE}\nD_MODE = ${json.D_MODE}`
9488
);
9589
});

electron/preload.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)