Skip to content

Apply debounce to DependencyDataProvider's refreshing #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
"description": "%configuration.java.dependency.autoRefresh%",
"default": true
},
"java.dependency.refreshDelay": {
"type": "number",
"description": "%configuration.java.dependency.refreshDelay%",
"default": 2000
},
"java.dependency.packagePresentation": {
"type": "string",
"enum": [
Expand Down Expand Up @@ -167,6 +172,7 @@
],
"devDependencies": {
"@types/fs-extra": "^5.0.4",
"@types/lodash": "^4.14.139",
"@types/mocha": "^5.2.5",
"@types/node": "^8.10.36",
"@types/xml2js": "^0.4.3",
Expand All @@ -186,6 +192,7 @@
"dependencies": {
"find-java-home": "^0.2.0",
"fs-extra": "^7.0.1",
"lodash": "^4.17.15",
"vscode-extension-telemetry-wrapper": "^0.4.0",
"xml2js": "^0.4.19"
}
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"configuration.java.dependency.showOutline": "Enable show outline in the Java Dependency explorer",
"configuration.java.dependency.syncWithFolderExplorer": "Synchronize dependency viewer selection with folder explorer",
"configuration.java.dependency.autoRefresh": "Synchronize dependency viewer with changes",
"configuration.java.dependency.refreshDelay": "The delay time (ms) the auto refresh is invoked when changes are detected.",
"configuration.java.dependency.packagePresentation": "Package presentation mode: flat or hierarchical"
}
1 change: 1 addition & 0 deletions package.nls.zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"configuration.java.dependency.showOutline": "在 Java 依赖项资源管理器中显示类成员大纲",
"configuration.java.dependency.syncWithFolderExplorer": "在 Java 依赖项资源管理器中同步关联当前打开的文件",
"configuration.java.dependency.autoRefresh": "在 Java 依赖项资源管理器中自动同步修改",
"configuration.java.dependency.refreshDelay": "控制Java 依赖项资源管理器刷新的延迟时间 (毫秒)。",
"configuration.java.dependency.packagePresentation": "Java 包显示方式: 平行显示或者分层显示"
}
2 changes: 1 addition & 1 deletion src/fileWather.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ export class SyncHandler {
}

private static refresh(): void {
commands.executeCommand(Commands.VIEW_PACKAGE_REFRESH);
commands.executeCommand(Commands.VIEW_PACKAGE_REFRESH, /* debounce = */true);
}
}
17 changes: 17 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export class Settings {
return;
}
const updatedConfig = workspace.getConfiguration("java.dependency");
for (const listener of this._configurationListeners) {
listener(updatedConfig, this._dependencyConfig);
}
if (updatedConfig.showOutline !== this._dependencyConfig.showOutline
|| updatedConfig.packagePresentation !== this._dependencyConfig.packagePresentation
|| (updatedConfig.syncWithFolderExplorer !== this._dependencyConfig.syncWithFolderExplorer
Expand All @@ -32,6 +35,8 @@ export class Settings {
}));
SyncHandler.updateFileWatcher(Settings.autoRefresh());

context.subscriptions.push({ dispose: () => { this._configurationListeners = []; } });

context.subscriptions.push(commands.registerCommand(Commands.VIEW_PACKAGE_LINKWITHFOLDER,
instrumentOperation(Commands.VIEW_PACKAGE_LINKWITHFOLDER, Settings.linkWithFolderCommand)));

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

public static registerConfigurationListener(listener: Listener) {
this._configurationListeners.push(listener);
}

public static linkWithFolderCommand(): void {
workspace.getConfiguration().update("java.dependency.syncWithFolderExplorer", true, false);
}
Expand Down Expand Up @@ -77,10 +86,18 @@ export class Settings {
return this._dependencyConfig.get("packagePresentation") === PackagePresentation.Hierarchical;
}

public static refreshDelay(): number {
return this._dependencyConfig.get("refreshDelay");
}

private static _dependencyConfig: WorkspaceConfiguration = workspace.getConfiguration("java.dependency");

private static _configurationListeners: Listener[] = [];
}

enum PackagePresentation {
Flat = "flat",
Hierarchical = "hierarchical",
}

type Listener = (updatedConfig: WorkspaceConfiguration, dependencyConfig: WorkspaceConfiguration) => void;
40 changes: 33 additions & 7 deletions src/views/dependencyDataProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

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

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

constructor(public readonly context: ExtensionContext) {
context.subscriptions.push(commands.registerCommand(Commands.VIEW_PACKAGE_REFRESH, () => this.refreshWithLog()));
context.subscriptions.push(commands.registerCommand(Commands.VIEW_PACKAGE_REFRESH, (debounce?: boolean) => this.refreshWithLog(debounce)));
context.subscriptions.push(commands.registerCommand(Commands.VIEW_PACKAGE_OPEN_FILE,
instrumentOperation(Commands.VIEW_PACKAGE_OPEN_FILE, (_operationId, uri) => this.openFile(uri))));
context.subscriptions.push(commands.registerCommand(Commands.VIEW_PACKAGE_OUTLINE,
instrumentOperation(Commands.VIEW_PACKAGE_OUTLINE, (_operationId, uri, range) => this.goToOutline(uri, range))));
Settings.registerConfigurationListener((updatedConfig, dependencyConfig) => {
if (updatedConfig.refreshDelay !== dependencyConfig.refreshDelay) {
this.setRefreshDelay(updatedConfig.refreshDelay);
}
});
this.setRefreshDelay();
}

public refreshWithLog() {
public refreshWithLog(debounce?: boolean) {
if (Settings.autoRefresh()) {
this.refresh();
this.refresh(debounce);
} else {
instrumentOperation(Commands.VIEW_PACKAGE_REFRESH, () => this.refresh())();
instrumentOperation(Commands.VIEW_PACKAGE_REFRESH, () => this.refresh(debounce))();
}
}

public refresh() {
this._rootItems = null;
this._onDidChangeTreeData.fire();
public refresh(debounce = false) {
if (debounce) {
this._refreshDelayTrigger();
} else { // Immediately refresh
this._refreshDelayTrigger.flush();
}
}

public setRefreshDelay(wait?: number) {
if (!wait) {
wait = Settings.refreshDelay();
}
if (this._refreshDelayTrigger) {
this._refreshDelayTrigger.cancel();
}
this._refreshDelayTrigger = _.debounce(() => this.doRefresh(), wait);
}

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

private doRefresh(): void {
this._rootItems = null;
this._onDidChangeTreeData.fire();
}

private async getRootProjects(): Promise<ExplorerNode[]> {
const rootElements = this._rootItems ? this._rootItems : await this.getChildren();
if (rootElements[0] instanceof ProjectNode) {
Expand Down