Commit c5ba826ee4
Unsigned
Layout: unified · split
src/room_buffer.rs +13 −15
| @@ -38,7 +38,7 @@ use async_trait::async_trait; | ||
| 38 | 38 | use crate::server::{Connection, TYPING_NOTICE_TIMEOUT}; |
| 39 | 39 | use std::cell::{Ref, RefCell, RefMut}; |
| 40 | 40 | use std::rc::Rc; |
| 41 | use std::sync::atomic::{AtomicBool, Ordering}; | |
| 41 | use std::sync::Mutex; | |
| 42 | 42 | use std::time::Instant; |
| 43 | 43 | use weechat::buffer::{ |
| 44 | 44 | Buffer, BufferHandle, BufferInputCallbackAsync, BufferSettingsAsync, |
| @@ -49,7 +49,7 @@ use weechat::Weechat; | ||
| 49 | 49 | pub struct RoomBuffer { |
| 50 | 50 | own_user_id: Rc<UserId>, |
| 51 | 51 | room_id: Rc<RoomId>, |
| 52 | typing_in_flight: Rc<AtomicBool>, | |
| 52 | typing_in_flight: Mutex<()>, | |
| 53 | 53 | inner: MatrixRoom, |
| 54 | 54 | buffer_handle: BufferHandle, |
| 55 | 55 | } |
| @@ -127,7 +127,7 @@ impl RoomBuffer { | ||
| 127 | 127 | RoomBuffer { |
| 128 | 128 | room_id: Rc::new(room_id), |
| 129 | 129 | own_user_id: Rc::new(own_user_id.to_owned()), |
| 130 | typing_in_flight: Rc::new(AtomicBool::from(false)), | |
| 130 | typing_in_flight: Mutex::new(()), | |
| 131 | 131 | inner: room, |
| 132 | 132 | buffer_handle, |
| 133 | 133 | } |
| @@ -221,9 +221,10 @@ impl RoomBuffer { | ||
| 221 | 221 | pub fn update_typing_notice(&self) { |
| 222 | 222 | // We're in the process of sending out a typing notice, so don't make |
| 223 | 223 | // the same request twice. |
| 224 | if self.typing_in_flight.load(Ordering::Relaxed) { | |
| 225 | return; | |
| 226 | } | |
| 224 | let guard = match self.typing_in_flight.try_lock() { | |
| 225 | Ok(guard) => guard, | |
| 226 | Err(_) => return, | |
| 227 | }; | |
| 227 | 228 | |
| 228 | 229 | let buffer = self.weechat_buffer(); |
| 229 | 230 | let input = buffer.input(); |
| @@ -237,9 +238,8 @@ impl RoomBuffer { | ||
| 237 | 238 | let room_id = self.room_id.clone(); |
| 238 | 239 | let user_id = self.own_user_id.clone(); |
| 239 | 240 | let typing_notice_time = self.inner.typing_notice_time.clone(); |
| 240 | let typing_in_flight = self.typing_in_flight.clone(); | |
| 241 | 241 | |
| 242 | let send = async move |typing: bool| { | |
| 242 | let send = async move |typing: bool, _guard| { | |
| 243 | 243 | let typing_time = typing_notice_time; |
| 244 | 244 | |
| 245 | 245 | if let Some(connection) = &*connection.borrow() { |
| @@ -264,7 +264,8 @@ impl RoomBuffer { | ||
| 264 | 264 | } |
| 265 | 265 | }; |
| 266 | 266 | |
| 267 | typing_in_flight.store(false, Ordering::Relaxed); | |
| 267 | // The `_guard` expires here, releasing the mutex taken at the beginning of | |
| 268 | // `update_typing_notice`. | |
| 268 | 269 | }; |
| 269 | 270 | |
| 270 | 271 | let typing_time = self.inner.typing_notice_time.borrow_mut(); |
| @@ -272,21 +273,18 @@ impl RoomBuffer { | ||
| 272 | 273 | if input.len() < 4 && typing_time.is_some() { |
| 273 | 274 | // If we have an active typing notice and our input is short, e.g. |
| 274 | 275 | // we removed the input set the typing notice to false. |
| 275 | self.typing_in_flight.store(true, Ordering::Release); | |
| 276 | Weechat::spawn(send(false)); | |
| 276 | Weechat::spawn(send(false, guard)); | |
| 277 | 277 | } else if input.len() >= 4 { |
| 278 | 278 | if let Some(typing_time) = &*typing_time { |
| 279 | 279 | // If we have some valid input, check if the typing notice |
| 280 | 280 | // expired and send one out if it indeed expired. |
| 281 | 281 | if typing_time.elapsed() > TYPING_NOTICE_TIMEOUT { |
| 282 | self.typing_in_flight.store(true, Ordering::Release); | |
| 283 | Weechat::spawn(send(true)); | |
| 282 | Weechat::spawn(send(true, guard)); | |
| 284 | 283 | } |
| 285 | 284 | } else { |
| 286 | 285 | // If we have some valid input and no active typing notice, send |
| 287 | 286 | // one out. |
| 288 | self.typing_in_flight.store(true, Ordering::Release); | |
| 289 | Weechat::spawn(send(true)); | |
| 287 | Weechat::spawn(send(true, guard)); | |
| 290 | 288 | } |
| 291 | 289 | } |
| 292 | 290 | } |