Increment last event count on next instead of iter (#2382)

# 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>
This commit is contained in:
TheRawMeatball 2022-02-02 02:29:33 +00:00
parent 6a499b125b
commit ce752d2522
2 changed files with 15 additions and 34 deletions

View file

@ -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)
@ -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.
@ -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> {

View file

@ -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)