diff --git a/crates/bevy_ecs/src/event.rs b/crates/bevy_ecs/src/event.rs index 889d9d22e8b4c..36113eb39dde4 100644 --- a/crates/bevy_ecs/src/event.rs +++ b/crates/bevy_ecs/src/event.rs @@ -328,14 +328,24 @@ impl Events { events.update(); } + #[inline] + fn reset_start_event_count(&mut self) { + self.a_start_event_count = self.event_count; + self.b_start_event_count = self.event_count; + } + /// Removes all events. + #[inline] pub fn clear(&mut self) { + self.reset_start_event_count(); self.events_a.clear(); self.events_b.clear(); } /// Creates a draining iterator that removes all events. pub fn drain(&mut self) -> impl Iterator + '_ { + self.reset_start_event_count(); + let map = |i: EventInstance| i.event; match self.state { State::A => self @@ -480,4 +490,40 @@ mod tests { ) -> Vec { reader.iter(events).cloned().collect::>() } + + #[derive(PartialEq, Eq, Debug)] + struct E(usize); + + fn events_clear_and_read_impl(clear_func: impl FnOnce(&mut Events)) { + let mut events = Events::::default(); + let mut reader = events.get_reader(); + + assert!(reader.iter(&events).next().is_none()); + + events.send(E(0)); + assert_eq!(*reader.iter(&events).next().unwrap(), E(0)); + assert_eq!(reader.iter(&events).next(), None); + + events.send(E(1)); + clear_func(&mut events); + assert!(reader.iter(&events).next().is_none()); + + events.send(E(2)); + events.update(); + events.send(E(3)); + + assert!(reader.iter(&events).eq([E(2), E(3)].iter())); + } + + #[test] + fn test_events_clear_and_read() { + events_clear_and_read_impl(|events| events.clear()); + } + + #[test] + fn test_events_drain_and_read() { + events_clear_and_read_impl(|events| { + assert!(events.drain().eq(vec![E(0), E(1)].into_iter())); + }); + } }