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

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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