Skip to content

Commit d2e4503

Browse files
author
Benjamin Pasero
committed
files2 - separate methods for watchFile/Folder
1 parent d77acee commit d2e4503

4 files changed

Lines changed: 32 additions & 16 deletions

File tree

src/vs/base/node/config.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as objects from 'vs/base/common/objects';
99
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
1010
import { Event, Emitter } from 'vs/base/common/event';
1111
import * as json from 'vs/base/common/json';
12-
import { watchNonRecursive } from 'vs/base/node/pfs';
12+
import { watchFolder, watchFile } from 'vs/base/node/pfs';
1313

1414
export interface IConfigurationChangeEvent<T> {
1515
config: T;
@@ -146,10 +146,11 @@ export class ConfigWatcher<T> implements IConfigWatcher<T>, IDisposable {
146146
return; // avoid watchers that will never get disposed by checking for being disposed
147147
}
148148

149-
this.disposables.push(watchNonRecursive({ path, isDirectory: isParentFolder },
150-
(type, path) => this.onConfigFileChange(type, path, isParentFolder),
151-
(error: string) => this.options.onError(error)
152-
));
149+
if (isParentFolder) {
150+
this.disposables.push(watchFolder(path, (type, path) => this.onConfigFileChange(type, path, isParentFolder), error => this.options.onError(error)));
151+
} else {
152+
this.disposables.push(watchFile(path, (type, path) => this.onConfigFileChange(type, path, isParentFolder), error => this.options.onError(error)));
153+
}
153154
}
154155

155156
private onConfigFileChange(eventType: 'added' | 'changed' | 'deleted', path: string, isParentFolder: boolean): void {

src/vs/base/node/pfs.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,15 @@ export async function mkdirp(path: string, mode?: number, token?: CancellationTo
673673
}
674674
}
675675

676-
export function watchNonRecursive(file: { path: string, isDirectory: boolean }, onChange: (type: 'added' | 'changed' | 'deleted', path: string) => void, onError: (error: string) => void): IDisposable {
676+
export function watchFile(path: string, onChange: (type: 'changed' | 'deleted', path: string) => void, onError: (error: string) => void): IDisposable {
677+
return doWatchNonRecursive({ path, isDirectory: false }, onChange, onError);
678+
}
679+
680+
export function watchFolder(path: string, onChange: (type: 'added' | 'changed' | 'deleted', path: string) => void, onError: (error: string) => void): IDisposable {
681+
return doWatchNonRecursive({ path, isDirectory: true }, onChange, onError);
682+
}
683+
684+
function doWatchNonRecursive(file: { path: string, isDirectory: boolean }, onChange: (type: 'added' | 'changed' | 'deleted', path: string) => void, onError: (error: string) => void): IDisposable {
677685
const mapPathToStatDisposable = new Map<string, IDisposable>();
678686

679687
let disposed = false;
@@ -746,7 +754,7 @@ export function watchNonRecursive(file: { path: string, isDirectory: boolean },
746754
if (fileExists) {
747755
onChange('changed', changedFilePath);
748756

749-
watcherDisposables = [watchNonRecursive(file, onChange, onError)];
757+
watcherDisposables = [doWatchNonRecursive(file, onChange, onError)];
750758
}
751759

752760
// File seems to be really gone, so emit a deleted event

src/vs/workbench/services/files2/node/diskFileSystemProvider.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { IFileSystemProvider, FileSystemProviderCapabilities, IFileChange, IWatc
1010
import { URI } from 'vs/base/common/uri';
1111
import { Event, Emitter } from 'vs/base/common/event';
1212
import { isLinux, isWindows } from 'vs/base/common/platform';
13-
import { statLink, readdir, unlink, move, copy, readFile, writeFile, fileExists, truncate, rimraf, RimRafMode, watchNonRecursive } from 'vs/base/node/pfs';
13+
import { statLink, readdir, unlink, move, copy, readFile, writeFile, fileExists, truncate, rimraf, RimRafMode, watchFolder, watchFile } from 'vs/base/node/pfs';
1414
import { normalize, basename, dirname } from 'vs/base/common/path';
1515
import { joinPath } from 'vs/base/common/resources';
1616
import { isEqual } from 'vs/base/common/extpath';
@@ -413,12 +413,19 @@ export class DiskFileSystemProvider extends Disposable implements IFileSystemPro
413413
return;
414414
}
415415

416-
disposable = watchNonRecursive({ path: resource.fsPath, isDirectory: fileStat.type === FileType.Directory }, (eventType: 'added' | 'changed' | 'deleted', path: string) => {
417-
this.onNonRecursiveFileChange({
418-
type: eventType === 'changed' ? FileChangeType.UPDATED : eventType === 'added' ? FileChangeType.ADDED : FileChangeType.DELETED,
419-
path
420-
});
421-
}, error => this.logService.error(error));
416+
// Watch Folder
417+
if (fileStat.type === FileType.Directory) {
418+
disposable = watchFolder(resource.fsPath, (eventType, path) => {
419+
this.onNonRecursiveFileChange({ type: eventType === 'changed' ? FileChangeType.UPDATED : eventType === 'added' ? FileChangeType.ADDED : FileChangeType.DELETED, path });
420+
}, error => this.logService.error(error));
421+
}
422+
423+
// Watch File
424+
else {
425+
disposable = watchFile(resource.fsPath, (eventType, path) => {
426+
this.onNonRecursiveFileChange({ type: eventType === 'changed' ? FileChangeType.UPDATED : FileChangeType.DELETED, path });
427+
}, error => this.logService.error(error));
428+
}
422429
}, error => this.logService.error(error));
423430

424431
return toDisposable(() => dispose(disposable));

src/vs/workbench/services/output/node/outputChannelModelService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
7-
import { watchNonRecursive } from 'vs/base/node/pfs';
7+
import { watchFolder } from 'vs/base/node/pfs';
88
import { dirname, join } from 'vs/base/common/path';
99
import * as resources from 'vs/base/common/resources';
1010
import { ITextModel } from 'vs/editor/common/model';
@@ -29,7 +29,7 @@ let callbacks: ((eventType: string, fileName?: string) => void)[] = [];
2929
function watchOutputDirectory(outputDir: string, logService: ILogService, onChange: (eventType: 'added' | 'changed' | 'deleted', path: string) => void): IDisposable {
3030
callbacks.push(onChange);
3131
if (!watchingOutputDir) {
32-
const watcherDisposable = watchNonRecursive({ path: outputDir, isDirectory: true }, (eventType, path) => {
32+
const watcherDisposable = watchFolder(outputDir, (eventType, path) => {
3333
for (const callback of callbacks) {
3434
callback(eventType, path);
3535
}

0 commit comments

Comments
 (0)