Skip to content

Commit f28935c

Browse files
committed
chore: node process managemeent in renderer process
1 parent a1d037b commit f28935c

1 file changed

Lines changed: 55 additions & 10 deletions

File tree

test/index.html

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,58 @@
8585
}
8686
// Electron detection and setup
8787
if(window.electronAPI){
88-
window.electronAPI.getNodeWSPort().then(port => {
89-
window.nodeWSEndpoint = `ws://localhost:${port}/phoenixFS`;
90-
fs.setNodeWSEndpoint(`ws://localhost:${port}/phoenixFS`);
91-
window.isNodeSetup = true;
92-
});
93-
// Heartbeat to keep Node server alive
94-
setInterval(() => {
95-
// Heartbeat is handled by main process in Electron
96-
}, 10000);
88+
window.electronAPI.getDocumentsDir().then(d => console.log("Electron documentsDir:", d));
89+
window.electronAPI.getAppDataDir().then(d => console.log("Electron appDataDir:", d));
90+
window.electronAPI.getAppPath().then(appPath => {
91+
const nodeSrcPath = appPath + "/../src-tauri/node-src/index.js";
92+
return window.electronAPI.spawnProcess('node', ['--inspect', nodeSrcPath]);
93+
}).then(async (myInstanceId) => {
94+
window.electronAPI.onProcessClose((instanceId, data) => {
95+
if(instanceId !== myInstanceId) return;
96+
window.isNodeTerminated = true;
97+
console.log(`Node: command finished with code ${data.code} and signal ${data.signal}`);
98+
});
99+
window.electronAPI.onProcessStdout((instanceId, line) => {
100+
if(instanceId !== myInstanceId) return;
101+
if(line){
102+
if(line.trim().startsWith("{")){
103+
const jsonMsg = JSON.parse(line);
104+
pendingCommands[jsonMsg.commandId].resolve(jsonMsg.message);
105+
delete pendingCommands[jsonMsg.commandId];
106+
} else {
107+
console.log(`Node: ${line}`);
108+
}
109+
}
110+
});
111+
window.electronAPI.onProcessStderr((instanceId, line) => {
112+
if(instanceId !== myInstanceId) return;
113+
console.error(`Node: ${line}`);
114+
});
115+
116+
window.execNode = function (commandCode) {
117+
const newCommandID = commandId++;
118+
window.electronAPI.writeToProcess(myInstanceId,
119+
JSON.stringify({commandCode: commandCode, commandId: newCommandID}) + "\n"
120+
);
121+
let resolveP, rejectP;
122+
const promise = new Promise((resolve, reject) => {resolveP = resolve; rejectP = reject;});
123+
pendingCommands[newCommandID] = {resolve: resolveP, reject: rejectP};
124+
return promise;
125+
};
126+
127+
execNode(NODE_COMMANDS.GET_PORT)
128+
.then(message => {
129+
window.nodeWSEndpoint = `ws://localhost:${message.port}/phoenixFS`;
130+
fs.setNodeWSEndpoint(`ws://localhost:${message.port}/phoenixFS`);
131+
window.isNodeSetup = true;
132+
});
133+
134+
setInterval(() => {
135+
if(!window.isNodeTerminated) {
136+
execNode(NODE_COMMANDS.HEART_BEAT);
137+
}
138+
}, 10000);
139+
}).catch(console.error);
97140
}
98141
</script>
99142
<script src="thirdparty/chai@4.2.0.js"></script>
@@ -150,8 +193,10 @@
150193
};
151194
quitIfNeeded = async function(exitStatus) {
152195
if(!location.href.startsWith("http://localhost:8081/test/")) {
153-
// during development, we dont quit at end of tests for ease of development and debug
196+
// during development, we dont switch off node at end of tests for ease of development and debug
154197
fs.stopNodeWSEndpoint();
198+
window.execNode(NODE_COMMANDS.TERMINATE);
199+
await waitForTrue(() => { return window.isNodeTerminated; }, 1000);
155200
}
156201
// Always quit in Electron since there's no CLI arg parsing
157202
window.electronAPI.quitApp(exitStatus);

0 commit comments

Comments
 (0)