Skip to content

Commit

Permalink
drop the complexity of the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip-Carneiro-KX committed Jan 5, 2024
1 parent 93db9ce commit 586364f
Showing 1 changed file with 42 additions and 22 deletions.
64 changes: 42 additions & 22 deletions src/models/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class Connection {
private options: nodeq.ConnectionParameters;
private connection?: nodeq.Connection;
public connected: boolean;
private result?: string;

constructor(connectionString: string, creds?: string[], tls?: boolean) {
const params = connectionString.split(":");
Expand Down Expand Up @@ -95,16 +96,11 @@ export class Connection {
context?: string,
stringify?: boolean
): Promise<any> {
let result;
let retryCount = 0;
while (this.connection === undefined) {
if (retryCount > ext.maxRetryCount) {
return "timeout";
}
await delay(500);
retryCount++;
}
await this.waitForConnection();

if (!this.connection) {
return "timeout";
}
const wrapper = queryWrapper();
this.connection.k(
wrapper,
Expand All @@ -113,23 +109,18 @@ export class Connection {
!!stringify,
(err: Error, res: QueryResult) => {
if (err) {
result = handleQueryResults(err.toString(), QueryResultType.Error);
} else if (res) {
if (res.errored) {
result = handleQueryResults(
res.error + (res.backtrace ? "\n" + res.backtrace : ""),
QueryResultType.Error
);
} else {
result = res.result;
}
this.result = handleQueryResults(
err.toString(),
QueryResultType.Error
);
}
if (res) {
this.handleQueryResult(res);
}
}
);

while (result === undefined || result === null) {
await delay(500);
}
const result = await this.waitForResult();

if (ext.resultsViewProvider.isVisible()) {
return convertStringToArray(result);
Expand All @@ -138,6 +129,35 @@ export class Connection {
return result;
}

private async waitForConnection(): Promise<void> {
let retryCount = 0;
while (this.connection === undefined) {
if (retryCount > ext.maxRetryCount) {
throw new Error("timeout");
}
await delay(500);
retryCount++;
}
}

private handleQueryResult = (res: QueryResult): void => {
if (res.errored) {
this.result = handleQueryResults(
res.error + (res.backtrace ? "\n" + res.backtrace : ""),
QueryResultType.Error
);
} else {
this.result = res.result;
}
};

private async waitForResult(): Promise<any> {
while (this.result === undefined || this.result === null) {
await delay(500);
}
return this.result;
}

public async executeQueryRaw(command: string): Promise<string> {
let result;
let retryCount = 0;
Expand Down

0 comments on commit 586364f

Please sign in to comment.