From e5d0e91e93a4e9eaf8c8bfd4a95e905d96a59f2b Mon Sep 17 00:00:00 2001 From: Gergo Turcsanyi Date: Fri, 29 May 2020 12:01:37 +0200 Subject: [PATCH] Fix message timing on empty queue --- .../message_queue_handler/message_queue_handler.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tfw/components/frontend/message_queue_handler/message_queue_handler.py b/tfw/components/frontend/message_queue_handler/message_queue_handler.py index b661877..08613f0 100644 --- a/tfw/components/frontend/message_queue_handler/message_queue_handler.py +++ b/tfw/components/frontend/message_queue_handler/message_queue_handler.py @@ -27,14 +27,15 @@ class MessageQueueHandler: def _dispatch_messages(self): for message in iter(self._queue.get, None): - message['typing'] = not self._queue.empty() + is_queue_empty = self._queue.empty() + message['typing'] = not is_queue_empty self.connector.send_message(message) - self._sleep(self._get_sleep_time(message)) + self._sleep(self._get_sleep_time(message, is_queue_empty)) - def _get_sleep_time(self, message): + def _get_sleep_time(self, message, is_queue_empty): words_per_min = message['wpm'] if 'wpm' in message else self.wpm chars_per_min = self.avg_word_len * words_per_min / 60 - return len(message['message']) / chars_per_min + return 0 if is_queue_empty else len(message['message']) / chars_per_min def _sleep(self, seconds): poll_freq = self.drain_poll_freq