summaryrefslogtreecommitdiffstats
path: root/src/action.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-12-07 02:41:28 -0500
committerJesse Luehrs <doy@tozt.net>2021-12-07 02:41:28 -0500
commit932a16d8fe617821a40332ec32ed9d5f682ca0dc (patch)
treeff4d71f96113c5f5c3faf52be87b4c8fe192b9c3 /src/action.rs
parent611e2bde934fccf0b66df089965d18051c76d6a0 (diff)
downloadnbsh-932a16d8fe617821a40332ec32ed9d5f682ca0dc.tar.gz
nbsh-932a16d8fe617821a40332ec32ed9d5f682ca0dc.zip
simplify
Diffstat (limited to 'src/action.rs')
-rw-r--r--src/action.rs56
1 files changed, 28 insertions, 28 deletions
diff --git a/src/action.rs b/src/action.rs
index bb8958f..1c6f2f3 100644
--- a/src/action.rs
+++ b/src/action.rs
@@ -10,25 +10,44 @@ pub enum Action {
Quit,
}
-pub struct Debouncer {
+pub struct Reader {
pending: async_std::sync::Mutex<Pending>,
cvar: async_std::sync::Condvar,
}
-impl Debouncer {
+impl Reader {
+ pub fn new(
+ input: async_std::channel::Receiver<Action>,
+ ) -> async_std::sync::Arc<Self> {
+ let this = std::sync::Arc::new(Self {
+ pending: async_std::sync::Mutex::new(Pending::new()),
+ cvar: async_std::sync::Condvar::new(),
+ });
+ {
+ let this = std::sync::Arc::clone(&this);
+ async_std::task::spawn(async move {
+ while let Ok(action) = input.recv().await {
+ this.new_action(Some(action)).await;
+ }
+ this.new_action(None).await;
+ });
+ }
+ this
+ }
+
pub async fn recv(&self) -> Option<Action> {
let mut pending = self
.cvar
.wait_until(self.pending.lock().await, |pending| {
- pending.has_event()
+ pending.has_action()
})
.await;
- pending.get_event()
+ pending.get_action()
}
- async fn send(&self, action: Option<Action>) {
+ async fn new_action(&self, action: Option<Action>) {
let mut pending = self.pending.lock().await;
- pending.new_event(&action);
+ pending.new_action(&action);
self.cvar.notify_one();
}
}
@@ -50,7 +69,7 @@ impl Pending {
Self::default()
}
- fn has_event(&self) -> bool {
+ fn has_action(&self) -> bool {
self.done
|| self.render.is_some()
|| self.force_redraw.is_some()
@@ -61,7 +80,7 @@ impl Pending {
|| self.size.is_some()
}
- fn get_event(&mut self) -> Option<Action> {
+ fn get_action(&mut self) -> Option<Action> {
if self.size.is_some() {
return Some(Action::Resize(self.size.take().unwrap()));
}
@@ -90,7 +109,7 @@ impl Pending {
unreachable!()
}
- fn new_event(&mut self, action: &Option<Action>) {
+ fn new_action(&mut self, action: &Option<Action>) {
match action {
Some(Action::Render) => self.render = Some(()),
Some(Action::ForceRedraw) => self.force_redraw = Some(()),
@@ -103,22 +122,3 @@ impl Pending {
}
}
}
-
-pub fn debounce(
- input: async_std::channel::Receiver<Action>,
-) -> async_std::sync::Arc<Debouncer> {
- let debouncer = std::sync::Arc::new(Debouncer {
- pending: async_std::sync::Mutex::new(Pending::new()),
- cvar: async_std::sync::Condvar::new(),
- });
- {
- let debouncer = std::sync::Arc::clone(&debouncer);
- async_std::task::spawn(async move {
- while let Ok(action) = input.recv().await {
- debouncer.send(Some(action)).await;
- }
- debouncer.send(None).await;
- });
- }
- debouncer
-}