Conversation
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.
|
Just ported my project to use this and it works great, thanks! I like the more flexible approach. |
|
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 func WithFilter(filter func(Model, Msg) Msg) ProgramOption {
return func(p *Program) {
p.filters = append(p.filters, filter)
}
} |
|
@meowgorithm in that case, do you think we would need/want/like to support things like |
Not sure if we need that right away, but this could be done by a proxy struct that wraps several other filters. |
|
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. |
|
yes, that would be my point... we can then wrap lists of filters in structs that implement a filter function or something like that |
|
Hi, any chance we could get this merged in? Thanks! |
WithFilterlets you supply an event filter that will be invoked before Bubble Tea processes atea.Msg. The event filter can return anytea.Msgwhich will then get handled by Bubble Tea instead of the original event. If the event filter returnsnil, 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.