intuicio_framework_ecs/
observer.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use crate::{commands::CommandBuffer, entity::Entity, world::World, Component};
use intuicio_data::type_hash::TypeHash;
use std::collections::HashMap;

#[derive(Default)]
pub struct ChangeObserver {
    pub commands: CommandBuffer,
    #[allow(clippy::type_complexity)]
    on_added:
        HashMap<TypeHash, Vec<Box<dyn FnMut(&World, &mut CommandBuffer, Entity) + Send + Sync>>>,
    #[allow(clippy::type_complexity)]
    on_removed:
        HashMap<TypeHash, Vec<Box<dyn FnMut(&World, &mut CommandBuffer, Entity) + Send + Sync>>>,
    #[allow(clippy::type_complexity)]
    on_updated:
        HashMap<TypeHash, Vec<Box<dyn FnMut(&World, &mut CommandBuffer, Entity) + Send + Sync>>>,
}

impl ChangeObserver {
    pub fn on_added<T: Component>(
        &mut self,
        callback: impl FnMut(&World, &mut CommandBuffer, Entity) + Send + Sync + 'static,
    ) {
        self.on_added_raw(TypeHash::of::<T>(), callback);
    }

    pub fn on_added_raw(
        &mut self,
        type_hash: TypeHash,
        callback: impl FnMut(&World, &mut CommandBuffer, Entity) + Send + Sync + 'static,
    ) {
        self.on_added
            .entry(type_hash)
            .or_default()
            .push(Box::new(callback));
    }

    pub fn on_removed<T: Component>(
        &mut self,
        callback: impl FnMut(&World, &mut CommandBuffer, Entity) + Send + Sync + 'static,
    ) {
        self.on_removed_raw(TypeHash::of::<T>(), callback);
    }

    pub fn on_removed_raw(
        &mut self,
        type_hash: TypeHash,
        callback: impl FnMut(&World, &mut CommandBuffer, Entity) + Send + Sync + 'static,
    ) {
        self.on_removed
            .entry(type_hash)
            .or_default()
            .push(Box::new(callback));
    }

    pub fn on_updated<T: Component>(
        &mut self,
        callback: impl FnMut(&World, &mut CommandBuffer, Entity) + Send + Sync + 'static,
    ) {
        self.on_updated_raw(TypeHash::of::<T>(), callback);
    }

    pub fn on_updated_raw(
        &mut self,
        type_hash: TypeHash,
        callback: impl FnMut(&World, &mut CommandBuffer, Entity) + Send + Sync + 'static,
    ) {
        self.on_updated
            .entry(type_hash)
            .or_default()
            .push(Box::new(callback));
    }

    pub fn process(&mut self, world: &mut World) {
        for (entity, types) in world.added().iter() {
            for type_hash in types {
                if let Some(listeners) = self.on_added.get_mut(type_hash) {
                    for listener in listeners {
                        listener(world, &mut self.commands, entity);
                    }
                }
            }
        }
        if let Some(updated) = world.updated() {
            for (entity, types) in updated.iter() {
                for type_hash in types {
                    if let Some(listeners) = self.on_updated.get_mut(type_hash) {
                        for listener in listeners {
                            listener(world, &mut self.commands, entity);
                        }
                    }
                }
            }
        }
        for (entity, types) in world.removed().iter() {
            for type_hash in types {
                if let Some(listeners) = self.on_removed.get_mut(type_hash) {
                    for listener in listeners {
                        listener(world, &mut self.commands, entity);
                    }
                }
            }
        }
    }

    pub fn process_execute(&mut self, world: &mut World) {
        self.process(world);
        self.commands.execute(world);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{commands::DespawnCommand, world::World};
    use std::sync::{Arc, RwLock};

    #[test]
    fn test_async() {
        fn is_async<T: Send + Sync>() {}

        is_async::<ChangeObserver>();
    }

    #[test]
    fn test_change_observer() {
        #[derive(Debug, Clone, Copy, PartialEq, Eq)]
        enum Phase {
            None,
            Added,
            Updated,
            Removed,
        }

        let phase = Arc::new(RwLock::new(Phase::None));
        let phase1 = phase.clone();
        let phase2 = phase.clone();
        let phase3 = phase.clone();

        let mut observer = ChangeObserver::default();
        observer.on_added::<bool>(move |_, commands, entity| {
            let phase1 = phase1.clone();
            // normally you don't need to schedule this code, but it helps here in tests
            // to test for separate phases. Without it you go from None to Updated phase.
            commands.schedule(move |world| {
                let mut access = world.get::<true, bool>(entity, true).unwrap();
                let data = access.write().unwrap();
                *data = !*data;
                world.update::<bool>(entity);
                *phase1.write().unwrap() = Phase::Added;
            });
        });
        observer.on_updated::<bool>(move |_, commands, entity| {
            commands.command(DespawnCommand::new(entity));
            *phase2.write().unwrap() = Phase::Updated;
        });
        observer.on_removed::<bool>(move |_, _, _| {
            *phase3.write().unwrap() = Phase::Removed;
        });

        let mut world = World::default();
        let entity = world.spawn((false,)).unwrap();
        assert!(!*world
            .get::<true, bool>(entity, false)
            .unwrap()
            .read()
            .unwrap());
        assert_eq!(*phase.read().unwrap(), Phase::None);

        observer.process(&mut world);
        world.clear_changes();
        observer.commands.execute(&mut world);
        assert_eq!(*phase.read().unwrap(), Phase::Added);

        observer.process(&mut world);
        world.clear_changes();
        observer.commands.execute(&mut world);
        assert_eq!(*phase.read().unwrap(), Phase::Updated);

        observer.process(&mut world);
        world.clear_changes();
        observer.commands.execute(&mut world);
        assert_eq!(*phase.read().unwrap(), Phase::Removed);
    }
}