diff --git a/src/handler.ts b/src/handler.ts index 2489a3fd..7bd12326 100644 --- a/src/handler.ts +++ b/src/handler.ts @@ -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); @@ -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; } @@ -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} closer for the watcher instance. + * @returns closer for the watcher instance. */ async _handleDir( dir: string, @@ -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) { diff --git a/src/index.ts b/src/index.ts index b0d56c23..5afd1ea6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; @@ -213,8 +213,7 @@ class DirEntry { constructor(dir: Path, removeWatcher: any) { this.path = dir; this._removeWatcher = removeWatcher; - /** @type {Set} */ - this.items = new Set(); + this.items = new Set(); } add(item: string) { @@ -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(); @@ -525,9 +523,8 @@ export class FSWatcher extends EventEmitter { /** * Close watchers and remove all listeners from watched paths. - * @returns {Promise}. */ - close() { + close(): Promise | undefined { if (this.closed) return this._closePromise; this.closed = true; @@ -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 { + const watchList: Record = {}; 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; @@ -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 */ const args: EmitArgs = [event, path]; if (stats != null) args.push(stats); @@ -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) { @@ -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} */ const action = this._throttled.get(actionType); if (!action) throw new Error('invalid throttle'); - /** @type {Object} */ const actionPath = action.get(path); if (actionPath) {