Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core-common/src/types/file-watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface IWatcher {
* @returns {Promise<void>}
* @memberof FileSystemWatcherServer
*/
unwatchFileChanges(uri: string): Promise<void>;
unwatchFileChanges(uri: string): void;

/**
* Update watcher file excludes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ export class RecursiveFileSystemWatcher extends Disposable implements IWatcher {
}
};

this.WATCHER_HANDLERS.set(uri, {
this.WATCHER_HANDLERS.set(watchPath, {
path: watchPath,
disposable: toDisposeWatcher,
handlers: [handler],
});

toDisposeWatcher.push(Disposable.create(() => this.WATCHER_HANDLERS.delete(uri)));
toDisposeWatcher.push(Disposable.create(() => this.WATCHER_HANDLERS.delete(watchPath)));
toDisposeWatcher.push(await this.start(watchPath, options));
this.addDispose(toDisposeWatcher);
}
Expand Down Expand Up @@ -350,14 +350,23 @@ export class RecursiveFileSystemWatcher extends Disposable implements IWatcher {
return disposables;
}

unwatchFileChanges(uri: string): Promise<void> {
this.logger.log('[Recursive] Un watch: ', uri);
const watcher = this.WATCHER_HANDLERS.get(uri);
private disposeWatcher(path: string) {
const watcher = this.WATCHER_HANDLERS.get(path);
if (watcher) {
this.WATCHER_HANDLERS.delete(uri);
watcher.disposable.dispose();
try {
watcher.disposable.dispose();
} catch (err) {
this.logger.error(`Dispose watcher failed for ${path}`, err);
} finally {
this.WATCHER_HANDLERS.delete(path);
}
}
return Promise.resolve();
}

unwatchFileChanges(uri: string): void {
this.logger.log('[Recursive] Un watch: ', uri);
const basePath = FileUri.fsPath(uri);
this.disposeWatcher(basePath);
}

setClient(client: FileSystemWatcherClient | undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class UnRecursiveFileSystemWatcher implements IWatcher {

protected client: FileSystemWatcherClient | undefined;

constructor(private readonly logger: ILogService) { }
constructor(private readonly logger: ILogService) {}

dispose(): void {
this.toDispose.dispose();
Expand Down Expand Up @@ -52,7 +52,9 @@ export class UnRecursiveFileSystemWatcher implements IWatcher {

// ๅผ€ๅง‹่ตฐ็›‘ๅฌๆต็จ‹
watcher.on('error', (code: number, signal: string) => {
this.logger.error(`[Un-Recursive] Failed to watch ${basePath} for changes using fs.watch() (${code}, ${signal})`);
this.logger.error(
`[Un-Recursive] Failed to watch ${basePath} for changes using fs.watch() (${code}, ${signal})`,
);
watcher.close();
});

Expand Down Expand Up @@ -157,7 +159,7 @@ export class UnRecursiveFileSystemWatcher implements IWatcher {
return disposables;
}

async unwatchFileChanges(uri: string): Promise<void> {
unwatchFileChanges(uri: string): void {
const basePath = FileUri.fsPath(uri);
if (this.watcherCollections.has(basePath)) {
const watcher = this.watcherCollections.get(basePath);
Expand Down
7 changes: 5 additions & 2 deletions packages/file-service/src/node/hosted/watcher.host.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export class WatcherHostServiceImpl implements IWatcherHostService {
for (const [_uri, { options, disposable }] of this.watcherCollection) {
this.logger.log('rewatch file changes: ', _uri, ' recursive: ', options?.recursive);
disposable.dispose();
this.watcherCollection.delete(_uri);
this.doWatch(Uri.parse(_uri), options);
}
}
Expand Down Expand Up @@ -135,8 +136,9 @@ export class WatcherHostServiceImpl implements IWatcherHostService {
await this.unrecursiveFileSystemWatcher!.watchFileChanges(uri.toString());

disposables.push(
Disposable.create(() => {
Disposable.create(async () => {
this.unrecursiveFileSystemWatcher!.unwatchFileChanges(uri.toString());
this.logger.log('dispose unrecursive watcher: ', uri.toString());
this.watchedDirs.delete(uri.toString());
this.WATCHER_HANDLERS.delete(watcherId);
}),
Expand All @@ -151,7 +153,8 @@ export class WatcherHostServiceImpl implements IWatcherHostService {
});

disposables.push(
Disposable.create(() => {
Disposable.create(async () => {
this.logger.log('dispose recursive watcher: ', uri.toString());
this.recursiveFileSystemWatcher!.unwatchFileChanges(uri.toString());
this.watchedDirs.delete(uri.toString());
this.WATCHER_HANDLERS.delete(watcherId);
Expand Down