Skip to content

Commit

Permalink
Fix segfault fsevents/fsevents#289.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Oct 31, 2019
1 parent 71d1073 commit 124ed20
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,15 +480,19 @@ unwatch(paths_) {

/**
* Close watchers and remove all listeners from watched paths.
* @returns {FSWatcher} for chaining.
* @returns {Promise<void>}.
*/
close() {
if (this.closed) return this;
this.closed = true;

// Memory management.
this.removeAllListeners();
this._closers.forEach(closerList => closerList.forEach(closer => closer()));
const closers = [];
this._closers.forEach(closerList => closerList.forEach(closer => {
const promise = closer();
if (promise instanceof Promise) closers.push(promise);
}));
this._streams.forEach(stream => stream.destroy());
this._userIgnored = undefined;
this._readyCount = 0;
Expand All @@ -497,7 +501,7 @@ close() {
['closers', 'watched', 'streams', 'symlinkPaths', 'throttled'].forEach(key => {
this[`_${key}`].clear();
});
return this;
return closers.length ? Promise.all(closers).then(() => undefined) : Promise.resolve();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2078,10 +2078,10 @@ describe('chokidar', function() {
currentDir = getFixturePath('');
});

afterEach(() => {
afterEach(async () => {
let watcher;
while ((watcher = allWatchers.pop())) {
watcher.close();
await watcher.close();
}
});

Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class FSWatcher extends EventEmitter implements fs.FSWatcher {
/**
* Removes all listeners from watched files.
*/
close(): void;
close(): Promise<void>;

on(event: 'add'|'addDir'|'change', listener: (path: string, stats?: fs.Stats) => void): this;

Expand Down

0 comments on commit 124ed20

Please sign in to comment.