From d2e45034c118c103a09b45c56bd31ebb7fe5dfd5 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Tue, 2 Apr 2019 17:55:46 +0200 Subject: [PATCH] files2 - separate methods for watchFile/Folder --- src/vs/base/node/config.ts | 11 +++++----- src/vs/base/node/pfs.ts | 12 +++++++++-- .../files2/node/diskFileSystemProvider.ts | 21 ++++++++++++------- .../output/node/outputChannelModelService.ts | 4 ++-- 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/vs/base/node/config.ts b/src/vs/base/node/config.ts index b81922190de1..b477e2f57683 100644 --- a/src/vs/base/node/config.ts +++ b/src/vs/base/node/config.ts @@ -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 { config: T; @@ -146,10 +146,11 @@ export class ConfigWatcher implements IConfigWatcher, 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 { diff --git a/src/vs/base/node/pfs.ts b/src/vs/base/node/pfs.ts index 092645fc8bbd..3ea892414498 100644 --- a/src/vs/base/node/pfs.ts +++ b/src/vs/base/node/pfs.ts @@ -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(); 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 diff --git a/src/vs/workbench/services/files2/node/diskFileSystemProvider.ts b/src/vs/workbench/services/files2/node/diskFileSystemProvider.ts index be2dba91d9e4..f424d251a008 100644 --- a/src/vs/workbench/services/files2/node/diskFileSystemProvider.ts +++ b/src/vs/workbench/services/files2/node/diskFileSystemProvider.ts @@ -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)); diff --git a/src/vs/workbench/services/output/node/outputChannelModelService.ts b/src/vs/workbench/services/output/node/outputChannelModelService.ts index 3adebed308c9..06fe8f49dd1d 100644 --- a/src/vs/workbench/services/output/node/outputChannelModelService.ts +++ b/src/vs/workbench/services/output/node/outputChannelModelService.ts @@ -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); }