Skip to content

Commit

Permalink
FSEventsWatcher: use common recReadDir (#1409)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #1409

`FSEventsWatcher` has its own implementation of `recReaddir` which is almost identical to the one in `./common.js`, except that it wraps handlers in `path.normalize`.

There’s only one handler, so we can trivially remove this proxy abstraction and reuse the common `recReaddir`.

(The end goal here is to slim down and tidy up FSEventsWatcher ahead of using it as a basis for native recursive watching)

Changelog: Internal

Reviewed By: huntie

Differential Revision: D67286092

fbshipit-source-id: 8a72f5dead13b0eae25e03bcc998397332ed58e6
  • Loading branch information
robhogan authored and facebook-github-bot committed Dec 17, 2024
1 parent d26b18c commit 6d2ccbf
Showing 1 changed file with 3 additions and 34 deletions.
37 changes: 3 additions & 34 deletions packages/metro-file-map/src/watchers/FSEventsWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@
*/

import type {ChangeEventMetadata} from '../flow-types';
import type {Stats} from 'fs';
// $FlowFixMe[cannot-resolve-module] - Optional, Darwin only
// $FlowFixMe[untyped-type-import]
import type {FSEvents} from 'fsevents';

import {isIncluded, typeFromStat} from './common';
import {isIncluded, recReaddir, typeFromStat} from './common';
// $FlowFixMe[untyped-import] - anymatch
import anymatch from 'anymatch';
import EventEmitter from 'events';
import {promises as fsPromises} from 'fs';
import * as path from 'path';
// $FlowFixMe[untyped-import] - walker
import walker from 'walker';

const debug = require('debug')('Metro:FSEventsWatcher');

Expand Down Expand Up @@ -64,34 +61,6 @@ export default class FSEventsWatcher extends EventEmitter {
return fsevents != null;
}

static _normalizeProxy(
callback: (normalizedPath: string, stats: Stats) => void,
): (filepath: string, stats: Stats) => void {
return (filepath: string, stats: Stats): void =>
callback(path.normalize(filepath), stats);
}

static _recReaddir(
dir: string,
dirCallback: (normalizedPath: string, stats: Stats) => void,
fileCallback: (normalizedPath: string, stats: Stats) => void,
symlinkCallback: (normalizedPath: string, stats: Stats) => void,
endCallback: () => void,
// $FlowFixMe[unclear-type] Add types for callback
errorCallback: Function,
ignored: ?RegExp,
) {
walker(dir)
.filterDir(
(currentDir: string) => !ignored || !anymatch(ignored, currentDir),
)
.on('dir', FSEventsWatcher._normalizeProxy(dirCallback))
.on('file', FSEventsWatcher._normalizeProxy(fileCallback))
.on('symlink', FSEventsWatcher._normalizeProxy(symlinkCallback))
.on('error', errorCallback)
.on('end', endCallback);
}

constructor(
dir: string,
opts: $ReadOnly<{
Expand Down Expand Up @@ -126,10 +95,10 @@ export default class FSEventsWatcher extends EventEmitter {

this._tracked = new Set();
const trackPath = (filePath: string) => {
this._tracked.add(filePath);
this._tracked.add(path.normalize(filePath));
};
this.watcherInitialReaddirPromise = new Promise(resolve => {
FSEventsWatcher._recReaddir(
recReaddir(
this.root,
trackPath,
trackPath,
Expand Down

0 comments on commit 6d2ccbf

Please sign in to comment.