未验证 提交 ce4d70b4 编写于 作者: S Sandeep Somavarapu 提交者: GitHub

Merge pull request #57809 from Microsoft/sandy081/40533

Use file service to listen to changes
......@@ -284,22 +284,24 @@ export class FileChangesEvent {
}
/**
* Returns true if this change event contains the provided file with the given change type. In case of
* Returns true if this change event contains the provided file with the given change type (if provided). In case of
* type DELETED, this method will also return true if a folder got deleted that is the parent of the
* provided file path.
*/
contains(resource: URI, type: FileChangeType): boolean {
contains(resource: URI, type?: FileChangeType): boolean {
if (!resource) {
return false;
}
const checkForChangeType = !isUndefinedOrNull(type);
return this._changes.some(change => {
if (change.type !== type) {
if (checkForChangeType && change.type !== type) {
return false;
}
// For deleted also return true when deleted folder is parent of target path
if (type === FileChangeType.DELETED) {
if (change.type === FileChangeType.DELETED) {
return isEqualOrParent(resource, change.resource, !isLinux /* ignorecase */);
}
......
......@@ -6,7 +6,6 @@
import * as nls from 'vs/nls';
import * as paths from 'vs/base/common/paths';
import * as extfs from 'vs/base/node/extfs';
import * as fs from 'fs';
import { TPromise } from 'vs/base/common/winjs.base';
import { Event, Emitter } from 'vs/base/common/event';
import { URI } from 'vs/base/common/uri';
......@@ -67,46 +66,6 @@ function watchOutputDirectory(outputDir: string, logService: ILogService, onChan
return toDisposable(() => { });
}
const fileWatchers: Map<string, any[]> = new Map<string, any[]>();
function watchFile(file: string, callback: () => void): IDisposable {
const onFileChange = (file: string) => {
for (const callback of fileWatchers.get(file)) {
callback();
}
};
let callbacks = fileWatchers.get(file);
if (!callbacks) {
callbacks = [];
fileWatchers.set(file, callbacks);
fs.watchFile(file, { interval: 1000 }, (current, previous) => {
if ((previous && !current) || (!previous && !current)) {
onFileChange(file);
return;
}
if (previous && current && previous.mtime !== current.mtime) {
onFileChange(file);
return;
}
});
}
callbacks.push(callback);
return toDisposable(() => {
let allCallbacks = fileWatchers.get(file);
allCallbacks.splice(allCallbacks.indexOf(callback), 1);
if (!allCallbacks.length) {
fs.unwatchFile(file);
fileWatchers.delete(file);
}
});
}
function unWatchAllFiles(): void {
fileWatchers.forEach((value, file) => fs.unwatchFile(file));
fileWatchers.clear();
}
interface OutputChannel extends IOutputChannel {
readonly file: URI;
readonly onDidAppendedContent: Event<void>;
......@@ -323,19 +282,26 @@ class OutputFileListener extends Disposable {
constructor(
private readonly file: URI,
private readonly fileService: IFileService
) {
super();
}
watch(): void {
if (!this.watching) {
this.disposables.push(watchFile(this.file.fsPath, () => this._onDidChange.fire()));
this.fileService.watchFileChanges(this.file);
this.disposables.push(this.fileService.onFileChanges(e => {
if (e.contains(this.file)) {
this._onDidChange.fire();
}
}));
this.watching = true;
}
}
unwatch(): void {
if (this.watching) {
this.fileService.unwatchFileChanges(this.file);
this.disposables = dispose(this.disposables);
this.watching = false;
}
......@@ -366,7 +332,7 @@ class FileOutputChannel extends AbstractFileOutputChannel implements OutputChann
) {
super(outputChannelIdentifier, modelUri, LOG_MIME, fileService, modelService, modeService);
this.fileHandler = this._register(new OutputFileListener(this.file));
this.fileHandler = this._register(new OutputFileListener(this.file, this.fileService));
this._register(this.fileHandler.onDidContentChange(() => this.onDidContentChange()));
this._register(toDisposable(() => this.fileHandler.unwatch()));
}
......@@ -458,8 +424,6 @@ export class OutputService extends Disposable implements IOutputService, ITextMo
panelService.onDidPanelOpen(this.onDidPanelOpen, this);
panelService.onDidPanelClose(this.onDidPanelClose, this);
this._register(toDisposable(() => unWatchAllFiles()));
// Set active channel to first channel if not set
if (!this.activeChannel) {
const channels = this.getChannels();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册