Skip to content

Commit

Permalink
Flow types for WatchmanWatcher
Browse files Browse the repository at this point in the history
Summary:
- Expand `fb-watchman` libdefs to include the functionality used by `WatchmanWatcher`
 - Add types to `WatchmanWatcher` and its dependencies
 - Prefix private `WatchmanWatcher` methods with `_`

Motivated by symlink-related work on crawlers and watchers.

Reviewed By: jacdebug

Differential Revision: D39812529

fbshipit-source-id: 65339ac8a06b208f32b6bc629a6d2d53239ccd2c
  • Loading branch information
robhogan authored and facebook-github-bot committed Sep 27, 2022
1 parent cd2801d commit 556a4c1
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 113 deletions.
45 changes: 45 additions & 0 deletions flow-typed/fb-watchman.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,41 @@
*/

declare module 'fb-watchman' {
declare type WatchmanClockResponse = $ReadOnly<{
clock: string,
warning?: string,
...
}>;

declare type WatchmanSubscribeResponse = $ReadOnly<{
warning?: string,
...
}>;

declare type WatchmanWatchResponse = $ReadOnly<{
watch: string,
relative_path: string,
warning?: string,
...
}>;

declare type WatchmanFileChange = $ReadOnly<{
name: string,
exists: boolean,
new: boolean,
...
}>;

declare type WatchmanSubscriptionEvent = {
subscription: string,
is_fresh_instance: boolean,
files: $ReadOnlyArray<WatchmanFileChange>,
'state-enter'?: ?string,
'state-leave'?: ?string,
};

declare type WatchmanLogEvent = mixed;

declare type SavedStateInfo = $ReadOnly<{
'manifold-path': ?string,
'manifold-bucket': ?string,
Expand Down Expand Up @@ -103,6 +132,7 @@ declare module 'fb-watchman' {
}>;

declare type WatchmanQuery = {
defer?: $ReadOnlyArray<string>,
expression?: WatchmanExpression,
fields: $ReadOnlyArray<string>,
glob?: $ReadOnlyArray<string>,
Expand Down Expand Up @@ -142,7 +172,22 @@ declare module 'fb-watchman' {
config: ['find', string, string],
callback: (error: ?Error, response: WatchmanQueryResponse) => void,
): void;
command(
config: ['clock', string],
callback: (error: ?Error, response: WatchmanClockResponse) => void,
): void;
command(
config: ['subscribe', string, string, WatchmanQuery],
callback: (error: ?Error, response: WatchmanSubscribeResponse) => void,
): void;
end(): void;

on('connect', () => void): void;
on('end', () => void): void;
on('error', (error: Error) => void): void;
on('subscription', (event: WatchmanSubscriptionEvent) => void): void;
on('log', (event: WatchmanLogEvent) => void): void;
removeAllListeners: (eventName?: string) => void;
}

declare module.exports: {Client: Class<Client>};
Expand Down
5 changes: 3 additions & 2 deletions packages/metro-file-map/src/crawlers/watchman/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ module.exports = async function watchmanCrawl({
abortSignal?.addEventListener('abort', () => client.end());

let clientError;
// $FlowFixMe[prop-missing] - Client is not typed as an EventEmitter
client.on('error', error => (clientError = makeWatchmanError(error)));
client.on('error', error => {
clientError = makeWatchmanError(error);
});

let didLogWatchmanWaitMessage = false;

Expand Down
7 changes: 4 additions & 3 deletions packages/metro-file-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type {
SerializableModuleMap,
WorkerMetadata,
} from './flow-types';
import type {WatcherOptions} from './watchers/common';
import type {Stats} from 'graceful-fs';

import {DiskCacheManager} from './cache/DiskCacheManager';
Expand All @@ -47,7 +48,6 @@ import HasteModuleMap from './ModuleMap';
import FSEventsWatcher from './watchers/FSEventsWatcher';
// $FlowFixMe[untyped-import] - it's a fork: https://github.com/facebook/jest/pull/10919
import NodeWatcher from './watchers/NodeWatcher';
// $FlowFixMe[untyped-import] - WatchmanWatcher
import WatchmanWatcher from './watchers/WatchmanWatcher';
import {getSha1, worker} from './worker';
import EventEmitter from 'events';
Expand Down Expand Up @@ -879,7 +879,7 @@ export default class HasteMap extends EventEmitter {
let mustCopy = true;

const createWatcher = (root: Path): Promise<Watcher> => {
const watcher = new WatcherImpl(root, {
const watcherOptions: WatcherOptions = {
dot: true,
glob: [
// Ensure we always include package.json files, which are crucial for
Expand All @@ -889,7 +889,8 @@ export default class HasteMap extends EventEmitter {
],
ignored: ignorePattern,
watchmanDeferStates: this._options.watchmanDeferStates,
});
};
const watcher = new WatcherImpl(root, watcherOptions);

return new Promise((resolve, reject) => {
const rejectTimeout = setTimeout(
Expand Down
33 changes: 22 additions & 11 deletions packages/metro-file-map/src/watchers/RecrawlWarning.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
/**
* vendored from https://github.com/amasad/sane/blob/64ff3a870c42e84f744086884bf55a4f9c22d376/src/utils/recrawl-warning-dedupe.js
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
* @oncall react_native
*/

/**
* Originally vendored from
* https://github.com/amasad/sane/blob/64ff3a870c42e84f744086884bf55a4f9c22d376/src/utils/recrawl-warning-dedupe.js
*/

'use strict';

class RecrawlWarning {
constructor(root, count) {
export default class RecrawlWarning {
static RECRAWL_WARNINGS: Array<RecrawlWarning> = [];
static REGEXP: RegExp =
/Recrawled this watch (\d+) times, most recently because:\n([^:]+)/;

root: string;
count: number;

constructor(root: string, count: number) {
this.root = root;
this.count = count;
}

static findByRoot(root) {
static findByRoot(root: string): ?RecrawlWarning {
for (let i = 0; i < this.RECRAWL_WARNINGS.length; i++) {
const warning = this.RECRAWL_WARNINGS[i];
if (warning.root === root) {
Expand All @@ -23,7 +40,7 @@ class RecrawlWarning {
return undefined;
}

static isRecrawlWarningDupe(warningMessage) {
static isRecrawlWarningDupe(warningMessage: mixed): boolean {
if (typeof warningMessage !== 'string') {
return false;
}
Expand Down Expand Up @@ -52,9 +69,3 @@ class RecrawlWarning {
}
}
}

RecrawlWarning.RECRAWL_WARNINGS = [];
RecrawlWarning.REGEXP =
/Recrawled this watch (\d+) times, most recently because:\n([^:]+)/;

module.exports = RecrawlWarning;

0 comments on commit 556a4c1

Please sign in to comment.