files2 - separate methods for watchFile/Folder

This commit is contained in:
Benjamin Pasero
2019-04-02 17:55:46 +02:00
parent d77aceee75
commit d2e45034c1
4 changed files with 32 additions and 16 deletions
+6 -5
View File
@@ -9,7 +9,7 @@ import * as objects from 'vs/base/common/objects';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { Event, Emitter } from 'vs/base/common/event';
import * as json from 'vs/base/common/json';
import { watchNonRecursive } from 'vs/base/node/pfs';
import { watchFolder, watchFile } from 'vs/base/node/pfs';
export interface IConfigurationChangeEvent<T> {
config: T;
@@ -146,10 +146,11 @@ export class ConfigWatcher<T> implements IConfigWatcher<T>, IDisposable {
return; // avoid watchers that will never get disposed by checking for being disposed
}
this.disposables.push(watchNonRecursive({ path, isDirectory: isParentFolder },
(type, path) => this.onConfigFileChange(type, path, isParentFolder),
(error: string) => this.options.onError(error)
));
if (isParentFolder) {
this.disposables.push(watchFolder(path, (type, path) => this.onConfigFileChange(type, path, isParentFolder), error => this.options.onError(error)));
} else {
this.disposables.push(watchFile(path, (type, path) => this.onConfigFileChange(type, path, isParentFolder), error => this.options.onError(error)));
}
}
private onConfigFileChange(eventType: 'added' | 'changed' | 'deleted', path: string, isParentFolder: boolean): void {
+10 -2
View File
@@ -673,7 +673,15 @@ export async function mkdirp(path: string, mode?: number, token?: CancellationTo
}
}
export function watchNonRecursive(file: { path: string, isDirectory: boolean }, onChange: (type: 'added' | 'changed' | 'deleted', path: string) => void, onError: (error: string) => void): IDisposable {
export function watchFile(path: string, onChange: (type: 'changed' | 'deleted', path: string) => void, onError: (error: string) => void): IDisposable {
return doWatchNonRecursive({ path, isDirectory: false }, onChange, onError);
}
export function watchFolder(path: string, onChange: (type: 'added' | 'changed' | 'deleted', path: string) => void, onError: (error: string) => void): IDisposable {
return doWatchNonRecursive({ path, isDirectory: true }, onChange, onError);
}
function doWatchNonRecursive(file: { path: string, isDirectory: boolean }, onChange: (type: 'added' | 'changed' | 'deleted', path: string) => void, onError: (error: string) => void): IDisposable {
const mapPathToStatDisposable = new Map<string, IDisposable>();
let disposed = false;
@@ -746,7 +754,7 @@ export function watchNonRecursive(file: { path: string, isDirectory: boolean },
if (fileExists) {
onChange('changed', changedFilePath);
watcherDisposables = [watchNonRecursive(file, onChange, onError)];
watcherDisposables = [doWatchNonRecursive(file, onChange, onError)];
}
// File seems to be really gone, so emit a deleted event
@@ -10,7 +10,7 @@ import { IFileSystemProvider, FileSystemProviderCapabilities, IFileChange, IWatc
import { URI } from 'vs/base/common/uri';
import { Event, Emitter } from 'vs/base/common/event';
import { isLinux, isWindows } from 'vs/base/common/platform';
import { statLink, readdir, unlink, move, copy, readFile, writeFile, fileExists, truncate, rimraf, RimRafMode, watchNonRecursive } from 'vs/base/node/pfs';
import { statLink, readdir, unlink, move, copy, readFile, writeFile, fileExists, truncate, rimraf, RimRafMode, watchFolder, watchFile } from 'vs/base/node/pfs';
import { normalize, basename, dirname } from 'vs/base/common/path';
import { joinPath } from 'vs/base/common/resources';
import { isEqual } from 'vs/base/common/extpath';
@@ -413,12 +413,19 @@ export class DiskFileSystemProvider extends Disposable implements IFileSystemPro
return;
}
disposable = watchNonRecursive({ path: resource.fsPath, isDirectory: fileStat.type === FileType.Directory }, (eventType: 'added' | 'changed' | 'deleted', path: string) => {
this.onNonRecursiveFileChange({
type: eventType === 'changed' ? FileChangeType.UPDATED : eventType === 'added' ? FileChangeType.ADDED : FileChangeType.DELETED,
path
});
}, error => this.logService.error(error));
// Watch Folder
if (fileStat.type === FileType.Directory) {
disposable = watchFolder(resource.fsPath, (eventType, path) => {
this.onNonRecursiveFileChange({ type: eventType === 'changed' ? FileChangeType.UPDATED : eventType === 'added' ? FileChangeType.ADDED : FileChangeType.DELETED, path });
}, error => this.logService.error(error));
}
// Watch File
else {
disposable = watchFile(resource.fsPath, (eventType, path) => {
this.onNonRecursiveFileChange({ type: eventType === 'changed' ? FileChangeType.UPDATED : FileChangeType.DELETED, path });
}, error => this.logService.error(error));
}
}, error => this.logService.error(error));
return toDisposable(() => dispose(disposable));
@@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { watchNonRecursive } from 'vs/base/node/pfs';
import { watchFolder } from 'vs/base/node/pfs';
import { dirname, join } from 'vs/base/common/path';
import * as resources from 'vs/base/common/resources';
import { ITextModel } from 'vs/editor/common/model';
@@ -29,7 +29,7 @@ let callbacks: ((eventType: string, fileName?: string) => void)[] = [];
function watchOutputDirectory(outputDir: string, logService: ILogService, onChange: (eventType: 'added' | 'changed' | 'deleted', path: string) => void): IDisposable {
callbacks.push(onChange);
if (!watchingOutputDir) {
const watcherDisposable = watchNonRecursive({ path: outputDir, isDirectory: true }, (eventType, path) => {
const watcherDisposable = watchFolder(outputDir, (eventType, path) => {
for (const callback of callbacks) {
callback(eventType, path);
}