Skip to content

Commit

Permalink
chore: update nodejs watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaaaash committed Jan 8, 2025
1 parent c820a87 commit a48505b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ export class RecursiveFileSystemWatcher extends Disposable implements IWatcher {
}

private async doWatchFileChange(uri: string, options?: WatchOptions) {
if (this.WATCHER_HANDLERS.has(uri)) {
return;
}

const basePath = FileUri.fsPath(uri);
this.logger.log('[Recursive] watch file changes: ', uri);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs, { watch } from 'fs-extra';
import { upperFirst } from 'lodash';
import debounce from 'lodash/debounce';

import { ILogService } from '@opensumi/ide-core-common/lib/log';
Expand All @@ -10,16 +11,7 @@ import { shouldIgnorePath } from '../shared';
const { join, basename, normalize } = path;

export class UnRecursiveFileSystemWatcher implements IWatcher {
private WATCHER_HANDLERS = new Map<
string,
{
path: string;
handlers: any;
disposable: IDisposable;
}
>();

private static WATCHER_SEQUENCE = 1;
private watcherCollections: Map<string, fs.FSWatcher> = new Map();

private static readonly FILE_DELETE_HANDLER_DELAY = 500;

Expand All @@ -34,12 +26,13 @@ export class UnRecursiveFileSystemWatcher implements IWatcher {

dispose(): void {
this.toDispose.dispose();
this.WATCHER_HANDLERS.clear();
}

private async doWatch(basePath: string) {
try {
const watcher = watch(basePath);
this.watcherCollections.set(basePath, watcher);

this.logger.log('[Un-Recursive] start watching', basePath);
const isDirectory = fs.lstatSync(basePath).isDirectory();

Expand Down Expand Up @@ -135,6 +128,10 @@ export class UnRecursiveFileSystemWatcher implements IWatcher {
}

const realPath = await fs.realpath(basePath);
if (this.watcherCollections.has(realPath)) {
return disposables;
}

const tryWatchDir = async (retryDelay = 1000) => {
try {
this.doWatch(realPath);
Expand All @@ -149,13 +146,13 @@ export class UnRecursiveFileSystemWatcher implements IWatcher {
return disposables;
}

unwatchFileChanges(uri: string): Promise<void> {
const watcher = this.WATCHER_HANDLERS.get(uri);
if (watcher) {
this.WATCHER_HANDLERS.delete(uri);
watcher.disposable.dispose();
async unwatchFileChanges(uri: string): Promise<void> {
const basePath = FileUri.fsPath(uri);
if (this.watcherCollections.has(basePath)) {
const watcher = this.watcherCollections.get(basePath);
watcher?.close();
this.watcherCollections.delete(basePath);
}
return Promise.resolve();
}

protected pushAdded(path: string): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class WatcherHostServiceImpl implements IWatcherHostService {

checkIsAlreadyWatched(watcherPath: string): number | undefined {
for (const [watcherId, watcher] of this.WATCHER_HANDLERS) {
if (watcherPath.indexOf(watcher.path) === 0) {
if (watcherPath === watcher.path) {
return watcherId;
}
}
Expand Down

0 comments on commit a48505b

Please sign in to comment.