Skip to content
Merged
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
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- run: npm run lint
test:
strategy:
fail-fast: false
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [10.x, 12.x, 14.x, 16.x, 18.x, 20.x, 22.x, 24.x]
Expand Down Expand Up @@ -72,9 +72,9 @@ jobs:
architecture: ${{ steps.calculate_architecture.outputs.result }}
cache: "npm"
- run: npm install --ignore-engines
if: matrix.node-version == '10.x' || matrix.node-version == '12.x' || matrix.node-version == '14.x' || matrix.node-version == '16.x'
if: matrix.node-version == '10.x' || matrix.node-version == '12.x' || matrix.node-version == '14.x'
- run: npm ci
if: matrix.node-version != '10.x' && matrix.node-version != '12.x' && matrix.node-version != '14.x' && matrix.node-version != '16.x'
if: matrix.node-version != '10.x' && matrix.node-version != '12.x' && matrix.node-version != '14.x'
- run: npm run test:coverage
env:
WATCHPACK_POLLING: ${{ matrix.polling }}
Expand Down
17 changes: 12 additions & 5 deletions lib/DirectoryWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
"use strict";

const { EventEmitter } = require("events");
const fs = require("graceful-fs");
const path = require("path");
const fs = require("graceful-fs");

const watchEventSource = require("./watchEventSource");

/** @typedef {import("./index").NormalizedWatchOptions} NormalizedWatchOptions */
/** @typedef {import("./index").IgnoredFunction} IgnoredFunction */
/** @typedef {import("./index").EventType} EventType */
/** @typedef {import("./index").TimeInfoEntries} TimeInfoEntries */
/** @typedef {import("./index").Entry} Entry */
Expand Down Expand Up @@ -135,16 +135,23 @@ class Watcher extends EventEmitter {
* @property {() => void} closed closed event
*/

/**
* @typedef {object} DirectoryWatcherOptions
* @property {boolean=} followSymlinks true when need to resolve symlinks and watch symlink and real file, otherwise false
* @property {IgnoredFunction=} ignored ignore some files from watching (glob pattern or regexp)
* @property {number | boolean=} poll true when need to enable polling mode for watching, otherwise false
*/

/**
* @extends {EventEmitter<{ [K in keyof WatchpackEvents]: Parameters<WatchpackEvents[K]> }>}
*/
class DirectoryWatcher extends EventEmitter {
/**
* @param {WatcherManager} watcherManager a watcher manager
* @param {string} directoryPath directory path
* @param {NormalizedWatchOptions} options options
* @param {DirectoryWatcherOptions=} options options
*/
constructor(watcherManager, directoryPath, options) {
constructor(watcherManager, directoryPath, options = {}) {
super();
if (FORCE_POLLING) {
options.poll = FORCE_POLLING;
Expand Down Expand Up @@ -954,5 +961,5 @@ class DirectoryWatcher extends EventEmitter {
}

module.exports = DirectoryWatcher;
module.exports.Watcher = Watcher;
module.exports.EXISTANCE_ONLY_TIME_ENTRY = EXISTANCE_ONLY_TIME_ENTRY;
module.exports.Watcher = Watcher;
8 changes: 4 additions & 4 deletions lib/getWatcherManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
const path = require("path");
const DirectoryWatcher = require("./DirectoryWatcher");

/** @typedef {import("./index").NormalizedWatchOptions} NormalizedWatchOptions */
/** @typedef {import("./index").EventMap} EventMap */
/** @typedef {import("./DirectoryWatcher").DirectoryWatcherOptions} DirectoryWatcherOptions */
/** @typedef {import("./DirectoryWatcher").DirectoryWatcherEvents} DirectoryWatcherEvents */
/** @typedef {import("./DirectoryWatcher").FileWatcherEvents} FileWatcherEvents */
/**
Expand All @@ -18,9 +18,9 @@ const DirectoryWatcher = require("./DirectoryWatcher");

class WatcherManager {
/**
* @param {NormalizedWatchOptions} options options
* @param {DirectoryWatcherOptions=} options options
*/
constructor(options) {
constructor(options = {}) {
this.options = options;
/** @type {Map<string, DirectoryWatcher>} */
this.directoryWatchers = new Map();
Expand Down Expand Up @@ -67,7 +67,7 @@ class WatcherManager {
const watcherManagers = new WeakMap();

/**
* @param {NormalizedWatchOptions} options options
* @param {DirectoryWatcherOptions} options options
* @returns {WatcherManager} the watcher manager
*/
module.exports = (options) => {
Expand Down
11 changes: 6 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
*/
"use strict";

const getWatcherManager = require("./getWatcherManager");
const LinkResolver = require("./LinkResolver");
const { EventEmitter } = require("events");
const globToRegExp = require("glob-to-regexp");
const LinkResolver = require("./LinkResolver");
const getWatcherManager = require("./getWatcherManager");
const watchEventSource = require("./watchEventSource");

/** @typedef {import("./getWatcherManager").WatcherManager} WatcherManager */
/** @typedef {import("./DirectoryWatcher")} DirectoryWatcher */
/** @typedef {import("./DirectoryWatcher").DirectoryWatcherEvents} DirectoryWatcherEvents */
/** @typedef {import("./DirectoryWatcher").FileWatcherEvents} FileWatcherEvents */

// eslint-disable-next-line jsdoc/no-restricted-syntax
// eslint-disable-next-line jsdoc/reject-any-type
/** @typedef {Record<string, (...args: any[]) => any>} EventMap */

/**
Expand Down Expand Up @@ -45,6 +45,7 @@ const watchEventSource = require("./watchEventSource");
/** @typedef {`scan (${string})` | "change" | "rename" | `watch ${string}` | `directory-removed ${string}`} EventType */
/** @typedef {{ safeTime: number, timestamp: number, accuracy: number }} Entry */
/** @typedef {{ safeTime: number }} OnlySafeTimeEntry */
// eslint-disable-next-line jsdoc/ts-no-empty-object-type
/** @typedef {{}} ExistanceOnlyTimeEntry */
/** @typedef {Map<string, Entry | OnlySafeTimeEntry | ExistanceOnlyTimeEntry | null>} TimeInfoEntries */
/** @typedef {Set<string>} Changes */
Expand Down Expand Up @@ -239,9 +240,9 @@ class WatchpackDirectoryWatcher {
*/
class Watchpack extends EventEmitter {
/**
* @param {WatchOptions} options options
* @param {WatchOptions=} options options
*/
constructor(options) {
constructor(options = {}) {
super();
if (!options) options = {};
/** @type {WatchOptions} */
Expand Down
37 changes: 20 additions & 17 deletions lib/watchEventSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
*/
"use strict";

const { EventEmitter } = require("events");
const fs = require("fs");
const path = require("path");
const { EventEmitter } = require("events");
const reducePlan = require("./reducePlan");

/** @typedef {import("fs").FSWatcher} FSWatcher */
Expand Down Expand Up @@ -398,6 +398,25 @@ const execute = () => {
}
};

module.exports.Watcher = Watcher;

/**
* @param {() => void} fn a function
*/
module.exports.batch = (fn) => {
isBatch = true;
try {
fn();
} finally {
isBatch = false;
execute();
}
};

module.exports.createHandleChangeEvent = createHandleChangeEvent;

module.exports.getNumberOfWatchers = () => watcherCount;

/**
* @param {string} filePath a file path
* @returns {Watcher} watcher
Expand Down Expand Up @@ -427,20 +446,4 @@ module.exports.watch = (filePath) => {
return watcher;
};

/**
* @param {() => void} fn a function
*/
module.exports.batch = (fn) => {
isBatch = true;
try {
fn();
} finally {
isBatch = false;
execute();
}
};

module.exports.getNumberOfWatchers = () => watcherCount;
module.exports.createHandleChangeEvent = createHandleChangeEvent;
module.exports.watcherLimit = watcherLimit;
module.exports.Watcher = Watcher;
Loading