Commit f8c9dcbbd1
Unsigned
Layout: unified · split
src/room_buffer.rs +16 −18
| @@ -70,8 +70,6 @@ use weechat::{ | ||
| 70 | 70 | |
| 71 | 71 | #[derive(Clone)] |
| 72 | 72 | pub struct RoomBuffer { |
| 73 | own_user_id: Rc<UserId>, | |
| 74 | room_id: Rc<RoomId>, | |
| 75 | 73 | typing_in_flight: Rc<Mutex<()>>, |
| 76 | 74 | inner: MatrixRoom, |
| 77 | 75 | buffer_handle: BufferHandle, |
| @@ -169,8 +167,6 @@ impl RoomBuffer { | ||
| 169 | 167 | buffer.enable_nicklist(); |
| 170 | 168 | |
| 171 | 169 | RoomBuffer { |
| 172 | room_id: Rc::new(room_id), | |
| 173 | own_user_id: Rc::new(own_user_id.to_owned()), | |
| 174 | 170 | typing_in_flight: Rc::new(Mutex::new(())), |
| 175 | 171 | inner: room, |
| 176 | 172 | buffer_handle, |
| @@ -394,13 +390,7 @@ impl RoomBuffer { | ||
| 394 | 390 | /// |
| 395 | 391 | /// If the input is empty the typing notice is disabled. |
| 396 | 392 | pub fn update_typing_notice(&self) { |
| 397 | // We're in the process of sending out a typing notice, so don't make | |
| 398 | // the same request twice. | |
| 399 | let guard = match self.typing_in_flight.try_lock() { | |
| 400 | Ok(guard) => guard, | |
| 401 | Err(_) => return, | |
| 402 | }; | |
| 403 | ||
| 393 | let typing_in_flight = self.typing_in_flight.clone(); | |
| 404 | 394 | let buffer = self.weechat_buffer(); |
| 405 | 395 | let input = buffer.input(); |
| 406 | 396 | |
| @@ -410,12 +400,21 @@ impl RoomBuffer { | ||
| 410 | 400 | } |
| 411 | 401 | |
| 412 | 402 | let connection = self.inner.connection.clone(); |
| 413 | let room_id = self.room_id.clone(); | |
| 403 | let room_id = self.inner.room_id.clone(); | |
| 414 | 404 | let typing_notice_time = self.inner.typing_notice_time.clone(); |
| 415 | 405 | |
| 416 | let send = async move |typing: bool, _guard| { | |
| 406 | let send = async move |typing: bool| { | |
| 417 | 407 | let typing_time = typing_notice_time; |
| 418 | 408 | |
| 409 | // We're in the process of sending out a typing notice, so don't make | |
| 410 | // the same request twice. | |
| 411 | let guard = match typing_in_flight.try_lock() { | |
| 412 | Ok(guard) => guard, | |
| 413 | Err(_) => { | |
| 414 | return; | |
| 415 | } | |
| 416 | }; | |
| 417 | ||
| 419 | 418 | if let Some(connection) = &*connection.borrow() { |
| 420 | 419 | let response = |
| 421 | 420 | connection.send_typing_notice(&*room_id, typing).await; |
| @@ -437,8 +436,7 @@ impl RoomBuffer { | ||
| 437 | 436 | } |
| 438 | 437 | }; |
| 439 | 438 | |
| 440 | // The `guard` expires here, releasing the mutex taken at the | |
| 441 | // beginning of `update_typing_notice`. | |
| 439 | drop(guard) | |
| 442 | 440 | }; |
| 443 | 441 | |
| 444 | 442 | let typing_time = self.inner.typing_notice_time.borrow_mut(); |
| @@ -446,18 +444,18 @@ impl RoomBuffer { | ||
| 446 | 444 | if input.len() < 4 && typing_time.is_some() { |
| 447 | 445 | // If we have an active typing notice and our input is short, e.g. |
| 448 | 446 | // we removed the input set the typing notice to false. |
| 449 | Weechat::spawn(send(false, guard)); | |
| 447 | Weechat::spawn(send(false)); | |
| 450 | 448 | } else if input.len() >= 4 { |
| 451 | 449 | if let Some(typing_time) = &*typing_time { |
| 452 | 450 | // If we have some valid input, check if the typing notice |
| 453 | 451 | // expired and send one out if it indeed expired. |
| 454 | 452 | if typing_time.elapsed() > TYPING_NOTICE_TIMEOUT { |
| 455 | Weechat::spawn(send(true, guard)); | |
| 453 | Weechat::spawn(send(true)); | |
| 456 | 454 | } |
| 457 | 455 | } else { |
| 458 | 456 | // If we have some valid input and no active typing notice, send |
| 459 | 457 | // one out. |
| 460 | Weechat::spawn(send(true, guard)); | |
| 458 | Weechat::spawn(send(true)); | |
| 461 | 459 | } |
| 462 | 460 | } |
| 463 | 461 | } |