Skip to content

Commit 234f5c0

Browse files
committed
Reuse per-instance web_session and api_helper
1 parent 80ace22 commit 234f5c0

1 file changed

Lines changed: 24 additions & 16 deletions

File tree

src/iSponsorBlockTV/setup_wizard.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,11 @@ class AddDevice(ModalWithClickExit):
241241
pairing_name_placeholder = reactive(DEFAULT_DEVICE_NAME_PLACEHOLDER)
242242
pairing_can_add = reactive(False)
243243

244-
def __init__(self, config, **kwargs) -> None:
244+
def __init__(self, config, web_session, api_helper, **kwargs) -> None:
245245
super().__init__(**kwargs)
246246
self.config = config
247-
self.web_session = aiohttp.ClientSession(trust_env=config.use_proxy)
248-
self.api_helper = api_helpers.ApiHelper(config, self.web_session)
247+
self.web_session = web_session
248+
self.api_helper = api_helper
249249
self.devices_discovered_dial = []
250250
self.discovery_task = None
251251
self.discovery_generation = 0
@@ -346,8 +346,6 @@ async def on_unmount(self) -> None:
346346
await self.pairing_validation_task
347347
except asyncio.CancelledError:
348348
pass
349-
if not self.web_session.closed:
350-
await self.web_session.close()
351349

352350
async def task_validate_pairing_code(
353351
self, pairing_code: str, pairing_validation_generation: int
@@ -527,15 +525,11 @@ class AddChannel(ModalWithClickExit):
527525

528526
BINDINGS = [("escape", "dismiss(())", "Return")]
529527

530-
def __init__(self, config, **kwargs) -> None:
528+
def __init__(self, config, web_session, api_helper, **kwargs) -> None:
531529
super().__init__(**kwargs)
532530
self.config = config
533-
self.web_session = aiohttp.ClientSession(trust_env=config.use_proxy)
534-
self.api_helper = api_helpers.ApiHelper(config, self.web_session)
535-
536-
async def on_unmount(self) -> None:
537-
if not self.web_session.closed:
538-
await self.web_session.close()
531+
self.web_session = web_session
532+
self.api_helper = api_helper
539533

540534
def compose(self) -> ComposeResult:
541535
with Container(id="add-channel-container"):
@@ -771,7 +765,10 @@ def remove_channel(self, event: Button.Pressed):
771765

772766
@on(Button.Pressed, "#add-device")
773767
def add_device(self, event: Button.Pressed):
774-
self.app.push_screen(AddDevice(self.config), callback=self.new_devices)
768+
self.app.push_screen(
769+
AddDevice(self.config, self.app.web_session, self.app.api_helper),
770+
callback=self.new_devices,
771+
)
775772

776773
@on(Button.Pressed, "#element-name")
777774
def edit_channel(self, event: Button.Pressed):
@@ -1009,7 +1006,10 @@ def remove_channel(self, event: Button.Pressed):
10091006

10101007
@on(Button.Pressed, "#add-channel")
10111008
def add_channel(self, event: Button.Pressed):
1012-
self.app.push_screen(AddChannel(self.config), callback=self.new_channel)
1009+
self.app.push_screen(
1010+
AddChannel(self.config, self.app.web_session, self.app.api_helper),
1011+
callback=self.new_channel,
1012+
)
10131013

10141014

10151015
class AutoPlayManager(Vertical):
@@ -1083,6 +1083,8 @@ def __init__(self, config, **kwargs) -> None:
10831083
self.dark = True
10841084
self.config = config
10851085
self.initial_config = copy.deepcopy(config)
1086+
self.web_session = None
1087+
self.api_helper = None
10861088

10871089
def compose(self) -> ComposeResult:
10881090
yield Header()
@@ -1110,9 +1112,15 @@ def compose(self) -> ComposeResult:
11101112
yield AutoPlayManager(config=self.config, id="autoplay-manager", classes="container")
11111113
yield UseProxyManager(config=self.config, id="useproxy-manager", classes="container")
11121114

1113-
def on_mount(self) -> None:
1115+
async def on_mount(self) -> None:
1116+
self.web_session = aiohttp.ClientSession(trust_env=self.config.use_proxy)
1117+
self.api_helper = api_helpers.ApiHelper(self.config, self.web_session)
11141118
if self.check_for_old_config_entries():
11151119
self.app.push_screen(MigrationScreen())
1120+
1121+
async def on_unmount(self) -> None:
1122+
if self.web_session and not self.web_session.closed:
1123+
await self.web_session.close()
11161124

11171125
def action_save(self) -> None:
11181126
self.config.save()
@@ -1122,7 +1130,7 @@ def action_exit_modal(self) -> None:
11221130
if self.config != self.initial_config:
11231131
self.app.push_screen(ExitScreen())
11241132
else: # No changes were made
1125-
self.app.exit()
1133+
self.exit()
11261134

11271135
def check_for_old_config_entries(self) -> bool:
11281136
if hasattr(self.config, "atvs"):

0 commit comments

Comments
 (0)