diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-02-14 17:12:18 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-02-14 17:12:18 +0000 |
| commit | bac1118d417261ee71738553b7c508f6ea6b48a8 (patch) | |
| tree | c3c3fb8c2bdd43c3beb00e31473a784b9c5bf1ab /src/client.rs | |
| parent | 115eca3d69310a58e8357fe091183d0a8e723967 (diff) | |
feat: get_events coutdown to timeout
add a ui feature to countdown to timeout so user doesn't consider
exiting early
Diffstat (limited to 'src/client.rs')
| -rw-r--r-- | src/client.rs | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/src/client.rs b/src/client.rs index 9bc171a..2b0c960 100644 --- a/src/client.rs +++ b/src/client.rs | |||
| @@ -10,12 +10,12 @@ | |||
| 10 | // which is currently in nightly. alternatively we can use nightly as it looks | 10 | // which is currently in nightly. alternatively we can use nightly as it looks |
| 11 | // certain that the implementation is going to make it to stable but we don't | 11 | // certain that the implementation is going to make it to stable but we don't |
| 12 | // want to inadvertlty use other features of nightly that might be removed. | 12 | // want to inadvertlty use other features of nightly that might be removed. |
| 13 | use std::time::Duration; | 13 | use std::{fmt::Write, time::Duration}; |
| 14 | 14 | ||
| 15 | use anyhow::{Context, Result}; | 15 | use anyhow::{Context, Result}; |
| 16 | use async_trait::async_trait; | 16 | use async_trait::async_trait; |
| 17 | use futures::stream::{self, StreamExt}; | 17 | use futures::stream::{self, StreamExt}; |
| 18 | use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; | 18 | use indicatif::{MultiProgress, ProgressBar, ProgressState, ProgressStyle}; |
| 19 | #[cfg(test)] | 19 | #[cfg(test)] |
| 20 | use mockall::*; | 20 | use mockall::*; |
| 21 | use nostr::Event; | 21 | use nostr::Event; |
| @@ -143,7 +143,18 @@ impl Connect for Client { | |||
| 143 | } | 143 | } |
| 144 | 144 | ||
| 145 | let m = MultiProgress::new(); | 145 | let m = MultiProgress::new(); |
| 146 | let pb_style = ProgressStyle::with_template(" {spinner} {prefix} {msg}")?; | 146 | let pb_style = ProgressStyle::with_template(" {spinner} {prefix} {msg} {timeout_in}")? |
| 147 | .with_key("timeout_in", |state: &ProgressState, w: &mut dyn Write| { | ||
| 148 | if state.elapsed().as_secs() > 3 { | ||
| 149 | write!( | ||
| 150 | w, | ||
| 151 | "timeout in {:.1}s", | ||
| 152 | GET_EVENTS_TIMEOUT - state.elapsed().as_secs() | ||
| 153 | ) | ||
| 154 | .unwrap(); | ||
| 155 | } | ||
| 156 | }); | ||
| 157 | |||
| 147 | let pb_after_style = |succeed| { | 158 | let pb_after_style = |succeed| { |
| 148 | ProgressStyle::with_template( | 159 | ProgressStyle::with_template( |
| 149 | format!( | 160 | format!( |
| @@ -228,6 +239,8 @@ impl Connect for Client { | |||
| 228 | } | 239 | } |
| 229 | } | 240 | } |
| 230 | 241 | ||
| 242 | static GET_EVENTS_TIMEOUT: u64 = 10; | ||
| 243 | |||
| 231 | async fn get_events_of( | 244 | async fn get_events_of( |
| 232 | relay: &nostr_sdk::Relay, | 245 | relay: &nostr_sdk::Relay, |
| 233 | filters: Vec<nostr::Filter>, | 246 | filters: Vec<nostr::Filter>, |
| @@ -244,7 +257,7 @@ async fn get_events_of( | |||
| 244 | .get_events_of( | 257 | .get_events_of( |
| 245 | filters, | 258 | filters, |
| 246 | // 20 is nostr_sdk default | 259 | // 20 is nostr_sdk default |
| 247 | std::time::Duration::from_secs(10), | 260 | std::time::Duration::from_secs(GET_EVENTS_TIMEOUT), |
| 248 | nostr_sdk::FilterOptions::ExitOnEOSE, | 261 | nostr_sdk::FilterOptions::ExitOnEOSE, |
| 249 | ) | 262 | ) |
| 250 | .await?; | 263 | .await?; |