Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
add dedicated try_wait method
Browse files Browse the repository at this point in the history
  • Loading branch information
wngr committed Dec 8, 2020
1 parent 365d004 commit e1e6037
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum Error {
NotImplementedError,
UnknownError,
Error(BoxedError),
TimeoutError,
}

impl From<BoxedError> for Error {
Expand All @@ -39,6 +40,7 @@ impl fmt::Display for Error {
NotImplementedError => write!(f, "Functionality is not implemented yet"),
UnknownError => write!(f, "Unknown error occurrred"),
Error(ref e) => write!(f, "Error: {}", e),
TimeoutError => write!(f, "Timeout"),
}
}
}
Expand Down Expand Up @@ -134,16 +136,17 @@ impl Application {
self.window.quit()
}

pub fn wait_for_message(&mut self, timeout: Option<Duration>) -> Result<(), Error> {
pub fn wait_for_message(&mut self) -> Result<(), Error> {
self.try_wait(Duration::new(u64::MAX, 0))
}

pub fn try_wait(&mut self, timeout: Duration) -> Result<(), Error> {
loop {
let msg;
match self
.rx
.recv_timeout(timeout.unwrap_or_else(|| Duration::new(u64::MAX, 0)))
{
match self.rx.recv_timeout(timeout) {
Ok(m) => msg = m,
// Yield and wait for the next poll
Err(RecvTimeoutError::Timeout) => break,
Err(RecvTimeoutError::Timeout) => return Err(Error::TimeoutError),
Err(RecvTimeoutError::Disconnected) => {
self.quit();
break;
Expand Down

0 comments on commit e1e6037

Please sign in to comment.