Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use file service to listen to changes #57809

Merged
merged 2 commits into from
Sep 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/vs/platform/files/common/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */);
}

Expand Down
54 changes: 9 additions & 45 deletions src/vs/workbench/parts/output/electron-browser/outputServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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>;
Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add this to the list of Disposable: toDisposable(() => this.fileService.unwatchFileChanges....)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bpasero unwatch is called in dispose method.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sandy081 yeah its fine as long as watch and unwatch are always called in sync.

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;
}
Expand Down Expand Up @@ -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()));
}
Expand Down Expand Up @@ -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();
Expand Down