Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: restructure watcher backend events #1408

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion packages/metro-file-map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
},
"license": "MIT",
"dependencies": {
"anymatch": "^3.0.3",
"debug": "^2.2.0",
"fb-watchman": "^2.0.0",
"flow-enums-runtime": "^0.0.6",
Expand Down
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
14 changes: 14 additions & 0 deletions packages/metro-file-map/src/flow-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,20 @@ export type RawMockMap = Map<string, Path>;

export type ReadOnlyRawMockMap = $ReadOnlyMap<string, Path>;

export type WatcherBackendChangeEvent =
| $ReadOnly<{
event: 'change' | 'add',
relativePath: string,
root: string,
metadata: ChangeEventMetadata,
}>
| $ReadOnly<{
event: 'delete',
relativePath: string,
root: string,
metadata?: void,
}>;

export type WatchmanClockSpec =
| string
| $ReadOnly<{scm: $ReadOnly<{'mergebase-with': string}>}>;
Expand Down
59 changes: 28 additions & 31 deletions packages/metro-file-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @oncall react_native
*/

import type {

Check warning on line 12 in packages/metro-file-map/src/index.js

View workflow job for this annotation

GitHub Actions / Type check, lint, smoke test

Requires should be sorted alphabetically
BuildParameters,
BuildResult,
CacheData,
Expand All @@ -33,6 +33,7 @@
PerfLoggerFactory,
WatchmanClocks,
WorkerMetadata,
WatcherBackendChangeEvent,
} from './flow-types';
import type {IJestWorker} from 'jest-worker';

Expand Down Expand Up @@ -862,27 +863,23 @@
nextEmit = null;
};

const onChange = (
type: string,
filePath: Path,
root: Path,
metadata: ?ChangeEventMetadata,
) => {
const onChange = (change: WatcherBackendChangeEvent) => {
if (
metadata &&
change.metadata &&
// Ignore all directory events
(metadata.type === 'd' ||
(change.metadata.type === 'd' ||
// Ignore regular files with unwatched extensions
(metadata.type === 'f' && !hasWatchedExtension(filePath)) ||
(change.metadata.type === 'f' &&
!hasWatchedExtension(change.relativePath)) ||
// Don't emit events relating to symlinks if enableSymlinks: false
(!this._options.enableSymlinks && metadata?.type === 'l'))
(!this._options.enableSymlinks && change.metadata?.type === 'l'))
) {
return;
}

const absoluteFilePath = path.join(
root,
normalizePathSeparatorsToSystem(filePath),
change.root,
normalizePathSeparatorsToSystem(change.relativePath),
);

// Ignore files (including symlinks) whose path matches ignorePattern
Expand All @@ -899,11 +896,10 @@
// null, then it is assumed that the watcher does not have capabilities
// to detect modified time, and change processing proceeds.
if (
type === 'change' &&
change.event === 'change' &&
linkStats != null &&
metadata &&
metadata.modifiedTime != null &&
linkStats.modifiedTime === metadata.modifiedTime
change.metadata.modifiedTime != null &&
linkStats.modifiedTime === change.metadata.modifiedTime
) {
return;
}
Expand All @@ -917,14 +913,15 @@
nextEmit != null &&
nextEmit.eventsQueue.find(
event =>
event.type === type &&
event.type === change.event &&
event.filePath === absoluteFilePath &&
((!event.metadata && !metadata) ||
((!event.metadata && !change.metadata) ||
(event.metadata &&
metadata &&
change.metadata &&
event.metadata.modifiedTime != null &&
metadata.modifiedTime != null &&
event.metadata.modifiedTime === metadata.modifiedTime)),
change.metadata.modifiedTime != null &&
event.metadata.modifiedTime ===
change.metadata.modifiedTime)),
)
) {
return null;
Expand All @@ -936,7 +933,7 @@
const event = {
filePath: absoluteFilePath,
metadata,
type,
type: change.event,
};
if (nextEmit == null) {
nextEmit = {
Expand All @@ -963,19 +960,19 @@

// If the file was added or changed,
// parse it and update the haste map.
if (type === 'add' || type === 'change') {
if (change.event === 'add' || change.event === 'change') {
invariant(
metadata != null && metadata.size != null,
'since the file exists or changed, it should have metadata',
change.metadata.size != null,
'since the file exists or changed, it should have known size',
);
const fileMetadata: FileMetaData = [
'',
metadata.modifiedTime,
metadata.size,
change.metadata.modifiedTime,
change.metadata.size,
0,
'',
null,
metadata.type === 'l' ? 1 : 0,
change.metadata.type === 'l' ? 1 : 0,
];

try {
Expand All @@ -987,7 +984,7 @@
{forceInBand: true}, // No need to clean up workers
);
fileSystem.addOrModify(relativeFilePath, fileMetadata);
enqueueEvent(metadata);
enqueueEvent(change.metadata);
} catch (e) {
if (!['ENOENT', 'EACCESS'].includes(e.code)) {
throw e;
Expand All @@ -999,7 +996,7 @@
// event for it, and we'll clean up in the usual way at that
// point.
}
} else if (type === 'delete') {
} else if (change.event === 'delete') {
if (linkStats == null) {
// Don't emit deletion events for files we weren't retaining.
// This is expected for deletion of an ignored file.
Expand All @@ -1012,7 +1009,7 @@
});
} else {
throw new Error(
`metro-file-map: Unrecognized event type from watcher: ${type}`,
`metro-file-map: Unrecognized event type from watcher: ${change.event}`,
);
}
return null;
Expand Down
Loading
Loading