detect unchanged computed vars as well

This commit is contained in:
Benedikt Bartscher 2024-12-05 16:06:36 +01:00
parent 02dd4a1313
commit 421ed98ebd
No known key found for this signature in database
2 changed files with 14 additions and 6 deletions

View File

@ -1290,7 +1290,6 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
if name in self.backend_vars:
if self._backend_vars.get(name) == value:
return
print(f"Setting {name} to {value}.")
self._backend_vars.__setitem__(name, value)
self.dirty_vars.add(name)
self._mark_dirty()
@ -1830,11 +1829,13 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
while dirty_vars:
calc_vars, dirty_vars = dirty_vars, set()
for cvar in self._dirty_computed_vars(from_vars=calc_vars):
self.dirty_vars.add(cvar)
dirty_vars.add(cvar)
actual_var = self.computed_vars.get(cvar)
if actual_var is not None:
actual_var.mark_dirty(instance=self)
changed = actual_var.mark_dirty(instance=self)
if not changed:
continue
self.dirty_vars.add(cvar)
dirty_vars.add(cvar)
def _expired_computed_vars(self) -> set[str]:
"""Determine ComputedVars that need to be recalculated based on the expiration time.

View File

@ -2158,15 +2158,22 @@ class ComputedVar(Var[RETURN_TYPE]):
self_is_top_of_stack = False
return d
def mark_dirty(self, instance) -> None:
def mark_dirty(self, instance: BaseState) -> bool:
"""Mark this ComputedVar as dirty.
Args:
instance: the state instance that needs to recompute the value.
Returns:
True if the value was marked dirty (has changed), False otherwise.
"""
print(f"Marking {self._js_expr} as dirty")
cached_value = getattr(instance, self._cache_attr, None)
new_value = self.fget(instance)
if cached_value == new_value:
return False
with contextlib.suppress(AttributeError):
delattr(instance, self._cache_attr)
return True
def _determine_var_type(self) -> Type:
"""Get the type of the var.