Skip to content

Commit

Permalink
refactor(db): rename serverReady variable
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Jun 12, 2023
1 parent 2ad0539 commit 11b21f8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
14 changes: 7 additions & 7 deletions src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { connectionOptions, mysql_transaction_isolation_level } from '../config'
import { typeCast } from '../utils/typeCast';

let pool: Pool;
let serverReady = false;
let isServerConnected = false;

export async function waitForConnection() {
if (!serverReady) {
await new Promise<void>((resolve) => {
if (!isServerConnected) {
return await new Promise<boolean>((resolve) => {
(function wait() {
if (serverReady) {
return resolve();
if (isServerConnected) {
return resolve(true);
}
setTimeout(wait);
})();
Expand All @@ -30,8 +30,8 @@ setTimeout(() => {
pool.query(mysql_transaction_isolation_level, (err) => {
if (err) return console.error(`^3Unable to establish a connection to the database!\n^3[${err}]^0`);
console.log(`^2Database server connection established!^0`);
serverReady = true;
isServerConnected = true;
});
});

export { pool, serverReady };
export { pool, isServerConnected };
4 changes: 2 additions & 2 deletions src/database/rawExecute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CFXCallback, CFXParameters } from '../types';
import { parseResponse } from '../utils/parseResponse';
import { executeType, parseExecute } from '../utils/parseExecute';
import { scheduleTick } from '../utils/scheduleTick';
import { serverReady, waitForConnection } from '../database';
import { isServerConnected, waitForConnection } from '../database';

export const rawExecute = (
invokingResource: string,
Expand All @@ -28,7 +28,7 @@ export const rawExecute = (
scheduleTick();

return new Promise(async (resolve, reject) => {
if (!serverReady) await waitForConnection();
if (!isServerConnected) await waitForConnection();

pool.getConnection((err, connection) => {
if (err) return reject(err.message);
Expand Down
4 changes: 2 additions & 2 deletions src/database/rawQuery.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pool, serverReady, waitForConnection } from '.';
import { pool, isServerConnected, waitForConnection } from '.';
import { parseArguments } from '../utils/parseArguments';
import { parseResponse } from '../utils/parseResponse';
import { logQuery } from '../logger';
Expand All @@ -23,7 +23,7 @@ export const rawQuery = (
scheduleTick();

return new Promise(async (resolve, reject) => {
if (!serverReady) await waitForConnection();
if (!isServerConnected) await waitForConnection();

pool.query(query, parameters, (err, result, _, executionTime) => {
if (err) return reject(err);
Expand Down
4 changes: 2 additions & 2 deletions src/database/rawTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pool, serverReady, waitForConnection } from '.';
import { pool, isServerConnected, waitForConnection } from '.';
import { logQuery } from '../logger';
import { CFXParameters, TransactionQuery } from '../types';
import { parseTransaction } from '../utils/parseTransaction';
Expand All @@ -15,7 +15,7 @@ export const rawTransaction = async (
parameters: CFXParameters,
callback?: (result: boolean) => void
) => {
if (!serverReady) await waitForConnection()
if (!isServerConnected) await waitForConnection()

scheduleTick();

Expand Down

0 comments on commit 11b21f8

Please sign in to comment.