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

Add trigger_map to support triggering mapped events #14664

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

cart
Copy link
Member

@cart cart commented Aug 8, 2024

Objective

Alternative to the suggestion in #14649

Sometimes people want observers to watch for multiple events instead of just one. We should provide some mechanism for this to happen.

Solution

Add a trigger_map function, which returns an ObserverSystem that will use a given map function to trigger a mapped event when a certain event type is triggered:

#[derive(Event, Clone, Debug)]
struct Foo(usize);

#[derive(Event, Debug)]
struct Bar(bool);

#[derive(Event, Debug)]
enum Combined {
    Foo(Foo),
    Bar(Bar),
}

app
    .observe(trigger_map(Combined::Foo))
    .observe(|trigger: Trigger<Combined>| {
        println!("Combined Event: {:?}", trigger.event());
    });

@cart cart added A-ECS Entities, components, systems, and events C-Feature A new feature, making something new possible labels Aug 8, 2024
@alice-i-cecile alice-i-cecile added M-Needs-Release-Note Work that should be called out in the blog due to impact D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Aug 8, 2024
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

A very elegant solution for Or-style logic. I've left a small docs suggestion that I think will help clarify the usage.

@ItsDoot
Copy link
Contributor

ItsDoot commented Aug 9, 2024

Would this work well with bubbling since it's being converted into a different event type? I'm imaging a scenario where someone could end up with this in a hierarchy:

[Observer<Foo>] ---> [Observer<Combined>] ---> [Observer<Foo>]

@ItsDoot
Copy link
Contributor

ItsDoot commented Aug 9, 2024

Alternative here: #14674

@BenjaminBrienen BenjaminBrienen added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Feature A new feature, making something new possible D-Straightforward Simple bug fixes and API improvements, docs, test and examples M-Needs-Release-Note Work that should be called out in the blog due to impact S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants