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

fs: add 'close' event to FSWatcher #19900

Closed
wants to merge 3 commits into from
Closed

fs: add 'close' event to FSWatcher #19900

wants to merge 3 commits into from

Conversation

aleclarson
Copy link
Contributor

Should process.nextTick wrap this? Are tests necessary here?

  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added the fs Issues and PRs related to the fs subsystem / file system. label Apr 9, 2018
@mscdex
Copy link
Contributor

mscdex commented Apr 9, 2018

A test would be good, yes.

@cjihrig
Copy link
Contributor

cjihrig commented Apr 9, 2018

Docs are needed as well.

@jasnell jasnell requested a review from mcollina April 9, 2018 20:17
@mcollina
Copy link
Member

Why do you need a 'close'  event? I'm not against this, I just want to understand better.
close() seems to be a synchronous operation.

@aleclarson
Copy link
Contributor Author

aleclarson commented Apr 10, 2018

@mcollina To keep the close logic in the same function as fs.watch(). The watcher's close method is called from multiple places.

function watch(root) {
  return fs.watch(root, (evt, file) => {
    // ...
  }).on('close', () => {
    // ...
  })
}

Currently, I'm monkey-patching the close method.

function watch(root) {
  let watcher = fs.watch(root, (evt, file) => {
    // ...
  })
  let {close} = watcher
  watcher.close = function() {
    close.call(watcher)
    // ...
  }
  return watcher
}

@mcollina
Copy link
Member

@aleclarson definitely better. Can you add a unit test?

@aleclarson
Copy link
Contributor Author

Added a test and docs. Note the <!-- YAML added: v --> is missing.

@mcollina
Copy link
Member

@aleclarson the <!-- YAML added: v -->  should be added. Use REPLACEME as the version.

@@ -54,6 +54,10 @@ for (const testCase of cases) {
}
assert.fail(err);
});
let closeCount = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

You can drop the closeCount, and just write watcher.on('close', common.mustCall());

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does that protect against multiple calls?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes

@aleclarson
Copy link
Contributor Author

Good to go 👍

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

LGTM

lib/fs.js Outdated
@@ -1387,6 +1387,7 @@ FSWatcher.prototype.close = function() {
return;
}
this._handle.close();
this.emit('close');
Copy link
Member

Choose a reason for hiding this comment

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

Can you wrap this in a process.nextTick()? Zalgo and all that.

@BridgeAR
Copy link
Member

Ping @aleclarson

@aleclarson
Copy link
Contributor Author

Now wrapped with process.nextTick

@joyeecheung joyeecheung added the semver-minor PRs that contain new features and should be released in the next minor version. label Apr 12, 2018
@jasnell
Copy link
Member

jasnell commented Apr 14, 2018

@lpinca
Copy link
Member

lpinca commented Apr 16, 2018

Landed in 5cc948b.

@lpinca lpinca closed this Apr 16, 2018
lpinca pushed a commit that referenced this pull request Apr 16, 2018
PR-URL: #19900
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
jasnell pushed a commit that referenced this pull request Apr 16, 2018
PR-URL: #19900
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
BridgeAR pushed a commit to BridgeAR/node that referenced this pull request May 1, 2018
PR-URL: nodejs#19900
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fs Issues and PRs related to the fs subsystem / file system. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.