Skip to content

Commit

Permalink
Improve performance by batch updating chat periodically instead of af…
Browse files Browse the repository at this point in the history
…ter every message
  • Loading branch information
douglascdev committed Oct 12, 2023
1 parent da2403e commit d3a5ff3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Changes:
* Improve performance by batch updating chat periodically instead of after every message
* Fix displayed messages lagging behind on high frequency chats
* Catch closed socket connection and reconnect, showing connection status to user(closes #19)
* Display error message when trying to send message without logging in(closes #16)
Expand Down
53 changes: 50 additions & 3 deletions hasherino/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import logging
from abc import ABC
from enum import Enum, auto
from math import isclose
from typing import Any, Awaitable, Coroutine

Expand Down Expand Up @@ -161,6 +162,13 @@ async def _get_general_tab(self) -> ft.Tab:
width=500,
on_change=self._max_messages_change,
),
ft.Text(),
ft.TextField(
value=await self.storage.get("chat_update_rate"),
label="Chat UI Update rate(lower = higher CPU usage):",
width=500,
on_change=self._chat_update_rate_change,
),
],
),
)
Expand Down Expand Up @@ -215,6 +223,23 @@ async def _font_size_change(self, e):
await self.font_size_pubsub.send(e.control.value)
await self.page.update_async()

async def _chat_update_rate_change(self, e):
try:
value = float(e.control.value)

if value < 0.3 or value > 1:
raise ValueError

e.control.error_text = ""
await self.storage.set("chat_update_rate", e.control.value)
logging.debug(f"Set chat_update_rate to {value}")

except ValueError:
e.control.error_text = "Value must be a decimal between 0.3 and 1."

finally:
await self.page.update_async()


class AccountDialog(ft.AlertDialog):
def __init__(self, storage: AsyncKeyValueStorage):
Expand Down Expand Up @@ -380,6 +405,11 @@ def __init__(self, select_chat_click: Awaitable, storage: AsyncKeyValueStorage):


class ChatContainer(ft.Container):
class _UiUpdateType(Enum):
NO_UPDATE = (auto(),)
SCROLL = (auto(),)
PAGE = (auto(),)

def __init__(self, storage: AsyncKeyValueStorage, font_size_pubsub: PubSub):
self.storage = storage
self.font_size_pubsub = font_size_pubsub
Expand All @@ -399,6 +429,20 @@ def __init__(self, storage: AsyncKeyValueStorage, font_size_pubsub: PubSub):
padding=10,
expand=True,
)
self.scheduled_ui_update: self._UiUpdateType = self._UiUpdateType.NO_UPDATE
asyncio.ensure_future(self.update_ui())

async def update_ui(self):
while True:
match self.scheduled_ui_update:
case self._UiUpdateType.SCROLL:
await self.chat.scroll_to_async(offset=-1, duration=10)
case self._UiUpdateType.PAGE:
await self.page.update_async()
case self._UiUpdateType.NO_UPDATE | _:
pass

await asyncio.sleep(float(await self.storage.get("chat_update_rate")))

async def on_scroll(self, event: ft.OnScrollEvent):
self.is_chat_scrolled_down = isclose(
Expand Down Expand Up @@ -428,9 +472,11 @@ async def on_message(self, message: Message):
del self.chat.controls[:n_messages_to_remove]

if self.is_chat_scrolled_down:
await self.chat.scroll_to_async(offset=-1, duration=10)
else:
await self.page.update_async()
self.scheduled_ui_update = self._UiUpdateType.SCROLL
elif (
self.scheduled_ui_update != self._UiUpdateType.SCROLL
): # Scroll already updates
self.scheduled_ui_updates.add(self.page.update_async())

logging.debug(f"Chat has {len(self.chat.controls)} lines in it")

Expand Down Expand Up @@ -628,6 +674,7 @@ async def main(page: ft.Page):
storage = MemoryOnlyStorage(page)
asyncio.gather(
storage.set("chat_font_size", 18),
storage.set("chat_update_rate", 0.5),
storage.set("max_messages_per_chat", 100),
storage.set("app_id", "hvmj7blkwy2gw3xf820n47i85g4sub"),
storage.set("websocket", TwitchWebsocket()),
Expand Down

0 comments on commit d3a5ff3

Please sign in to comment.