|
9 | 9 |
|
10 | 10 | from ...core.config import AppConfig |
11 | 11 | from ...core.constants import APP_DIR, USER_CONFIG |
| 12 | +from ...core.utils.concurrency import thread_manager |
12 | 13 | from .state import InternalDirective, MenuName, State |
13 | 14 |
|
14 | 15 | if TYPE_CHECKING: |
15 | 16 | from ...libs.media_api.base import BaseApiClient |
16 | 17 | from ...libs.provider.anime.base import BaseAnimeProvider |
17 | 18 | from ...libs.selectors.base import BaseSelector |
18 | 19 | from ..service.auth import AuthService |
| 20 | + from ..service.download.service import DownloadService |
19 | 21 | from ..service.feedback import FeedbackService |
20 | 22 | from ..service.player import PlayerService |
21 | 23 | from ..service.registry import MediaRegistryService |
@@ -86,6 +88,7 @@ class Context: |
86 | 88 | _selector: Optional["BaseSelector"] = None |
87 | 89 | _media_api: Optional["BaseApiClient"] = None |
88 | 90 |
|
| 91 | + _download: Optional["DownloadService"] = None |
89 | 92 | _feedback: Optional["FeedbackService"] = None |
90 | 93 | _media_registry: Optional["MediaRegistryService"] = None |
91 | 94 | _watch_history: Optional["WatchHistoryService"] = None |
@@ -137,6 +140,16 @@ def media_api(self) -> "BaseApiClient": |
137 | 140 |
|
138 | 141 | return self._media_api |
139 | 142 |
|
| 143 | + @property |
| 144 | + def download(self) -> "DownloadService": |
| 145 | + if not self._download: |
| 146 | + from ..service.download.service import DownloadService |
| 147 | + |
| 148 | + self._download = DownloadService( |
| 149 | + self.config, self.media_registry, self.media_api, self.provider |
| 150 | + ) |
| 151 | + return self._download |
| 152 | + |
140 | 153 | @property |
141 | 154 | def player(self) -> "PlayerService": |
142 | 155 | if not self._player: |
@@ -206,7 +219,12 @@ class Session: |
206 | 219 | _history: List[State] = [] |
207 | 220 | _menus: dict[MenuName, Menu] = {} |
208 | 221 |
|
| 222 | + def _shutdown_download_worker(self): |
| 223 | + if hasattr(self, "_context") and self._context._download: |
| 224 | + thread_manager.shutdown_worker("download_worker", wait=False, timeout=5.0) |
| 225 | + |
209 | 226 | def _load_context(self, config: AppConfig): |
| 227 | + self._shutdown_download_worker() |
210 | 228 | self._context = Context(config) |
211 | 229 | logger.info("Application context reloaded.") |
212 | 230 |
|
@@ -243,6 +261,7 @@ def run( |
243 | 261 | self._context.session.create_crash_backup(self._history) |
244 | 262 | raise |
245 | 263 | finally: |
| 264 | + self._shutdown_download_worker() |
246 | 265 | # Clean up preview workers when session ends |
247 | 266 | self._cleanup_preview_workers() |
248 | 267 | self._context.session.save_session(self._history) |
|
0 commit comments