home · contact · privacy
Refactor Events and their handling.
authorChristian Heller <c.heller@plomlompom.de>
Fri, 30 May 2025 12:54:59 +0000 (14:54 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Fri, 30 May 2025 12:54:59 +0000 (14:54 +0200)
ircplom.py

index 9107e1684a0cd4e117192c09f1f8585f1f924b1d..ef58fe6e2cc64b5df50e100782ceac62df879612 100755 (executable)
@@ -7,7 +7,7 @@ from signal import SIGWINCH, signal
 from socket import socket
 from threading import Thread
 from typing import (
-        Callable, Generator, Iterator, Literal, NamedTuple, Optional, Self)
+    Any, Callable, Generator, Iterator, Literal, NamedTuple, Optional, Self)
 
 from blessed import Terminal as BlessedTerminal
 
@@ -31,7 +31,7 @@ IRCSPEC_TAG_ESCAPES = ((r'\:', ';'),
 class Event(NamedTuple):
     'Communication unit between threads.'
     type_: str
-    args: tuple = tuple()
+    payload: Any = None
 
 
 class YX(NamedTuple):
@@ -295,9 +295,9 @@ class Loop:
         'Send event into thread loop.'
         self._q_input.put(event)
 
-    def broadcast(self, type_, *args) -> None:
+    def broadcast(self, type_: str, payload: Any = None) -> None:
         'Send event to main loop via queue.'
-        self._q_to_main.put(Event(type_, args))
+        self._q_to_main.put(Event(type_, payload))
 
     def process_main(self, event: Event) -> bool:
         'Process event yielded from input queue.'
@@ -329,7 +329,7 @@ class Loop:
                     if yield_bonus:
                         self.process_bonus(yield_bonus)
         except Exception as e:  # pylint: disable=broad-exception-caught
-            self._q_to_main.put(Event('EXCEPTION', (e,)))
+            self._q_to_main.put(Event('EXCEPTION', e))
 
 
 class TuiLoop(Loop):
@@ -347,26 +347,26 @@ class TuiLoop(Loop):
         if not super().process_main(event):
             return False
         if event.type_ == 'ALERT':
-            self._log_buffer += [f'???? {event.args[0]}']
+            self._log_buffer += [f'???? {event.payload}']
             self._draw_log()
         elif event.type_ == 'RECV':
-            self._log_buffer += [f'<--- {event.args[0]}']
+            self._log_buffer += [f'<--- {event.payload}']
             self._draw_log()
         elif event.type_ == 'SEND':
-            self._log_buffer += [f'---> {event.args[0]}']
+            self._log_buffer += [f'---> {event.payload}']
             self._draw_log()
         elif event.type_ == 'INPUT_PROMPT':
-            if event.args[0] == 'ENTER':
+            if event.payload[0] == 'ENTER':
                 toks = self._prompt.split(maxsplit=1)
                 if toks and toks[0] in {'QUIT'}:
                     self.broadcast('SEND', IrcMessage(toks[0], toks[1:]))
                 else:
                     self.broadcast('ALERT', f'invalid message: {self._prompt}')
                 self._prompt = ''
-            elif event.args[0] == 'BACKSPACE':
+            elif event.payload[0] == 'BACKSPACE':
                 self._prompt = self._prompt[:-1]
-            elif event.args[0] == 'CHARACTER':
-                self._prompt += event.args[1]
+            elif event.payload[0] == 'CHARACTER':
+                self._prompt += event.payload[1]
             self._draw_prompt()
         elif event.type_ == 'SIGWINCH':
             self._calc_and_draw_all()
@@ -374,7 +374,7 @@ class TuiLoop(Loop):
         #     from traceback import format_exception
         #     self._log_buffer += [
         #         f'DEBUG {line}' for line
-        #         in '\n'.join(format_exception(event.args[0])).split('\n')]
+        #         in '\n'.join(format_exception(event.payload)).split('\n')]
         #     self._draw_log()
         self._term.flush()
         return True
@@ -417,11 +417,11 @@ class KeyboardLoop(Loop):
 
     def process_bonus(self, yielded: str) -> None:
         if yielded == 'KEY_ENTER':
-            self.broadcast('INPUT_PROMPT', 'ENTER')
+            self.broadcast('INPUT_PROMPT', ('ENTER',))
         elif yielded == 'KEY_BACKSPACE':
-            self.broadcast('INPUT_PROMPT', 'BACKSPACE')
+            self.broadcast('INPUT_PROMPT', ('BACKSPACE',))
         elif len(yielded) == 1:
-            self.broadcast('INPUT_PROMPT', 'CHARACTER', yielded)
+            self.broadcast('INPUT_PROMPT', ('CHARACTER', yielded))
         elif yielded == '[81]':
             self.broadcast('QUIT')
         else:
@@ -433,12 +433,9 @@ def run() -> None:
     q_to_main: SimpleQueue[Event] = SimpleQueue()
     with Terminal().context(q_to_main) as term:
         with Connection().context((HOST, PORT), q_to_main) as conn:
-            q_to_main.put(
-                Event('SEND',
-                      (IrcMessage('USER', [USERNAME, '0', '*', REALNAME]),)))
-            q_to_main.put(
-                Event('SEND',
-                      (IrcMessage('NICK', [NICKNAME]),)))
+            q_to_main.put(Event('SEND', IrcMessage('USER', [USERNAME, '0', '*',
+                                                            REALNAME])))
+            q_to_main.put(Event('SEND', IrcMessage('NICK', [NICKNAME])))
             while True:
                 event = q_to_main.get()
                 if event.type_ == 'QUIT':
@@ -447,15 +444,14 @@ def run() -> None:
                     term.tui.put(event)
                 elif event.type_ == 'SEND':
                     term.tui.put(event)
-                    event.args[0].send(conn)
+                    event.payload.send(conn)
                 elif event.type_ == 'PING':
                     q_to_main.put(
-                            Event('SEND',
-                                  (IrcMessage('PONG', [event.args[0]]),)))
+                        Event('SEND', IrcMessage('PONG', [event.payload])))
                 elif event.type_ == 'RECV':
                     term.tui.put(event)
                 elif event.type_ == 'EXCEPTION':
-                    raise event.args[0]
+                    raise event.payload
                 elif event.type_ == 'ALERT':
                     term.tui.put(event)
                 # elif event.type_ == 'DEBUG':