
* WiP * Save the var from get_var_name * flatten StateManagerRedis.get_state algorithm simplify fetching of states and avoid repeatedly fetching the same state * Get all the states in a single redis round-trip * update docstrings in StateManagerRedis * Move computed var dep tracking to separate module * Fix pre-commit issues * ComputedVar.add_dependency: explicitly dependency declaration Allow var dependencies to be added at runtime, for example, when defining a ComponentState that depends on vars that cannot be known statically. Fix more pyright issues. * Fix/ignore more pyright issues from recent merge * handle cleaning out _potentially_dirty_states on reload * ignore accessed attributes missing on state class these might be added dynamically later in which case we recompute the dependency tracking dicts... if not, they'll blow up anyway at runtime. * fix playwright tests, which insist on running an asyncio loop --------- Co-authored-by: Khaleel Al-Adhami <khaleel.aladhami@gmail.com>
51 lines
1.5 KiB
Python
51 lines
1.5 KiB
Python
"""Middleware to hydrate the state."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import dataclasses
|
|
from typing import TYPE_CHECKING, Optional
|
|
|
|
from reflex import constants
|
|
from reflex.event import Event, get_hydrate_event
|
|
from reflex.middleware.middleware import Middleware
|
|
from reflex.state import BaseState, StateUpdate, _resolve_delta
|
|
|
|
if TYPE_CHECKING:
|
|
from reflex.app import App
|
|
|
|
|
|
@dataclasses.dataclass(init=True)
|
|
class HydrateMiddleware(Middleware):
|
|
"""Middleware to handle initial app hydration."""
|
|
|
|
async def preprocess(
|
|
self, app: App, state: BaseState, event: Event
|
|
) -> Optional[StateUpdate]:
|
|
"""Preprocess the event.
|
|
|
|
Args:
|
|
app: The app to apply the middleware to.
|
|
state: The client state.
|
|
event: The event to preprocess.
|
|
|
|
Returns:
|
|
An optional delta or list of state updates to return.
|
|
"""
|
|
# If this is not the hydrate event, return None
|
|
if event.name != get_hydrate_event(state):
|
|
return None
|
|
|
|
# Clear client storage, to respect clearing cookies
|
|
state._reset_client_storage()
|
|
|
|
# Mark state as not hydrated (until on_loads are complete)
|
|
setattr(state, constants.CompileVars.IS_HYDRATED, False)
|
|
|
|
# Get the initial state.
|
|
delta = await _resolve_delta(state.dict())
|
|
# since a full dict was captured, clean any dirtiness
|
|
state._clean()
|
|
|
|
# Return the state update.
|
|
return StateUpdate(delta=delta, events=[])
|