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: add generic event filter #536

Merged
merged 2 commits into from
Apr 17, 2023
Merged

feat: add generic event filter #536

merged 2 commits into from
Apr 17, 2023

Conversation

muesli
Copy link
Contributor

@muesli muesli commented Oct 13, 2022

WithFilter lets you supply an event filter that will be invoked before Bubble Tea processes a tea.Msg. The event filter can return any tea.Msg which will then get handled by Bubble Tea instead of the original event. If the event filter returns nil, the event will be ignored and Bubble Tea will not process it.

As an example, this could be used to prevent a program from shutting down if there are unsaved changes.

Based on the fantastic work by @aschey and supersedes #521.

Might make #114 more relevant.
Resolves #472.

@muesli muesli added the enhancement New feature or request label Oct 13, 2022
WithFilter lets you supply an event filter that will be invoked
before Bubble Tea processes a tea.Msg. The event filter can return
any tea.Msg which will then get handled by Bubble Tea instead of
the original event. If the event filter returns nil, the event
will be ignored and Bubble Tea will not process it.

As an example, this could be used to prevent a program from
shutting down if there are unsaved changes.

Based on the fantastic work by @aschey and supersedes #521.

Resolves #472.
@aschey
Copy link
Contributor

aschey commented Oct 13, 2022

Just ported my project to use this and it works great, thanks! I like the more flexible approach.

Copy link
Member

@caarlos0 caarlos0 left a comment

Choose a reason for hiding this comment

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

nice!

@meowgorithm
Copy link
Member

meowgorithm commented Oct 18, 2022

This is great, Muesli. My only thought (and this can come later per the implementation) do we want to allow multiple filters to be registered? It could allow for simpler filters, or potentially reusable filters.

NewProgram(WithFilter(funcA), WithFilter(funcB))

This would make the ProgramOption something like:

func WithFilter(filter func(Model, Msg) Msg) ProgramOption {
 	return func(p *Program) {
 		p.filters = append(p.filters, filter)
 	}
 }

@caarlos0
Copy link
Member

@meowgorithm in that case, do you think we would need/want/like to support things like AND and OR in the filters as well?

@muesli
Copy link
Contributor Author

muesli commented Oct 18, 2022

@meowgorithm in that case, do you think we would need/want/like to support things like AND and OR in the filters as well?

Not sure if we need that right away, but this could be done by a proxy struct that wraps several other filters.

@meowgorithm
Copy link
Member

Boolean operators and/or a proxy could be cool but we should probably merge this feature as-is and address those separately, especially since this feature is (so far) fairly niche.

@caarlos0
Copy link
Member

yes, that would be my point... we can then wrap lists of filters in structs that implement a filter function or something like that

@aschey
Copy link
Contributor

aschey commented Mar 13, 2023

Hi, any chance we could get this merged in? Thanks!

@muesli muesli merged commit c56884c into master Apr 17, 2023
@muesli muesli deleted the event-filter branch April 17, 2023 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a way to prevent tea.Quit from terminating the program
4 participants