Skip to content

Commit

Permalink
No longer wait for a Tick signal to record events
Browse files Browse the repository at this point in the history
  • Loading branch information
garious committed May 9, 2018
1 parent 1d4d027 commit bfbee98
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/recorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use entry::{create_entry_mut, Entry};
use event::Event;
use hash::{hash, Hash};
use std::mem;
use std::sync::mpsc::{Receiver, SyncSender, TryRecvError};
use std::time::{Duration, Instant};

Expand All @@ -28,7 +27,6 @@ pub struct Recorder {
sender: SyncSender<Entry>,
receiver: Receiver<Signal>,
last_hash: Hash,
events: Vec<Event>,
num_hashes: u64,
num_ticks: u64,
}
Expand All @@ -39,7 +37,6 @@ impl Recorder {
receiver,
sender,
last_hash,
events: vec![],
num_hashes: 0,
num_ticks: 0,
}
Expand All @@ -50,8 +47,7 @@ impl Recorder {
self.num_hashes += 1;
}

pub fn record_entry(&mut self) -> Result<(), ExitReason> {
let events = mem::replace(&mut self.events, vec![]);
pub fn record_entry(&mut self, events: Vec<Event>) -> Result<(), ExitReason> {
let entry = create_entry_mut(&mut self.last_hash, &mut self.num_hashes, events);
self.sender
.send(entry)
Expand All @@ -67,19 +63,18 @@ impl Recorder {
loop {
if let Some(ms) = ms_per_tick {
if epoch.elapsed() > Duration::from_millis((self.num_ticks + 1) * ms) {
self.record_entry()?;
self.record_entry(vec![])?;
self.num_ticks += 1;
}
}

match self.receiver.try_recv() {
Ok(signal) => match signal {
Signal::Tick => {
self.record_entry()?;
self.record_entry(vec![])?;
}
Signal::Events(events) => {
self.events.extend(events);
self.record_entry()?;
self.record_entry(events)?;
}
},
Err(TryRecvError::Empty) => return Ok(()),
Expand Down

0 comments on commit bfbee98

Please sign in to comment.