-
-
Notifications
You must be signed in to change notification settings - Fork 4k
ObserverTrigger: Add wrapper type around ComponentId
#19755
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
base: main
Are you sure you want to change the base?
ObserverTrigger: Add wrapper type around ComponentId
#19755
Conversation
Welcome, new contributor! Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨ |
ComponentId
092309b
to
ea87a3f
Compare
9e73ec7
to
abc49da
Compare
668c8bd
to
84eb8df
Compare
9b079d4
to
0addb40
Compare
1f1040e
to
fcecaa0
Compare
It looks like your PR is a breaking change, but you didn't provide a migration guide. Please review the instructions for writing migration guides, then expand or revise the content in the migration guides directory to reflect your changes. |
There's an existing observers migration guide; we should add the changes here to that :) |
fn register_component_id(world: &mut World) -> ComponentId { | ||
world.register_component::<EventWrapperComponent<Self>>() | ||
/// and should always correspond to the implementation of [`event_type`](Event::event_type). | ||
fn register_event_type(world: &mut World) -> EventKey { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that these names would be clearer and more consistent as register_event_key
/ event_key
.
/// # Warning | ||
/// | ||
/// This struct should only be instantiated internally to this crate. | ||
/// | ||
/// [`EventKey`]s are created in: | ||
/// - `crate::observer::trigger_dynamic_ref_with_caller` | ||
/// - `crate::observer::trigger_dynamic_ref_with_caller` | ||
/// - `crate::observer::observer_multiple_events` | ||
/// - `crate::observer::observer_dynamic_trigger` | ||
/// - `crate::observer::runner::hook_on_add` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// # Warning | |
/// | |
/// This struct should only be instantiated internally to this crate. | |
/// | |
/// [`EventKey`]s are created in: | |
/// - `crate::observer::trigger_dynamic_ref_with_caller` | |
/// - `crate::observer::trigger_dynamic_ref_with_caller` | |
/// - `crate::observer::observer_multiple_events` | |
/// - `crate::observer::observer_dynamic_trigger` | |
/// - `crate::observer::runner::hook_on_add` | |
/// A unique identifier for an [`Event`], used by [observers]. | |
/// | |
/// You can look up the key for your event by calling the [`Event::event_key`] method. | |
/// | |
/// [observers]: crate::observer |
We don't need to give a warning about non-public construction: Rust makes that impossible with correct usage of the visibility system. But we should give some docs about what this type is for and how to get it.
|
||
impl EventKey { | ||
/// Returns id of the underlying [`Event`]. | ||
/// Used internally in: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This list will be really hard to maintain. Better to leave this off, and allow users to use their IDEs go-to-uses instead.
pub struct EventKey(pub(crate) ComponentId); | ||
|
||
impl EventKey { | ||
/// Returns id of the underlying [`Event`]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Returns id of the underlying [`Event`]. | |
/// Returns the internal [`ComponentId`]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some small suggestions for names and docs that we should tackle, but this is looking much better. The clarity of this code is much improved!
Objective
Add a newtype to store events, as mentioned in this HackMD.
Solution
EventKey
struct that wraps around ComponentId.EventKey
and refactored all previous instances ofComponentId
in observer related functions.Testing
I tried three examples, whose behavior matches that of
main
:examples/observer.rs
examples/observer_propagation.rs
examples/ui/core_widgets.rs
There is an issue with
examples/observer_propagation.rs
, but this is also occurs inmain
: Warning: missing IDs inobserver_propagation
#19753.