Unify start method

This commit is contained in:
DevilXD committed 2022-02-14 22:14:43 +01:00
1 parent 3a5d105235
commit 3653ecf152
1 file changed
+13 -13
+13 -13
View File
@@ -81,17 +81,16 @@ class Websocket:
await ws.close() await ws.close()
async def start(self): async def start(self):
if self.connected: if not self.connected:
return self.start_nowait()
if self._handle_task is None: await self.wait_until_connected()
self._handle_task = asyncio.create_task(self._handle())
await self.wait_until_connected()
def start_nowait(self): def start_nowait(self):
if self.connected: if not self.connected:
return if self._handle_task is None:
if self._handle_task is None: self._handle_task = asyncio.create_task(self._handle())
self._handle_task = asyncio.create_task(self._handle()) elif self._handle_task.done():
ws_logger.error(f"Detected zombified handle task: {self._handle_task!r}")
async def stop(self): async def stop(self):
await self.close() await self.close()
@@ -101,12 +100,13 @@ class Websocket:
self._handle_task = None self._handle_task = None
def stop_nowait(self): def stop_nowait(self):
asyncio.create_task(self.close()) # weird syntax but that's what we get for using a decorator for this
# note: this detaches the handle task, so we have to assume it closes properly # return type of 'task_wrapper' is a coro, so we need to instance it for the task
self._handle_task = None asyncio.create_task(task_wrapper(self.close)())
def remove(self): def remove(self):
# this stops the websocket, and then removes it from the gui list # this stops the websocket, and then removes it from the gui list
@task_wrapper
async def remover(): async def remover():
await self.stop() await self.stop()
self._twitch.gui.websockets.remove(self._idx) self._twitch.gui.websockets.remove(self._idx)
@@ -134,7 +134,7 @@ class Websocket:
finally: finally:
self._ws.clear() self._ws.clear()
self._submitted.clear() self._submitted.clear()
self._topics_changed.set() # lets the next ws connection resub to the topics self._topics_changed.set() # lets the next WS connection resub to the topics
# A reconnect was requested # A reconnect was requested
except ConnectionClosed as exc: except ConnectionClosed as exc:
if isinstance(exc, ConnectionClosedOK): if isinstance(exc, ConnectionClosedOK):