1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { insert } from 'vs/base/common/arrays';
import { ThrottledDelayer } from 'vs/base/common/async';
import { onUnexpectedError } from 'vs/base/common/errors';
import { Emitter } from 'vs/base/common/event';
import { Disposable, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { normalize } from 'vs/base/common/path';
import { URI } from 'vs/base/common/uri';
import { IFileChange, IFileSystemProvider, IWatchOptions } from 'vs/platform/files/common/files';
import { AbstractNonRecursiveWatcherClient, AbstractUniversalWatcherClient, IDiskFileChange, ILogMessage, INonRecursiveWatchRequest, IRecursiveWatcherOptions, isRecursiveWatchRequest, IUniversalWatchRequest, toFileChanges } from 'vs/platform/files/common/watcher';
import { ILogService, LogLevel } from 'vs/platform/log/common/log';
export interface IDiskFileSystemProviderOptions {
watcher?: {
/**
* Extra options for the recursive file watching.
*/
recursive?: IRecursiveWatcherOptions;
/**
* Forces all file watch requests to run through a
* single universal file watcher, both recursive
* and non-recursively.
*
* Enabling this option might cause some overhead,
* specifically the universal file watcher will run
* in a separate process given its complexity. Only
* enable it when you understand the consequences.
*/
forceUniversal?: boolean;
};
}
export abstract class AbstractDiskFileSystemProvider extends Disposable implements
Pick<IFileSystemProvider, 'watch'>,
Pick<IFileSystemProvider, 'onDidChangeFile'>,
Pick<IFileSystemProvider, 'onDidWatchError'> {
constructor(
protected readonly logService: ILogService,
private readonly options?: IDiskFileSystemProviderOptions
) {
super();
}
protected readonly _onDidChangeFile = this._register(new Emitter<readonly IFileChange[]>());
readonly onDidChangeFile = this._onDidChangeFile.event;
protected readonly _onDidWatchError = this._register(new Emitter<string>());
readonly onDidWatchError = this._onDidWatchError.event;
watch(resource: URI, opts: IWatchOptions): IDisposable {
if (opts.recursive || this.options?.watcher?.forceUniversal) {
return this.watchUniversal(resource, opts);
}
return this.watchNonRecursive(resource, opts);
}
//#region File Watching (universal)
private universalWatcher: AbstractUniversalWatcherClient | undefined;
private readonly universalPathsToWatch: IUniversalWatchRequest[] = [];
private readonly universalWatchRequestDelayer = this._register(new ThrottledDelayer<void>(0));
private watchUniversal(resource: URI, opts: IWatchOptions): IDisposable {
// Add to list of paths to watch universally
const pathToWatch: IUniversalWatchRequest = { path: this.toFilePath(resource), excludes: opts.excludes, includes: opts.includes, recursive: opts.recursive };
const remove = insert(this.universalPathsToWatch, pathToWatch);
// Trigger update
this.refreshUniversalWatchers();
return toDisposable(() => {
// Remove from list of paths to watch universally
remove();
// Trigger update
this.refreshUniversalWatchers();
});
}
private refreshUniversalWatchers(): void {
// Buffer requests for universal watching to decide on right watcher
// that supports potentially watching more than one path at once
this.universalWatchRequestDelayer.trigger(() => {
return this.doRefreshUniversalWatchers();
}).catch(error => onUnexpectedError(error));
}
private doRefreshUniversalWatchers(): Promise<void> {
// Create watcher if this is the first time
if (!this.universalWatcher) {
this.universalWatcher = this._register(this.createUniversalWatcher(
changes => this._onDidChangeFile.fire(toFileChanges(changes)),
msg => this.onWatcherLogMessage(msg),
this.logService.getLevel() === LogLevel.Trace
));
// Apply log levels dynamically
this._register(this.logService.onDidChangeLogLevel(() => {
this.universalWatcher?.setVerboseLogging(this.logService.getLevel() === LogLevel.Trace);
}));
}
// Adjust for polling
const usePolling = this.options?.watcher?.recursive?.usePolling;
if (usePolling === true) {
for (const request of this.universalPathsToWatch) {
if (isRecursiveWatchRequest(request)) {
request.pollingInterval = this.options?.watcher?.recursive?.pollingInterval ?? 5000;
}
}
} else if (Array.isArray(usePolling)) {
for (const request of this.universalPathsToWatch) {
if (isRecursiveWatchRequest(request)) {
if (usePolling.includes(request.path)) {
request.pollingInterval = this.options?.watcher?.recursive?.pollingInterval ?? 5000;
}
}
}
}
// Ask to watch the provided paths
return this.universalWatcher.watch(this.universalPathsToWatch);
}
protected abstract createUniversalWatcher(
onChange: (changes: IDiskFileChange[]) => void,
onLogMessage: (msg: ILogMessage) => void,
verboseLogging: boolean
): AbstractUniversalWatcherClient;
//#endregion
//#region File Watching (non-recursive)
private nonRecursiveWatcher: AbstractNonRecursiveWatcherClient | undefined;
private readonly nonRecursivePathsToWatch: INonRecursiveWatchRequest[] = [];
private readonly nonRecursiveWatchRequestDelayer = this._register(new ThrottledDelayer<void>(0));
private watchNonRecursive(resource: URI, opts: IWatchOptions): IDisposable {
// Add to list of paths to watch non-recursively
const pathToWatch: INonRecursiveWatchRequest = { path: this.toFilePath(resource), excludes: opts.excludes, includes: opts.includes, recursive: false };
const remove = insert(this.nonRecursivePathsToWatch, pathToWatch);
// Trigger update
this.refreshNonRecursiveWatchers();
return toDisposable(() => {
// Remove from list of paths to watch non-recursively
remove();
// Trigger update
this.refreshNonRecursiveWatchers();
});
}
private refreshNonRecursiveWatchers(): void {
// Buffer requests for nonrecursive watching to decide on right watcher
// that supports potentially watching more than one path at once
this.nonRecursiveWatchRequestDelayer.trigger(() => {
return this.doRefreshNonRecursiveWatchers();
}).catch(error => onUnexpectedError(error));
}
private doRefreshNonRecursiveWatchers(): Promise<void> {
// Create watcher if this is the first time
if (!this.nonRecursiveWatcher) {
this.nonRecursiveWatcher = this._register(this.createNonRecursiveWatcher(
changes => this._onDidChangeFile.fire(toFileChanges(changes)),
msg => this.onWatcherLogMessage(msg),
this.logService.getLevel() === LogLevel.Trace
));
// Apply log levels dynamically
this._register(this.logService.onDidChangeLogLevel(() => {
this.nonRecursiveWatcher?.setVerboseLogging(this.logService.getLevel() === LogLevel.Trace);
}));
}
// Ask to watch the provided paths
return this.nonRecursiveWatcher.watch(this.nonRecursivePathsToWatch);
}
protected abstract createNonRecursiveWatcher(
onChange: (changes: IDiskFileChange[]) => void,
onLogMessage: (msg: ILogMessage) => void,
verboseLogging: boolean
): AbstractNonRecursiveWatcherClient;
//#endregion
private onWatcherLogMessage(msg: ILogMessage): void {
if (msg.type === 'error') {
this._onDidWatchError.fire(msg.message);
}
this.logService[msg.type](msg.message);
}
protected toFilePath(resource: URI): string {
return normalize(resource.fsPath);
}
}
|