Implement a way to instantly exit the request loop when trying to close the window;
this removes a bunch of plug-in exit possibilities since they're technically no longer needed
This commit is contained in:
1 parent
431fa6adcc
commit
f1e0716445
1 file changed
+24
-16
@@ -869,22 +869,37 @@ class Twitch:
|
|||||||
kwargs["proxy"] = self.settings.proxy
|
kwargs["proxy"] = self.settings.proxy
|
||||||
logger.debug(f"Request: ({method=}, {url=}, {kwargs=})")
|
logger.debug(f"Request: ({method=}, {url=}, {kwargs=})")
|
||||||
for delay in ExponentialBackoff(shift=1, maximum=3*60):
|
for delay in ExponentialBackoff(shift=1, maximum=3*60):
|
||||||
|
if self.gui.close_requested:
|
||||||
|
raise ExitRequest()
|
||||||
try:
|
try:
|
||||||
async with session.request(method, url, **kwargs) as response:
|
response: aiohttp.ClientResponse | None = None
|
||||||
|
done, pending = await asyncio.wait(
|
||||||
|
[session.request(method, url, **kwargs), self.gui.wait_until_closed()],
|
||||||
|
return_when=asyncio.FIRST_COMPLETED,
|
||||||
|
)
|
||||||
|
if self.gui.close_requested:
|
||||||
|
raise ExitRequest()
|
||||||
|
for task in pending:
|
||||||
|
task.cancel()
|
||||||
|
for task in done:
|
||||||
|
response = task.result()
|
||||||
|
break
|
||||||
|
if response is None:
|
||||||
|
raise RuntimeError("Close request leak")
|
||||||
logger.debug(f"Response: {response.status}: {response}")
|
logger.debug(f"Response: {response.status}: {response}")
|
||||||
if response.status < 500:
|
if response.status >= 500:
|
||||||
|
self.print(_("error", "site_down").format(seconds=round(delay)))
|
||||||
yield response
|
yield response
|
||||||
return
|
return
|
||||||
self.print(_("error", "site_down").format(seconds=round(delay)))
|
|
||||||
except (aiohttp.ClientConnectionError, asyncio.TimeoutError):
|
except (aiohttp.ClientConnectionError, asyncio.TimeoutError):
|
||||||
# just so that quick 2nd retries that often happen, aren't shown
|
# just so that quick 2nd retries that often happen, aren't shown
|
||||||
if delay > 1:
|
if delay > 1:
|
||||||
self.print(_("error", "no_connection").format(seconds=round(delay)))
|
self.print(_("error", "no_connection").format(seconds=round(delay)))
|
||||||
if self.gui.close_requested:
|
finally:
|
||||||
raise ExitRequest()
|
if response is not None:
|
||||||
await asyncio.sleep(delay)
|
response.release()
|
||||||
if self.gui.close_requested:
|
with suppress(asyncio.TimeoutError):
|
||||||
raise ExitRequest()
|
await asyncio.wait_for(self.gui.wait_until_closed(), timeout=delay)
|
||||||
|
|
||||||
async def gql_request(self, op: GQLOperation) -> JsonType:
|
async def gql_request(self, op: GQLOperation) -> JsonType:
|
||||||
gql_logger.debug(f"GQL Request: {op}")
|
gql_logger.debug(f"GQL Request: {op}")
|
||||||
@@ -924,8 +939,6 @@ class Twitch:
|
|||||||
status_update(_("gui", "status", "fetching_inventory"))
|
status_update(_("gui", "status", "fetching_inventory"))
|
||||||
# fetch in-progress campaigns (inventory)
|
# fetch in-progress campaigns (inventory)
|
||||||
response = await self.gql_request(GQL_OPERATIONS["Inventory"])
|
response = await self.gql_request(GQL_OPERATIONS["Inventory"])
|
||||||
if self.gui.close_requested:
|
|
||||||
raise ExitRequest()
|
|
||||||
inventory: JsonType = response["data"]["currentUser"]["inventory"]
|
inventory: JsonType = response["data"]["currentUser"]["inventory"]
|
||||||
ongoing_campaigns: list[JsonType] = inventory["dropCampaignsInProgress"] or []
|
ongoing_campaigns: list[JsonType] = inventory["dropCampaignsInProgress"] or []
|
||||||
# this contains claimed benefit edge IDs, not drop IDs
|
# this contains claimed benefit edge IDs, not drop IDs
|
||||||
@@ -938,8 +951,6 @@ class Twitch:
|
|||||||
]
|
]
|
||||||
# fetch all available campaigns data
|
# fetch all available campaigns data
|
||||||
response = await self.gql_request(GQL_OPERATIONS["Campaigns"])
|
response = await self.gql_request(GQL_OPERATIONS["Campaigns"])
|
||||||
if self.gui.close_requested:
|
|
||||||
raise ExitRequest()
|
|
||||||
available_list: list[JsonType] = response["data"]["currentUser"]["dropCampaigns"] or []
|
available_list: list[JsonType] = response["data"]["currentUser"]["dropCampaigns"] or []
|
||||||
applicable_statuses = ("ACTIVE", "UPCOMING")
|
applicable_statuses = ("ACTIVE", "UPCOMING")
|
||||||
existing_campaigns: set[str] = set(c.id for c in campaigns)
|
existing_campaigns: set[str] = set(c.id for c in campaigns)
|
||||||
@@ -973,8 +984,6 @@ class Twitch:
|
|||||||
counter=f"({i}/{len(available_campaigns)})"
|
counter=f"({i}/{len(available_campaigns)})"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
if self.gui.close_requested:
|
|
||||||
raise ExitRequest()
|
|
||||||
campaigns.extend(fetched_campaigns)
|
campaigns.extend(fetched_campaigns)
|
||||||
campaigns.sort(key=lambda c: c.active, reverse=True)
|
campaigns.sort(key=lambda c: c.active, reverse=True)
|
||||||
campaigns.sort(key=lambda c: c.upcoming and c.starts_at or c.ends_at)
|
campaigns.sort(key=lambda c: c.upcoming and c.starts_at or c.ends_at)
|
||||||
@@ -987,10 +996,9 @@ class Twitch:
|
|||||||
_("gui", "status", "adding_campaigns").format(counter=f"({i}/{len(campaigns)})")
|
_("gui", "status", "adding_campaigns").format(counter=f"({i}/{len(campaigns)})")
|
||||||
)
|
)
|
||||||
self._drops.update({drop.id: drop for drop in campaign.drops})
|
self._drops.update({drop.id: drop for drop in campaign.drops})
|
||||||
|
# NOTE: this adds pictures, so might be slow sometimes
|
||||||
await self.gui.inv.add_campaign(campaign)
|
await self.gui.inv.add_campaign(campaign)
|
||||||
self.inventory.append(campaign)
|
self.inventory.append(campaign)
|
||||||
if self.gui.close_requested:
|
|
||||||
raise ExitRequest()
|
|
||||||
|
|
||||||
def get_active_drop(self, channel: Channel | None = None) -> TimedDrop | None:
|
def get_active_drop(self, channel: Channel | None = None) -> TimedDrop | None:
|
||||||
if not self.games:
|
if not self.games:
|
||||||
|
|||||||
Reference in new issue
Block a user