From 0f5f3978145a91b45caced76793c784d43d1971f Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Tue, 28 Nov 2023 11:38:37 +1100 Subject: [PATCH] refactor(db/connection): tweak waitForConnection logic --- src/database/connection.ts | 12 +++--------- src/utils/sleep.ts | 3 +++ 2 files changed, 6 insertions(+), 9 deletions(-) create mode 100644 src/utils/sleep.ts diff --git a/src/database/connection.ts b/src/database/connection.ts index 0f1945e..da6e778 100644 --- a/src/database/connection.ts +++ b/src/database/connection.ts @@ -1,21 +1,15 @@ import { createPool, Pool, RowDataPacket } from 'mysql2/promise'; import { connectionOptions, mysql_transaction_isolation_level } from '../config'; import { scheduleTick } from '../utils/scheduleTick'; +import { sleep } from '../utils/sleep'; export let pool: Pool; export let isServerConnected = false; export let dbVersion = ''; export async function waitForConnection() { - if (!isServerConnected) { - return await new Promise((resolve) => { - (function wait() { - if (isServerConnected) { - return resolve(true); - } - setTimeout(wait); - })(); - }); + while (!isServerConnected) { + await sleep(0); } } diff --git a/src/utils/sleep.ts b/src/utils/sleep.ts new file mode 100644 index 0000000..0d7f188 --- /dev/null +++ b/src/utils/sleep.ts @@ -0,0 +1,3 @@ +export function sleep(ms: number) { + return new Promise((resolve) => setTimeout(resolve, ms)); +}