Commit f8c9dcbbd1

f8c9dcbbd167f9c901268d3bbd81b93a27c93f86

parent: ed132c6e40

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-09-06 10:18 UTC

room: Use the typing notice lock so we don't send out multiple requests.

Layout: unified · split

src/room_buffer.rs +16 −18
@@ -70,8 +70,6 @@ use weechat::{
7070
7171#[derive(Clone)]
7272pub struct RoomBuffer {
73 own_user_id: Rc<UserId>,
74 room_id: Rc<RoomId>,
7573 typing_in_flight: Rc<Mutex<()>>,
7674 inner: MatrixRoom,
7775 buffer_handle: BufferHandle,
@@ -169,8 +167,6 @@ impl RoomBuffer {
169167 buffer.enable_nicklist();
170168
171169 RoomBuffer {
172 room_id: Rc::new(room_id),
173 own_user_id: Rc::new(own_user_id.to_owned()),
174170 typing_in_flight: Rc::new(Mutex::new(())),
175171 inner: room,
176172 buffer_handle,
@@ -394,13 +390,7 @@ impl RoomBuffer {
394390 ///
395391 /// If the input is empty the typing notice is disabled.
396392 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();
404394 let buffer = self.weechat_buffer();
405395 let input = buffer.input();
406396
@@ -410,12 +400,21 @@ impl RoomBuffer {
410400 }
411401
412402 let connection = self.inner.connection.clone();
413 let room_id = self.room_id.clone();
403 let room_id = self.inner.room_id.clone();
414404 let typing_notice_time = self.inner.typing_notice_time.clone();
415405
416 let send = async move |typing: bool, _guard| {
406 let send = async move |typing: bool| {
417407 let typing_time = typing_notice_time;
418408
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
419418 if let Some(connection) = &*connection.borrow() {
420419 let response =
421420 connection.send_typing_notice(&*room_id, typing).await;
@@ -437,8 +436,7 @@ impl RoomBuffer {
437436 }
438437 };
439438
440 // The `guard` expires here, releasing the mutex taken at the
441 // beginning of `update_typing_notice`.
439 drop(guard)
442440 };
443441
444442 let typing_time = self.inner.typing_notice_time.borrow_mut();
@@ -446,18 +444,18 @@ impl RoomBuffer {
446444 if input.len() < 4 && typing_time.is_some() {
447445 // If we have an active typing notice and our input is short, e.g.
448446 // we removed the input set the typing notice to false.
449 Weechat::spawn(send(false, guard));
447 Weechat::spawn(send(false));
450448 } else if input.len() >= 4 {
451449 if let Some(typing_time) = &*typing_time {
452450 // If we have some valid input, check if the typing notice
453451 // expired and send one out if it indeed expired.
454452 if typing_time.elapsed() > TYPING_NOTICE_TIMEOUT {
455 Weechat::spawn(send(true, guard));
453 Weechat::spawn(send(true));
456454 }
457455 } else {
458456 // If we have some valid input and no active typing notice, send
459457 // one out.
460 Weechat::spawn(send(true, guard));
458 Weechat::spawn(send(true));
461459 }
462460 }
463461 }