Skip to content

Commit

Permalink
#142027 listen to file operations
Browse files Browse the repository at this point in the history
  • Loading branch information
sandy081 committed Feb 8, 2022
1 parent 6cd64a3 commit c95a310
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
43 changes: 24 additions & 19 deletions src/vs/workbench/services/configuration/browser/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,8 @@ class FileServiceBasedRemoteUserConfiguration extends Disposable {

this.parser = new ConfigurationModelParser(this.configurationResource.toString());
this.parseOptions = configurationParseOptions;
this._register(fileService.onDidFilesChange(e => this.handleFileEvents(e)));
this._register(fileService.onDidFilesChange(e => this.handleFileChangesEvent(e)));
this._register(fileService.onDidRunOperation(e => this.handleFileOperationEvent(e)));
this.reloadConfigurationScheduler = this._register(new RunOnceScheduler(() => this.reload().then(configurationModel => this._onDidChangeConfiguration.fire(configurationModel)), 50));
this._register(toDisposable(() => {
this.stopWatchingResource();
Expand Down Expand Up @@ -485,7 +486,7 @@ class FileServiceBasedRemoteUserConfiguration extends Disposable {
return this.parser.restrictedConfigurations;
}

private async handleFileEvents(event: FileChangesEvent): Promise<void> {
private handleFileChangesEvent(event: FileChangesEvent): void {

// Find changes that affect the resource
let affectedByChanges = event.contains(this.configurationResource, FileChangeType.UPDATED);
Expand All @@ -502,6 +503,13 @@ class FileServiceBasedRemoteUserConfiguration extends Disposable {
}
}

private handleFileOperationEvent(event: FileOperationEvent): void {
if ((event.isOperation(FileOperation.CREATE) || event.isOperation(FileOperation.DELETE) || event.isOperation(FileOperation.WRITE))
&& this.uriIdentityService.extUri.isEqual(event.resource, this.configurationResource)) {
this.reloadConfigurationScheduler.schedule();
}
}

private onResourceExists(exists: boolean): void {
if (exists) {
this.stopWatchingDirectory();
Expand Down Expand Up @@ -577,7 +585,6 @@ class CachedRemoteUserConfiguration extends Disposable {

export class WorkspaceConfiguration extends Disposable {

private readonly _fileService: IFileService;
private readonly _cachedConfiguration: CachedWorkspaceConfiguration;
private _workspaceConfiguration: CachedWorkspaceConfiguration | FileServiceBasedWorkspaceConfiguration;
private _workspaceConfigurationDisposables = this._register(new DisposableStore());
Expand All @@ -591,10 +598,11 @@ export class WorkspaceConfiguration extends Disposable {
get initialized(): boolean { return this._initialized; }
constructor(
private readonly configurationCache: IConfigurationCache,
fileService: IFileService
private readonly fileService: IFileService,
private readonly uriIdentityService: IUriIdentityService,
) {
super();
this._fileService = fileService;
this.fileService = fileService;
this._workspaceConfiguration = this._cachedConfiguration = new CachedWorkspaceConfiguration(configurationCache);
}

Expand All @@ -606,7 +614,7 @@ export class WorkspaceConfiguration extends Disposable {
this._workspaceConfiguration = this._cachedConfiguration;
this.waitAndInitialize(this._workspaceIdentifier);
} else {
this.doInitialize(new FileServiceBasedWorkspaceConfiguration(this._fileService));
this.doInitialize(new FileServiceBasedWorkspaceConfiguration(this.fileService, this.uriIdentityService));
}
}
await this.reload();
Expand Down Expand Up @@ -653,9 +661,9 @@ export class WorkspaceConfiguration extends Disposable {
}

private async waitAndInitialize(workspaceIdentifier: IWorkspaceIdentifier): Promise<void> {
await whenProviderRegistered(workspaceIdentifier.configPath, this._fileService);
await whenProviderRegistered(workspaceIdentifier.configPath, this.fileService);
if (!(this._workspaceConfiguration instanceof FileServiceBasedWorkspaceConfiguration)) {
const fileServiceBasedWorkspaceConfiguration = this._register(new FileServiceBasedWorkspaceConfiguration(this._fileService));
const fileServiceBasedWorkspaceConfiguration = this._register(new FileServiceBasedWorkspaceConfiguration(this.fileService, this.uriIdentityService));
await fileServiceBasedWorkspaceConfiguration.load(workspaceIdentifier, { scopes: WORKSPACE_SCOPES, skipRestricted: this.isUntrusted() });
this.doInitialize(fileServiceBasedWorkspaceConfiguration);
this.onDidWorkspaceConfigurationChange(false, true);
Expand Down Expand Up @@ -700,13 +708,19 @@ class FileServiceBasedWorkspaceConfiguration extends Disposable {
protected readonly _onDidChange: Emitter<void> = this._register(new Emitter<void>());
readonly onDidChange: Event<void> = this._onDidChange.event;

constructor(private fileService: IFileService) {
constructor(
private readonly fileService: IFileService,
uriIdentityService: IUriIdentityService
) {
super();

this.workspaceConfigurationModelParser = new WorkspaceConfigurationModelParser('');
this.workspaceSettings = new ConfigurationModel();

this._register(fileService.onDidFilesChange(e => this.handleWorkspaceFileEvents(e)));
this._register(Event.any(
Event.filter(this.fileService.onDidFilesChange, e => !!this._workspaceIdentifier && e.contains(this._workspaceIdentifier.configPath)),
Event.filter(this.fileService.onDidRunOperation, e => !!this._workspaceIdentifier && (e.isOperation(FileOperation.CREATE) || e.isOperation(FileOperation.DELETE) || e.isOperation(FileOperation.WRITE)) && uriIdentityService.extUri.isEqual(e.resource, this._workspaceIdentifier.configPath))
)(() => this.reloadConfigurationScheduler.schedule()));
this.reloadConfigurationScheduler = this._register(new RunOnceScheduler(() => this._onDidChange.fire(), 50));
this.workspaceConfigWatcher = this._register(this.watchWorkspaceConfigurationFile());
}
Expand Down Expand Up @@ -774,15 +788,6 @@ class FileServiceBasedWorkspaceConfiguration extends Disposable {
return this._workspaceIdentifier ? this.fileService.watch(this._workspaceIdentifier.configPath) : Disposable.None;
}

private handleWorkspaceFileEvents(event: FileChangesEvent): void {
if (this._workspaceIdentifier) {

// Find changes that affect workspace file
if (event.contains(this._workspaceIdentifier.configPath)) {
this.reloadConfigurationScheduler.schedule();
}
}
}
}

class CachedWorkspaceConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class WorkspaceService extends Disposable implements IWorkbenchConfigurat
this.initRemoteUserConfigurationBarrier.open();
}

this.workspaceConfiguration = this._register(new WorkspaceConfiguration(configurationCache, fileService));
this.workspaceConfiguration = this._register(new WorkspaceConfiguration(configurationCache, fileService, uriIdentityService));
this._register(this.workspaceConfiguration.onDidUpdateConfiguration(fromCache => {
this.onWorkspaceConfigurationChanged(fromCache).then(() => {
this.workspace.initialized = this.workspaceConfiguration.initialized;
Expand Down

0 comments on commit c95a310

Please sign in to comment.