Commit c5ba826ee4

c5ba826ee4948bb6618e91460c7b50ac2d58bfc1

parent: 48374d9746

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-07-13 15:33 UTC

room_buffer: Use a mutex to make sure that we don't send mutliple typing notifs.

Layout: unified · split

src/room_buffer.rs +13 −15
@@ -38,7 +38,7 @@ use async_trait::async_trait;
3838use crate::server::{Connection, TYPING_NOTICE_TIMEOUT};
3939use std::cell::{Ref, RefCell, RefMut};
4040use std::rc::Rc;
41use std::sync::atomic::{AtomicBool, Ordering};
41use std::sync::Mutex;
4242use std::time::Instant;
4343use weechat::buffer::{
4444 Buffer, BufferHandle, BufferInputCallbackAsync, BufferSettingsAsync,
@@ -49,7 +49,7 @@ use weechat::Weechat;
4949pub struct RoomBuffer {
5050 own_user_id: Rc<UserId>,
5151 room_id: Rc<RoomId>,
52 typing_in_flight: Rc<AtomicBool>,
52 typing_in_flight: Mutex<()>,
5353 inner: MatrixRoom,
5454 buffer_handle: BufferHandle,
5555}
@@ -127,7 +127,7 @@ impl RoomBuffer {
127127 RoomBuffer {
128128 room_id: Rc::new(room_id),
129129 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(()),
131131 inner: room,
132132 buffer_handle,
133133 }
@@ -221,9 +221,10 @@ impl RoomBuffer {
221221 pub fn update_typing_notice(&self) {
222222 // We're in the process of sending out a typing notice, so don't make
223223 // 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 };
227228
228229 let buffer = self.weechat_buffer();
229230 let input = buffer.input();
@@ -237,9 +238,8 @@ impl RoomBuffer {
237238 let room_id = self.room_id.clone();
238239 let user_id = self.own_user_id.clone();
239240 let typing_notice_time = self.inner.typing_notice_time.clone();
240 let typing_in_flight = self.typing_in_flight.clone();
241241
242 let send = async move |typing: bool| {
242 let send = async move |typing: bool, _guard| {
243243 let typing_time = typing_notice_time;
244244
245245 if let Some(connection) = &*connection.borrow() {
@@ -264,7 +264,8 @@ impl RoomBuffer {
264264 }
265265 };
266266
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`.
268269 };
269270
270271 let typing_time = self.inner.typing_notice_time.borrow_mut();
@@ -272,21 +273,18 @@ impl RoomBuffer {
272273 if input.len() < 4 && typing_time.is_some() {
273274 // If we have an active typing notice and our input is short, e.g.
274275 // 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));
277277 } else if input.len() >= 4 {
278278 if let Some(typing_time) = &*typing_time {
279279 // If we have some valid input, check if the typing notice
280280 // expired and send one out if it indeed expired.
281281 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));
284283 }
285284 } else {
286285 // If we have some valid input and no active typing notice, send
287286 // one out.
288 self.typing_in_flight.store(true, Ordering::Release);
289 Weechat::spawn(send(true));
287 Weechat::spawn(send(true, guard));
290288 }
291289 }
292290 }