-
-
Notifications
You must be signed in to change notification settings - Fork 586
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
feat: strongly type event emitter methods #1381
Conversation
Adds strong types to the event emitter interface such that `on` and similar methods can infer parameters. For example: ```ts // these are strongly typed and will give hints in editors etc watcher.on('ready', () => {}); watcher.on('error', (err) => {}); // this will fail watcher.on('nonsense', () => {}); ``` Fixes #1372
@@ -58,7 +59,8 @@ export type FSWInstanceOptions = BasicOpts & { | |||
}; | |||
|
|||
export type ThrottleType = 'readdir' | 'watch' | 'add' | 'remove' | 'change'; | |||
export type EmitArgs = [EventName, Path | Error, any?, any?, any?]; | |||
export type EmitArgs = [Path | Error, Stats?]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that OK for backwards compat?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I forgot to reply to this. We do export it so it's technically a breaking change to the types. But as far as I'm aware, it's not part of our "public API".
I couldn't find any usages of it in the wild. So I think it's really just an internal type exported for completeness
@@ -567,8 +580,8 @@ export class FSWatcher extends EventEmitter { | |||
} | |||
|
|||
emitWithAll(event: EventName, args: EmitArgs) { | |||
this.emit(...args); | |||
if (event !== EV.ERROR) this.emit(EV.ALL, ...args); | |||
this.emit(event, ...args); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
args
used to contain the event name too and we basically didn't make use of event
even though we passed it
I simplified so we use the event name and the args separately
We only had them combined for storing in the pending unlinks map. Everywhere else, we already had the name separately or didn't need it
i.e args[0] === event
in the existing code
I'm waiting on this PR to upgrade on some of my projects, is a release planned? |
cc @paulmillr i think we just need to cut a release of main, as we haven't done one in a while and have since merged a few things |
FYI, this was a breaking change for us. Fixed with hypermodeinc/modus@680f422 |
Why is the first argument error now @43081j ? |
For me, it was |
it was always this is because the generic code for emitting events can sometimes pass an error, and we should probably just add Lines 301 to 306 in 69c115a
as something like:
there may be other handlers in the same situation. a quick scan around tells me meanwhile, it seems like |
i've opened #1397 to at least fix the unlink events there may be other events but we can look into that separately |
Makes sense. Any suggestions for how to handle such errors? I think in my case, ignoring them is fine. Maybe some specific examples of the kinds of errors that could be passed would be helpful to others. Thanks. |
How so? It was my assumption from day one it's always |
i think its mostly in situations where i suspect you can mostly ignore them if that's the case (i.e. just skip your handler if it is an error) |
Can you produce a test example? |
its difficult to rep;roduce, but its basically here: Lines 637 to 651 in 69c115a
this gets called over time (polling) as it waits for the file size to settle it is checking the file size each time via a stat call. so if one of those calls fails, or the file has been removed, it will pass an error back here: Lines 770 to 773 in 69c115a
edit: i'm an idiot - it also sets the event type to |
this ill chop the code up a bit and see where i get to |
@paulmillr i've updated #1397 to try fix this, please take a look if you can |
Adds strong types to the event emitter interface such that
on
and similar methods can infer parameters.For example:
Fixes #1372
cc @paulmillr