Skip to content

Commit

Permalink
Improve quoter
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier committed Jul 13, 2024
1 parent f277e4e commit c1ab464
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Services {
pub fn new(ctx: &Context, components: &Components, state: &State) -> Result<Self> {
let keyboard = Keyboard::new();
let rt = Runtime::new()?;
let quoter = Quoter::new(&rt, state.chat.quote.clone());
let quoter = Quoter::new(&rt, state.chat.quote.clone(), state.chat.input.clone());
let is_chatting = Arc::new(AtomicBool::new(false));
let chat = Chat::new(
keyboard.clone(),
Expand Down
12 changes: 8 additions & 4 deletions src/service/quoter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ use crate::component::quote::Quoter as QuoterC;
#[derive(Debug)]
pub struct Quoter(AbortHandle);
impl Quoter {
pub fn new(rt: &Runtime, quote: Arc<RwLock<String>>) -> Self {
pub fn new(rt: &Runtime, quote: Arc<RwLock<String>>, input: Arc<RwLock<String>>) -> Self {
*quote.write() = QuoterC::DEFAULT.into();

let quoter = QuoterC;
let abort_handle = rt
.spawn(async move {
loop {
// TODO: skip if the chat input is not empty.

*quote.write() = quoter.fetch().await.unwrap_or(QuoterC::DEFAULT.into());
if input.read().is_empty() {
if let Ok(quote_) = quoter.fetch().await {
*quote.write() = quote_;
}
}

time::sleep(Duration::from_millis(30_000)).await;
}
Expand Down

0 comments on commit c1ab464

Please sign in to comment.