Skip to content

Commit

Permalink
fix: always return promise in close() (#1359)
Browse files Browse the repository at this point in the history
Updates the `close` method to always return a `Promise` rather than
having a mixed return type.

We do not need to check `closed` since we only care if the close
logic/promise has already started, so switching the condition should be
enough.
  • Loading branch information
43081j authored Sep 19, 2024
1 parent 9b15d99 commit 95aa953
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ export class FSWatcher extends EventEmitter {
add(paths_: Path | Path[], _origAdd?: string, _internal?: boolean): FSWatcher {
const { cwd } = this.options;
this.closed = false;
this._closePromise = undefined;
let paths = unifyPaths(paths_);
if (cwd) {
paths = paths.map((path) => {
Expand Down Expand Up @@ -524,13 +525,15 @@ export class FSWatcher extends EventEmitter {
/**
* Close watchers and remove all listeners from watched paths.
*/
close(): Promise<void> | undefined {
if (this.closed) return this._closePromise;
close(): Promise<void> {
if (this._closePromise) {
return this._closePromise;
}
this.closed = true;

// Memory management.
this.removeAllListeners();
const closers: any[] = [];
const closers: Array<Promise<void>> = [];
this._closers.forEach((closerList) =>
closerList.forEach((closer) => {
const promise = closer();
Expand Down

0 comments on commit 95aa953

Please sign in to comment.