Skip to content

Commit f1b2498

Browse files
authored
Re-render db panel on config update (#1763)
1 parent 39ae3cd commit f1b2498

File tree

5 files changed

+44
-6
lines changed

5 files changed

+44
-6
lines changed

extensions/ql-vscode/src/databases/db-config-store.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ import { DisposableObject } from '../pure/disposable-object';
66
import { DbConfigValidator } from './db-config-validator';
77
import { ValueResult } from '../common/value-result';
88
import { App } from '../common/app';
9+
import { AppEvent, AppEventEmitter } from '../common/events';
910

1011
export class DbConfigStore extends DisposableObject {
12+
public readonly onDidChangeConfig: AppEvent<void>;
13+
private readonly onDidChangeConfigEventEmitter: AppEventEmitter<void>;
14+
1115
private readonly configPath: string;
1216
private readonly configValidator: DbConfigValidator;
1317

@@ -25,6 +29,8 @@ export class DbConfigStore extends DisposableObject {
2529
this.configErrors = [];
2630
this.configWatcher = undefined;
2731
this.configValidator = new DbConfigValidator(app.extensionPath);
32+
this.onDidChangeConfigEventEmitter = app.createEventEmitter<void>();
33+
this.onDidChangeConfig = this.onDidChangeConfigEventEmitter.event;
2834
}
2935

3036
public async initialize(): Promise<void> {
@@ -85,6 +91,8 @@ export class DbConfigStore extends DisposableObject {
8591
}
8692

8793
this.config = this.configErrors.length === 0 ? newConfig : undefined;
94+
95+
this.onDidChangeConfigEventEmitter.fire();
8896
}
8997

9098
private watchConfig(): void {

extensions/ql-vscode/src/databases/db-manager.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1+
import { App } from '../common/app';
2+
import { AppEvent, AppEventEmitter } from '../common/events';
13
import { ValueResult } from '../common/value-result';
24
import { DbConfigStore } from './db-config-store';
35
import { DbItem } from './db-item';
46
import { createLocalTree, createRemoteTree } from './db-tree-creator';
57

68
export class DbManager {
9+
public readonly onDbItemsChanged: AppEvent<void>;
10+
private readonly onDbItemsChangesEventEmitter: AppEventEmitter<void>;
11+
712
constructor(
13+
app: App,
814
private readonly dbConfigStore: DbConfigStore
915
) {
16+
this.onDbItemsChangesEventEmitter = app.createEventEmitter<void>();
17+
this.onDbItemsChanged = this.onDbItemsChangesEventEmitter.event;
18+
19+
this.dbConfigStore.onDidChangeConfig(() => {
20+
this.onDbItemsChangesEventEmitter.fire();
21+
});
1022
}
1123

1224
public getDbItems(): ValueResult<DbItem[]> {

extensions/ql-vscode/src/databases/db-module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class DbModule extends DisposableObject {
2222
const dbConfigStore = new DbConfigStore(app);
2323
await dbConfigStore.initialize();
2424

25-
const dbManager = new DbManager(dbConfigStore);
25+
const dbManager = new DbManager(app, dbConfigStore);
2626
const dbPanel = new DbPanel(dbManager);
2727
await dbPanel.initialize();
2828

extensions/ql-vscode/src/databases/ui/db-tree-data-provider.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1-
import { ProviderResult, TreeDataProvider, TreeItem } from 'vscode';
1+
import { Event, EventEmitter, ProviderResult, TreeDataProvider, TreeItem } from 'vscode';
22
import { createDbTreeViewItemError, DbTreeViewItem } from './db-tree-view-item';
33
import { DbManager } from '../db-manager';
44
import { mapDbItemToTreeViewItem } from './db-item-mapper';
5+
import { DisposableObject } from '../../pure/disposable-object';
56

6-
export class DbTreeDataProvider implements TreeDataProvider<DbTreeViewItem> {
7+
export class DbTreeDataProvider extends DisposableObject implements TreeDataProvider<DbTreeViewItem> {
8+
9+
// This is an event to signal that there's been a change in the tree which
10+
// will case the view to refresh. It is part of the TreeDataProvider interface.
11+
public readonly onDidChangeTreeData: Event<DbTreeViewItem | undefined>;
12+
13+
private _onDidChangeTreeData = this.push(new EventEmitter<DbTreeViewItem | undefined>());
714
private dbTreeItems: DbTreeViewItem[];
815

916
public constructor(
1017
private readonly dbManager: DbManager
1118
) {
19+
super();
1220
this.dbTreeItems = this.createTree();
21+
this.onDidChangeTreeData = this._onDidChangeTreeData.event;
22+
23+
dbManager.onDbItemsChanged(() => {
24+
this.dbTreeItems = this.createTree();
25+
this._onDidChangeTreeData.fire(undefined);
26+
});
1327
}
1428

1529
/**

extensions/ql-vscode/src/vscode-tests/minimal-workspace/databases/db-panel.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ describe('db panel', async () => {
3131
globalStoragePath,
3232
workspaceStoragePath
3333
});
34+
await fs.ensureDir(workspaceStoragePath);
35+
3436
const app = new ExtensionApp(extensionContext);
3537

3638
dbConfigStore = new DbConfigStore(app);
37-
dbManager = new DbManager(dbConfigStore);
39+
dbManager = new DbManager(app, dbConfigStore);
3840

3941
// Create a modified version of the DbPanel module that allows
4042
// us to override the creation of the DbTreeDataProvider
@@ -228,8 +230,10 @@ describe('db panel', async () => {
228230
async function saveDbConfig(dbConfig: DbConfig): Promise<void> {
229231
await fs.writeJson(dbConfigFilePath, dbConfig);
230232

231-
// Once we have watching of the db config, this can happen
232-
// at the start of the test.
233+
// Ideally we would just initialise the db config store at the start
234+
// of each test and then rely on the file watcher to update the config.
235+
// However, this requires adding sleep to the tests to allow for the
236+
// file watcher to catch up, so we instead initialise the config store here.
233237
await dbConfigStore.initialize();
234238
dbTreeDataProvider = new DbTreeDataProvider(dbManager);
235239
}

0 commit comments

Comments
 (0)