Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_ecs/src/observer/centralized_storage.rs
6604 views
1
//! Centralized storage for observers, allowing for efficient look-ups.
2
//!
3
//! This has multiple levels:
4
//! - [`World::observers`] provides access to [`Observers`], which is a central storage for all observers.
5
//! - [`Observers`] contains multiple distinct caches in the form of [`CachedObservers`].
6
//! - Most observers are looked up by the [`ComponentId`] of the event they are observing
7
//! - Lifecycle observers have their own fields to save lookups.
8
//! - [`CachedObservers`] contains maps of [`ObserverRunner`]s, which are the actual functions that will be run when the observer is triggered.
9
//! - These are split by target type, in order to allow for different lookup strategies.
10
//! - [`CachedComponentObservers`] is one of these maps, which contains observers that are specifically targeted at a component.
11
12
use bevy_platform::collections::HashMap;
13
14
use crate::{
15
archetype::ArchetypeFlags, component::ComponentId, entity::EntityHashMap,
16
observer::ObserverRunner, prelude::*,
17
};
18
19
/// An internal lookup table tracking all of the observers in the world.
20
///
21
/// Stores a cache mapping event ids to their registered observers.
22
/// Some observer kinds (like [lifecycle](crate::lifecycle) observers) have a dedicated field,
23
/// saving lookups for the most common triggers.
24
///
25
/// This can be accessed via [`World::observers`].
26
#[derive(Default, Debug)]
27
pub struct Observers {
28
// Cached ECS observers to save a lookup for high-traffic built-in event types.
29
add: CachedObservers,
30
insert: CachedObservers,
31
replace: CachedObservers,
32
remove: CachedObservers,
33
despawn: CachedObservers,
34
// Map from event type to set of observers watching for that event
35
cache: HashMap<EventKey, CachedObservers>,
36
}
37
38
impl Observers {
39
pub(crate) fn get_observers_mut(&mut self, event_key: EventKey) -> &mut CachedObservers {
40
use crate::lifecycle::*;
41
42
match event_key {
43
ADD => &mut self.add,
44
INSERT => &mut self.insert,
45
REPLACE => &mut self.replace,
46
REMOVE => &mut self.remove,
47
DESPAWN => &mut self.despawn,
48
_ => self.cache.entry(event_key).or_default(),
49
}
50
}
51
52
/// Attempts to get the observers for the given `event_key`.
53
///
54
/// When accessing the observers for lifecycle events, such as [`Add`], [`Insert`], [`Replace`], [`Remove`], and [`Despawn`],
55
/// use the [`EventKey`] constants from the [`lifecycle`](crate::lifecycle) module.
56
pub fn try_get_observers(&self, event_key: EventKey) -> Option<&CachedObservers> {
57
use crate::lifecycle::*;
58
59
match event_key {
60
ADD => Some(&self.add),
61
INSERT => Some(&self.insert),
62
REPLACE => Some(&self.replace),
63
REMOVE => Some(&self.remove),
64
DESPAWN => Some(&self.despawn),
65
_ => self.cache.get(&event_key),
66
}
67
}
68
69
pub(crate) fn is_archetype_cached(event_key: EventKey) -> Option<ArchetypeFlags> {
70
use crate::lifecycle::*;
71
72
match event_key {
73
ADD => Some(ArchetypeFlags::ON_ADD_OBSERVER),
74
INSERT => Some(ArchetypeFlags::ON_INSERT_OBSERVER),
75
REPLACE => Some(ArchetypeFlags::ON_REPLACE_OBSERVER),
76
REMOVE => Some(ArchetypeFlags::ON_REMOVE_OBSERVER),
77
DESPAWN => Some(ArchetypeFlags::ON_DESPAWN_OBSERVER),
78
_ => None,
79
}
80
}
81
82
pub(crate) fn update_archetype_flags(
83
&self,
84
component_id: ComponentId,
85
flags: &mut ArchetypeFlags,
86
) {
87
if self.add.component_observers.contains_key(&component_id) {
88
flags.insert(ArchetypeFlags::ON_ADD_OBSERVER);
89
}
90
91
if self.insert.component_observers.contains_key(&component_id) {
92
flags.insert(ArchetypeFlags::ON_INSERT_OBSERVER);
93
}
94
95
if self.replace.component_observers.contains_key(&component_id) {
96
flags.insert(ArchetypeFlags::ON_REPLACE_OBSERVER);
97
}
98
99
if self.remove.component_observers.contains_key(&component_id) {
100
flags.insert(ArchetypeFlags::ON_REMOVE_OBSERVER);
101
}
102
103
if self.despawn.component_observers.contains_key(&component_id) {
104
flags.insert(ArchetypeFlags::ON_DESPAWN_OBSERVER);
105
}
106
}
107
}
108
109
/// Collection of [`ObserverRunner`] for [`Observer`] registered to a particular event.
110
///
111
/// This is stored inside of [`Observers`], specialized for each kind of observer.
112
#[derive(Default, Debug)]
113
pub struct CachedObservers {
114
/// Observers watching for any time this event is triggered, regardless of target.
115
/// These will also respond to events targeting specific components or entities
116
pub(super) global_observers: ObserverMap,
117
/// Observers watching for triggers of events for a specific component
118
pub(super) component_observers: HashMap<ComponentId, CachedComponentObservers>,
119
/// Observers watching for triggers of events for a specific entity
120
pub(super) entity_observers: EntityHashMap<ObserverMap>,
121
}
122
123
impl CachedObservers {
124
/// Observers watching for any time this event is triggered, regardless of target.
125
/// These will also respond to events targeting specific components or entities
126
pub fn global_observers(&self) -> &ObserverMap {
127
&self.global_observers
128
}
129
130
/// Returns observers watching for triggers of events for a specific component.
131
pub fn component_observers(&self) -> &HashMap<ComponentId, CachedComponentObservers> {
132
&self.component_observers
133
}
134
135
/// Returns observers watching for triggers of events for a specific entity.
136
pub fn entity_observers(&self) -> &EntityHashMap<ObserverMap> {
137
&self.entity_observers
138
}
139
}
140
141
/// Map between an observer entity and its [`ObserverRunner`]
142
pub type ObserverMap = EntityHashMap<ObserverRunner>;
143
144
/// Collection of [`ObserverRunner`] for [`Observer`] registered to a particular event targeted at a specific component.
145
///
146
/// This is stored inside of [`CachedObservers`].
147
#[derive(Default, Debug)]
148
pub struct CachedComponentObservers {
149
// Observers watching for events targeting this component, but not a specific entity
150
pub(super) global_observers: ObserverMap,
151
// Observers watching for events targeting this component on a specific entity
152
pub(super) entity_component_observers: EntityHashMap<ObserverMap>,
153
}
154
155
impl CachedComponentObservers {
156
/// Returns observers watching for events targeting this component, but not a specific entity
157
pub fn global_observers(&self) -> &ObserverMap {
158
&self.global_observers
159
}
160
161
/// Returns observers watching for events targeting this component on a specific entity
162
pub fn entity_component_observers(&self) -> &EntityHashMap<ObserverMap> {
163
&self.entity_component_observers
164
}
165
}
166
167