-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
81 lines (71 loc) · 2.12 KB
/
main.js
File metadata and controls
81 lines (71 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const { ipcMain, BrowserWindow, app } = require("electron");
const path = require("path");
app.allowRendererProcessReuse = true;
const cpus = require("os").cpus().length;
console.log("cpus: " + cpus);
// stack of available background threads
var available = [];
// queue of tasks to be done
var tasks = [];
// hand the tasks out to waiting threads
function doIt() {
while (available.length > 0 && tasks.length > 0) {
var task = tasks.shift();
available.shift().send(task[0], task[1]);
}
renderer.webContents.send("status", available.length, tasks.length);
}
// Create a hidden background window
function createBgWindow() {
result = new BrowserWindow({
show: false,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
},
});
result.loadURL("file://" + __dirname + "/background.html");
result.on("closed", () => {
console.log("background window closed");
});
return result;
}
app.whenReady().then(function () {
// Create the "renderer" window which contains the visible UI
renderer = new BrowserWindow({
width: 500,
height: 400,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
},
});
renderer.loadURL("file://" + __dirname + "/renderer.html");
renderer.show();
renderer.on("closed", () => {
// call quit to exit, otherwise the background windows will keep the app running
app.quit();
});
// create background thread for each cpu
for (var i = 0; i < cpus; i++) createBgWindow();
// Main thread can receive directly from windows
ipcMain.on("to-main", (event, arg) => {
console.log(arg);
});
// Windows can talk to each other via main
ipcMain.on("for-renderer", (event, arg) => {
renderer.webContents.send("to-renderer", arg);
});
ipcMain.on("for-background", (event, arg) => {
tasks.push(["message", arg]);
doIt();
});
// heavy processing done in the background thread
// so UI and main threads remain responsive
ipcMain.on("assign-task", (event, arg) => {
tasks.push(["task", arg]);
doIt();
});
ipcMain.on("ready", (event, arg) => {
available.push(event.sender);
doIt();
});
});