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

add requireFseventsCallback option #602

Closed
wants to merge 5 commits into from

Conversation

vjpr
Copy link
Contributor

@vjpr vjpr commented May 8, 2017

First draft.

I wanted to make the smallest change possible.

I initialized var fsevents = {} to get around the 'use strict' restrictions of modifying uninitialised variables.

I chose a callback over an emitter because some of the options are updated depending on the result of FsEventsHandler.canUse() which require fsevents to have attempted to be required.

Not sure on the name of the option. Maybe its better as requireFseventsErrorCallback.

No tests at present but wanted to get feedback on approach first.

#599

@es128

@es128
Copy link
Contributor

es128 commented May 8, 2017

Yeah the name is clunky, but I don't have a better suggestion atm.

Approach LGTM.

index.js Outdated
@@ -82,6 +82,8 @@ function FSWatcher(_opts) {
// Enable fsevents on OS X when polling isn't explicitly enabled.
if (undef('useFsEvents')) opts.useFsEvents = !opts.usePolling;

FsEventsHandler.init(opts.requireFseventsCallback);
Copy link
Contributor

Choose a reason for hiding this comment

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

May as well put if (opts.useFsEvents) in front of this. It will add the advantage of sparing some initialization time for everyone else.

@vjpr
Copy link
Contributor Author

vjpr commented May 8, 2017

Maybe renaming from init to tryRequireFsEvents? Or tryInstantiateFsEvents? tryUseFsEvents?

@es128
Copy link
Contributor

es128 commented May 8, 2017

initFsEvents

@vjpr
Copy link
Contributor Author

vjpr commented May 8, 2017

canUse is used at the top-level scope, so maybe its better to initialise it in canUse?

Or I move the init function to here.

// Attach watch handler prototype methods
function importHandler(handler) {
  Object.keys(handler.prototype).forEach(function(method) {
    FSWatcher.prototype[method] = handler.prototype[method];
  });
}
importHandler(NodeFsHandler);
if (FsEventsHandler.canUse()) importHandler(FsEventsHandler);

Hmm, this becomes tricky, because we need access to those opts. I will attach the fsevents after we run the check in the constructor...

@vjpr
Copy link
Contributor Author

vjpr commented May 8, 2017

  • I have made the callback a Node errback which will report the resolve path of fsevents.
  • I have added an option to configure the resolved path to fsevents. If a package is symlinked, it will not be able to find the root project's fsevents (without using --preserve-symlinks). Allowing this option will mean consumers can have a way to manually locate fsevents.
  • I have moved importHandler inside FSWatcher.

@es128
Copy link
Contributor

es128 commented May 8, 2017

What just happened? Now it looks like kind of a mess.

What was so wrong with where you had it before aside from initializing the fsevents var with an empty object instead of leaving it undefined or some other falsy value?

@vjpr
Copy link
Contributor Author

vjpr commented May 8, 2017

What was so wrong with where you had it before aside from initializing the fsevents var with an empty object instead of leaving it undefined or some other falsy value?

See #602 (comment).

@vjpr
Copy link
Contributor Author

vjpr commented May 8, 2017

I'm not happy with splitting up the importHandler methods. Maybe we should move the other importHandler into the constructor. Or we just leave the FsEventsHandler methods on the class and don't do the canUse check.

@es128
Copy link
Contributor

es128 commented May 8, 2017

Sorry, I'm not understanding it right now. I'll need to try it locally when I get some time later to figure out why it couldn't have stayed simpler.

@vjpr
Copy link
Contributor Author

vjpr commented May 8, 2017

Without moving the importHandler it broke, because FsEventsHandler.canUse was being called in the top-level scope of index.js (when require('chokidar') is called) before opts are parsed.

Also, let me know if you would allow opts.fseventsPath.

The rationale is: if you npm link to webpack-dev-server (for example) for debugging (or a package that requires webpack-dev-server), unless you npm install fsevents in webpack-dev-server's dir, it will not use fsevents. And even if you do, your app would end up using two different instances of fsevents.

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.2%) to 97.201% when pulling 2c6a383 on vjpr:feature/599-fsevents-error into 3b1071a on paulmillr:master.

@coveralls
Copy link

coveralls commented May 10, 2017

Coverage Status

Coverage increased (+0.009%) to 98.455% when pulling 839b0b7 on vjpr:feature/599-fsevents-error into 3b1071a on paulmillr:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.2%) to 97.201% when pulling 2c6a383 on vjpr:feature/599-fsevents-error into 3b1071a on paulmillr:master.

@vjpr
Copy link
Contributor Author

vjpr commented May 11, 2017

Just ran into this issue once again with a silent Error: Module did not self-register. message.

Its tough, because cpu spikes to 120%, which could be caused by something else in the app, but now I always have to find and then add console.log to that try/catch block.

With the current solution, if library devs (such as webpack-dev-server) don't implement the hook, then I am stuck, and we don't help anyone.

I think we really need an environment variable. I would set this env var permanently in my ~/.bashrc on macOS so I could always get notified if fsevents wasn't used.

@coveralls
Copy link

coveralls commented May 11, 2017

Coverage Status

Coverage decreased (-0.1%) to 98.309% when pulling 1588e03 on vjpr:feature/599-fsevents-error into 3b1071a on paulmillr:master.

@coveralls
Copy link

coveralls commented May 11, 2017

Coverage Status

Coverage decreased (-0.4%) to 98.02% when pulling ba928b0 on vjpr:feature/599-fsevents-error into 3b1071a on paulmillr:master.

@es128
Copy link
Contributor

es128 commented May 11, 2017

I haven't had time to review closely, but I'm sorry to tell you that I think as this PR grows it becomes less likely to be accepted. A note in the readme reminding you to npm rebuild if you switch node versions may be where we end up if we can't get to any other effective solution that is also simple and small.

I suggest you focus this down to the one simple solution that will have a positive impact on your quality of life relating to this problem, within the constraints I've already described. Perhaps that's going back to before the other changes you've made and only adding something catch block to print a message ONLY WHEN the user has set a specific env variable set. Yes, that only covers users savvy enough to set the env var, but I haven't seen any other proposal that won't cause collateral problems/complaints.

@vjpr
Copy link
Contributor Author

vjpr commented May 11, 2017 via email

@es128 es128 closed this May 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants