Skip to content

Commit

Permalink
Increment last event count on next instead of iter (#2382)
Browse files Browse the repository at this point in the history
# Objective

Currently, simply calling `iter` on an event reader will mark all of it's events as read, even if the returned iterator is never used

## Solution

With this, the cursor will simply move to the last unread, but available event when iter is called, and incremented by one per `next` call.


Co-authored-by: Carter Anderson <mcanders1@gmail.com>
  • Loading branch information
TheRawMeatball and cart committed Feb 2, 2022
1 parent 6a499b1 commit ce752d2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 34 deletions.
48 changes: 14 additions & 34 deletions crates/bevy_ecs/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,13 @@ impl<T> Default for ManualEventReader<T> {

impl<T> ManualEventReader<T> {
/// See [`EventReader::iter`]
pub fn iter<'a>(&mut self, events: &'a Events<T>) -> impl DoubleEndedIterator<Item = &'a T> {
pub fn iter<'a>(&'a mut self, events: &'a Events<T>) -> impl DoubleEndedIterator<Item = &'a T> {
internal_event_reader(&mut self.last_event_count, events).map(|(e, _)| e)
}

/// See [`EventReader::iter_with_id`]
pub fn iter_with_id<'a>(
&mut self,
&'a mut self,
events: &'a Events<T>,
) -> impl DoubleEndedIterator<Item = (&'a T, EventId<T>)> {
internal_event_reader(&mut self.last_event_count, events)
Expand All @@ -210,7 +210,7 @@ impl<T> ManualEventReader<T> {
/// Like [`iter_with_id`](EventReader::iter_with_id) except not emitting any traces for read
/// messages.
fn internal_event_reader<'a, T>(
last_event_count: &mut usize,
last_event_count: &'a mut usize,
events: &'a Events<T>,
) -> impl DoubleEndedIterator<Item = (&'a T, EventId<T>)> {
// if the reader has seen some of the events in a buffer, find the proper index offset.
Expand All @@ -225,37 +225,17 @@ fn internal_event_reader<'a, T>(
} else {
0
};
*last_event_count = events.event_count;
match events.state {
State::A => events
.events_b
.get(b_index..)
.unwrap_or_else(|| &[])
.iter()
.map(map_instance_event_with_id)
.chain(
events
.events_a
.get(a_index..)
.unwrap_or_else(|| &[])
.iter()
.map(map_instance_event_with_id),
),
State::B => events
.events_a
.get(a_index..)
.unwrap_or_else(|| &[])
.iter()
.map(map_instance_event_with_id)
.chain(
events
.events_b
.get(b_index..)
.unwrap_or_else(|| &[])
.iter()
.map(map_instance_event_with_id),
),
}
let a = events.events_a.get(a_index..).unwrap_or_else(|| &[]);
let b = events.events_b.get(b_index..).unwrap_or_else(|| &[]);
let unread_count = a.len() + b.len();
*last_event_count = events.event_count - unread_count;
let iterator = match events.state {
State::A => b.iter().chain(a.iter()),
State::B => a.iter().chain(b.iter()),
};
iterator
.map(map_instance_event_with_id)
.inspect(move |(_, id)| *last_event_count = (id.id + 1).max(*last_event_count))
}

impl<'w, 's, T: Resource> EventReader<'w, 's, T> {
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_scene/src/scene_spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ pub fn scene_spawner_system(world: &mut World) {
.unwrap();

let mut updated_spawned_scenes = Vec::new();
let scene_spawner = &mut *scene_spawner;
for event in scene_spawner
.scene_asset_event_reader
.iter(scene_asset_events)
Expand Down

0 comments on commit ce752d2

Please sign in to comment.