Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Expose timeout parameter to the main event loop #1

Merged
merged 2 commits into from
Dec 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ pub mod api;
use std::{
collections::HashMap,
error, fmt,
sync::mpsc::{channel, Receiver},
sync::mpsc::{channel, Receiver, RecvTimeoutError},
time::Duration,
};

type BoxedError = Box<dyn error::Error + Send + Sync + 'static>;
Expand All @@ -15,6 +16,7 @@ pub enum Error {
NotImplementedError,
UnknownError,
Error(BoxedError),
TimeoutError,
}

impl From<BoxedError> for Error {
Expand All @@ -38,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,11 +137,17 @@ impl Application {
}

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() {
match self.rx.recv_timeout(timeout) {
Ok(m) => msg = m,
Err(_) => {
// Yield and wait for the next poll
Err(RecvTimeoutError::Timeout) => return Err(Error::TimeoutError),
Err(RecvTimeoutError::Disconnected) => {
self.quit();
break;
}
Expand Down