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

fix: remove jsdoc types and improve internal types #1356

Merged
merged 1 commit into from
Sep 18, 2024
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
17 changes: 11 additions & 6 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,14 @@ const FsWatchFileInstances = new Map();
* @param fullPath absolute path
* @param options options to be passed to fs_watchFile
* @param handlers container for event listener functions
* @returns {Function} closer
* @returns closer
*/
const setFsWatchFileListener = (path: Path, fullPath: Path, options: any, handlers: any) => {
const setFsWatchFileListener = (
path: Path,
fullPath: Path,
options: any,
handlers: any
): (() => void) => {
const { listener, rawEmitter } = handlers;
let cont = FsWatchFileInstances.get(fullPath);

Expand Down Expand Up @@ -400,9 +405,9 @@ export class NodeFsHandler {

/**
* Watch a file and emit add event if warranted.
* @returns {Function} closer for the watcher instance
* @returns closer for the watcher instance
*/
_handleFile(file: Path, stats: Stats, initialAdd: boolean) {
_handleFile(file: Path, stats: Stats, initialAdd: boolean): (() => void) | undefined {
if (this.fsw.closed) {
return;
}
Expand Down Expand Up @@ -615,7 +620,7 @@ export class NodeFsHandler {
* @param target child path targeted for watch
* @param wh Common watch helpers for this path
* @param realpath
* @returns {Promise<Function>} closer for the watcher instance.
* @returns closer for the watcher instance.
*/
async _handleDir(
dir: string,
Expand All @@ -625,7 +630,7 @@ export class NodeFsHandler {
target: string,
wh: WatchHelper,
realpath: string
) {
): Promise<(() => void) | undefined> {
const parentDir = this.fsw._getWatchedDir(sysPath.dirname(dir));
const tracked = parentDir.has(sysPath.basename(dir));
if (!(initialAdd && this.fsw.options.ignoreInitial) && !target && !tracked) {
Expand Down
34 changes: 18 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export type FSWInstanceOptions = BasicOpts & {
};

export type ThrottleType = 'readdir' | 'watch' | 'add' | 'remove' | 'change';
export type EmitArgs = [EventName, Path, any?, any?, any?];
export type EmitArgs = [EventName, Path | Error, any?, any?, any?];
export type MatchFunction = (val: string, stats?: Stats) => boolean;
export interface MatcherObject {
path: string;
Expand Down Expand Up @@ -213,8 +213,7 @@ class DirEntry {
constructor(dir: Path, removeWatcher: any) {
this.path = dir;
this._removeWatcher = removeWatcher;
/** @type {Set<Path>} */
this.items = new Set();
this.items = new Set<Path>();
}

add(item: string) {
Expand Down Expand Up @@ -277,7 +276,6 @@ export class WatchHelper {
this.path = path = path.replace(REPLACER_RE, '');
this.watchPath = watchPath;
this.fullWatchPath = sysPath.resolve(watchPath);
/** @type {object|boolean} */
this.dirParts = [];
this.dirParts.forEach((parts) => {
if (parts.length > 1) parts.pop();
Expand Down Expand Up @@ -525,9 +523,8 @@ export class FSWatcher extends EventEmitter {

/**
* Close watchers and remove all listeners from watched paths.
* @returns {Promise<void>}.
*/
close() {
close(): Promise<void> | undefined {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benmccann this._closePromise may be undefined, should we return undefined as well, or use a non-null assertion?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't get to look at this before it was merged, but to answer your question I think Promise<void> is the correct return type here and we should not return undefined. I've opened a new issue to discuss it: #1357

if (this.closed) return this._closePromise;
this.closed = true;

Expand Down Expand Up @@ -560,14 +557,13 @@ export class FSWatcher extends EventEmitter {

/**
* Expose list of watched paths
* @returns {Object} for chaining
* @returns for chaining
*/
getWatched() {
const watchList: Object = {};
getWatched(): Record<string, string[]> {
const watchList: Record<string, string[]> = {};
this._watched.forEach((entry, dir) => {
const key = this.options.cwd ? sysPath.relative(this.options.cwd, dir) : dir;
const index = key || ONE_DOT;
// @ts-ignore
watchList[index] = entry.getChildren().sort();
});
return watchList;
Expand Down Expand Up @@ -595,7 +591,6 @@ export class FSWatcher extends EventEmitter {
const opts = this.options;
if (isWindows) path = sysPath.normalize(path);
if (opts.cwd) path = sysPath.relative(opts.cwd, path);
/** @type Array<any> */
const args: EmitArgs = [event, path];
if (stats != null) args.push(stats);

Expand Down Expand Up @@ -631,7 +626,6 @@ export class FSWatcher extends EventEmitter {
const awfEmit = (err: Error, stats: Stats) => {
if (err) {
event = args[0] = EV.ERROR;
// @ts-ignore
args[1] = err;
this.emitWithAll(event, args);
} else if (stats) {
Expand Down Expand Up @@ -697,17 +691,25 @@ export class FSWatcher extends EventEmitter {
* @param actionType type being throttled
* @param path being acted upon
* @param timeout duration of time to suppress duplicate actions
* @returns {Object|false} tracking object or false if action should be suppressed
* @returns tracking object or false if action should be suppressed
*/
_throttle(actionType: ThrottleType, path: Path, timeout: number) {
_throttle(
actionType: ThrottleType,
path: Path,
timeout: number
):
| {
timeoutObject: any;
clear: () => any;
count: number;
}
| false {
if (!this._throttled.has(actionType)) {
this._throttled.set(actionType, new Map());
}

/** @type {Map<Path, Object>} */
const action = this._throttled.get(actionType);
if (!action) throw new Error('invalid throttle');
/** @type {Object} */
const actionPath = action.get(path);

if (actionPath) {
Expand Down
Loading