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

feat: strongly type event emitter methods #1381

Merged
merged 1 commit into from
Nov 1, 2024
Merged

feat: strongly type event emitter methods #1381

merged 1 commit into from
Nov 1, 2024

Conversation

43081j
Copy link
Collaborator

@43081j 43081j commented Oct 29, 2024

Adds strong types to the event emitter interface such that on and similar methods can infer parameters.

For example:

// 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

cc @paulmillr

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?];
Copy link
Owner

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?

Copy link
Collaborator Author

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);
Copy link
Owner

Choose a reason for hiding this comment

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

Huh?

Copy link
Collaborator Author

@43081j 43081j Oct 31, 2024

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

@paulmillr paulmillr merged commit 5e6daaa into main Nov 1, 2024
20 checks passed
@43081j 43081j deleted the event-types branch November 2, 2024 02:56
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.

Missing types for event callbacks
2 participants