From 852dd011529959b085b51b127767b12ab4e299c6 Mon Sep 17 00:00:00 2001 From: Tobias Hopp Date: Fri, 2 Dec 2022 11:06:55 +0100 Subject: [PATCH] Change a bit on startup #7 Took 33 minutes --- src/Utils.ts | 23 +++++++++++++++++++++++ src/main.ts | 21 +++++++-------------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/Utils.ts b/src/Utils.ts index cb76107..77ea52f 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -2,6 +2,7 @@ import * as dns from "dns"; import * as fs from "fs"; import * as https from 'https'; import path from "path"; +import {Settings} from "./Settings"; export class Utils { public static checkInternet(): Promise { @@ -61,4 +62,26 @@ export class Utils { return fs.existsSync("./public/images/" + id + ".png"); } + static waitForReady(timeout: number = 0): Promise { + let millis = 0; + return new Promise((resolve, reject) => { + function wait() { + millis += 100; + if (Settings.setupDone) { + resolve() + return; + } + if (timeout != 0 && timeout <= millis) { + reject(); + return; + } + + setTimeout(() => wait(), 100); + } + + setTimeout(wait, 100); + }); + + + } } \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 37d84cd..78d8a57 100644 --- a/src/main.ts +++ b/src/main.ts @@ -33,22 +33,15 @@ const wsApp = new WebsocketApp(); } - function checkStart() { - setTimeout(async () => { - if (!Settings.setupDone) { - checkStart(); - return; - } - Settings.saveSettings(); + log("Waiting for ready status..."); + await Utils.waitForReady(); - log("Check OK, starting..."); - await init(); - setInterval(refresh, 1000 * 60); - iTender.setStatus(iTenderStatus.READY); - }, 1000); - } + Settings.saveSettings(); - checkStart(); + log("Check OK, starting..."); + await init(); + setInterval(refresh, 1000 * 60); + iTender.setStatus(iTenderStatus.READY); } catch (e) { console.error("---- ERROR ----");