home · contact · privacy
Simplify Client connecting steps.
authorChristian Heller <c.heller@plomlompom.de>
Tue, 16 Sep 2025 00:16:29 +0000 (02:16 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 16 Sep 2025 00:16:29 +0000 (02:16 +0200)
ircplom/client.py
ircplom/client_tui.py

index 508e382c32cfd25b9af9c9cb6b2e027f679f9ad1..58b48d52550e8ebad4dd8fc139193ebe5eb97e73 100644 (file)
@@ -5,13 +5,11 @@ from base64 import b64encode
 from dataclasses import dataclass, InitVar
 from enum import Enum, auto
 from getpass import getuser
-from threading import Thread
 from typing import (Any, Callable, Collection, Generic, Iterable, Iterator,
                     Optional, Self, Set, TypeVar)
 from uuid import uuid4
 # ourselves
-from ircplom.events import (
-    AffectiveEvent, CrashingException, ExceptionEvent, QueueMixin)
+from ircplom.events import AffectiveEvent, QueueMixin
 from ircplom.irc_conn import (
     BaseIrcConnection, IrcConnAbortException, IrcMessage,
     ILLEGAL_NICK_CHARS, ILLEGAL_NICK_FIRSTCHARS, ISUPPORT_DEFAULTS, PORT_SSL)
@@ -734,28 +732,16 @@ class Client(ABC, ClientQueueMixin):
         if self.db.port <= 0:
             self.db.port = PORT_SSL
 
-    def start_connecting(self) -> None:
-        'Start thread establishing connection, then triggering ._on_connect.'
-
-        def connect(self) -> None:
-            try:
-                if self.conn:
-                    raise IrcConnAbortException('already connected')
-                self.conn = self._cls_conn(
-                    hostname=self.db.hostname, port=self.db.port,
-                    _q_out=self._q_out, client_id=self.client_id)
-                self._client_trigger('_on_connect')
-            except IrcConnAbortException as e:
-                self._log(f'failed to connect: {e}', alert=True)
-            # catch _all_ just so they exit readably with the main loop
-            except Exception as e:  # pylint: disable=broad-exception-caught
-                self._put(ExceptionEvent(CrashingException(e)))
-
+    def connect(self) -> None:
+        'Attempt to open connection, on success perform session init steps.'
         self.db.connection_state = 'connecting'
-        Thread(target=connect, daemon=True, args=(self,)).start()
-
-    def _on_connect(self) -> None:
-        assert self.conn is not None
+        try:
+            self.conn = self._cls_conn(
+                hostname=self.db.hostname, port=self.db.port,
+                _q_out=self._q_out, client_id=self.client_id)
+        except IrcConnAbortException as e:
+            self.db.connection_state = f'failed to connect: {e}'
+            return
         self.db.users['me'].nickuserhost = NickUserHost(user=getuser())
         self.db.connection_state = 'connected'
         self.caps.start_negotation()
@@ -904,7 +890,8 @@ class NewClientEvent(AffectiveEvent):
 
     def affect(self, target: ClientsDb) -> None:
         target[self.payload.client_id] = self.payload
-        self.payload.start_connecting()
+        # only run _after_ spot in ClientsDb secure, for ClientEvents to target
+        self.payload.connect()
 
 
 @dataclass
index 41887c658d67479cee945139c2fc2aa6e7a36477..8a8a166036f1cd06ed390ddb41c69e9441f69d26 100644 (file)
@@ -488,7 +488,7 @@ class ClientKnowingTui(Client):
             self._log('not re-connecting since already connected',
                       scope=LogScope.SAME, alert=True)
             return
-        self.start_connecting()
+        self.connect()
 
     def _log(self, msg: str, scope: Optional[LogScope] = None, **kwargs
              ) -> None: