From 41f1a23194e98f2e573a21c80dea133e7159863c Mon Sep 17 00:00:00 2001 From: mmarkauskas Date: Wed, 8 Nov 2023 14:55:51 +0200 Subject: [PATCH] Add ability to refresh single connection. --- package.json | 10 ++++++ src/extension.ts | 9 +++++ src/treeview/DatabaseListProvider.ts | 6 +++- src/treeview/DbConnectionNode.ts | 17 ++++++++++ src/treeview/DbConnectionUpdater.ts | 49 ++++++++++++++++++++++++++++ src/treeview/GroupListProvider.ts | 4 +-- src/treeview/GroupNode.ts | 6 ++-- 7 files changed, 96 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 5d61dab1..eb8624ca 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/extension.ts b/src/extension.ts index 62877718..3e82b30b 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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`, diff --git a/src/treeview/DatabaseListProvider.ts b/src/treeview/DatabaseListProvider.ts index e1bcd08c..91fea9e9 100644 --- a/src/treeview/DatabaseListProvider.ts +++ b/src/treeview/DatabaseListProvider.ts @@ -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 { private _onDidChangeTreeData: vscode.EventEmitter = @@ -12,7 +13,8 @@ export class DatabaseListProvider implements vscode.TreeDataProvider { constructor( private context: vscode.ExtensionContext, - private readonly groupName: string + private readonly groupName: string, + private readonly refreshCallback: IRefreshCallback ) {} refresh(): void { @@ -54,6 +56,7 @@ export class DatabaseListProvider implements vscode.TreeDataProvider { let node = new connectionNode.DbConnectionNode( id, connections[id], + this.refreshCallback, this.context ); connectionNodes.push(node); @@ -71,6 +74,7 @@ export class DatabaseListProvider implements vscode.TreeDataProvider { let node = new connectionNode.DbConnectionNode( id, workspaceConnections[id], + this.refreshCallback, this.context ); connectionNodes.push(node); diff --git a/src/treeview/DbConnectionNode.ts b/src/treeview/DbConnectionNode.ts index 293de1c1..2ae66475 100644 --- a/src/treeview/DbConnectionNode.ts +++ b/src/treeview/DbConnectionNode.ts @@ -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 { @@ -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(), diff --git a/src/treeview/DbConnectionUpdater.ts b/src/treeview/DbConnectionUpdater.ts index 53d9cff6..d9a80831 100644 --- a/src/treeview/DbConnectionUpdater.ts +++ b/src/treeview/DbConnectionUpdater.ts @@ -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 { return new Promise((resolve) => { setTimeout(() => { diff --git a/src/treeview/GroupListProvider.ts b/src/treeview/GroupListProvider.ts index 6e365f36..f703ce36 100644 --- a/src/treeview/GroupListProvider.ts +++ b/src/treeview/GroupListProvider.ts @@ -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)); } } } @@ -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)); } } } diff --git a/src/treeview/GroupNode.ts b/src/treeview/GroupNode.ts index d1a11511..9751bff4 100644 --- a/src/treeview/GroupNode.ts +++ b/src/treeview/GroupNode.ts @@ -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 { @@ -14,7 +16,7 @@ export class GroupNode implements INode { } public async getChildren(): Promise { - return new DatabaseListProvider(this.context, this.groupName).getChildren(undefined); + return new DatabaseListProvider(this.context, this.groupName, this.refreshCallback).getChildren(undefined); } } \ No newline at end of file