|
@@ -305,10 +305,10 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
|
|
|
# Vars inherited by the parent state.
|
|
|
inherited_vars: ClassVar[Dict[str, Var]] = {}
|
|
|
|
|
|
- # Backend vars that are never sent to the client.
|
|
|
+ # Backend base vars that are never sent to the client.
|
|
|
backend_vars: ClassVar[Dict[str, Any]] = {}
|
|
|
|
|
|
- # Backend vars inherited
|
|
|
+ # Backend base vars inherited
|
|
|
inherited_backend_vars: ClassVar[Dict[str, Any]] = {}
|
|
|
|
|
|
# The event handlers.
|
|
@@ -344,7 +344,7 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
|
|
|
# The routing path that triggered the state
|
|
|
router_data: Dict[str, Any] = {}
|
|
|
|
|
|
- # Per-instance copy of backend variable values
|
|
|
+ # Per-instance copy of backend base variable values
|
|
|
_backend_vars: Dict[str, Any] = {}
|
|
|
|
|
|
# The router data for the current page
|
|
@@ -492,21 +492,12 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
|
|
|
new_backend_vars = {
|
|
|
name: value
|
|
|
for name, value in cls.__dict__.items()
|
|
|
- if types.is_backend_variable(name, cls)
|
|
|
- }
|
|
|
-
|
|
|
- # Get backend computed vars
|
|
|
- backend_computed_vars = {
|
|
|
- v._var_name: v._var_set_state(cls)
|
|
|
- for v in computed_vars
|
|
|
- if types.is_backend_variable(v._var_name, cls)
|
|
|
- and v._var_name not in cls.inherited_backend_vars
|
|
|
+ if types.is_backend_base_variable(name, cls)
|
|
|
}
|
|
|
|
|
|
cls.backend_vars = {
|
|
|
**cls.inherited_backend_vars,
|
|
|
**new_backend_vars,
|
|
|
- **backend_computed_vars,
|
|
|
}
|
|
|
|
|
|
# Set the base and computed vars.
|
|
@@ -548,7 +539,7 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
|
|
|
cls.computed_vars[newcv._var_name] = newcv
|
|
|
cls.vars[newcv._var_name] = newcv
|
|
|
continue
|
|
|
- if types.is_backend_variable(name, mixin):
|
|
|
+ if types.is_backend_base_variable(name, mixin):
|
|
|
cls.backend_vars[name] = copy.deepcopy(value)
|
|
|
continue
|
|
|
if events.get(name) is not None:
|
|
@@ -1087,7 +1078,7 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
|
|
|
setattr(self.parent_state, name, value)
|
|
|
return
|
|
|
|
|
|
- if types.is_backend_variable(name, type(self)):
|
|
|
+ if name in self.backend_vars:
|
|
|
self._backend_vars.__setitem__(name, value)
|
|
|
self.dirty_vars.add(name)
|
|
|
self._mark_dirty()
|
|
@@ -1538,11 +1529,14 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
|
|
|
if self.computed_vars[cvar].needs_update(instance=self)
|
|
|
)
|
|
|
|
|
|
- def _dirty_computed_vars(self, from_vars: set[str] | None = None) -> set[str]:
|
|
|
+ def _dirty_computed_vars(
|
|
|
+ self, from_vars: set[str] | None = None, include_backend: bool = True
|
|
|
+ ) -> set[str]:
|
|
|
"""Determine ComputedVars that need to be recalculated based on the given vars.
|
|
|
|
|
|
Args:
|
|
|
from_vars: find ComputedVar that depend on this set of vars. If unspecified, will use the dirty_vars.
|
|
|
+ include_backend: whether to include backend vars in the calculation.
|
|
|
|
|
|
Returns:
|
|
|
Set of computed vars to include in the delta.
|
|
@@ -1551,6 +1545,7 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
|
|
|
cvar
|
|
|
for dirty_var in from_vars or self.dirty_vars
|
|
|
for cvar in self._computed_var_dependencies[dirty_var]
|
|
|
+ if include_backend or not self.computed_vars[cvar]._backend
|
|
|
)
|
|
|
|
|
|
@classmethod
|
|
@@ -1586,19 +1581,23 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
|
|
|
self.dirty_vars.update(self._always_dirty_computed_vars)
|
|
|
self._mark_dirty()
|
|
|
|
|
|
+ frontend_computed_vars: set[str] = {
|
|
|
+ name for name, cv in self.computed_vars.items() if not cv._backend
|
|
|
+ }
|
|
|
+
|
|
|
# Return the dirty vars for this instance, any cached/dependent computed vars,
|
|
|
# and always dirty computed vars (cache=False)
|
|
|
delta_vars = (
|
|
|
self.dirty_vars.intersection(self.base_vars)
|
|
|
- .union(self.dirty_vars.intersection(self.computed_vars))
|
|
|
- .union(self._dirty_computed_vars())
|
|
|
+ .union(self.dirty_vars.intersection(frontend_computed_vars))
|
|
|
+ .union(self._dirty_computed_vars(include_backend=False))
|
|
|
.union(self._always_dirty_computed_vars)
|
|
|
)
|
|
|
|
|
|
subdelta = {
|
|
|
prop: getattr(self, prop)
|
|
|
for prop in delta_vars
|
|
|
- if not types.is_backend_variable(prop, type(self))
|
|
|
+ if not types.is_backend_base_variable(prop, type(self))
|
|
|
}
|
|
|
if len(subdelta) > 0:
|
|
|
delta[self.get_full_name()] = subdelta
|
|
@@ -1727,12 +1726,14 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
|
|
|
else self.get_value(getattr(self, prop_name))
|
|
|
)
|
|
|
for prop_name, cv in self.computed_vars.items()
|
|
|
+ if not cv._backend
|
|
|
}
|
|
|
elif include_computed:
|
|
|
computed_vars = {
|
|
|
# Include the computed vars.
|
|
|
prop_name: self.get_value(getattr(self, prop_name))
|
|
|
- for prop_name in self.computed_vars
|
|
|
+ for prop_name, cv in self.computed_vars.items()
|
|
|
+ if not cv._backend
|
|
|
}
|
|
|
else:
|
|
|
computed_vars = {}
|
|
@@ -1745,6 +1746,7 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
|
|
|
for v in self.substates.values()
|
|
|
]:
|
|
|
d.update(substate_d)
|
|
|
+
|
|
|
return d
|
|
|
|
|
|
async def __aenter__(self) -> BaseState:
|