Skip to content

Commit

Permalink
Add ability to refresh single connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarkauskas committed Nov 8, 2023
1 parent 992a6f5 commit 41f1a23
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 5 deletions.
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@
"title": "pro-bro: Refresh",
"icon": "$(refresh)"
},
{
"command": "pro-bro.refreshConnection",
"title": "pro-bro: Refresh Connection",
"icon": "$(refresh)"
},
{
"command": "pro-bro.addEntry",
"title": "pro-bro: Add Connection",
Expand Down Expand Up @@ -244,6 +249,11 @@
"when": "view == pro-bro-databases && viewItem == dbConnection",
"group": "inline"
},
{
"command": "pro-bro.refreshConnection",
"when": "view == pro-bro-databases && viewItem == dbConnection",
"group": "inline"
},
{
"command": "pro-bro.deleteConnection",
"when": "view == pro-bro-databases && viewItem == dbConnection",
Expand Down
9 changes: 9 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,15 @@ export function activate(context: vscode.ExtensionContext) {
)
);

context.subscriptions.push(
vscode.commands.registerCommand(
`${Constants.globalExtensionKey}.refreshConnection`,
(dbConnectionNode: DbConnectionNode) => {
dbConnectionNode.refreshConnection(context);
}
)
);

context.subscriptions.push(
vscode.commands.registerCommand(
`${Constants.globalExtensionKey}.editConnection`,
Expand Down
6 changes: 5 additions & 1 deletion src/treeview/DatabaseListProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Constants } from "../common/Constants";
import { IConfig } from "../view/app/model";
import { INode } from "./INode";
import * as connectionNode from "./DbConnectionNode";
import { IRefreshCallback } from "./IRefreshCallback";

export class DatabaseListProvider implements vscode.TreeDataProvider<INode> {
private _onDidChangeTreeData: vscode.EventEmitter<INode | undefined | void> =
Expand All @@ -12,7 +13,8 @@ export class DatabaseListProvider implements vscode.TreeDataProvider<INode> {

constructor(
private context: vscode.ExtensionContext,
private readonly groupName: string
private readonly groupName: string,
private readonly refreshCallback: IRefreshCallback
) {}

refresh(): void {
Expand Down Expand Up @@ -54,6 +56,7 @@ export class DatabaseListProvider implements vscode.TreeDataProvider<INode> {
let node = new connectionNode.DbConnectionNode(
id,
connections[id],
this.refreshCallback,
this.context
);
connectionNodes.push(node);
Expand All @@ -71,6 +74,7 @@ export class DatabaseListProvider implements vscode.TreeDataProvider<INode> {
let node = new connectionNode.DbConnectionNode(
id,
workspaceConnections[id],
this.refreshCallback,
this.context
);
connectionNodes.push(node);
Expand Down
17 changes: 17 additions & 0 deletions src/treeview/DbConnectionNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@ import { ConnectionEditor } from "../webview/ConnectionEditor";
import { spawn } from "child_process";
import { Constants } from "../common/Constants";
import { v4 as uuid } from "uuid";
import { DbConnectionUpdater } from "./DbConnectionUpdater";
import { IRefreshCallback } from "./IRefreshCallback";

export class DbConnectionNode implements INode {
public readonly id: string;
public readonly config: IConfig;
private readonly refreshCallback: IRefreshCallback;


constructor(
id: string,
config: IConfig,
refreshCallback: IRefreshCallback,
private context: vscode.ExtensionContext
) {
this.id = id;
this.config = config;
this.refreshCallback = refreshCallback;
}

public getTreeItem(): vscode.TreeItem {
Expand Down Expand Up @@ -72,6 +78,17 @@ export class DbConnectionNode implements INode {
new ConnectionEditor(context, "Edit Connection", this.id);
}

public refreshConnection(context: vscode.ExtensionContext) {
const dbConnectionUpdater = new DbConnectionUpdater();
const connectionId = this.config.id;

dbConnectionUpdater.updateSingleConnectionStatusWithRefreshCallback(
connectionId,
context,
this.refreshCallback,
);
}

private getAllConnections() {
let configDB: IConfig = {
id: uuid(),
Expand Down
49 changes: 49 additions & 0 deletions src/treeview/DbConnectionUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,60 @@ export class DbConnectionUpdater {
}
}

public async updateSingleConnectionStatusWithRefreshCallback(
connectionId: string,
context: vscode.ExtensionContext,
callback: IRefreshCallback
) {
this.context = context;
this.callback = callback;

try {
if (this.locked === false) {
this.locked = true;
await this.updateSingleConnectionStatus(connectionId);
}
} finally {
this.locked = false;
}
}

private async updateSingleConnectionStatus(connectionId: string) {
let connections = this.context.globalState.get<{ [id: string]: IConfig }>(
`pro-bro.dbconfig`
);

if (!connections || !connections[connectionId]) {
return;
}

connections[connectionId].conStatus = ConnectionStatus.Connecting;
this.updateStatus(connections);
await this.wait();

try {
const data = await ProcessorFactory.getProcessorInstance().getDBVersion(
connections[connectionId]
);

if (data instanceof Error || "error" in data) {
connections[connectionId].conStatus = ConnectionStatus.NotConnected;
} else {
connections[connectionId].conStatus = ConnectionStatus.Connected;
}
} catch (error) {
connections[connectionId].conStatus = ConnectionStatus.NotConnected;
} finally {
this.updateStatus(connections);
}
}

private updateStatus(connections: { [id: string]: IConfig }) {
this.context.globalState.update(`pro-bro.dbconfig`, connections);
this.callback.refresh();
}


private wait(): Promise<void> {
return new Promise((resolve) => {
setTimeout(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/treeview/GroupListProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class GroupListProvider
}
if (groupNames.indexOf(group) === -1) {
groupNames.push(group);
groupNodes.push(new groupNode.GroupNode(this.context, group));
groupNodes.push(new groupNode.GroupNode(this.context, group, this));
}
}
}
Expand All @@ -93,7 +93,7 @@ export class GroupListProvider
}
if (groupNames.indexOf(group) === -1) {
groupNames.push(group);
groupNodes.push(new groupNode.GroupNode(this.context, group));
groupNodes.push(new groupNode.GroupNode(this.context, group, this));
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/treeview/GroupNode.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as vscode from "vscode";
import { DatabaseListProvider } from "./DatabaseListProvider";
import { INode } from "./INode";
import { IRefreshCallback } from "./IRefreshCallback";

export class GroupNode implements INode {
constructor(private context: vscode.ExtensionContext, private readonly groupName: string) {}
constructor(private context: vscode.ExtensionContext, private readonly groupName: string, private readonly refreshCallback: IRefreshCallback) {}


public getTreeItem(): vscode.TreeItem {
return {
Expand All @@ -14,7 +16,7 @@ export class GroupNode implements INode {
}

public async getChildren(): Promise<INode[]> {
return new DatabaseListProvider(this.context, this.groupName).getChildren(undefined);
return new DatabaseListProvider(this.context, this.groupName, this.refreshCallback).getChildren(undefined);
}

}

0 comments on commit 41f1a23

Please sign in to comment.