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

View File

@ -155,8 +155,8 @@ function useAllControllers(app, electron) {
useController(app, electron, '/apps', apps);
}
function initializeElectronSender(electronSender) {
function 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 electronSender = null;
let init = '';
let init = [];
module.exports = {
setSseResponse(value) {
@ -12,15 +12,25 @@ module.exports = {
},
emit(message, data) {
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);
} else if (sseResponse) {
if (init) {
sseResponse.write(init);
init = '';
if (init.length > 0) {
for (const item of 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`);
} else {
init += sseResponse;
init.push([{ message, data }]);
}
},
emitChanged(key) {