Skip to content

Commit

Permalink
refactor: ensure start order (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpvalente authored Apr 3, 2023
1 parent 32b5ab0 commit b778835
Showing 1 changed file with 36 additions and 30 deletions.
66 changes: 36 additions & 30 deletions apps/electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,23 @@ let win;
let splash;
let tray = null;

(async () => {
// in dev mode, we expect both UI and server to be running
if (!isProduction) {
return;
}
async function startBackend() {
await (async () => {
// in dev mode, we expect both UI and server to be running
if (!isProduction) {
return;
}

try {
const ontimeServer = require(nodePath)
const ontimeServer = require(nodePath);
const { initAssets, startServer, startOSCServer, startIntegrations } = ontimeServer;

await initAssets();

loaded = await startServer();
await startOSCServer();
await startIntegrations();
} catch (error) {
loaded = error;
}
})();
})();
}

/**
* @description utility function to create a notification
Expand Down Expand Up @@ -158,26 +156,34 @@ app.whenReady().then(() => {
bringToFront();
});

// cheat to schedule process
setTimeout(() => {
// Load page served by node or use React dev run
const clientUrl = isProduction ? electronConfig.reactAppUrl.production : electronConfig.reactAppUrl.development;

win.loadURL(clientUrl).then(() => {
win.webContents.setBackgroundThrottling(false);

win.show();
win.focus();

splash.destroy();

if (typeof loaded === 'string') {
tray.setToolTip(loaded);
} else {
tray.setToolTip('Initialising error: please restart Ontime');
}
startBackend()
.then(() => {
// Load page served by node or use React dev run
const clientUrl = isProduction ? electronConfig.reactAppUrl.production : electronConfig.reactAppUrl.development;

win
.loadURL(clientUrl)
.then(() => {
win.webContents.setBackgroundThrottling(false);

win.show();
win.focus();

splash.destroy();

if (typeof loaded === 'string') {
tray.setToolTip(loaded);
} else {
tray.setToolTip('Initialising error: please restart Ontime');
}
})
.catch((error) => {
dialog.showErrorBox('Ontime failed to reach server', error);
});
})
.catch((error) => {
dialog.showErrorBox('Ontime failed to start', error);
});
}, 0);

// recreate window if no others open
app.on('activate', () => {
Expand Down

0 comments on commit b778835

Please sign in to comment.