Skip to content

Commit

Permalink
Poll with setTimeout instead of setInterval
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Rak <[email protected]>
  • Loading branch information
rak-phillip committed Aug 8, 2023
1 parent 0f7dd98 commit f8700fd
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions scripts/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,35 +128,41 @@ class DevRunner extends events.EventEmitter {

// Wait for the renderer to finish, so that vue-cli output doesn't
// clobber debugging output.
// Wait for the renderer to finish, so that vue-cli output doesn't clobber debugging output.
const rendererEnv = this.rendererEnv();

const maxRetries = 10;
let retryCount = 0;
let devServerStarted = false;

const serverCheckInterval = setInterval(async() => {
if (devServerStarted) {
return;
}
const retryInterval = 1000;

const checkDevServer = async() => {
try {
const response = await fetch(rendererEnv.home, { agent: rendererEnv.agent });

if (response.ok) {
clearInterval(serverCheckInterval);
if (!devServerStarted) {
console.info('Renderer process: dev server started');
devServerStarted = true;
}
console.info('Renderer process: dev server started');
resolve();
} else {
// Retry if response is not okay
retryCount++;
if (retryCount < maxRetries) {
setTimeout(checkDevServer, retryInterval);
} else {
reject(new Error(`Renderer process: failed to connect`));
}
}
} catch (error) {
// Retry if fetch throws an error
retryCount++;
if (retryCount >= maxRetries) {
clearInterval(serverCheckInterval);
reject(new Error(`Renderer build failed to connect after ${ maxRetries } attempts`));
if (retryCount < maxRetries) {
setTimeout(checkDevServer, retryInterval);
} else {
reject(new Error(`Renderer process: failed to connect`));
}
}
}, 1000);
};

checkDevServer();
});
}

Expand Down

0 comments on commit f8700fd

Please sign in to comment.