fixed object destroyed error

This commit is contained in:
Jan Prochazka 2022-04-24 13:20:49 +02:00
parent df743e8ade
commit 30b054dbec
3 changed files with 19 additions and 8 deletions

View File

@ -336,10 +336,10 @@ function createWindow() {
// ) // )
// ); // );
const main = api.getMainModule(); const main = api.getMainModule();
main.initializeElectronSender(mainWindow.webContents);
main.useAllControllers(null, electron); main.useAllControllers(null, electron);
apiLoaded = true; apiLoaded = true;
} }
main.setElectronSender(mainWindow.webContents);
loadMainWindow(); loadMainWindow();
@ -349,6 +349,7 @@ function createWindow() {
// in an array if your app supports multi windows, this is the time // in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element. // when you should delete the corresponding element.
mainWindow = null; mainWindow = null;
main.setElectronSender(null);
}); });
} }

View File

@ -155,8 +155,8 @@ function useAllControllers(app, electron) {
useController(app, electron, '/apps', apps); useController(app, electron, '/apps', apps);
} }
function initializeElectronSender(electronSender) { function setElectronSender(electronSender) {
socket.setElectronSender(electronSender); socket.setElectronSender(electronSender);
} }
module.exports = { start, useAllControllers, initializeElectronSender, configController: config }; module.exports = { start, useAllControllers, setElectronSender, configController: config };

View File

@ -1,6 +1,6 @@
let sseResponse = null; let sseResponse = null;
let electronSender = null; let electronSender = null;
let init = ''; let init = [];
module.exports = { module.exports = {
setSseResponse(value) { setSseResponse(value) {
@ -12,15 +12,25 @@ module.exports = {
}, },
emit(message, data) { emit(message, data) {
if (electronSender) { if (electronSender) {
if (init.length > 0) {
for (const item of init) {
electronSender.send(item.message, item.data == null ? null : item.data);
}
init = [];
}
electronSender.send(message, data == null ? null : data); electronSender.send(message, data == null ? null : data);
} else if (sseResponse) { } else if (sseResponse) {
if (init) { if (init.length > 0) {
sseResponse.write(init); for (const item of init) {
init = ''; sseResponse.write(
`event: ${item.message}\ndata: ${JSON.stringify(item.data == null ? null : item.data)}\n\n`
);
}
init = [];
} }
sseResponse.write(`event: ${message}\ndata: ${JSON.stringify(data == null ? null : data)}\n\n`); sseResponse.write(`event: ${message}\ndata: ${JSON.stringify(data == null ? null : data)}\n\n`);
} else { } else {
init += sseResponse; init.push([{ message, data }]);
} }
}, },
emitChanged(key) { emitChanged(key) {