Skip to content

Commit

Permalink
Watcher backends: use Flow-typed object payloads (#1411)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #1411

Ensure `'add'` and `'change'` events fired by watcher backends always have associated `metadata` (and that `'delete'` events never do), by typing change event payloads as objects with the `event: 'change' | 'add' | 'delete'` as a discriminator.

(This is in preparation for refactoring out the distinction between `'change'` and `'add'` from backends, which means they don't need to track files and can do much less IO)

Changelog: Internal

Reviewed By: hoxyq

Differential Revision: D67287046

fbshipit-source-id: 24773ec5e1a72657c16b9529aa988aa83da404a3
  • Loading branch information
robhogan authored and facebook-github-bot committed Dec 23, 2024
1 parent d0e630b commit f7343da
Show file tree
Hide file tree
Showing 8 changed files with 322 additions and 333 deletions.
47 changes: 16 additions & 31 deletions packages/metro-file-map/src/Watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
*/

import type {
ChangeEventMetadata,
Console,
CrawlerOptions,
FileData,
Path,
PerfLogger,
WatcherBackendChangeEvent,
WatchmanClocks,
} from './flow-types';
import type {WatcherOptions as WatcherBackendOptions} from './watchers/common';
Expand Down Expand Up @@ -163,14 +163,7 @@ export class Watcher extends EventEmitter {
}
}

async watch(
onChange: (
type: string,
filePath: string,
root: string,
metadata: ChangeEventMetadata,
) => void,
) {
async watch(onChange: (change: WatcherBackendChangeEvent) => void) {
const {extensions, ignorePattern, useWatchman} = this._options;

// WatchmanWatcher > FSEventsWatcher > sane.NodeWatcher
Expand Down Expand Up @@ -214,29 +207,21 @@ export class Watcher extends EventEmitter {

watcher.once('ready', () => {
clearTimeout(rejectTimeout);
watcher.on(
'all',
(
type: string,
filePath: string,
root: string,
metadata: ChangeEventMetadata,
) => {
const basename = path.basename(filePath);
if (basename.startsWith(this._options.healthCheckFilePrefix)) {
if (type === ADD_EVENT || type === CHANGE_EVENT) {
debug(
'Observed possible health check cookie: %s in %s',
filePath,
root,
);
this._handleHealthCheckObservation(basename);
}
return;
watcher.on('all', (change: WatcherBackendChangeEvent) => {
const basename = path.basename(change.relativePath);
if (basename.startsWith(this._options.healthCheckFilePrefix)) {
if (change.event === ADD_EVENT || change.event === CHANGE_EVENT) {
debug(
'Observed possible health check cookie: %s in %s',
change.relativePath,
root,
);
this._handleHealthCheckObservation(basename);
}
onChange(type, filePath, root, metadata);
},
);
return;
}
onChange(change);
});
resolve(watcher);
});
});
Expand Down
Loading

0 comments on commit f7343da

Please sign in to comment.