From: Christian Heller Date: Tue, 16 Sep 2025 00:16:29 +0000 (+0200) Subject: Simplify Client connecting steps. X-Git-Url: https://plomlompom.com/repos/do_todos?a=commitdiff_plain;h=e4b4b2ceb96b26b2ff77c92c1c938cf500f6d4b6;p=ircplom Simplify Client connecting steps. --- diff --git a/ircplom/client.py b/ircplom/client.py index 508e382..58b48d5 100644 --- a/ircplom/client.py +++ b/ircplom/client.py @@ -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 diff --git a/ircplom/client_tui.py b/ircplom/client_tui.py index 41887c6..8a8a166 100644 --- a/ircplom/client_tui.py +++ b/ircplom/client_tui.py @@ -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: