Skip to content

Commit bdc5fa8

Browse files
authored
refactor: Simplify the settings change event handling (#737)
1 parent 004fdb6 commit bdc5fa8

File tree

2 files changed

+22
-45
lines changed

2 files changed

+22
-45
lines changed

src/settings.ts

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,32 @@
33

44
import {
55
commands, ConfigurationChangeEvent, ExtensionContext,
6-
workspace, WorkspaceConfiguration,
6+
workspace,
77
} from "vscode";
88
import { instrumentOperationAsVsCodeCommand } from "vscode-extension-telemetry-wrapper";
99
import { Commands } from "./commands";
1010
import { syncHandler } from "./syncHandler";
11+
import { contextManager, DependencyExplorer } from "../extension.bundle";
1112

1213
export class Settings {
1314

1415
public static initialize(context: ExtensionContext): void {
1516
context.subscriptions.push(workspace.onDidChangeConfiguration((e: ConfigurationChangeEvent) => {
16-
if (!e.affectsConfiguration("java.dependency")) {
17-
return;
18-
}
19-
const oldConfig = this._dependencyConfig;
20-
this._dependencyConfig = workspace.getConfiguration("java.dependency");
21-
for (const listener of this._configurationListeners) {
22-
listener(this._dependencyConfig, oldConfig);
23-
}
24-
}));
25-
this.registerConfigurationListener((updatedConfig, oldConfig) => {
26-
if (updatedConfig.showMembers !== oldConfig.showMembers
27-
|| updatedConfig.packagePresentation !== oldConfig.packagePresentation
28-
|| (updatedConfig.syncWithFolderExplorer !== oldConfig.syncWithFolderExplorer
29-
&& updatedConfig.syncWithFolderExplorer)) {
17+
if ((e.affectsConfiguration("java.dependency.syncWithFolderExplorer") && Settings.syncWithFolderExplorer()) ||
18+
e.affectsConfiguration("java.dependency.showMembers") ||
19+
e.affectsConfiguration("java.dependency.packagePresentation")) {
3020
commands.executeCommand(Commands.VIEW_PACKAGE_INTERNAL_REFRESH);
31-
} else if (updatedConfig.autoRefresh !== oldConfig.autoRefresh) {
32-
syncHandler.updateFileWatcher(updatedConfig.autoRefresh);
21+
} else if (e.affectsConfiguration("java.dependency.autoRefresh")) {
22+
syncHandler.updateFileWatcher(Settings.autoRefresh());
23+
} else if (e.affectsConfiguration("java.dependency.refreshDelay")) {
24+
// TODO: getInstance() should not have parameter if it means to be a singleton.
25+
DependencyExplorer.getInstance(contextManager.context)
26+
.dataProvider.setRefreshDebounceFunc(Settings.refreshDelay());
3327
}
34-
});
28+
}));
3529

3630
syncHandler.updateFileWatcher(Settings.autoRefresh());
3731

38-
context.subscriptions.push({ dispose: () => { this._configurationListeners = []; } });
39-
4032
context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_LINKWITHFOLDER, Settings.linkWithFolderCommand));
4133

4234
context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_UNLINKWITHFOLDER, Settings.unlinkWithFolderCommand));
@@ -48,24 +40,20 @@ export class Settings {
4840
Settings.changeToHierarchicalPackageView));
4941
}
5042

51-
public static registerConfigurationListener(listener: Listener) {
52-
this._configurationListeners.push(listener);
53-
}
54-
5543
public static linkWithFolderCommand(): void {
56-
workspace.getConfiguration().update("java.dependency.syncWithFolderExplorer", true, false);
44+
workspace.getConfiguration("java.dependency").update("syncWithFolderExplorer", true, false);
5745
}
5846

5947
public static unlinkWithFolderCommand(): void {
60-
workspace.getConfiguration().update("java.dependency.syncWithFolderExplorer", false, false);
48+
workspace.getConfiguration("java.dependency").update("syncWithFolderExplorer", false, false);
6149
}
6250

6351
public static changeToFlatPackageView(): void {
64-
workspace.getConfiguration().update("java.dependency.packagePresentation", PackagePresentation.Flat, false);
52+
workspace.getConfiguration("java.dependency").update("packagePresentation", PackagePresentation.Flat, false);
6553
}
6654

6755
public static changeToHierarchicalPackageView(): void {
68-
workspace.getConfiguration().update("java.dependency.packagePresentation", PackagePresentation.Hierarchical, false);
56+
workspace.getConfiguration("java.dependency").update("packagePresentation", PackagePresentation.Hierarchical, false);
6957
}
7058

7159
public static updateReferencedLibraries(libraries: IReferencedLibraries): void {
@@ -77,7 +65,7 @@ export class Settings {
7765
if (!updateSetting.exclude && !updateSetting.sources) {
7866
updateSetting = libraries.include;
7967
}
80-
workspace.getConfiguration().update("java.project.referencedLibraries", updateSetting);
68+
workspace.getConfiguration("java.project").update("referencedLibraries", updateSetting);
8169
}
8270

8371
public static referencedLibraries(): IReferencedLibraries {
@@ -91,42 +79,36 @@ export class Settings {
9179
}
9280

9381
public static showMembers(): boolean {
94-
return this._dependencyConfig.get("showMembers", false);
82+
return workspace.getConfiguration("java.dependency").get("showMembers", false);
9583
}
9684

9785
public static autoRefresh(): boolean {
98-
return this._dependencyConfig.get("autoRefresh", true);
86+
return workspace.getConfiguration("java.dependency").get("autoRefresh", true);
9987
}
10088

10189
public static syncWithFolderExplorer(): boolean {
102-
return this._dependencyConfig.get("syncWithFolderExplorer", true);
90+
return workspace.getConfiguration("java.dependency").get("syncWithFolderExplorer", true);
10391
}
10492

10593
public static isHierarchicalView(): boolean {
106-
return this._dependencyConfig.get("packagePresentation") === PackagePresentation.Hierarchical;
94+
return workspace.getConfiguration("java.dependency").get("packagePresentation") === PackagePresentation.Hierarchical;
10795
}
10896

10997
public static refreshDelay(): number {
110-
return this._dependencyConfig.get("refreshDelay", 2000);
98+
return workspace.getConfiguration("java.dependency").get("refreshDelay", 2000);
11199
}
112100

113101
public static getExportJarTargetPath(): string {
114102
// tslint:disable-next-line: no-invalid-template-strings
115103
return workspace.getConfiguration("java.project.exportJar").get<string>("targetPath", "${workspaceFolder}/${workspaceFolderBasename}.jar");
116104
}
117-
118-
private static _dependencyConfig: WorkspaceConfiguration = workspace.getConfiguration("java.dependency");
119-
120-
private static _configurationListeners: Listener[] = [];
121105
}
122106

123107
enum PackagePresentation {
124108
Flat = "flat",
125109
Hierarchical = "hierarchical",
126110
}
127111

128-
type Listener = (updatedConfig: WorkspaceConfiguration, oldConfig: WorkspaceConfiguration) => void;
129-
130112
export interface IReferencedLibraries {
131113
include: string[];
132114
exclude: string[];

src/views/dependencyDataProvider.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,6 @@ export class DependencyDataProvider implements TreeDataProvider<ExplorerNode> {
7979
commands.executeCommand(Commands.BUILD_PROJECT, Uri.parse(node.uri), true);
8080
}));
8181

82-
Settings.registerConfigurationListener((updatedConfig, oldConfig) => {
83-
if (updatedConfig.refreshDelay !== oldConfig.refreshDelay) {
84-
this.setRefreshDebounceFunc(updatedConfig.refreshDelay);
85-
}
86-
});
8782
this.setRefreshDebounceFunc();
8883
}
8984

0 commit comments

Comments
 (0)