|
19 | 19 | import tempfile |
20 | 20 | import typing |
21 | 21 | import zipfile |
22 | | -from collections.abc import Callable, Sequence |
| 22 | +from collections.abc import Sequence |
23 | 23 | from datetime import datetime |
24 | 24 | from pathlib import Path |
25 | 25 | from types import ModuleType |
|
37 | 37 | from reflex.config import Config, get_config |
38 | 38 | from reflex.environment import environment |
39 | 39 | from reflex.utils import console, net, path_ops, processes, redir |
| 40 | +from reflex.utils.decorator import cached_procedure |
40 | 41 | from reflex.utils.exceptions import SystemPackageMissingError |
41 | 42 | from reflex.utils.misc import get_module_path |
42 | 43 | from reflex.utils.registry import get_npm_registry |
@@ -1249,71 +1250,9 @@ def install_bun(): |
1249 | 1250 | ) |
1250 | 1251 |
|
1251 | 1252 |
|
1252 | | -def _write_cached_procedure_file(payload: str, cache_file: str | Path): |
1253 | | - cache_file = Path(cache_file) |
1254 | | - cache_file.write_text(payload) |
1255 | | - |
1256 | | - |
1257 | | -def _read_cached_procedure_file(cache_file: str | Path) -> str | None: |
1258 | | - cache_file = Path(cache_file) |
1259 | | - if cache_file.exists(): |
1260 | | - return cache_file.read_text() |
1261 | | - return None |
1262 | | - |
1263 | | - |
1264 | | -def _clear_cached_procedure_file(cache_file: str | Path): |
1265 | | - cache_file = Path(cache_file) |
1266 | | - if cache_file.exists(): |
1267 | | - cache_file.unlink() |
1268 | | - |
1269 | | - |
1270 | | -def cached_procedure( |
1271 | | - cache_file: str | None, |
1272 | | - payload_fn: Callable[..., str], |
1273 | | - cache_file_fn: Callable[[], str] | None = None, |
1274 | | -): |
1275 | | - """Decorator to cache the runs of a procedure on disk. Procedures should not have |
1276 | | - a return value. |
1277 | | -
|
1278 | | - Args: |
1279 | | - cache_file: The file to store the cache payload in. |
1280 | | - payload_fn: Function that computes cache payload from function args. |
1281 | | - cache_file_fn: Function that computes the cache file name at runtime. |
1282 | | -
|
1283 | | - Returns: |
1284 | | - The decorated function. |
1285 | | -
|
1286 | | - Raises: |
1287 | | - ValueError: If both cache_file and cache_file_fn are provided. |
1288 | | - """ |
1289 | | - if cache_file and cache_file_fn is not None: |
1290 | | - msg = "cache_file and cache_file_fn cannot both be provided." |
1291 | | - raise ValueError(msg) |
1292 | | - |
1293 | | - def _inner_decorator(func: Callable): |
1294 | | - def _inner(*args, **kwargs): |
1295 | | - _cache_file = cache_file_fn() if cache_file_fn is not None else cache_file |
1296 | | - if not _cache_file: |
1297 | | - msg = "Unknown cache file, cannot cache result." |
1298 | | - raise ValueError(msg) |
1299 | | - payload = _read_cached_procedure_file(_cache_file) |
1300 | | - new_payload = payload_fn(*args, **kwargs) |
1301 | | - if payload != new_payload: |
1302 | | - _clear_cached_procedure_file(_cache_file) |
1303 | | - func(*args, **kwargs) |
1304 | | - _write_cached_procedure_file(new_payload, _cache_file) |
1305 | | - |
1306 | | - return _inner |
1307 | | - |
1308 | | - return _inner_decorator |
1309 | | - |
1310 | | - |
1311 | 1253 | @cached_procedure( |
1312 | | - cache_file_fn=lambda: str( |
1313 | | - get_web_dir() / "reflex.install_frontend_packages.cached" |
1314 | | - ), |
1315 | | - payload_fn=lambda p, c: f"{sorted(p)!r},{c.json()}", |
1316 | | - cache_file=None, |
| 1254 | + cache_file_path=lambda: get_web_dir() / "reflex.install_frontend_packages.cached", |
| 1255 | + payload_fn=lambda packages, config: f"{sorted(packages)!r},{config.json()}", |
1317 | 1256 | ) |
1318 | 1257 | def install_frontend_packages(packages: set[str], config: Config): |
1319 | 1258 | """Installs the base and custom frontend packages. |
|
0 commit comments