Skip to content

Commit

Permalink
Merge pull request #1356 from talentlessguy/remove-type-fromjsdoc
Browse files Browse the repository at this point in the history
fix: remove jsdoc types and improve internal types
  • Loading branch information
paulmillr authored Sep 18, 2024
2 parents 27fc5c5 + f4d8ad0 commit 9b15d99
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
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 {
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

0 comments on commit 9b15d99

Please sign in to comment.