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