Skip to content

Commit d6fc145

Browse files
Vigilanstestforstephen
authored andcommitted
Apply debounce to DependencyDataProvider's refreshing (#183)
* Add debounce to DependencyDataProvider's refreshing * Add configuration for refresh delay * Add parameter to control whether dobounce should be applied * Change wording
1 parent cf70dcd commit d6fc145

7 files changed

+71
-8
lines changed

package-lock.json

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+7
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@
104104
"description": "%configuration.java.dependency.autoRefresh%",
105105
"default": true
106106
},
107+
"java.dependency.refreshDelay": {
108+
"type": "number",
109+
"description": "%configuration.java.dependency.refreshDelay%",
110+
"default": 2000
111+
},
107112
"java.dependency.packagePresentation": {
108113
"type": "string",
109114
"enum": [
@@ -167,6 +172,7 @@
167172
],
168173
"devDependencies": {
169174
"@types/fs-extra": "^5.0.4",
175+
"@types/lodash": "^4.14.139",
170176
"@types/mocha": "^5.2.5",
171177
"@types/node": "^8.10.36",
172178
"@types/xml2js": "^0.4.3",
@@ -186,6 +192,7 @@
186192
"dependencies": {
187193
"find-java-home": "^0.2.0",
188194
"fs-extra": "^7.0.1",
195+
"lodash": "^4.17.15",
189196
"vscode-extension-telemetry-wrapper": "^0.4.0",
190197
"xml2js": "^0.4.19"
191198
}

package.nls.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
"configuration.java.dependency.showOutline": "Enable show outline in the Java Dependency explorer",
1212
"configuration.java.dependency.syncWithFolderExplorer": "Synchronize dependency viewer selection with folder explorer",
1313
"configuration.java.dependency.autoRefresh": "Synchronize dependency viewer with changes",
14+
"configuration.java.dependency.refreshDelay": "The delay time (ms) the auto refresh is invoked when changes are detected.",
1415
"configuration.java.dependency.packagePresentation": "Package presentation mode: flat or hierarchical"
1516
}

package.nls.zh.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
"configuration.java.dependency.showOutline": "在 Java 依赖项资源管理器中显示类成员大纲",
1212
"configuration.java.dependency.syncWithFolderExplorer": "在 Java 依赖项资源管理器中同步关联当前打开的文件",
1313
"configuration.java.dependency.autoRefresh": "在 Java 依赖项资源管理器中自动同步修改",
14+
"configuration.java.dependency.refreshDelay": "控制Java 依赖项资源管理器刷新的延迟时间 (毫秒)。",
1415
"configuration.java.dependency.packagePresentation": "Java 包显示方式: 平行显示或者分层显示"
1516
}

src/fileWather.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ export class SyncHandler {
4545
}
4646

4747
private static refresh(): void {
48-
commands.executeCommand(Commands.VIEW_PACKAGE_REFRESH);
48+
commands.executeCommand(Commands.VIEW_PACKAGE_REFRESH, /* debounce = */true);
4949
}
5050
}

src/settings.ts

+17
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ export class Settings {
1717
return;
1818
}
1919
const updatedConfig = workspace.getConfiguration("java.dependency");
20+
for (const listener of this._configurationListeners) {
21+
listener(updatedConfig, this._dependencyConfig);
22+
}
2023
if (updatedConfig.showOutline !== this._dependencyConfig.showOutline
2124
|| updatedConfig.packagePresentation !== this._dependencyConfig.packagePresentation
2225
|| (updatedConfig.syncWithFolderExplorer !== this._dependencyConfig.syncWithFolderExplorer
@@ -32,6 +35,8 @@ export class Settings {
3235
}));
3336
SyncHandler.updateFileWatcher(Settings.autoRefresh());
3437

38+
context.subscriptions.push({ dispose: () => { this._configurationListeners = []; } });
39+
3540
context.subscriptions.push(commands.registerCommand(Commands.VIEW_PACKAGE_LINKWITHFOLDER,
3641
instrumentOperation(Commands.VIEW_PACKAGE_LINKWITHFOLDER, Settings.linkWithFolderCommand)));
3742

@@ -45,6 +50,10 @@ export class Settings {
4550
instrumentOperation(Commands.VIEW_PACKAGE_CHANGETOHIERARCHICALPACKAGEVIEW, Settings.changeToHierarchicalPackageView)));
4651
}
4752

53+
public static registerConfigurationListener(listener: Listener) {
54+
this._configurationListeners.push(listener);
55+
}
56+
4857
public static linkWithFolderCommand(): void {
4958
workspace.getConfiguration().update("java.dependency.syncWithFolderExplorer", true, false);
5059
}
@@ -77,10 +86,18 @@ export class Settings {
7786
return this._dependencyConfig.get("packagePresentation") === PackagePresentation.Hierarchical;
7887
}
7988

89+
public static refreshDelay(): number {
90+
return this._dependencyConfig.get("refreshDelay");
91+
}
92+
8093
private static _dependencyConfig: WorkspaceConfiguration = workspace.getConfiguration("java.dependency");
94+
95+
private static _configurationListeners: Listener[] = [];
8196
}
8297

8398
enum PackagePresentation {
8499
Flat = "flat",
85100
Hierarchical = "hierarchical",
86101
}
102+
103+
type Listener = (updatedConfig: WorkspaceConfiguration, dependencyConfig: WorkspaceConfiguration) => void;

src/views/dependencyDataProvider.ts

+33-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
import * as _ from "lodash";
45
import {
56
commands, Event, EventEmitter, ExtensionContext, ProviderResult, Range,
67
Selection, TextEditorRevealType, TreeDataProvider, TreeItem, Uri, window, workspace,
@@ -23,26 +24,46 @@ export class DependencyDataProvider implements TreeDataProvider<ExplorerNode> {
2324
public onDidChangeTreeData: Event<null> = this._onDidChangeTreeData.event;
2425

2526
private _rootItems: ExplorerNode[] = null;
27+
private _refreshDelayTrigger: (() => void) & _.Cancelable;
2628

2729
constructor(public readonly context: ExtensionContext) {
28-
context.subscriptions.push(commands.registerCommand(Commands.VIEW_PACKAGE_REFRESH, () => this.refreshWithLog()));
30+
context.subscriptions.push(commands.registerCommand(Commands.VIEW_PACKAGE_REFRESH, (debounce?: boolean) => this.refreshWithLog(debounce)));
2931
context.subscriptions.push(commands.registerCommand(Commands.VIEW_PACKAGE_OPEN_FILE,
3032
instrumentOperation(Commands.VIEW_PACKAGE_OPEN_FILE, (_operationId, uri) => this.openFile(uri))));
3133
context.subscriptions.push(commands.registerCommand(Commands.VIEW_PACKAGE_OUTLINE,
3234
instrumentOperation(Commands.VIEW_PACKAGE_OUTLINE, (_operationId, uri, range) => this.goToOutline(uri, range))));
35+
Settings.registerConfigurationListener((updatedConfig, dependencyConfig) => {
36+
if (updatedConfig.refreshDelay !== dependencyConfig.refreshDelay) {
37+
this.setRefreshDelay(updatedConfig.refreshDelay);
38+
}
39+
});
40+
this.setRefreshDelay();
3341
}
3442

35-
public refreshWithLog() {
43+
public refreshWithLog(debounce?: boolean) {
3644
if (Settings.autoRefresh()) {
37-
this.refresh();
45+
this.refresh(debounce);
3846
} else {
39-
instrumentOperation(Commands.VIEW_PACKAGE_REFRESH, () => this.refresh())();
47+
instrumentOperation(Commands.VIEW_PACKAGE_REFRESH, () => this.refresh(debounce))();
4048
}
4149
}
4250

43-
public refresh() {
44-
this._rootItems = null;
45-
this._onDidChangeTreeData.fire();
51+
public refresh(debounce = false) {
52+
if (debounce) {
53+
this._refreshDelayTrigger();
54+
} else { // Immediately refresh
55+
this._refreshDelayTrigger.flush();
56+
}
57+
}
58+
59+
public setRefreshDelay(wait?: number) {
60+
if (!wait) {
61+
wait = Settings.refreshDelay();
62+
}
63+
if (this._refreshDelayTrigger) {
64+
this._refreshDelayTrigger.cancel();
65+
}
66+
this._refreshDelayTrigger = _.debounce(() => this.doRefresh(), wait);
4667
}
4768

4869
public openFile(uri: string) {
@@ -83,6 +104,11 @@ export class DependencyDataProvider implements TreeDataProvider<ExplorerNode> {
83104
return project ? project.revealPaths(paths) : null;
84105
}
85106

107+
private doRefresh(): void {
108+
this._rootItems = null;
109+
this._onDidChangeTreeData.fire();
110+
}
111+
86112
private async getRootProjects(): Promise<ExplorerNode[]> {
87113
const rootElements = this._rootItems ? this._rootItems : await this.getChildren();
88114
if (rootElements[0] instanceof ProjectNode) {

0 commit comments

Comments
 (0)