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(emitter): add listenIf method #25

Merged
merged 2 commits into from
Aug 31, 2024
Merged

feat(emitter): add listenIf method #25

merged 2 commits into from
Aug 31, 2024

Conversation

RomainLanz
Copy link
Member

@RomainLanz RomainLanz commented Aug 30, 2024

Hey there! 👋🏻

This PR adds the listenIf method. This method allows listening to an event only if the condition returns true.

Instead of...

if (app.inDev) {
  emitter.on(...)
}

You can write:

emitter.listenIf(app.inDev, () => {})

It seems that TypeScript is not happy about the overloading when calling this.on(), I am not sure how to fix that.

@thetutlage
Copy link
Member

Yeah, one of those weirdo issues where TypeScript is struggling to match the correct overload. For you we can say this.on(event as any, listener)

@RomainLanz
Copy link
Member Author

Yup, I went with a @ts-expect-error. If TypeScript "fixes" this one day, we will have a warning and be able to remove the comments.

It is more elegant than casting event to any.

@thetutlage
Copy link
Member

Agree!

@thetutlage thetutlage merged commit 26d0153 into develop Aug 31, 2024
10 checks passed
@thetutlage thetutlage deleted the feat/listen-if branch August 31, 2024 08:43
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.

2 participants