state.py 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723
  1. """Define the reflex state specification."""
  2. from __future__ import annotations
  3. import asyncio
  4. import contextlib
  5. import copy
  6. import dataclasses
  7. import functools
  8. import inspect
  9. import os
  10. import pickle
  11. import uuid
  12. from abc import ABC, abstractmethod
  13. from collections import defaultdict
  14. from hashlib import md5
  15. from pathlib import Path
  16. from types import FunctionType, MethodType
  17. from typing import (
  18. TYPE_CHECKING,
  19. Any,
  20. AsyncIterator,
  21. BinaryIO,
  22. Callable,
  23. ClassVar,
  24. Dict,
  25. List,
  26. Optional,
  27. Sequence,
  28. Set,
  29. Tuple,
  30. Type,
  31. Union,
  32. cast,
  33. get_type_hints,
  34. )
  35. from sqlalchemy.orm import DeclarativeBase
  36. from typing_extensions import Self
  37. from reflex.config import get_config
  38. from reflex.istate.data import RouterData
  39. from reflex.vars.base import (
  40. ComputedVar,
  41. DynamicRouteVar,
  42. Var,
  43. computed_var,
  44. dispatch,
  45. get_unique_variable_name,
  46. is_computed_var,
  47. )
  48. try:
  49. import pydantic.v1 as pydantic
  50. except ModuleNotFoundError:
  51. import pydantic
  52. import wrapt
  53. from redis.asyncio import Redis
  54. from redis.exceptions import ResponseError
  55. from reflex import constants
  56. from reflex.base import Base
  57. from reflex.event import (
  58. BACKGROUND_TASK_MARKER,
  59. Event,
  60. EventHandler,
  61. EventSpec,
  62. fix_events,
  63. )
  64. from reflex.utils import console, format, path_ops, prerequisites, types
  65. from reflex.utils.exceptions import (
  66. ComputedVarShadowsBaseVars,
  67. ComputedVarShadowsStateVar,
  68. DynamicRouteArgShadowsStateVar,
  69. EventHandlerShadowsBuiltInStateMethod,
  70. ImmutableStateError,
  71. LockExpiredError,
  72. SetUndefinedStateVarError,
  73. StateSchemaMismatchError,
  74. )
  75. from reflex.utils.exec import is_testing_env
  76. from reflex.utils.serializers import serializer
  77. from reflex.utils.types import override
  78. from reflex.vars import VarData
  79. if TYPE_CHECKING:
  80. from reflex.components.component import Component
  81. Delta = Dict[str, Any]
  82. var = computed_var
  83. # If the state is this large, it's considered a performance issue.
  84. TOO_LARGE_SERIALIZED_STATE = 100 * 1024 # 100kb
  85. def _no_chain_background_task(
  86. state_cls: Type["BaseState"], name: str, fn: Callable
  87. ) -> Callable:
  88. """Protect against directly chaining a background task from another event handler.
  89. Args:
  90. state_cls: The state class that the event handler is in.
  91. name: The name of the background task.
  92. fn: The background task coroutine function / generator.
  93. Returns:
  94. A compatible coroutine function / generator that raises a runtime error.
  95. Raises:
  96. TypeError: If the background task is not async.
  97. """
  98. call = f"{state_cls.__name__}.{name}"
  99. message = (
  100. f"Cannot directly call background task {name!r}, use "
  101. f"`yield {call}` or `return {call}` instead."
  102. )
  103. if inspect.iscoroutinefunction(fn):
  104. async def _no_chain_background_task_co(*args, **kwargs):
  105. raise RuntimeError(message)
  106. return _no_chain_background_task_co
  107. if inspect.isasyncgenfunction(fn):
  108. async def _no_chain_background_task_gen(*args, **kwargs):
  109. yield
  110. raise RuntimeError(message)
  111. return _no_chain_background_task_gen
  112. raise TypeError(f"{fn} is marked as a background task, but is not async.")
  113. def _substate_key(
  114. token: str,
  115. state_cls_or_name: BaseState | Type[BaseState] | str | Sequence[str],
  116. ) -> str:
  117. """Get the substate key.
  118. Args:
  119. token: The token of the state.
  120. state_cls_or_name: The state class/instance or name or sequence of name parts.
  121. Returns:
  122. The substate key.
  123. """
  124. if isinstance(state_cls_or_name, BaseState) or (
  125. isinstance(state_cls_or_name, type) and issubclass(state_cls_or_name, BaseState)
  126. ):
  127. state_cls_or_name = state_cls_or_name.get_full_name()
  128. elif isinstance(state_cls_or_name, (list, tuple)):
  129. state_cls_or_name = ".".join(state_cls_or_name)
  130. return f"{token}_{state_cls_or_name}"
  131. def _split_substate_key(substate_key: str) -> tuple[str, str]:
  132. """Split the substate key into token and state name.
  133. Args:
  134. substate_key: The substate key.
  135. Returns:
  136. Tuple of token and state name.
  137. """
  138. token, _, state_name = substate_key.partition("_")
  139. return token, state_name
  140. @dataclasses.dataclass(frozen=True, init=False)
  141. class EventHandlerSetVar(EventHandler):
  142. """A special event handler to wrap setvar functionality."""
  143. state_cls: Type[BaseState] = dataclasses.field(init=False)
  144. def __init__(self, state_cls: Type[BaseState]):
  145. """Initialize the EventHandlerSetVar.
  146. Args:
  147. state_cls: The state class that vars will be set on.
  148. """
  149. super().__init__(
  150. fn=type(self).setvar,
  151. state_full_name=state_cls.get_full_name(),
  152. )
  153. object.__setattr__(self, "state_cls", state_cls)
  154. def setvar(self, var_name: str, value: Any):
  155. """Set the state variable to the value of the event.
  156. Note: `self` here will be an instance of the state, not EventHandlerSetVar.
  157. Args:
  158. var_name: The name of the variable to set.
  159. value: The value to set the variable to.
  160. """
  161. getattr(self, constants.SETTER_PREFIX + var_name)(value)
  162. def __call__(self, *args: Any) -> EventSpec:
  163. """Performs pre-checks and munging on the provided args that will become an EventSpec.
  164. Args:
  165. *args: The event args.
  166. Returns:
  167. The (partial) EventSpec that will be used to create the event to setvar.
  168. Raises:
  169. AttributeError: If the given Var name does not exist on the state.
  170. EventHandlerValueError: If the given Var name is not a str
  171. """
  172. from reflex.utils.exceptions import EventHandlerValueError
  173. if args:
  174. if not isinstance(args[0], str):
  175. raise EventHandlerValueError(
  176. f"Var name must be passed as a string, got {args[0]!r}"
  177. )
  178. # Check that the requested Var setter exists on the State at compile time.
  179. if getattr(self.state_cls, constants.SETTER_PREFIX + args[0], None) is None:
  180. raise AttributeError(
  181. f"Variable `{args[0]}` cannot be set on `{self.state_cls.get_full_name()}`"
  182. )
  183. return super().__call__(*args)
  184. if TYPE_CHECKING:
  185. from pydantic.v1.fields import ModelField
  186. def get_var_for_field(cls: Type[BaseState], f: ModelField):
  187. """Get a Var instance for a Pydantic field.
  188. Args:
  189. cls: The state class.
  190. f: The Pydantic field.
  191. Returns:
  192. The Var instance.
  193. """
  194. field_name = format.format_state_name(cls.get_full_name()) + "." + f.name
  195. return dispatch(
  196. field_name=field_name,
  197. var_data=VarData.from_state(cls, f.name),
  198. result_var_type=f.outer_type_,
  199. )
  200. class BaseState(Base, ABC, extra=pydantic.Extra.allow):
  201. """The state of the app."""
  202. # A map from the var name to the var.
  203. vars: ClassVar[Dict[str, Var]] = {}
  204. # The base vars of the class.
  205. base_vars: ClassVar[Dict[str, Var]] = {}
  206. # The computed vars of the class.
  207. computed_vars: ClassVar[Dict[str, ComputedVar]] = {}
  208. # Vars inherited by the parent state.
  209. inherited_vars: ClassVar[Dict[str, Var]] = {}
  210. # Backend base vars that are never sent to the client.
  211. backend_vars: ClassVar[Dict[str, Any]] = {}
  212. # Backend base vars inherited
  213. inherited_backend_vars: ClassVar[Dict[str, Any]] = {}
  214. # The event handlers.
  215. event_handlers: ClassVar[Dict[str, EventHandler]] = {}
  216. # A set of subclassses of this class.
  217. class_subclasses: ClassVar[Set[Type[BaseState]]] = set()
  218. # Mapping of var name to set of computed variables that depend on it
  219. _computed_var_dependencies: ClassVar[Dict[str, Set[str]]] = {}
  220. # Mapping of var name to set of substates that depend on it
  221. _substate_var_dependencies: ClassVar[Dict[str, Set[str]]] = {}
  222. # Set of vars which always need to be recomputed
  223. _always_dirty_computed_vars: ClassVar[Set[str]] = set()
  224. # Set of substates which always need to be recomputed
  225. _always_dirty_substates: ClassVar[Set[str]] = set()
  226. # The parent state.
  227. parent_state: Optional[BaseState] = None
  228. # The substates of the state.
  229. substates: Dict[str, BaseState] = {}
  230. # The set of dirty vars.
  231. dirty_vars: Set[str] = set()
  232. # The set of dirty substates.
  233. dirty_substates: Set[str] = set()
  234. # The routing path that triggered the state
  235. router_data: Dict[str, Any] = {}
  236. # Per-instance copy of backend base variable values
  237. _backend_vars: Dict[str, Any] = {}
  238. # The router data for the current page
  239. router: RouterData = RouterData()
  240. # Whether the state has ever been touched since instantiation.
  241. _was_touched: bool = False
  242. # Whether this state class is a mixin and should not be instantiated.
  243. _mixin: ClassVar[bool] = False
  244. # A special event handler for setting base vars.
  245. setvar: ClassVar[EventHandler]
  246. def __init__(
  247. self,
  248. *args,
  249. parent_state: BaseState | None = None,
  250. init_substates: bool = True,
  251. _reflex_internal_init: bool = False,
  252. **kwargs,
  253. ):
  254. """Initialize the state.
  255. DO NOT INSTANTIATE STATE CLASSES DIRECTLY! Use StateManager.get_state() instead.
  256. Args:
  257. *args: The args to pass to the Pydantic init method.
  258. parent_state: The parent state.
  259. init_substates: Whether to initialize the substates in this instance.
  260. _reflex_internal_init: A flag to indicate that the state is being initialized by the framework.
  261. **kwargs: The kwargs to pass to the Pydantic init method.
  262. Raises:
  263. ReflexRuntimeError: If the state is instantiated directly by end user.
  264. """
  265. from reflex.utils.exceptions import ReflexRuntimeError
  266. if not _reflex_internal_init and not is_testing_env():
  267. raise ReflexRuntimeError(
  268. "State classes should not be instantiated directly in a Reflex app. "
  269. "See https://reflex.dev/docs/state/ for further information."
  270. )
  271. kwargs["parent_state"] = parent_state
  272. super().__init__(*args, **kwargs)
  273. # Setup the substates (for memory state manager only).
  274. if init_substates:
  275. for substate in self.get_substates():
  276. self.substates[substate.get_name()] = substate(
  277. parent_state=self,
  278. _reflex_internal_init=True,
  279. )
  280. # Create a fresh copy of the backend variables for this instance
  281. self._backend_vars = copy.deepcopy(
  282. {name: item for name, item in self.backend_vars.items()}
  283. )
  284. def __repr__(self) -> str:
  285. """Get the string representation of the state.
  286. Returns:
  287. The string representation of the state.
  288. """
  289. return f"{self.__class__.__name__}({self.dict()})"
  290. @classmethod
  291. def _get_computed_vars(cls) -> list[ComputedVar]:
  292. """Helper function to get all computed vars of a instance.
  293. Returns:
  294. A list of computed vars.
  295. """
  296. return [
  297. v
  298. for mixin in cls._mixins() + [cls]
  299. for name, v in mixin.__dict__.items()
  300. if is_computed_var(v) and name not in cls.inherited_vars
  301. ]
  302. @classmethod
  303. def _validate_module_name(cls) -> None:
  304. """Check if the module name is valid.
  305. Reflex uses ___ as state name module separator.
  306. Raises:
  307. NameError: If the module name is invalid.
  308. """
  309. if "___" in cls.__module__:
  310. raise NameError(
  311. "The module name of a State class cannot contain '___'. "
  312. "Please rename the module."
  313. )
  314. @classmethod
  315. def __init_subclass__(cls, mixin: bool = False, **kwargs):
  316. """Do some magic for the subclass initialization.
  317. Args:
  318. mixin: Whether the subclass is a mixin and should not be initialized.
  319. **kwargs: The kwargs to pass to the pydantic init_subclass method.
  320. Raises:
  321. StateValueError: If a substate class shadows another.
  322. """
  323. from reflex.utils.exceptions import StateValueError
  324. super().__init_subclass__(**kwargs)
  325. cls._mixin = mixin
  326. if mixin:
  327. return
  328. # Validate the module name.
  329. cls._validate_module_name()
  330. # Event handlers should not shadow builtin state methods.
  331. cls._check_overridden_methods()
  332. # Computed vars should not shadow builtin state props.
  333. cls._check_overridden_basevars()
  334. # Reset subclass tracking for this class.
  335. cls.class_subclasses = set()
  336. # Reset dirty substate tracking for this class.
  337. cls._always_dirty_substates = set()
  338. # Get the parent vars.
  339. parent_state = cls.get_parent_state()
  340. if parent_state is not None:
  341. cls.inherited_vars = parent_state.vars
  342. cls.inherited_backend_vars = parent_state.backend_vars
  343. # Check if another substate class with the same name has already been defined.
  344. if cls.get_name() in set(
  345. c.get_name() for c in parent_state.class_subclasses
  346. ):
  347. # This should not happen, since we have added module prefix to state names in #3214
  348. raise StateValueError(
  349. f"The substate class '{cls.get_name()}' has been defined multiple times. "
  350. "Shadowing substate classes is not allowed."
  351. )
  352. # Track this new subclass in the parent state's subclasses set.
  353. parent_state.class_subclasses.add(cls)
  354. # Get computed vars.
  355. computed_vars = cls._get_computed_vars()
  356. cls._check_overridden_computed_vars()
  357. new_backend_vars = {
  358. name: value
  359. for name, value in cls.__dict__.items()
  360. if types.is_backend_base_variable(name, cls)
  361. }
  362. # Add annotated backend vars that do not have a default value.
  363. new_backend_vars.update(
  364. {
  365. name: Var("", _var_type=annotation_value).get_default_value()
  366. for name, annotation_value in get_type_hints(cls).items()
  367. if name not in new_backend_vars
  368. and types.is_backend_base_variable(name, cls)
  369. }
  370. )
  371. cls.backend_vars = {
  372. **cls.inherited_backend_vars,
  373. **new_backend_vars,
  374. }
  375. # Set the base and computed vars.
  376. cls.base_vars = {
  377. f.name: get_var_for_field(cls, f)
  378. for f in cls.get_fields().values()
  379. if f.name not in cls.get_skip_vars()
  380. }
  381. cls.computed_vars = {
  382. v._js_expr: v._replace(merge_var_data=VarData.from_state(cls))
  383. for v in computed_vars
  384. }
  385. cls.vars = {
  386. **cls.inherited_vars,
  387. **cls.base_vars,
  388. **cls.computed_vars,
  389. }
  390. cls.event_handlers = {}
  391. # Setup the base vars at the class level.
  392. for prop in cls.base_vars.values():
  393. cls._init_var(prop)
  394. # Set up the event handlers.
  395. events = {
  396. name: fn
  397. for name, fn in cls.__dict__.items()
  398. if cls._item_is_event_handler(name, fn)
  399. }
  400. for mixin in cls._mixins():
  401. for name, value in mixin.__dict__.items():
  402. if name in cls.inherited_vars:
  403. continue
  404. if is_computed_var(value):
  405. fget = cls._copy_fn(value.fget)
  406. newcv = value._replace(fget=fget, _var_data=VarData.from_state(cls))
  407. # cleanup refs to mixin cls in var_data
  408. setattr(cls, name, newcv)
  409. cls.computed_vars[newcv._js_expr] = newcv
  410. cls.vars[newcv._js_expr] = newcv
  411. continue
  412. if types.is_backend_base_variable(name, mixin):
  413. cls.backend_vars[name] = copy.deepcopy(value)
  414. continue
  415. if events.get(name) is not None:
  416. continue
  417. if not cls._item_is_event_handler(name, value):
  418. continue
  419. if parent_state is not None and parent_state.event_handlers.get(name):
  420. continue
  421. value = cls._copy_fn(value)
  422. value.__qualname__ = f"{cls.__name__}.{name}"
  423. events[name] = value
  424. # Create the setvar event handler for this state
  425. cls._create_setvar()
  426. for name, fn in events.items():
  427. handler = cls._create_event_handler(fn)
  428. cls.event_handlers[name] = handler
  429. setattr(cls, name, handler)
  430. # Initialize per-class var dependency tracking.
  431. cls._computed_var_dependencies = defaultdict(set)
  432. cls._substate_var_dependencies = defaultdict(set)
  433. cls._init_var_dependency_dicts()
  434. @staticmethod
  435. def _copy_fn(fn: Callable) -> Callable:
  436. """Copy a function. Used to copy ComputedVars and EventHandlers from mixins.
  437. Args:
  438. fn: The function to copy.
  439. Returns:
  440. The copied function.
  441. """
  442. newfn = FunctionType(
  443. fn.__code__,
  444. fn.__globals__,
  445. name=fn.__name__,
  446. argdefs=fn.__defaults__,
  447. closure=fn.__closure__,
  448. )
  449. newfn.__annotations__ = fn.__annotations__
  450. if mark := getattr(fn, BACKGROUND_TASK_MARKER, None):
  451. setattr(newfn, BACKGROUND_TASK_MARKER, mark)
  452. return newfn
  453. @staticmethod
  454. def _item_is_event_handler(name: str, value: Any) -> bool:
  455. """Check if the item is an event handler.
  456. Args:
  457. name: The name of the item.
  458. value: The value of the item.
  459. Returns:
  460. Whether the item is an event handler.
  461. """
  462. return (
  463. not name.startswith("_")
  464. and isinstance(value, Callable)
  465. and not isinstance(value, EventHandler)
  466. and hasattr(value, "__code__")
  467. )
  468. @classmethod
  469. def _evaluate(
  470. cls, f: Callable[[Self], Any], of_type: Union[type, None] = None
  471. ) -> Var:
  472. """Evaluate a function to a ComputedVar. Experimental.
  473. Args:
  474. f: The function to evaluate.
  475. of_type: The type of the ComputedVar. Defaults to Component.
  476. Returns:
  477. The ComputedVar.
  478. """
  479. console.warn(
  480. "The _evaluate method is experimental and may be removed in future versions."
  481. )
  482. from reflex.components.component import Component
  483. of_type = of_type or Component
  484. unique_var_name = get_unique_variable_name()
  485. @computed_var(_js_expr=unique_var_name, return_type=of_type)
  486. def computed_var_func(state: Self):
  487. result = f(state)
  488. if not isinstance(result, of_type):
  489. console.warn(
  490. f"Inline ComputedVar {f} expected type {of_type}, got {type(result)}. "
  491. "You can specify expected type with `of_type` argument."
  492. )
  493. return result
  494. setattr(cls, unique_var_name, computed_var_func)
  495. cls.computed_vars[unique_var_name] = computed_var_func
  496. cls.vars[unique_var_name] = computed_var_func
  497. cls._update_substate_inherited_vars({unique_var_name: computed_var_func})
  498. cls._always_dirty_computed_vars.add(unique_var_name)
  499. return getattr(cls, unique_var_name)
  500. @classmethod
  501. def _mixins(cls) -> List[Type]:
  502. """Get the mixin classes of the state.
  503. Returns:
  504. The mixin classes of the state.
  505. """
  506. return [
  507. mixin
  508. for mixin in cls.__mro__
  509. if (
  510. mixin not in [pydantic.BaseModel, Base, cls]
  511. and issubclass(mixin, BaseState)
  512. and mixin._mixin is True
  513. )
  514. ]
  515. @classmethod
  516. def _init_var_dependency_dicts(cls):
  517. """Initialize the var dependency tracking dicts.
  518. Allows the state to know which vars each ComputedVar depends on and
  519. whether a ComputedVar depends on a var in its parent state.
  520. Additional updates tracking dicts for vars and substates that always
  521. need to be recomputed.
  522. """
  523. inherited_vars = set(cls.inherited_vars).union(
  524. set(cls.inherited_backend_vars),
  525. )
  526. for cvar_name, cvar in cls.computed_vars.items():
  527. # Add the dependencies.
  528. for var in cvar._deps(objclass=cls):
  529. cls._computed_var_dependencies[var].add(cvar_name)
  530. if var in inherited_vars:
  531. # track that this substate depends on its parent for this var
  532. state_name = cls.get_name()
  533. parent_state = cls.get_parent_state()
  534. while parent_state is not None and var in {
  535. **parent_state.vars,
  536. **parent_state.backend_vars,
  537. }:
  538. parent_state._substate_var_dependencies[var].add(state_name)
  539. state_name, parent_state = (
  540. parent_state.get_name(),
  541. parent_state.get_parent_state(),
  542. )
  543. # ComputedVar with cache=False always need to be recomputed
  544. cls._always_dirty_computed_vars = set(
  545. cvar_name
  546. for cvar_name, cvar in cls.computed_vars.items()
  547. if not cvar._cache
  548. )
  549. # Any substate containing a ComputedVar with cache=False always needs to be recomputed
  550. if cls._always_dirty_computed_vars:
  551. # Tell parent classes that this substate has always dirty computed vars
  552. state_name = cls.get_name()
  553. parent_state = cls.get_parent_state()
  554. while parent_state is not None:
  555. parent_state._always_dirty_substates.add(state_name)
  556. state_name, parent_state = (
  557. parent_state.get_name(),
  558. parent_state.get_parent_state(),
  559. )
  560. @classmethod
  561. def _check_overridden_methods(cls):
  562. """Check for shadow methods and raise error if any.
  563. Raises:
  564. EventHandlerShadowsBuiltInStateMethod: When an event handler shadows an inbuilt state method.
  565. """
  566. overridden_methods = set()
  567. state_base_functions = cls._get_base_functions()
  568. for name, method in inspect.getmembers(cls, inspect.isfunction):
  569. # Check if the method is overridden and not a dunder method
  570. if (
  571. not name.startswith("__")
  572. and method.__name__ in state_base_functions
  573. and state_base_functions[method.__name__] != method
  574. ):
  575. overridden_methods.add(method.__name__)
  576. for method_name in overridden_methods:
  577. raise EventHandlerShadowsBuiltInStateMethod(
  578. f"The event handler name `{method_name}` shadows a builtin State method; use a different name instead"
  579. )
  580. @classmethod
  581. def _check_overridden_basevars(cls):
  582. """Check for shadow base vars and raise error if any.
  583. Raises:
  584. ComputedVarShadowsBaseVars: When a computed var shadows a base var.
  585. """
  586. for computed_var_ in cls._get_computed_vars():
  587. if computed_var_._js_expr in cls.__annotations__:
  588. raise ComputedVarShadowsBaseVars(
  589. f"The computed var name `{computed_var_._js_expr}` shadows a base var in {cls.__module__}.{cls.__name__}; use a different name instead"
  590. )
  591. @classmethod
  592. def _check_overridden_computed_vars(cls) -> None:
  593. """Check for shadow computed vars and raise error if any.
  594. Raises:
  595. ComputedVarShadowsStateVar: When a computed var shadows another.
  596. """
  597. for name, cv in cls.__dict__.items():
  598. if not is_computed_var(cv):
  599. continue
  600. name = cv._js_expr
  601. if name in cls.inherited_vars or name in cls.inherited_backend_vars:
  602. raise ComputedVarShadowsStateVar(
  603. f"The computed var name `{cv._js_expr}` shadows a var in {cls.__module__}.{cls.__name__}; use a different name instead"
  604. )
  605. @classmethod
  606. def get_skip_vars(cls) -> set[str]:
  607. """Get the vars to skip when serializing.
  608. Returns:
  609. The vars to skip when serializing.
  610. """
  611. return (
  612. set(cls.inherited_vars)
  613. | {
  614. "parent_state",
  615. "substates",
  616. "dirty_vars",
  617. "dirty_substates",
  618. "router_data",
  619. }
  620. | types.RESERVED_BACKEND_VAR_NAMES
  621. )
  622. @classmethod
  623. @functools.lru_cache()
  624. def get_parent_state(cls) -> Type[BaseState] | None:
  625. """Get the parent state.
  626. Raises:
  627. ValueError: If more than one parent state is found.
  628. Returns:
  629. The parent state.
  630. """
  631. parent_states = [
  632. base
  633. for base in cls.__bases__
  634. if issubclass(base, BaseState) and base is not BaseState and not base._mixin
  635. ]
  636. if len(parent_states) >= 2:
  637. raise ValueError(f"Only one parent state is allowed {parent_states}.")
  638. return parent_states[0] if len(parent_states) == 1 else None # type: ignore
  639. @classmethod
  640. def get_substates(cls) -> set[Type[BaseState]]:
  641. """Get the substates of the state.
  642. Returns:
  643. The substates of the state.
  644. """
  645. return cls.class_subclasses
  646. @classmethod
  647. @functools.lru_cache()
  648. def get_name(cls) -> str:
  649. """Get the name of the state.
  650. Returns:
  651. The name of the state.
  652. """
  653. module = cls.__module__.replace(".", "___")
  654. return format.to_snake_case(f"{module}___{cls.__name__}")
  655. @classmethod
  656. @functools.lru_cache()
  657. def get_full_name(cls) -> str:
  658. """Get the full name of the state.
  659. Returns:
  660. The full name of the state.
  661. """
  662. name = cls.get_name()
  663. parent_state = cls.get_parent_state()
  664. if parent_state is not None:
  665. name = ".".join((parent_state.get_full_name(), name))
  666. return name
  667. @classmethod
  668. @functools.lru_cache()
  669. def get_class_substate(cls, path: Sequence[str] | str) -> Type[BaseState]:
  670. """Get the class substate.
  671. Args:
  672. path: The path to the substate.
  673. Returns:
  674. The class substate.
  675. Raises:
  676. ValueError: If the substate is not found.
  677. """
  678. if isinstance(path, str):
  679. path = tuple(path.split("."))
  680. if len(path) == 0:
  681. return cls
  682. if path[0] == cls.get_name():
  683. if len(path) == 1:
  684. return cls
  685. path = path[1:]
  686. for substate in cls.get_substates():
  687. if path[0] == substate.get_name():
  688. return substate.get_class_substate(path[1:])
  689. raise ValueError(f"Invalid path: {path}")
  690. @classmethod
  691. def get_class_var(cls, path: Sequence[str]) -> Any:
  692. """Get the class var.
  693. Args:
  694. path: The path to the var.
  695. Returns:
  696. The class var.
  697. Raises:
  698. ValueError: If the path is invalid.
  699. """
  700. path, name = path[:-1], path[-1]
  701. substate = cls.get_class_substate(tuple(path))
  702. if not hasattr(substate, name):
  703. raise ValueError(f"Invalid path: {path}")
  704. return getattr(substate, name)
  705. @classmethod
  706. def _init_var(cls, prop: Var):
  707. """Initialize a variable.
  708. Args:
  709. prop: The variable to initialize
  710. Raises:
  711. VarTypeError: if the variable has an incorrect type
  712. """
  713. from reflex.utils.exceptions import VarTypeError
  714. if not types.is_valid_var_type(prop._var_type):
  715. raise VarTypeError(
  716. "State vars must be primitive Python types, "
  717. "Plotly figures, Pandas dataframes, "
  718. "or subclasses of rx.Base. "
  719. f'Found var "{prop._js_expr}" with type {prop._var_type}.'
  720. )
  721. cls._set_var(prop)
  722. cls._create_setter(prop)
  723. cls._set_default_value(prop)
  724. @classmethod
  725. def add_var(cls, name: str, type_: Any, default_value: Any = None):
  726. """Add dynamically a variable to the State.
  727. The variable added this way can be used in the same way as a variable
  728. defined statically in the model.
  729. Args:
  730. name: The name of the variable
  731. type_: The type of the variable
  732. default_value: The default value of the variable
  733. Raises:
  734. NameError: if a variable of this name already exists
  735. """
  736. if name in cls.__fields__:
  737. raise NameError(
  738. f"The variable '{name}' already exist. Use a different name"
  739. )
  740. # create the variable based on name and type
  741. var = Var(
  742. _js_expr=format.format_state_name(cls.get_full_name()) + "." + name,
  743. _var_type=type_,
  744. _var_data=VarData.from_state(cls, name),
  745. ).guess_type()
  746. # add the pydantic field dynamically (must be done before _init_var)
  747. cls.add_field(var, default_value)
  748. cls._init_var(var)
  749. # update the internal dicts so the new variable is correctly handled
  750. cls.base_vars.update({name: var})
  751. cls.vars.update({name: var})
  752. # let substates know about the new variable
  753. for substate_class in cls.class_subclasses:
  754. substate_class.vars.setdefault(name, var)
  755. # Reinitialize dependency tracking dicts.
  756. cls._init_var_dependency_dicts()
  757. @classmethod
  758. def _set_var(cls, prop: Var):
  759. """Set the var as a class member.
  760. Args:
  761. prop: The var instance to set.
  762. """
  763. setattr(cls, prop._var_field_name, prop)
  764. @classmethod
  765. def _create_event_handler(cls, fn):
  766. """Create an event handler for the given function.
  767. Args:
  768. fn: The function to create an event handler for.
  769. Returns:
  770. The event handler.
  771. """
  772. return EventHandler(fn=fn, state_full_name=cls.get_full_name())
  773. @classmethod
  774. def _create_setvar(cls):
  775. """Create the setvar method for the state."""
  776. cls.setvar = cls.event_handlers["setvar"] = EventHandlerSetVar(state_cls=cls)
  777. @classmethod
  778. def _create_setter(cls, prop: Var):
  779. """Create a setter for the var.
  780. Args:
  781. prop: The var to create a setter for.
  782. """
  783. setter_name = prop.get_setter_name(include_state=False)
  784. if setter_name not in cls.__dict__:
  785. event_handler = cls._create_event_handler(prop.get_setter())
  786. cls.event_handlers[setter_name] = event_handler
  787. setattr(cls, setter_name, event_handler)
  788. @classmethod
  789. def _set_default_value(cls, prop: Var):
  790. """Set the default value for the var.
  791. Args:
  792. prop: The var to set the default value for.
  793. """
  794. # Get the pydantic field for the var.
  795. field = cls.get_fields()[prop._var_field_name]
  796. if field.required:
  797. default_value = prop.get_default_value()
  798. if default_value is not None:
  799. field.required = False
  800. field.default = default_value
  801. if (
  802. not field.required
  803. and field.default is None
  804. and not types.is_optional(prop._var_type)
  805. ):
  806. # Ensure frontend uses null coalescing when accessing.
  807. object.__setattr__(prop, "_var_type", Optional[prop._var_type])
  808. @staticmethod
  809. def _get_base_functions() -> dict[str, FunctionType]:
  810. """Get all functions of the state class excluding dunder methods.
  811. Returns:
  812. The functions of rx.State class as a dict.
  813. """
  814. return {
  815. func[0]: func[1]
  816. for func in inspect.getmembers(BaseState, predicate=inspect.isfunction)
  817. if not func[0].startswith("__")
  818. }
  819. @classmethod
  820. def _update_substate_inherited_vars(cls, vars_to_add: dict[str, Var]):
  821. """Update the inherited vars of substates recursively when new vars are added.
  822. Also updates the var dependency tracking dicts after adding vars.
  823. Args:
  824. vars_to_add: names to Var instances to add to substates
  825. """
  826. for substate_class in cls.class_subclasses:
  827. for name, var in vars_to_add.items():
  828. if types.is_backend_base_variable(name, cls):
  829. substate_class.backend_vars.setdefault(name, var)
  830. substate_class.inherited_backend_vars.setdefault(name, var)
  831. else:
  832. substate_class.vars.setdefault(name, var)
  833. substate_class.inherited_vars.setdefault(name, var)
  834. substate_class._update_substate_inherited_vars(vars_to_add)
  835. # Reinitialize dependency tracking dicts.
  836. cls._init_var_dependency_dicts()
  837. @classmethod
  838. def setup_dynamic_args(cls, args: dict[str, str]):
  839. """Set up args for easy access in renderer.
  840. Args:
  841. args: a dict of args
  842. """
  843. if not args:
  844. return
  845. cls._check_overwritten_dynamic_args(list(args.keys()))
  846. def argsingle_factory(param):
  847. def inner_func(self) -> str:
  848. return self.router.page.params.get(param, "")
  849. return inner_func
  850. def arglist_factory(param):
  851. def inner_func(self) -> List[str]:
  852. return self.router.page.params.get(param, [])
  853. return inner_func
  854. dynamic_vars = {}
  855. for param, value in args.items():
  856. if value == constants.RouteArgType.SINGLE:
  857. func = argsingle_factory(param)
  858. elif value == constants.RouteArgType.LIST:
  859. func = arglist_factory(param)
  860. else:
  861. continue
  862. dynamic_vars[param] = DynamicRouteVar(
  863. fget=func,
  864. cache=True,
  865. _js_expr=param,
  866. _var_data=VarData.from_state(cls),
  867. )
  868. setattr(cls, param, dynamic_vars[param])
  869. # Update tracking dicts.
  870. cls.computed_vars.update(dynamic_vars)
  871. cls.vars.update(dynamic_vars)
  872. cls._update_substate_inherited_vars(dynamic_vars)
  873. @classmethod
  874. def _check_overwritten_dynamic_args(cls, args: list[str]):
  875. """Check if dynamic args are shadowing existing vars. Recursively checks all child states.
  876. Args:
  877. args: a dict of args
  878. Raises:
  879. DynamicRouteArgShadowsStateVar: If a dynamic arg is shadowing an existing var.
  880. """
  881. for arg in args:
  882. if (
  883. arg in cls.computed_vars
  884. and not isinstance(cls.computed_vars[arg], DynamicRouteVar)
  885. ) or arg in cls.base_vars:
  886. raise DynamicRouteArgShadowsStateVar(
  887. f"Dynamic route arg '{arg}' is shadowing an existing var in {cls.__module__}.{cls.__name__}"
  888. )
  889. for substate in cls.get_substates():
  890. substate._check_overwritten_dynamic_args(args)
  891. def __getattribute__(self, name: str) -> Any:
  892. """Get the state var.
  893. If the var is inherited, get the var from the parent state.
  894. Args:
  895. name: The name of the var.
  896. Returns:
  897. The value of the var.
  898. """
  899. # If the state hasn't been initialized yet, return the default value.
  900. if not super().__getattribute__("__dict__"):
  901. return super().__getattribute__(name)
  902. inherited_vars = {
  903. **super().__getattribute__("inherited_vars"),
  904. **super().__getattribute__("inherited_backend_vars"),
  905. }
  906. # For now, handle router_data updates as a special case.
  907. if name in inherited_vars or name == constants.ROUTER_DATA:
  908. parent_state = super().__getattribute__("parent_state")
  909. if parent_state is not None:
  910. return getattr(parent_state, name)
  911. # Allow event handlers to be called on the instance directly.
  912. event_handlers = super().__getattribute__("event_handlers")
  913. if name in event_handlers:
  914. handler = event_handlers[name]
  915. if handler.is_background:
  916. fn = _no_chain_background_task(type(self), name, handler.fn)
  917. else:
  918. fn = functools.partial(handler.fn, self)
  919. fn.__module__ = handler.fn.__module__ # type: ignore
  920. fn.__qualname__ = handler.fn.__qualname__ # type: ignore
  921. return fn
  922. backend_vars = super().__getattribute__("_backend_vars")
  923. if name in backend_vars:
  924. value = backend_vars[name]
  925. else:
  926. value = super().__getattribute__(name)
  927. if isinstance(value, EventHandler):
  928. # The event handler is inherited from a parent, so let the parent convert
  929. # it to a callable function.
  930. parent_state = super().__getattribute__("parent_state")
  931. if parent_state is not None:
  932. return getattr(parent_state, name)
  933. if isinstance(value, MutableProxy.__mutable_types__) and (
  934. name in super().__getattribute__("base_vars") or name in backend_vars
  935. ):
  936. # track changes in mutable containers (list, dict, set, etc)
  937. return MutableProxy(wrapped=value, state=self, field_name=name)
  938. return value
  939. def __setattr__(self, name: str, value: Any):
  940. """Set the attribute.
  941. If the attribute is inherited, set the attribute on the parent state.
  942. Args:
  943. name: The name of the attribute.
  944. value: The value of the attribute.
  945. Raises:
  946. SetUndefinedStateVarError: If a value of a var is set without first defining it.
  947. """
  948. if isinstance(value, MutableProxy):
  949. # unwrap proxy objects when assigning back to the state
  950. value = value.__wrapped__
  951. # Set the var on the parent state.
  952. inherited_vars = {**self.inherited_vars, **self.inherited_backend_vars}
  953. if name in inherited_vars:
  954. setattr(self.parent_state, name, value)
  955. return
  956. if name in self.backend_vars:
  957. self._backend_vars.__setitem__(name, value)
  958. self.dirty_vars.add(name)
  959. self._mark_dirty()
  960. return
  961. if (
  962. name not in self.vars
  963. and name not in self.get_skip_vars()
  964. and not name.startswith("__")
  965. and not name.startswith(f"_{type(self).__name__}__")
  966. ):
  967. raise SetUndefinedStateVarError(
  968. f"The state variable '{name}' has not been defined in '{type(self).__name__}'. "
  969. f"All state variables must be declared before they can be set."
  970. )
  971. # Set the attribute.
  972. super().__setattr__(name, value)
  973. # Add the var to the dirty list.
  974. if name in self.vars or name in self._computed_var_dependencies:
  975. self.dirty_vars.add(name)
  976. self._mark_dirty()
  977. # For now, handle router_data updates as a special case
  978. if name == constants.ROUTER_DATA:
  979. self.dirty_vars.add(name)
  980. self._mark_dirty()
  981. def reset(self):
  982. """Reset all the base vars to their default values."""
  983. # Reset the base vars.
  984. fields = self.get_fields()
  985. for prop_name in self.base_vars:
  986. if prop_name == constants.ROUTER:
  987. continue # never reset the router data
  988. field = fields[prop_name]
  989. if default_factory := field.default_factory:
  990. default = default_factory()
  991. else:
  992. default = copy.deepcopy(field.default)
  993. setattr(self, prop_name, default)
  994. # Recursively reset the substates.
  995. for substate in self.substates.values():
  996. substate.reset()
  997. def _reset_client_storage(self):
  998. """Reset client storage base vars to their default values."""
  999. # Client-side storage is reset during hydrate so that clearing cookies
  1000. # on the browser also resets the values on the backend.
  1001. fields = self.get_fields()
  1002. for prop_name in self.base_vars:
  1003. field = fields[prop_name]
  1004. if isinstance(field.default, ClientStorageBase) or (
  1005. isinstance(field.type_, type)
  1006. and issubclass(field.type_, ClientStorageBase)
  1007. ):
  1008. setattr(self, prop_name, copy.deepcopy(field.default))
  1009. # Recursively reset the substate client storage.
  1010. for substate in self.substates.values():
  1011. substate._reset_client_storage()
  1012. def get_substate(self, path: Sequence[str]) -> BaseState:
  1013. """Get the substate.
  1014. Args:
  1015. path: The path to the substate.
  1016. Returns:
  1017. The substate.
  1018. Raises:
  1019. ValueError: If the substate is not found.
  1020. """
  1021. if len(path) == 0:
  1022. return self
  1023. if path[0] == self.get_name():
  1024. if len(path) == 1:
  1025. return self
  1026. path = path[1:]
  1027. if path[0] not in self.substates:
  1028. raise ValueError(f"Invalid path: {path}")
  1029. return self.substates[path[0]].get_substate(path[1:])
  1030. @classmethod
  1031. def _get_common_ancestor(cls, other: Type[BaseState]) -> str:
  1032. """Find the name of the nearest common ancestor shared by this and the other state.
  1033. Args:
  1034. other: The other state.
  1035. Returns:
  1036. Full name of the nearest common ancestor.
  1037. """
  1038. common_ancestor_parts = []
  1039. for part1, part2 in zip(
  1040. cls.get_full_name().split("."),
  1041. other.get_full_name().split("."),
  1042. ):
  1043. if part1 != part2:
  1044. break
  1045. common_ancestor_parts.append(part1)
  1046. return ".".join(common_ancestor_parts)
  1047. @classmethod
  1048. def _determine_missing_parent_states(
  1049. cls, target_state_cls: Type[BaseState]
  1050. ) -> tuple[str, list[str]]:
  1051. """Determine the missing parent states between the target_state_cls and common ancestor of this state.
  1052. Args:
  1053. target_state_cls: The class of the state to find missing parent states for.
  1054. Returns:
  1055. The name of the common ancestor and the list of missing parent states.
  1056. """
  1057. common_ancestor_name = cls._get_common_ancestor(target_state_cls)
  1058. common_ancestor_parts = common_ancestor_name.split(".")
  1059. target_state_parts = tuple(target_state_cls.get_full_name().split("."))
  1060. relative_target_state_parts = target_state_parts[len(common_ancestor_parts) :]
  1061. # Determine which parent states to fetch from the common ancestor down to the target_state_cls.
  1062. fetch_parent_states = [common_ancestor_name]
  1063. for relative_parent_state_name in relative_target_state_parts:
  1064. fetch_parent_states.append(
  1065. ".".join((fetch_parent_states[-1], relative_parent_state_name))
  1066. )
  1067. return common_ancestor_name, fetch_parent_states[1:-1]
  1068. def _get_parent_states(self) -> list[tuple[str, BaseState]]:
  1069. """Get all parent state instances up to the root of the state tree.
  1070. Returns:
  1071. A list of tuples containing the name and the instance of each parent state.
  1072. """
  1073. parent_states_with_name = []
  1074. parent_state = self
  1075. while parent_state.parent_state is not None:
  1076. parent_state = parent_state.parent_state
  1077. parent_states_with_name.append((parent_state.get_full_name(), parent_state))
  1078. return parent_states_with_name
  1079. def _get_root_state(self) -> BaseState:
  1080. """Get the root state of the state tree.
  1081. Returns:
  1082. The root state of the state tree.
  1083. """
  1084. parent_state = self
  1085. while parent_state.parent_state is not None:
  1086. parent_state = parent_state.parent_state
  1087. return parent_state
  1088. async def _populate_parent_states(self, target_state_cls: Type[BaseState]):
  1089. """Populate substates in the tree between the target_state_cls and common ancestor of this state.
  1090. Args:
  1091. target_state_cls: The class of the state to populate parent states for.
  1092. Returns:
  1093. The parent state instance of target_state_cls.
  1094. Raises:
  1095. RuntimeError: If redis is not used in this backend process.
  1096. """
  1097. state_manager = get_state_manager()
  1098. if not isinstance(state_manager, StateManagerRedis):
  1099. raise RuntimeError(
  1100. f"Cannot populate parent states of {target_state_cls.get_full_name()} without redis. "
  1101. "(All states should already be available -- this is likely a bug).",
  1102. )
  1103. # Find the missing parent states up to the common ancestor.
  1104. (
  1105. common_ancestor_name,
  1106. missing_parent_states,
  1107. ) = self._determine_missing_parent_states(target_state_cls)
  1108. # Fetch all missing parent states and link them up to the common ancestor.
  1109. parent_states_tuple = self._get_parent_states()
  1110. root_state = parent_states_tuple[-1][1]
  1111. parent_states_by_name = dict(parent_states_tuple)
  1112. parent_state = parent_states_by_name[common_ancestor_name]
  1113. for parent_state_name in missing_parent_states:
  1114. try:
  1115. parent_state = root_state.get_substate(parent_state_name.split("."))
  1116. # The requested state is already cached, do NOT fetch it again.
  1117. continue
  1118. except ValueError:
  1119. # The requested state is missing, fetch from redis.
  1120. pass
  1121. parent_state = await state_manager.get_state(
  1122. token=_substate_key(
  1123. self.router.session.client_token, parent_state_name
  1124. ),
  1125. top_level=False,
  1126. get_substates=False,
  1127. parent_state=parent_state,
  1128. )
  1129. # Return the direct parent of target_state_cls for subsequent linking.
  1130. return parent_state
  1131. def _get_state_from_cache(self, state_cls: Type[BaseState]) -> BaseState:
  1132. """Get a state instance from the cache.
  1133. Args:
  1134. state_cls: The class of the state.
  1135. Returns:
  1136. The instance of state_cls associated with this state's client_token.
  1137. """
  1138. root_state = self._get_root_state()
  1139. return root_state.get_substate(state_cls.get_full_name().split("."))
  1140. async def _get_state_from_redis(self, state_cls: Type[BaseState]) -> BaseState:
  1141. """Get a state instance from redis.
  1142. Args:
  1143. state_cls: The class of the state.
  1144. Returns:
  1145. The instance of state_cls associated with this state's client_token.
  1146. Raises:
  1147. RuntimeError: If redis is not used in this backend process.
  1148. """
  1149. # Fetch all missing parent states from redis.
  1150. parent_state_of_state_cls = await self._populate_parent_states(state_cls)
  1151. # Then get the target state and all its substates.
  1152. state_manager = get_state_manager()
  1153. if not isinstance(state_manager, StateManagerRedis):
  1154. raise RuntimeError(
  1155. f"Requested state {state_cls.get_full_name()} is not cached and cannot be accessed without redis. "
  1156. "(All states should already be available -- this is likely a bug).",
  1157. )
  1158. return await state_manager.get_state(
  1159. token=_substate_key(self.router.session.client_token, state_cls),
  1160. top_level=False,
  1161. get_substates=True,
  1162. parent_state=parent_state_of_state_cls,
  1163. )
  1164. async def get_state(self, state_cls: Type[BaseState]) -> BaseState:
  1165. """Get an instance of the state associated with this token.
  1166. Allows for arbitrary access to sibling states from within an event handler.
  1167. Args:
  1168. state_cls: The class of the state.
  1169. Returns:
  1170. The instance of state_cls associated with this state's client_token.
  1171. """
  1172. # Fast case - if this state instance is already cached, get_substate from root state.
  1173. try:
  1174. return self._get_state_from_cache(state_cls)
  1175. except ValueError:
  1176. pass
  1177. # Slow case - fetch missing parent states from redis.
  1178. return await self._get_state_from_redis(state_cls)
  1179. def _get_event_handler(
  1180. self, event: Event
  1181. ) -> tuple[BaseState | StateProxy, EventHandler]:
  1182. """Get the event handler for the given event.
  1183. Args:
  1184. event: The event to get the handler for.
  1185. Returns:
  1186. The event handler.
  1187. Raises:
  1188. ValueError: If the event handler or substate is not found.
  1189. """
  1190. # Get the event handler.
  1191. path = event.name.split(".")
  1192. path, name = path[:-1], path[-1]
  1193. substate = self.get_substate(path)
  1194. if not substate:
  1195. raise ValueError(
  1196. "The value of state cannot be None when processing an event."
  1197. )
  1198. handler = substate.event_handlers[name]
  1199. # For background tasks, proxy the state
  1200. if handler.is_background:
  1201. substate = StateProxy(substate)
  1202. return substate, handler
  1203. async def _process(self, event: Event) -> AsyncIterator[StateUpdate]:
  1204. """Obtain event info and process event.
  1205. Args:
  1206. event: The event to process.
  1207. Yields:
  1208. The state update after processing the event.
  1209. """
  1210. # Get the event handler.
  1211. substate, handler = self._get_event_handler(event)
  1212. # Run the event generator and yield state updates.
  1213. async for update in self._process_event(
  1214. handler=handler,
  1215. state=substate,
  1216. payload=event.payload,
  1217. ):
  1218. yield update
  1219. def _check_valid(self, handler: EventHandler, events: Any) -> Any:
  1220. """Check if the events yielded are valid. They must be EventHandlers or EventSpecs.
  1221. Args:
  1222. handler: EventHandler.
  1223. events: The events to be checked.
  1224. Raises:
  1225. TypeError: If any of the events are not valid.
  1226. Returns:
  1227. The events as they are if valid.
  1228. """
  1229. def _is_valid_type(events: Any) -> bool:
  1230. return isinstance(events, (Event, EventHandler, EventSpec))
  1231. if events is None or _is_valid_type(events):
  1232. return events
  1233. try:
  1234. if all(_is_valid_type(e) for e in events):
  1235. return events
  1236. except TypeError:
  1237. pass
  1238. raise TypeError(
  1239. f"Your handler {handler.fn.__qualname__} must only return/yield: None, Events or other EventHandlers referenced by their class (not using `self`)"
  1240. )
  1241. def _as_state_update(
  1242. self,
  1243. handler: EventHandler,
  1244. events: EventSpec | list[EventSpec] | None,
  1245. final: bool,
  1246. ) -> StateUpdate:
  1247. """Convert the events to a StateUpdate.
  1248. Fixes the events and checks for validity before converting.
  1249. Args:
  1250. handler: The handler where the events originated from.
  1251. events: The events to queue with the update.
  1252. final: Whether the handler is done processing.
  1253. Returns:
  1254. The valid StateUpdate containing the events and final flag.
  1255. """
  1256. # get the delta from the root of the state tree
  1257. state = self._get_root_state()
  1258. token = self.router.session.client_token
  1259. # Convert valid EventHandler and EventSpec into Event
  1260. fixed_events = fix_events(self._check_valid(handler, events), token)
  1261. try:
  1262. # Get the delta after processing the event.
  1263. delta = state.get_delta()
  1264. state._clean()
  1265. return StateUpdate(
  1266. delta=delta,
  1267. events=fixed_events,
  1268. final=final if not handler.is_background else True,
  1269. )
  1270. except Exception as ex:
  1271. state._clean()
  1272. app_instance = getattr(prerequisites.get_app(), constants.CompileVars.APP)
  1273. event_specs = app_instance.backend_exception_handler(ex)
  1274. if event_specs is None:
  1275. return StateUpdate()
  1276. event_specs_correct_type = cast(
  1277. Union[List[Union[EventSpec, EventHandler]], None],
  1278. [event_specs] if isinstance(event_specs, EventSpec) else event_specs,
  1279. )
  1280. fixed_events = fix_events(
  1281. event_specs_correct_type,
  1282. token,
  1283. router_data=state.router_data,
  1284. )
  1285. return StateUpdate(
  1286. events=fixed_events,
  1287. final=True,
  1288. )
  1289. async def _process_event(
  1290. self, handler: EventHandler, state: BaseState | StateProxy, payload: Dict
  1291. ) -> AsyncIterator[StateUpdate]:
  1292. """Process event.
  1293. Args:
  1294. handler: EventHandler to process.
  1295. state: State to process the handler.
  1296. payload: The event payload.
  1297. Yields:
  1298. StateUpdate object
  1299. """
  1300. from reflex.utils import telemetry
  1301. # Get the function to process the event.
  1302. fn = functools.partial(handler.fn, state)
  1303. # Wrap the function in a try/except block.
  1304. try:
  1305. # Handle async functions.
  1306. if asyncio.iscoroutinefunction(fn.func):
  1307. events = await fn(**payload)
  1308. # Handle regular functions.
  1309. else:
  1310. events = fn(**payload)
  1311. # Handle async generators.
  1312. if inspect.isasyncgen(events):
  1313. async for event in events:
  1314. yield state._as_state_update(handler, event, final=False)
  1315. yield state._as_state_update(handler, events=None, final=True)
  1316. # Handle regular generators.
  1317. elif inspect.isgenerator(events):
  1318. try:
  1319. while True:
  1320. yield state._as_state_update(handler, next(events), final=False)
  1321. except StopIteration as si:
  1322. # the "return" value of the generator is not available
  1323. # in the loop, we must catch StopIteration to access it
  1324. if si.value is not None:
  1325. yield state._as_state_update(handler, si.value, final=False)
  1326. yield state._as_state_update(handler, events=None, final=True)
  1327. # Handle regular event chains.
  1328. else:
  1329. yield state._as_state_update(handler, events, final=True)
  1330. # If an error occurs, throw a window alert.
  1331. except Exception as ex:
  1332. telemetry.send_error(ex, context="backend")
  1333. app_instance = getattr(prerequisites.get_app(), constants.CompileVars.APP)
  1334. event_specs = app_instance.backend_exception_handler(ex)
  1335. yield state._as_state_update(
  1336. handler,
  1337. event_specs,
  1338. final=True,
  1339. )
  1340. def _mark_dirty_computed_vars(self) -> None:
  1341. """Mark ComputedVars that need to be recalculated based on dirty_vars."""
  1342. dirty_vars = self.dirty_vars
  1343. while dirty_vars:
  1344. calc_vars, dirty_vars = dirty_vars, set()
  1345. for cvar in self._dirty_computed_vars(from_vars=calc_vars):
  1346. self.dirty_vars.add(cvar)
  1347. dirty_vars.add(cvar)
  1348. actual_var = self.computed_vars.get(cvar)
  1349. if actual_var is not None:
  1350. actual_var.mark_dirty(instance=self)
  1351. def _expired_computed_vars(self) -> set[str]:
  1352. """Determine ComputedVars that need to be recalculated based on the expiration time.
  1353. Returns:
  1354. Set of computed vars to include in the delta.
  1355. """
  1356. return set(
  1357. cvar
  1358. for cvar in self.computed_vars
  1359. if self.computed_vars[cvar].needs_update(instance=self)
  1360. )
  1361. def _dirty_computed_vars(
  1362. self, from_vars: set[str] | None = None, include_backend: bool = True
  1363. ) -> set[str]:
  1364. """Determine ComputedVars that need to be recalculated based on the given vars.
  1365. Args:
  1366. from_vars: find ComputedVar that depend on this set of vars. If unspecified, will use the dirty_vars.
  1367. include_backend: whether to include backend vars in the calculation.
  1368. Returns:
  1369. Set of computed vars to include in the delta.
  1370. """
  1371. return set(
  1372. cvar
  1373. for dirty_var in from_vars or self.dirty_vars
  1374. for cvar in self._computed_var_dependencies[dirty_var]
  1375. if include_backend or not self.computed_vars[cvar]._backend
  1376. )
  1377. @classmethod
  1378. def _potentially_dirty_substates(cls) -> set[Type[BaseState]]:
  1379. """Determine substates which could be affected by dirty vars in this state.
  1380. Returns:
  1381. Set of State classes that may need to be fetched to recalc computed vars.
  1382. """
  1383. # _always_dirty_substates need to be fetched to recalc computed vars.
  1384. fetch_substates = set(
  1385. cls.get_class_substate((cls.get_name(), *substate_name.split(".")))
  1386. for substate_name in cls._always_dirty_substates
  1387. )
  1388. for dependent_substates in cls._substate_var_dependencies.values():
  1389. fetch_substates.update(
  1390. set(
  1391. cls.get_class_substate((cls.get_name(), *substate_name.split(".")))
  1392. for substate_name in dependent_substates
  1393. )
  1394. )
  1395. return fetch_substates
  1396. def get_delta(self) -> Delta:
  1397. """Get the delta for the state.
  1398. Returns:
  1399. The delta for the state.
  1400. """
  1401. delta = {}
  1402. # Apply dirty variables down into substates
  1403. self.dirty_vars.update(self._always_dirty_computed_vars)
  1404. self._mark_dirty()
  1405. frontend_computed_vars: set[str] = {
  1406. name for name, cv in self.computed_vars.items() if not cv._backend
  1407. }
  1408. # Return the dirty vars for this instance, any cached/dependent computed vars,
  1409. # and always dirty computed vars (cache=False)
  1410. delta_vars = (
  1411. self.dirty_vars.intersection(self.base_vars)
  1412. .union(self.dirty_vars.intersection(frontend_computed_vars))
  1413. .union(self._dirty_computed_vars(include_backend=False))
  1414. .union(self._always_dirty_computed_vars)
  1415. )
  1416. subdelta: Dict[str, Any] = {
  1417. prop: self.get_value(getattr(self, prop))
  1418. for prop in delta_vars
  1419. if not types.is_backend_base_variable(prop, type(self))
  1420. }
  1421. if len(subdelta) > 0:
  1422. delta[self.get_full_name()] = subdelta
  1423. # Recursively find the substate deltas.
  1424. substates = self.substates
  1425. for substate in self.dirty_substates.union(self._always_dirty_substates):
  1426. delta.update(substates[substate].get_delta())
  1427. # Return the delta.
  1428. return delta
  1429. def _mark_dirty(self):
  1430. """Mark the substate and all parent states as dirty."""
  1431. state_name = self.get_name()
  1432. if (
  1433. self.parent_state is not None
  1434. and state_name not in self.parent_state.dirty_substates
  1435. ):
  1436. self.parent_state.dirty_substates.add(self.get_name())
  1437. self.parent_state._mark_dirty()
  1438. # Append expired computed vars to dirty_vars to trigger recalculation
  1439. self.dirty_vars.update(self._expired_computed_vars())
  1440. # have to mark computed vars dirty to allow access to newly computed
  1441. # values within the same ComputedVar function
  1442. self._mark_dirty_computed_vars()
  1443. self._mark_dirty_substates()
  1444. def _mark_dirty_substates(self):
  1445. """Propagate dirty var / computed var status into substates."""
  1446. substates = self.substates
  1447. for var in self.dirty_vars:
  1448. for substate_name in self._substate_var_dependencies[var]:
  1449. self.dirty_substates.add(substate_name)
  1450. substate = substates[substate_name]
  1451. substate.dirty_vars.add(var)
  1452. substate._mark_dirty()
  1453. def _update_was_touched(self):
  1454. """Update the _was_touched flag based on dirty_vars."""
  1455. if self.dirty_vars and not self._was_touched:
  1456. for var in self.dirty_vars:
  1457. if var in self.base_vars or var in self._backend_vars:
  1458. self._was_touched = True
  1459. break
  1460. def _get_was_touched(self) -> bool:
  1461. """Check current dirty_vars and flag to determine if state instance was modified.
  1462. If any dirty vars belong to this state, mark _was_touched.
  1463. This flag determines whether this state instance should be persisted to redis.
  1464. Returns:
  1465. Whether this state instance was ever modified.
  1466. """
  1467. # Ensure the flag is up to date based on the current dirty_vars
  1468. self._update_was_touched()
  1469. return self._was_touched
  1470. def _clean(self):
  1471. """Reset the dirty vars."""
  1472. # Update touched status before cleaning dirty_vars.
  1473. self._update_was_touched()
  1474. # Recursively clean the substates.
  1475. for substate in self.dirty_substates:
  1476. if substate not in self.substates:
  1477. continue
  1478. self.substates[substate]._clean()
  1479. # Clean this state.
  1480. self.dirty_vars = set()
  1481. self.dirty_substates = set()
  1482. def get_value(self, key: str) -> Any:
  1483. """Get the value of a field (without proxying).
  1484. The returned value will NOT track dirty state updates.
  1485. Args:
  1486. key: The key of the field.
  1487. Returns:
  1488. The value of the field.
  1489. """
  1490. if isinstance(key, MutableProxy):
  1491. return super().get_value(key.__wrapped__)
  1492. return super().get_value(key)
  1493. def dict(
  1494. self, include_computed: bool = True, initial: bool = False, **kwargs
  1495. ) -> dict[str, Any]:
  1496. """Convert the object to a dictionary.
  1497. Args:
  1498. include_computed: Whether to include computed vars.
  1499. initial: Whether to get the initial value of computed vars.
  1500. **kwargs: Kwargs to pass to the pydantic dict method.
  1501. Returns:
  1502. The object as a dictionary.
  1503. """
  1504. if include_computed:
  1505. # Apply dirty variables down into substates to allow never-cached ComputedVar to
  1506. # trigger recalculation of dependent vars
  1507. self.dirty_vars.update(self._always_dirty_computed_vars)
  1508. self._mark_dirty()
  1509. def dictify(value: Any):
  1510. if dataclasses.is_dataclass(value) and not isinstance(value, type):
  1511. return dataclasses.asdict(value)
  1512. return value
  1513. base_vars = {
  1514. prop_name: dictify(self.get_value(getattr(self, prop_name)))
  1515. for prop_name in self.base_vars
  1516. }
  1517. if initial and include_computed:
  1518. computed_vars = {
  1519. # Include initial computed vars.
  1520. prop_name: (
  1521. cv._initial_value
  1522. if is_computed_var(cv)
  1523. and not isinstance(cv._initial_value, types.Unset)
  1524. else self.get_value(getattr(self, prop_name))
  1525. )
  1526. for prop_name, cv in self.computed_vars.items()
  1527. if not cv._backend
  1528. }
  1529. elif include_computed:
  1530. computed_vars = {
  1531. # Include the computed vars.
  1532. prop_name: self.get_value(getattr(self, prop_name))
  1533. for prop_name, cv in self.computed_vars.items()
  1534. if not cv._backend
  1535. }
  1536. else:
  1537. computed_vars = {}
  1538. variables = {**base_vars, **computed_vars}
  1539. d = {
  1540. self.get_full_name(): {k: variables[k] for k in sorted(variables)},
  1541. }
  1542. for substate_d in [
  1543. v.dict(include_computed=include_computed, initial=initial, **kwargs)
  1544. for v in self.substates.values()
  1545. ]:
  1546. d.update(substate_d)
  1547. return d
  1548. async def __aenter__(self) -> BaseState:
  1549. """Enter the async context manager protocol.
  1550. This should not be used for the State class, but exists for
  1551. type-compatibility with StateProxy.
  1552. Raises:
  1553. TypeError: always, because async contextmanager protocol is only supported for background task.
  1554. """
  1555. raise TypeError(
  1556. "Only background task should use `async with self` to modify state."
  1557. )
  1558. async def __aexit__(self, *exc_info: Any) -> None:
  1559. """Exit the async context manager protocol.
  1560. This should not be used for the State class, but exists for
  1561. type-compatibility with StateProxy.
  1562. Args:
  1563. exc_info: The exception info tuple.
  1564. """
  1565. pass
  1566. def __getstate__(self):
  1567. """Get the state for redis serialization.
  1568. This method is called by pickle to serialize the object.
  1569. It explicitly removes parent_state and substates because those are serialized separately
  1570. by the StateManagerRedis to allow for better horizontal scaling as state size increases.
  1571. Returns:
  1572. The state dict for serialization.
  1573. """
  1574. state = super().__getstate__()
  1575. # Never serialize parent_state or substates
  1576. state["__dict__"] = state["__dict__"].copy()
  1577. state["__dict__"]["parent_state"] = None
  1578. state["__dict__"]["substates"] = {}
  1579. state["__dict__"].pop("_was_touched", None)
  1580. return state
  1581. def _serialize(self) -> bytes:
  1582. """Serialize the state for redis.
  1583. Returns:
  1584. The serialized state.
  1585. """
  1586. return pickle.dumps((state_to_schema(self), self))
  1587. @classmethod
  1588. def _deserialize(
  1589. cls, data: bytes | None = None, fp: BinaryIO | None = None
  1590. ) -> BaseState:
  1591. """Deserialize the state from redis/disk.
  1592. data and fp are mutually exclusive, but one must be provided.
  1593. Args:
  1594. data: The serialized state data.
  1595. fp: The file pointer to the serialized state data.
  1596. Returns:
  1597. The deserialized state.
  1598. Raises:
  1599. ValueError: If both data and fp are provided, or neither are provided.
  1600. StateSchemaMismatchError: If the state schema does not match the expected schema.
  1601. """
  1602. if data is not None and fp is None:
  1603. (substate_schema, state) = pickle.loads(data)
  1604. elif fp is not None and data is None:
  1605. (substate_schema, state) = pickle.load(fp)
  1606. else:
  1607. raise ValueError("Only one of `data` or `fp` must be provided")
  1608. if substate_schema != state_to_schema(state):
  1609. raise StateSchemaMismatchError()
  1610. return state
  1611. class State(BaseState):
  1612. """The app Base State."""
  1613. # The hydrated bool.
  1614. is_hydrated: bool = False
  1615. class FrontendEventExceptionState(State):
  1616. """Substate for handling frontend exceptions."""
  1617. def handle_frontend_exception(self, stack: str) -> None:
  1618. """Handle frontend exceptions.
  1619. If a frontend exception handler is provided, it will be called.
  1620. Otherwise, the default frontend exception handler will be called.
  1621. Args:
  1622. stack: The stack trace of the exception.
  1623. """
  1624. app_instance = getattr(prerequisites.get_app(), constants.CompileVars.APP)
  1625. app_instance.frontend_exception_handler(Exception(stack))
  1626. class UpdateVarsInternalState(State):
  1627. """Substate for handling internal state var updates."""
  1628. async def update_vars_internal(self, vars: dict[str, Any]) -> None:
  1629. """Apply updates to fully qualified state vars.
  1630. The keys in `vars` should be in the form of `{state.get_full_name()}.{var_name}`,
  1631. and each value will be set on the appropriate substate instance.
  1632. This function is primarily used to apply cookie and local storage
  1633. updates from the frontend to the appropriate substate.
  1634. Args:
  1635. vars: The fully qualified vars and values to update.
  1636. """
  1637. for var, value in vars.items():
  1638. state_name, _, var_name = var.rpartition(".")
  1639. var_state_cls = State.get_class_substate(state_name)
  1640. var_state = await self.get_state(var_state_cls)
  1641. setattr(var_state, var_name, value)
  1642. class OnLoadInternalState(State):
  1643. """Substate for handling on_load event enumeration.
  1644. This is a separate substate to avoid deserializing the entire state tree for every page navigation.
  1645. """
  1646. def on_load_internal(self) -> list[Event | EventSpec] | None:
  1647. """Queue on_load handlers for the current page.
  1648. Returns:
  1649. The list of events to queue for on load handling.
  1650. """
  1651. # Do not app._compile()! It should be already compiled by now.
  1652. app = getattr(prerequisites.get_app(), constants.CompileVars.APP)
  1653. load_events = app.get_load_events(self.router.page.path)
  1654. if not load_events:
  1655. self.is_hydrated = True
  1656. return # Fast path for navigation with no on_load events defined.
  1657. self.is_hydrated = False
  1658. return [
  1659. *fix_events(
  1660. load_events,
  1661. self.router.session.client_token,
  1662. router_data=self.router_data,
  1663. ),
  1664. State.set_is_hydrated(True), # type: ignore
  1665. ]
  1666. class ComponentState(State, mixin=True):
  1667. """Base class to allow for the creation of a state instance per component.
  1668. This allows for the bundling of UI and state logic into a single class,
  1669. where each instance has a separate instance of the state.
  1670. Subclass this class and define vars and event handlers in the traditional way.
  1671. Then define a `get_component` method that returns the UI for the component instance.
  1672. See the full [docs](https://reflex.dev/docs/substates/component-state/) for more.
  1673. Basic example:
  1674. ```python
  1675. # Subclass ComponentState and define vars and event handlers.
  1676. class Counter(rx.ComponentState):
  1677. # Define vars that change.
  1678. count: int = 0
  1679. # Define event handlers.
  1680. def increment(self):
  1681. self.count += 1
  1682. def decrement(self):
  1683. self.count -= 1
  1684. @classmethod
  1685. def get_component(cls, **props):
  1686. # Access the state vars and event handlers using `cls`.
  1687. return rx.hstack(
  1688. rx.button("Decrement", on_click=cls.decrement),
  1689. rx.text(cls.count),
  1690. rx.button("Increment", on_click=cls.increment),
  1691. **props,
  1692. )
  1693. counter = Counter.create()
  1694. ```
  1695. """
  1696. # The number of components created from this class.
  1697. _per_component_state_instance_count: ClassVar[int] = 0
  1698. @classmethod
  1699. def __init_subclass__(cls, mixin: bool = True, **kwargs):
  1700. """Overwrite mixin default to True.
  1701. Args:
  1702. mixin: Whether the subclass is a mixin and should not be initialized.
  1703. **kwargs: The kwargs to pass to the pydantic init_subclass method.
  1704. """
  1705. super().__init_subclass__(mixin=mixin, **kwargs)
  1706. @classmethod
  1707. def get_component(cls, *children, **props) -> "Component":
  1708. """Get the component instance.
  1709. Args:
  1710. children: The children of the component.
  1711. props: The props of the component.
  1712. Raises:
  1713. NotImplementedError: if the subclass does not override this method.
  1714. """
  1715. raise NotImplementedError(
  1716. f"{cls.__name__} must implement get_component to return the component instance."
  1717. )
  1718. @classmethod
  1719. def create(cls, *children, **props) -> "Component":
  1720. """Create a new instance of the Component.
  1721. Args:
  1722. children: The children of the component.
  1723. props: The props of the component.
  1724. Returns:
  1725. A new instance of the Component with an independent copy of the State.
  1726. """
  1727. cls._per_component_state_instance_count += 1
  1728. state_cls_name = f"{cls.__name__}_n{cls._per_component_state_instance_count}"
  1729. component_state = type(
  1730. state_cls_name, (cls, State), {"__module__": __name__}, mixin=False
  1731. )
  1732. # Save a reference to the dynamic state for pickle/unpickle.
  1733. globals()[state_cls_name] = component_state
  1734. component = component_state.get_component(*children, **props)
  1735. component.State = component_state
  1736. return component
  1737. class StateProxy(wrapt.ObjectProxy):
  1738. """Proxy of a state instance to control mutability of vars for a background task.
  1739. Since a background task runs against a state instance without holding the
  1740. state_manager lock for the token, the reference may become stale if the same
  1741. state is modified by another event handler.
  1742. The proxy object ensures that writes to the state are blocked unless
  1743. explicitly entering a context which refreshes the state from state_manager
  1744. and holds the lock for the token until exiting the context. After exiting
  1745. the context, a StateUpdate may be emitted to the frontend to notify the
  1746. client of the state change.
  1747. A background task will be passed the `StateProxy` as `self`, so mutability
  1748. can be safely performed inside an `async with self` block.
  1749. class State(rx.State):
  1750. counter: int = 0
  1751. @rx.background
  1752. async def bg_increment(self):
  1753. await asyncio.sleep(1)
  1754. async with self:
  1755. self.counter += 1
  1756. """
  1757. def __init__(
  1758. self, state_instance, parent_state_proxy: Optional["StateProxy"] = None
  1759. ):
  1760. """Create a proxy for a state instance.
  1761. If `get_state` is used on a StateProxy, the resulting state will be
  1762. linked to the given state via parent_state_proxy. The first state in the
  1763. chain is the state that initiated the background task.
  1764. Args:
  1765. state_instance: The state instance to proxy.
  1766. parent_state_proxy: The parent state proxy, for linked mutability and context tracking.
  1767. """
  1768. super().__init__(state_instance)
  1769. # compile is not relevant to backend logic
  1770. self._self_app = getattr(prerequisites.get_app(), constants.CompileVars.APP)
  1771. self._self_substate_path = tuple(state_instance.get_full_name().split("."))
  1772. self._self_actx = None
  1773. self._self_mutable = False
  1774. self._self_actx_lock = asyncio.Lock()
  1775. self._self_actx_lock_holder = None
  1776. self._self_parent_state_proxy = parent_state_proxy
  1777. def _is_mutable(self) -> bool:
  1778. """Check if the state is mutable.
  1779. Returns:
  1780. Whether the state is mutable.
  1781. """
  1782. if self._self_parent_state_proxy is not None:
  1783. return self._self_parent_state_proxy._is_mutable() or self._self_mutable
  1784. return self._self_mutable
  1785. async def __aenter__(self) -> StateProxy:
  1786. """Enter the async context manager protocol.
  1787. Sets mutability to True and enters the `App.modify_state` async context,
  1788. which refreshes the state from state_manager and holds the lock for the
  1789. given state token until exiting the context.
  1790. Background tasks should avoid blocking calls while inside the context.
  1791. Returns:
  1792. This StateProxy instance in mutable mode.
  1793. Raises:
  1794. ImmutableStateError: If the state is already mutable.
  1795. """
  1796. if self._self_parent_state_proxy is not None:
  1797. parent_state = (
  1798. await self._self_parent_state_proxy.__aenter__()
  1799. ).__wrapped__
  1800. super().__setattr__(
  1801. "__wrapped__",
  1802. await parent_state.get_state(
  1803. State.get_class_substate(self._self_substate_path)
  1804. ),
  1805. )
  1806. return self
  1807. current_task = asyncio.current_task()
  1808. if (
  1809. self._self_actx_lock.locked()
  1810. and current_task == self._self_actx_lock_holder
  1811. ):
  1812. raise ImmutableStateError(
  1813. "The state is already mutable. Do not nest `async with self` blocks."
  1814. )
  1815. await self._self_actx_lock.acquire()
  1816. self._self_actx_lock_holder = current_task
  1817. self._self_actx = self._self_app.modify_state(
  1818. token=_substate_key(
  1819. self.__wrapped__.router.session.client_token,
  1820. self._self_substate_path,
  1821. )
  1822. )
  1823. mutable_state = await self._self_actx.__aenter__()
  1824. super().__setattr__(
  1825. "__wrapped__", mutable_state.get_substate(self._self_substate_path)
  1826. )
  1827. self._self_mutable = True
  1828. return self
  1829. async def __aexit__(self, *exc_info: Any) -> None:
  1830. """Exit the async context manager protocol.
  1831. Sets proxy mutability to False and persists any state changes.
  1832. Args:
  1833. exc_info: The exception info tuple.
  1834. """
  1835. if self._self_parent_state_proxy is not None:
  1836. await self._self_parent_state_proxy.__aexit__(*exc_info)
  1837. return
  1838. if self._self_actx is None:
  1839. return
  1840. self._self_mutable = False
  1841. try:
  1842. await self._self_actx.__aexit__(*exc_info)
  1843. finally:
  1844. self._self_actx_lock_holder = None
  1845. self._self_actx_lock.release()
  1846. self._self_actx = None
  1847. def __enter__(self):
  1848. """Enter the regular context manager protocol.
  1849. This is not supported for background tasks, and exists only to raise a more useful exception
  1850. when the StateProxy is used incorrectly.
  1851. Raises:
  1852. TypeError: always, because only async contextmanager protocol is supported.
  1853. """
  1854. raise TypeError("Background task must use `async with self` to modify state.")
  1855. def __exit__(self, *exc_info: Any) -> None:
  1856. """Exit the regular context manager protocol.
  1857. Args:
  1858. exc_info: The exception info tuple.
  1859. """
  1860. pass
  1861. def __getattr__(self, name: str) -> Any:
  1862. """Get the attribute from the underlying state instance.
  1863. Args:
  1864. name: The name of the attribute.
  1865. Returns:
  1866. The value of the attribute.
  1867. Raises:
  1868. ImmutableStateError: If the state is not in mutable mode.
  1869. """
  1870. if name in ["substates", "parent_state"] and not self._is_mutable():
  1871. raise ImmutableStateError(
  1872. "Background task StateProxy is immutable outside of a context "
  1873. "manager. Use `async with self` to modify state."
  1874. )
  1875. value = super().__getattr__(name)
  1876. if not name.startswith("_self_") and isinstance(value, MutableProxy):
  1877. # ensure mutations to these containers are blocked unless proxy is _mutable
  1878. return ImmutableMutableProxy(
  1879. wrapped=value.__wrapped__,
  1880. state=self, # type: ignore
  1881. field_name=value._self_field_name,
  1882. )
  1883. if isinstance(value, functools.partial) and value.args[0] is self.__wrapped__:
  1884. # Rebind event handler to the proxy instance
  1885. value = functools.partial(
  1886. value.func,
  1887. self,
  1888. *value.args[1:],
  1889. **value.keywords,
  1890. )
  1891. if isinstance(value, MethodType) and value.__self__ is self.__wrapped__:
  1892. # Rebind methods to the proxy instance
  1893. value = type(value)(value.__func__, self) # type: ignore
  1894. return value
  1895. def __setattr__(self, name: str, value: Any) -> None:
  1896. """Set the attribute on the underlying state instance.
  1897. If the attribute is internal, set it on the proxy instance instead.
  1898. Args:
  1899. name: The name of the attribute.
  1900. value: The value of the attribute.
  1901. Raises:
  1902. ImmutableStateError: If the state is not in mutable mode.
  1903. """
  1904. if (
  1905. name.startswith("_self_") # wrapper attribute
  1906. or self._is_mutable() # lock held
  1907. # non-persisted state attribute
  1908. or name in self.__wrapped__.get_skip_vars()
  1909. ):
  1910. super().__setattr__(name, value)
  1911. return
  1912. raise ImmutableStateError(
  1913. "Background task StateProxy is immutable outside of a context "
  1914. "manager. Use `async with self` to modify state."
  1915. )
  1916. def get_substate(self, path: Sequence[str]) -> BaseState:
  1917. """Only allow substate access with lock held.
  1918. Args:
  1919. path: The path to the substate.
  1920. Returns:
  1921. The substate.
  1922. Raises:
  1923. ImmutableStateError: If the state is not in mutable mode.
  1924. """
  1925. if not self._is_mutable():
  1926. raise ImmutableStateError(
  1927. "Background task StateProxy is immutable outside of a context "
  1928. "manager. Use `async with self` to modify state."
  1929. )
  1930. return self.__wrapped__.get_substate(path)
  1931. async def get_state(self, state_cls: Type[BaseState]) -> BaseState:
  1932. """Get an instance of the state associated with this token.
  1933. Args:
  1934. state_cls: The class of the state.
  1935. Returns:
  1936. The state.
  1937. Raises:
  1938. ImmutableStateError: If the state is not in mutable mode.
  1939. """
  1940. if not self._is_mutable():
  1941. raise ImmutableStateError(
  1942. "Background task StateProxy is immutable outside of a context "
  1943. "manager. Use `async with self` to modify state."
  1944. )
  1945. return type(self)(
  1946. await self.__wrapped__.get_state(state_cls), parent_state_proxy=self
  1947. )
  1948. def _as_state_update(self, *args, **kwargs) -> StateUpdate:
  1949. """Temporarily allow mutability to access parent_state.
  1950. Args:
  1951. *args: The args to pass to the underlying state instance.
  1952. **kwargs: The kwargs to pass to the underlying state instance.
  1953. Returns:
  1954. The state update.
  1955. """
  1956. original_mutable = self._self_mutable
  1957. self._self_mutable = True
  1958. try:
  1959. return self.__wrapped__._as_state_update(*args, **kwargs)
  1960. finally:
  1961. self._self_mutable = original_mutable
  1962. @dataclasses.dataclass(
  1963. frozen=True,
  1964. )
  1965. class StateUpdate:
  1966. """A state update sent to the frontend."""
  1967. # The state delta.
  1968. delta: Delta = dataclasses.field(default_factory=dict)
  1969. # Events to be added to the event queue.
  1970. events: List[Event] = dataclasses.field(default_factory=list)
  1971. # Whether this is the final state update for the event.
  1972. final: bool = True
  1973. def json(self) -> str:
  1974. """Convert the state update to a JSON string.
  1975. Returns:
  1976. The state update as a JSON string.
  1977. """
  1978. return format.json_dumps(self)
  1979. class StateManager(Base, ABC):
  1980. """A class to manage many client states."""
  1981. # The state class to use.
  1982. state: Type[BaseState]
  1983. @classmethod
  1984. def create(cls, state: Type[BaseState]):
  1985. """Create a new state manager.
  1986. Args:
  1987. state: The state class to use.
  1988. Returns:
  1989. The state manager (either memory or redis).
  1990. """
  1991. redis = prerequisites.get_redis()
  1992. if redis is not None:
  1993. # make sure expiration values are obtained only from the config object on creation
  1994. config = get_config()
  1995. return StateManagerRedis(
  1996. state=state,
  1997. redis=redis,
  1998. token_expiration=config.redis_token_expiration,
  1999. lock_expiration=config.redis_lock_expiration,
  2000. )
  2001. return StateManagerDisk(state=state)
  2002. @abstractmethod
  2003. async def get_state(self, token: str) -> BaseState:
  2004. """Get the state for a token.
  2005. Args:
  2006. token: The token to get the state for.
  2007. Returns:
  2008. The state for the token.
  2009. """
  2010. pass
  2011. @abstractmethod
  2012. async def set_state(self, token: str, state: BaseState):
  2013. """Set the state for a token.
  2014. Args:
  2015. token: The token to set the state for.
  2016. state: The state to set.
  2017. """
  2018. pass
  2019. @abstractmethod
  2020. @contextlib.asynccontextmanager
  2021. async def modify_state(self, token: str) -> AsyncIterator[BaseState]:
  2022. """Modify the state for a token while holding exclusive lock.
  2023. Args:
  2024. token: The token to modify the state for.
  2025. Yields:
  2026. The state for the token.
  2027. """
  2028. yield self.state()
  2029. class StateManagerMemory(StateManager):
  2030. """A state manager that stores states in memory."""
  2031. # The mapping of client ids to states.
  2032. states: Dict[str, BaseState] = {}
  2033. # The mutex ensures the dict of mutexes is updated exclusively
  2034. _state_manager_lock = asyncio.Lock()
  2035. # The dict of mutexes for each client
  2036. _states_locks: Dict[str, asyncio.Lock] = pydantic.PrivateAttr({})
  2037. class Config:
  2038. """The Pydantic config."""
  2039. fields = {
  2040. "_states_locks": {"exclude": True},
  2041. }
  2042. @override
  2043. async def get_state(self, token: str) -> BaseState:
  2044. """Get the state for a token.
  2045. Args:
  2046. token: The token to get the state for.
  2047. Returns:
  2048. The state for the token.
  2049. """
  2050. # Memory state manager ignores the substate suffix and always returns the top-level state.
  2051. token = _split_substate_key(token)[0]
  2052. if token not in self.states:
  2053. self.states[token] = self.state(_reflex_internal_init=True)
  2054. return self.states[token]
  2055. @override
  2056. async def set_state(self, token: str, state: BaseState):
  2057. """Set the state for a token.
  2058. Args:
  2059. token: The token to set the state for.
  2060. state: The state to set.
  2061. """
  2062. pass
  2063. @override
  2064. @contextlib.asynccontextmanager
  2065. async def modify_state(self, token: str) -> AsyncIterator[BaseState]:
  2066. """Modify the state for a token while holding exclusive lock.
  2067. Args:
  2068. token: The token to modify the state for.
  2069. Yields:
  2070. The state for the token.
  2071. """
  2072. # Memory state manager ignores the substate suffix and always returns the top-level state.
  2073. token = _split_substate_key(token)[0]
  2074. if token not in self._states_locks:
  2075. async with self._state_manager_lock:
  2076. if token not in self._states_locks:
  2077. self._states_locks[token] = asyncio.Lock()
  2078. async with self._states_locks[token]:
  2079. state = await self.get_state(token)
  2080. yield state
  2081. await self.set_state(token, state)
  2082. def _default_token_expiration() -> int:
  2083. """Get the default token expiration time.
  2084. Returns:
  2085. The default token expiration time.
  2086. """
  2087. return get_config().redis_token_expiration
  2088. def _serialize_type(type_: Any) -> str:
  2089. """Serialize a type.
  2090. Args:
  2091. type_: The type to serialize.
  2092. Returns:
  2093. The serialized type.
  2094. """
  2095. if not inspect.isclass(type_):
  2096. return f"{type_}"
  2097. return f"{type_.__module__}.{type_.__qualname__}"
  2098. def is_serializable(value: Any) -> bool:
  2099. """Check if a value is serializable.
  2100. Args:
  2101. value: The value to check.
  2102. Returns:
  2103. Whether the value is serializable.
  2104. """
  2105. try:
  2106. return bool(pickle.dumps(value))
  2107. except Exception:
  2108. return False
  2109. def state_to_schema(
  2110. state: BaseState,
  2111. ) -> List[Tuple[str, str, Any, Union[bool, None], Any]]:
  2112. """Convert a state to a schema.
  2113. Args:
  2114. state: The state to convert to a schema.
  2115. Returns:
  2116. The schema.
  2117. """
  2118. return list(
  2119. sorted(
  2120. (
  2121. field_name,
  2122. model_field.name,
  2123. _serialize_type(model_field.type_),
  2124. (
  2125. model_field.required
  2126. if isinstance(model_field.required, bool)
  2127. else None
  2128. ),
  2129. (model_field.default if is_serializable(model_field.default) else None),
  2130. )
  2131. for field_name, model_field in state.__fields__.items()
  2132. )
  2133. )
  2134. def reset_disk_state_manager():
  2135. """Reset the disk state manager."""
  2136. states_directory = prerequisites.get_web_dir() / constants.Dirs.STATES
  2137. if states_directory.exists():
  2138. for path in states_directory.iterdir():
  2139. path.unlink()
  2140. class StateManagerDisk(StateManager):
  2141. """A state manager that stores states in memory."""
  2142. # The mapping of client ids to states.
  2143. states: Dict[str, BaseState] = {}
  2144. # The mutex ensures the dict of mutexes is updated exclusively
  2145. _state_manager_lock = asyncio.Lock()
  2146. # The dict of mutexes for each client
  2147. _states_locks: Dict[str, asyncio.Lock] = pydantic.PrivateAttr({})
  2148. # The token expiration time (s).
  2149. token_expiration: int = pydantic.Field(default_factory=_default_token_expiration)
  2150. class Config:
  2151. """The Pydantic config."""
  2152. fields = {
  2153. "_states_locks": {"exclude": True},
  2154. }
  2155. keep_untouched = (functools.cached_property,)
  2156. def __init__(self, state: Type[BaseState]):
  2157. """Create a new state manager.
  2158. Args:
  2159. state: The state class to use.
  2160. """
  2161. super().__init__(state=state)
  2162. path_ops.mkdir(self.states_directory)
  2163. self._purge_expired_states()
  2164. @functools.cached_property
  2165. def states_directory(self) -> Path:
  2166. """Get the states directory.
  2167. Returns:
  2168. The states directory.
  2169. """
  2170. return prerequisites.get_web_dir() / constants.Dirs.STATES
  2171. def _purge_expired_states(self):
  2172. """Purge expired states from the disk."""
  2173. import time
  2174. for path in path_ops.ls(self.states_directory):
  2175. # check path is a pickle file
  2176. if path.suffix != ".pkl":
  2177. continue
  2178. # load last edited field from file
  2179. last_edited = path.stat().st_mtime
  2180. # check if the file is older than the token expiration time
  2181. if time.time() - last_edited > self.token_expiration:
  2182. # remove the file
  2183. path.unlink()
  2184. def token_path(self, token: str) -> Path:
  2185. """Get the path for a token.
  2186. Args:
  2187. token: The token to get the path for.
  2188. Returns:
  2189. The path for the token.
  2190. """
  2191. return (
  2192. self.states_directory / f"{md5(token.encode()).hexdigest()}.pkl"
  2193. ).absolute()
  2194. async def load_state(self, token: str) -> BaseState | None:
  2195. """Load a state object based on the provided token.
  2196. Args:
  2197. token: The token used to identify the state object.
  2198. Returns:
  2199. The loaded state object or None.
  2200. """
  2201. token_path = self.token_path(token)
  2202. if token_path.exists():
  2203. try:
  2204. with token_path.open(mode="rb") as file:
  2205. return BaseState._deserialize(fp=file)
  2206. except Exception:
  2207. pass
  2208. async def populate_substates(
  2209. self, client_token: str, state: BaseState, root_state: BaseState
  2210. ):
  2211. """Populate the substates of a state object.
  2212. Args:
  2213. client_token: The client token.
  2214. state: The state object to populate.
  2215. root_state: The root state object.
  2216. """
  2217. for substate in state.get_substates():
  2218. substate_token = _substate_key(client_token, substate)
  2219. instance = await self.load_state(substate_token)
  2220. if instance is None:
  2221. instance = await root_state.get_state(substate)
  2222. state.substates[substate.get_name()] = instance
  2223. instance.parent_state = state
  2224. await self.populate_substates(client_token, instance, root_state)
  2225. @override
  2226. async def get_state(
  2227. self,
  2228. token: str,
  2229. ) -> BaseState:
  2230. """Get the state for a token.
  2231. Args:
  2232. token: The token to get the state for.
  2233. Returns:
  2234. The state for the token.
  2235. """
  2236. client_token = _split_substate_key(token)[0]
  2237. root_state = self.states.get(client_token)
  2238. if root_state is not None:
  2239. # Retrieved state from memory.
  2240. return root_state
  2241. # Deserialize root state from disk.
  2242. root_state = await self.load_state(_substate_key(client_token, self.state))
  2243. # Create a new root state tree with all substates instantiated.
  2244. fresh_root_state = self.state(_reflex_internal_init=True)
  2245. if root_state is None:
  2246. root_state = fresh_root_state
  2247. else:
  2248. # Ensure all substates exist, even if they were not serialized previously.
  2249. root_state.substates = fresh_root_state.substates
  2250. self.states[client_token] = root_state
  2251. await self.populate_substates(client_token, root_state, root_state)
  2252. return root_state
  2253. async def set_state_for_substate(self, client_token: str, substate: BaseState):
  2254. """Set the state for a substate.
  2255. Args:
  2256. client_token: The client token.
  2257. substate: The substate to set.
  2258. """
  2259. substate_token = _substate_key(client_token, substate)
  2260. if substate._get_was_touched():
  2261. substate._was_touched = False # Reset the touched flag after serializing.
  2262. pickle_state = substate._serialize()
  2263. if not self.states_directory.exists():
  2264. self.states_directory.mkdir(parents=True, exist_ok=True)
  2265. self.token_path(substate_token).write_bytes(pickle_state)
  2266. for substate_substate in substate.substates.values():
  2267. await self.set_state_for_substate(client_token, substate_substate)
  2268. @override
  2269. async def set_state(self, token: str, state: BaseState):
  2270. """Set the state for a token.
  2271. Args:
  2272. token: The token to set the state for.
  2273. state: The state to set.
  2274. """
  2275. client_token, substate = _split_substate_key(token)
  2276. await self.set_state_for_substate(client_token, state)
  2277. @override
  2278. @contextlib.asynccontextmanager
  2279. async def modify_state(self, token: str) -> AsyncIterator[BaseState]:
  2280. """Modify the state for a token while holding exclusive lock.
  2281. Args:
  2282. token: The token to modify the state for.
  2283. Yields:
  2284. The state for the token.
  2285. """
  2286. # Memory state manager ignores the substate suffix and always returns the top-level state.
  2287. client_token, substate = _split_substate_key(token)
  2288. if client_token not in self._states_locks:
  2289. async with self._state_manager_lock:
  2290. if client_token not in self._states_locks:
  2291. self._states_locks[client_token] = asyncio.Lock()
  2292. async with self._states_locks[client_token]:
  2293. state = await self.get_state(token)
  2294. yield state
  2295. await self.set_state(token, state)
  2296. def _default_lock_expiration() -> int:
  2297. """Get the default lock expiration time.
  2298. Returns:
  2299. The default lock expiration time.
  2300. """
  2301. return get_config().redis_lock_expiration
  2302. class StateManagerRedis(StateManager):
  2303. """A state manager that stores states in redis."""
  2304. # The redis client to use.
  2305. redis: Redis
  2306. # The token expiration time (s).
  2307. token_expiration: int = pydantic.Field(default_factory=_default_token_expiration)
  2308. # The maximum time to hold a lock (ms).
  2309. lock_expiration: int = pydantic.Field(default_factory=_default_lock_expiration)
  2310. # The keyspace subscription string when redis is waiting for lock to be released
  2311. _redis_notify_keyspace_events: str = (
  2312. "K" # Enable keyspace notifications (target a particular key)
  2313. "g" # For generic commands (DEL, EXPIRE, etc)
  2314. "x" # For expired events
  2315. "e" # For evicted events (i.e. maxmemory exceeded)
  2316. )
  2317. # These events indicate that a lock is no longer held
  2318. _redis_keyspace_lock_release_events: Set[bytes] = {
  2319. b"del",
  2320. b"expire",
  2321. b"expired",
  2322. b"evicted",
  2323. }
  2324. # Only warn about each state class size once.
  2325. _warned_about_state_size: ClassVar[Set[str]] = set()
  2326. async def _get_parent_state(self, token: str) -> BaseState | None:
  2327. """Get the parent state for the state requested in the token.
  2328. Args:
  2329. token: The token to get the state for (_substate_key).
  2330. Returns:
  2331. The parent state for the state requested by the token or None if there is no such parent.
  2332. """
  2333. parent_state = None
  2334. client_token, state_path = _split_substate_key(token)
  2335. parent_state_name = state_path.rpartition(".")[0]
  2336. if parent_state_name:
  2337. # Retrieve the parent state to populate event handlers onto this substate.
  2338. parent_state = await self.get_state(
  2339. token=_substate_key(client_token, parent_state_name),
  2340. top_level=False,
  2341. get_substates=False,
  2342. )
  2343. return parent_state
  2344. async def _populate_substates(
  2345. self,
  2346. token: str,
  2347. state: BaseState,
  2348. all_substates: bool = False,
  2349. ):
  2350. """Fetch and link substates for the given state instance.
  2351. There is no return value; the side-effect is that `state` will have `substates` populated,
  2352. and each substate will have its `parent_state` set to `state`.
  2353. Args:
  2354. token: The token to get the state for.
  2355. state: The state instance to populate substates for.
  2356. all_substates: Whether to fetch all substates or just required substates.
  2357. """
  2358. client_token, _ = _split_substate_key(token)
  2359. if all_substates:
  2360. # All substates are requested.
  2361. fetch_substates = state.get_substates()
  2362. else:
  2363. # Only _potentially_dirty_substates need to be fetched to recalc computed vars.
  2364. fetch_substates = state._potentially_dirty_substates()
  2365. tasks = {}
  2366. # Retrieve the necessary substates from redis.
  2367. for substate_cls in fetch_substates:
  2368. substate_name = substate_cls.get_name()
  2369. tasks[substate_name] = asyncio.create_task(
  2370. self.get_state(
  2371. token=_substate_key(client_token, substate_cls),
  2372. top_level=False,
  2373. get_substates=all_substates,
  2374. parent_state=state,
  2375. )
  2376. )
  2377. for substate_name, substate_task in tasks.items():
  2378. state.substates[substate_name] = await substate_task
  2379. @override
  2380. async def get_state(
  2381. self,
  2382. token: str,
  2383. top_level: bool = True,
  2384. get_substates: bool = True,
  2385. parent_state: BaseState | None = None,
  2386. ) -> BaseState:
  2387. """Get the state for a token.
  2388. Args:
  2389. token: The token to get the state for.
  2390. top_level: If true, return an instance of the top-level state (self.state).
  2391. get_substates: If true, also retrieve substates.
  2392. parent_state: If provided, use this parent_state instead of getting it from redis.
  2393. Returns:
  2394. The state for the token.
  2395. Raises:
  2396. RuntimeError: when the state_cls is not specified in the token
  2397. """
  2398. # Split the actual token from the fully qualified substate name.
  2399. _, state_path = _split_substate_key(token)
  2400. if state_path:
  2401. # Get the State class associated with the given path.
  2402. state_cls = self.state.get_class_substate(state_path)
  2403. else:
  2404. raise RuntimeError(
  2405. "StateManagerRedis requires token to be specified in the form of {token}_{state_full_name}"
  2406. )
  2407. # Fetch the serialized substate from redis.
  2408. redis_state = await self.redis.get(token)
  2409. if redis_state is not None:
  2410. # Deserialize the substate.
  2411. state = BaseState._deserialize(data=redis_state)
  2412. # Populate parent state if missing and requested.
  2413. if parent_state is None:
  2414. parent_state = await self._get_parent_state(token)
  2415. # Set up Bidirectional linkage between this state and its parent.
  2416. if parent_state is not None:
  2417. parent_state.substates[state.get_name()] = state
  2418. state.parent_state = parent_state
  2419. # Populate substates if requested.
  2420. await self._populate_substates(token, state, all_substates=get_substates)
  2421. # To retain compatibility with previous implementation, by default, we return
  2422. # the top-level state by chasing `parent_state` pointers up the tree.
  2423. if top_level:
  2424. return state._get_root_state()
  2425. return state
  2426. # TODO: dedupe the following logic with the above block
  2427. # Key didn't exist so we have to create a new instance for this token.
  2428. if parent_state is None:
  2429. parent_state = await self._get_parent_state(token)
  2430. # Instantiate the new state class (but don't persist it yet).
  2431. state = state_cls(
  2432. parent_state=parent_state,
  2433. init_substates=False,
  2434. _reflex_internal_init=True,
  2435. )
  2436. # Set up Bidirectional linkage between this state and its parent.
  2437. if parent_state is not None:
  2438. parent_state.substates[state.get_name()] = state
  2439. state.parent_state = parent_state
  2440. # Populate substates for the newly created state.
  2441. await self._populate_substates(token, state, all_substates=get_substates)
  2442. # To retain compatibility with previous implementation, by default, we return
  2443. # the top-level state by chasing `parent_state` pointers up the tree.
  2444. if top_level:
  2445. return state._get_root_state()
  2446. return state
  2447. def _warn_if_too_large(
  2448. self,
  2449. state: BaseState,
  2450. pickle_state_size: int,
  2451. ):
  2452. """Print a warning when the state is too large.
  2453. Args:
  2454. state: The state to check.
  2455. pickle_state_size: The size of the pickled state.
  2456. """
  2457. state_full_name = state.get_full_name()
  2458. if (
  2459. state_full_name not in self._warned_about_state_size
  2460. and pickle_state_size > TOO_LARGE_SERIALIZED_STATE
  2461. and state.substates
  2462. ):
  2463. console.warn(
  2464. f"State {state_full_name} serializes to {pickle_state_size} bytes "
  2465. "which may present performance issues. Consider reducing the size of this state."
  2466. )
  2467. self._warned_about_state_size.add(state_full_name)
  2468. @override
  2469. async def set_state(
  2470. self,
  2471. token: str,
  2472. state: BaseState,
  2473. lock_id: bytes | None = None,
  2474. ):
  2475. """Set the state for a token.
  2476. Args:
  2477. token: The token to set the state for.
  2478. state: The state to set.
  2479. lock_id: If provided, the lock_key must be set to this value to set the state.
  2480. Raises:
  2481. LockExpiredError: If lock_id is provided and the lock for the token is not held by that ID.
  2482. RuntimeError: If the state instance doesn't match the state name in the token.
  2483. """
  2484. # Check that we're holding the lock.
  2485. if (
  2486. lock_id is not None
  2487. and await self.redis.get(self._lock_key(token)) != lock_id
  2488. ):
  2489. raise LockExpiredError(
  2490. f"Lock expired for token {token} while processing. Consider increasing "
  2491. f"`app.state_manager.lock_expiration` (currently {self.lock_expiration}) "
  2492. "or use `@rx.background` decorator for long-running tasks."
  2493. )
  2494. client_token, substate_name = _split_substate_key(token)
  2495. # If the substate name on the token doesn't match the instance name, it cannot have a parent.
  2496. if state.parent_state is not None and state.get_full_name() != substate_name:
  2497. raise RuntimeError(
  2498. f"Cannot `set_state` with mismatching token {token} and substate {state.get_full_name()}."
  2499. )
  2500. # Recursively set_state on all known substates.
  2501. tasks = []
  2502. for substate in state.substates.values():
  2503. tasks.append(
  2504. asyncio.create_task(
  2505. self.set_state(
  2506. token=_substate_key(client_token, substate),
  2507. state=substate,
  2508. lock_id=lock_id,
  2509. )
  2510. )
  2511. )
  2512. # Persist only the given state (parents or substates are excluded by BaseState.__getstate__).
  2513. if state._get_was_touched():
  2514. pickle_state = state._serialize()
  2515. self._warn_if_too_large(state, len(pickle_state))
  2516. await self.redis.set(
  2517. _substate_key(client_token, state),
  2518. pickle_state,
  2519. ex=self.token_expiration,
  2520. )
  2521. # Wait for substates to be persisted.
  2522. for t in tasks:
  2523. await t
  2524. @override
  2525. @contextlib.asynccontextmanager
  2526. async def modify_state(self, token: str) -> AsyncIterator[BaseState]:
  2527. """Modify the state for a token while holding exclusive lock.
  2528. Args:
  2529. token: The token to modify the state for.
  2530. Yields:
  2531. The state for the token.
  2532. """
  2533. async with self._lock(token) as lock_id:
  2534. state = await self.get_state(token)
  2535. yield state
  2536. await self.set_state(token, state, lock_id)
  2537. @staticmethod
  2538. def _lock_key(token: str) -> bytes:
  2539. """Get the redis key for a token's lock.
  2540. Args:
  2541. token: The token to get the lock key for.
  2542. Returns:
  2543. The redis lock key for the token.
  2544. """
  2545. # All substates share the same lock domain, so ignore any substate path suffix.
  2546. client_token = _split_substate_key(token)[0]
  2547. return f"{client_token}_lock".encode()
  2548. async def _try_get_lock(self, lock_key: bytes, lock_id: bytes) -> bool | None:
  2549. """Try to get a redis lock for a token.
  2550. Args:
  2551. lock_key: The redis key for the lock.
  2552. lock_id: The ID of the lock.
  2553. Returns:
  2554. True if the lock was obtained.
  2555. """
  2556. return await self.redis.set(
  2557. lock_key,
  2558. lock_id,
  2559. px=self.lock_expiration,
  2560. nx=True, # only set if it doesn't exist
  2561. )
  2562. async def _wait_lock(self, lock_key: bytes, lock_id: bytes) -> None:
  2563. """Wait for a redis lock to be released via pubsub.
  2564. Coroutine will not return until the lock is obtained.
  2565. Args:
  2566. lock_key: The redis key for the lock.
  2567. lock_id: The ID of the lock.
  2568. Raises:
  2569. ResponseError: when the keyspace config cannot be set.
  2570. """
  2571. state_is_locked = False
  2572. lock_key_channel = f"__keyspace@0__:{lock_key.decode()}"
  2573. # Enable keyspace notifications for the lock key, so we know when it is available.
  2574. try:
  2575. await self.redis.config_set(
  2576. "notify-keyspace-events",
  2577. self._redis_notify_keyspace_events,
  2578. )
  2579. except ResponseError:
  2580. # Some redis servers only allow out-of-band configuration, so ignore errors here.
  2581. ignore_config_error = os.environ.get(
  2582. "REFLEX_IGNORE_REDIS_CONFIG_ERROR",
  2583. None,
  2584. )
  2585. if not ignore_config_error:
  2586. raise
  2587. async with self.redis.pubsub() as pubsub:
  2588. await pubsub.psubscribe(lock_key_channel)
  2589. while not state_is_locked:
  2590. # wait for the lock to be released
  2591. while True:
  2592. if not await self.redis.exists(lock_key):
  2593. break # key was removed, try to get the lock again
  2594. message = await pubsub.get_message(
  2595. ignore_subscribe_messages=True,
  2596. timeout=self.lock_expiration / 1000.0,
  2597. )
  2598. if message is None:
  2599. continue
  2600. if message["data"] in self._redis_keyspace_lock_release_events:
  2601. break
  2602. state_is_locked = await self._try_get_lock(lock_key, lock_id)
  2603. @contextlib.asynccontextmanager
  2604. async def _lock(self, token: str):
  2605. """Obtain a redis lock for a token.
  2606. Args:
  2607. token: The token to obtain a lock for.
  2608. Yields:
  2609. The ID of the lock (to be passed to set_state).
  2610. Raises:
  2611. LockExpiredError: If the lock has expired while processing the event.
  2612. """
  2613. lock_key = self._lock_key(token)
  2614. lock_id = uuid.uuid4().hex.encode()
  2615. if not await self._try_get_lock(lock_key, lock_id):
  2616. # Missed the fast-path to get lock, subscribe for lock delete/expire events
  2617. await self._wait_lock(lock_key, lock_id)
  2618. state_is_locked = True
  2619. try:
  2620. yield lock_id
  2621. except LockExpiredError:
  2622. state_is_locked = False
  2623. raise
  2624. finally:
  2625. if state_is_locked:
  2626. # only delete our lock
  2627. await self.redis.delete(lock_key)
  2628. async def close(self):
  2629. """Explicitly close the redis connection and connection_pool.
  2630. It is necessary in testing scenarios to close between asyncio test cases
  2631. to avoid having lingering redis connections associated with event loops
  2632. that will be closed (each test case uses its own event loop).
  2633. Note: Connections will be automatically reopened when needed.
  2634. """
  2635. await self.redis.aclose(close_connection_pool=True)
  2636. def get_state_manager() -> StateManager:
  2637. """Get the state manager for the app that is currently running.
  2638. Returns:
  2639. The state manager.
  2640. """
  2641. app = getattr(prerequisites.get_app(), constants.CompileVars.APP)
  2642. return app.state_manager
  2643. class ClientStorageBase:
  2644. """Base class for client-side storage."""
  2645. def options(self) -> dict[str, Any]:
  2646. """Get the options for the storage.
  2647. Returns:
  2648. All set options for the storage (not None).
  2649. """
  2650. return {
  2651. format.to_camel_case(k): v for k, v in vars(self).items() if v is not None
  2652. }
  2653. class Cookie(ClientStorageBase, str):
  2654. """Represents a state Var that is stored as a cookie in the browser."""
  2655. name: str | None
  2656. path: str
  2657. max_age: int | None
  2658. domain: str | None
  2659. secure: bool | None
  2660. same_site: str
  2661. def __new__(
  2662. cls,
  2663. object: Any = "",
  2664. encoding: str | None = None,
  2665. errors: str | None = None,
  2666. /,
  2667. name: str | None = None,
  2668. path: str = "/",
  2669. max_age: int | None = None,
  2670. domain: str | None = None,
  2671. secure: bool | None = None,
  2672. same_site: str = "lax",
  2673. ):
  2674. """Create a client-side Cookie (str).
  2675. Args:
  2676. object: The initial object.
  2677. encoding: The encoding to use.
  2678. errors: The error handling scheme to use.
  2679. name: The name of the cookie on the client side.
  2680. path: Cookie path. Use / as the path if the cookie should be accessible on all pages.
  2681. max_age: Relative max age of the cookie in seconds from when the client receives it.
  2682. domain: Domain for the cookie (sub.domain.com or .allsubdomains.com).
  2683. secure: Is the cookie only accessible through HTTPS?
  2684. same_site: Whether the cookie is sent with third party requests.
  2685. One of (true|false|none|lax|strict)
  2686. Returns:
  2687. The client-side Cookie object.
  2688. Note: expires (absolute Date) is not supported at this time.
  2689. """
  2690. if encoding or errors:
  2691. inst = super().__new__(cls, object, encoding or "utf-8", errors or "strict")
  2692. else:
  2693. inst = super().__new__(cls, object)
  2694. inst.name = name
  2695. inst.path = path
  2696. inst.max_age = max_age
  2697. inst.domain = domain
  2698. inst.secure = secure
  2699. inst.same_site = same_site
  2700. return inst
  2701. class LocalStorage(ClientStorageBase, str):
  2702. """Represents a state Var that is stored in localStorage in the browser."""
  2703. name: str | None
  2704. sync: bool = False
  2705. def __new__(
  2706. cls,
  2707. object: Any = "",
  2708. encoding: str | None = None,
  2709. errors: str | None = None,
  2710. /,
  2711. name: str | None = None,
  2712. sync: bool = False,
  2713. ) -> "LocalStorage":
  2714. """Create a client-side localStorage (str).
  2715. Args:
  2716. object: The initial object.
  2717. encoding: The encoding to use.
  2718. errors: The error handling scheme to use.
  2719. name: The name of the storage key on the client side.
  2720. sync: Whether changes should be propagated to other tabs.
  2721. Returns:
  2722. The client-side localStorage object.
  2723. """
  2724. if encoding or errors:
  2725. inst = super().__new__(cls, object, encoding or "utf-8", errors or "strict")
  2726. else:
  2727. inst = super().__new__(cls, object)
  2728. inst.name = name
  2729. inst.sync = sync
  2730. return inst
  2731. class SessionStorage(ClientStorageBase, str):
  2732. """Represents a state Var that is stored in sessionStorage in the browser."""
  2733. name: str | None
  2734. def __new__(
  2735. cls,
  2736. object: Any = "",
  2737. encoding: str | None = None,
  2738. errors: str | None = None,
  2739. /,
  2740. name: str | None = None,
  2741. ) -> "SessionStorage":
  2742. """Create a client-side sessionStorage (str).
  2743. Args:
  2744. object: The initial object.
  2745. encoding: The encoding to use.
  2746. errors: The error handling scheme to use
  2747. name: The name of the storage on the client side
  2748. Returns:
  2749. The client-side sessionStorage object.
  2750. """
  2751. if encoding or errors:
  2752. inst = super().__new__(cls, object, encoding or "utf-8", errors or "strict")
  2753. else:
  2754. inst = super().__new__(cls, object)
  2755. inst.name = name
  2756. return inst
  2757. class MutableProxy(wrapt.ObjectProxy):
  2758. """A proxy for a mutable object that tracks changes."""
  2759. # Methods on wrapped objects which should mark the state as dirty.
  2760. __mark_dirty_attrs__ = set(
  2761. [
  2762. "add",
  2763. "append",
  2764. "clear",
  2765. "difference_update",
  2766. "discard",
  2767. "extend",
  2768. "insert",
  2769. "intersection_update",
  2770. "pop",
  2771. "popitem",
  2772. "remove",
  2773. "reverse",
  2774. "setdefault",
  2775. "sort",
  2776. "symmetric_difference_update",
  2777. "update",
  2778. ]
  2779. )
  2780. # Methods on wrapped objects might return mutable objects that should be tracked.
  2781. __wrap_mutable_attrs__ = set(
  2782. [
  2783. "get",
  2784. "setdefault",
  2785. ]
  2786. )
  2787. # These internal attributes on rx.Base should NOT be wrapped in a MutableProxy.
  2788. __never_wrap_base_attrs__ = set(Base.__dict__) - {"set"} | set(
  2789. pydantic.BaseModel.__dict__
  2790. )
  2791. __mutable_types__ = (list, dict, set, Base, DeclarativeBase)
  2792. def __init__(self, wrapped: Any, state: BaseState, field_name: str):
  2793. """Create a proxy for a mutable object that tracks changes.
  2794. Args:
  2795. wrapped: The object to proxy.
  2796. state: The state to mark dirty when the object is changed.
  2797. field_name: The name of the field on the state associated with the
  2798. wrapped object.
  2799. """
  2800. super().__init__(wrapped)
  2801. self._self_state = state
  2802. self._self_field_name = field_name
  2803. def _mark_dirty(
  2804. self,
  2805. wrapped=None,
  2806. instance=None,
  2807. args=tuple(),
  2808. kwargs=None,
  2809. ) -> Any:
  2810. """Mark the state as dirty, then call a wrapped function.
  2811. Intended for use with `FunctionWrapper` from the `wrapt` library.
  2812. Args:
  2813. wrapped: The wrapped function.
  2814. instance: The instance of the wrapped function.
  2815. args: The args for the wrapped function.
  2816. kwargs: The kwargs for the wrapped function.
  2817. Returns:
  2818. The result of the wrapped function.
  2819. """
  2820. self._self_state.dirty_vars.add(self._self_field_name)
  2821. self._self_state._mark_dirty()
  2822. if wrapped is not None:
  2823. return wrapped(*args, **(kwargs or {}))
  2824. def _wrap_recursive(self, value: Any) -> Any:
  2825. """Wrap a value recursively if it is mutable.
  2826. Args:
  2827. value: The value to wrap.
  2828. Returns:
  2829. The wrapped value.
  2830. """
  2831. # Recursively wrap mutable types, but do not re-wrap MutableProxy instances.
  2832. if isinstance(value, self.__mutable_types__) and not isinstance(
  2833. value, MutableProxy
  2834. ):
  2835. return type(self)(
  2836. wrapped=value,
  2837. state=self._self_state,
  2838. field_name=self._self_field_name,
  2839. )
  2840. return value
  2841. def _wrap_recursive_decorator(self, wrapped, instance, args, kwargs) -> Any:
  2842. """Wrap a function that returns a possibly mutable value.
  2843. Intended for use with `FunctionWrapper` from the `wrapt` library.
  2844. Args:
  2845. wrapped: The wrapped function.
  2846. instance: The instance of the wrapped function.
  2847. args: The args for the wrapped function.
  2848. kwargs: The kwargs for the wrapped function.
  2849. Returns:
  2850. The result of the wrapped function (possibly wrapped in a MutableProxy).
  2851. """
  2852. return self._wrap_recursive(wrapped(*args, **kwargs))
  2853. def __getattr__(self, __name: str) -> Any:
  2854. """Get the attribute on the proxied object and return a proxy if mutable.
  2855. Args:
  2856. __name: The name of the attribute.
  2857. Returns:
  2858. The attribute value.
  2859. """
  2860. value = super().__getattr__(__name)
  2861. if callable(value):
  2862. if __name in self.__mark_dirty_attrs__:
  2863. # Wrap special callables, like "append", which should mark state dirty.
  2864. value = wrapt.FunctionWrapper(value, self._mark_dirty)
  2865. if __name in self.__wrap_mutable_attrs__:
  2866. # Wrap methods that may return mutable objects tied to the state.
  2867. value = wrapt.FunctionWrapper(
  2868. value,
  2869. self._wrap_recursive_decorator,
  2870. )
  2871. if (
  2872. isinstance(self.__wrapped__, Base)
  2873. and __name not in self.__never_wrap_base_attrs__
  2874. and hasattr(value, "__func__")
  2875. ):
  2876. # Wrap methods called on Base subclasses, which might do _anything_
  2877. return wrapt.FunctionWrapper(
  2878. functools.partial(value.__func__, self),
  2879. self._wrap_recursive_decorator,
  2880. )
  2881. if isinstance(value, self.__mutable_types__) and __name not in (
  2882. "__wrapped__",
  2883. "_self_state",
  2884. ):
  2885. # Recursively wrap mutable attribute values retrieved through this proxy.
  2886. return self._wrap_recursive(value)
  2887. return value
  2888. def __getitem__(self, key) -> Any:
  2889. """Get the item on the proxied object and return a proxy if mutable.
  2890. Args:
  2891. key: The key of the item.
  2892. Returns:
  2893. The item value.
  2894. """
  2895. value = super().__getitem__(key)
  2896. # Recursively wrap mutable items retrieved through this proxy.
  2897. return self._wrap_recursive(value)
  2898. def __iter__(self) -> Any:
  2899. """Iterate over the proxied object and return a proxy if mutable.
  2900. Yields:
  2901. Each item value (possibly wrapped in MutableProxy).
  2902. """
  2903. for value in super().__iter__():
  2904. # Recursively wrap mutable items retrieved through this proxy.
  2905. yield self._wrap_recursive(value)
  2906. def __delattr__(self, name):
  2907. """Delete the attribute on the proxied object and mark state dirty.
  2908. Args:
  2909. name: The name of the attribute.
  2910. """
  2911. self._mark_dirty(super().__delattr__, args=(name,))
  2912. def __delitem__(self, key):
  2913. """Delete the item on the proxied object and mark state dirty.
  2914. Args:
  2915. key: The key of the item.
  2916. """
  2917. self._mark_dirty(super().__delitem__, args=(key,))
  2918. def __setitem__(self, key, value):
  2919. """Set the item on the proxied object and mark state dirty.
  2920. Args:
  2921. key: The key of the item.
  2922. value: The value of the item.
  2923. """
  2924. self._mark_dirty(super().__setitem__, args=(key, value))
  2925. def __setattr__(self, name, value):
  2926. """Set the attribute on the proxied object and mark state dirty.
  2927. If the attribute starts with "_self_", then the state is NOT marked
  2928. dirty as these are internal proxy attributes.
  2929. Args:
  2930. name: The name of the attribute.
  2931. value: The value of the attribute.
  2932. """
  2933. if name.startswith("_self_"):
  2934. # Special case attributes of the proxy itself, not applied to the wrapped object.
  2935. super().__setattr__(name, value)
  2936. return
  2937. self._mark_dirty(super().__setattr__, args=(name, value))
  2938. def __copy__(self) -> Any:
  2939. """Return a copy of the proxy.
  2940. Returns:
  2941. A copy of the wrapped object, unconnected to the proxy.
  2942. """
  2943. return copy.copy(self.__wrapped__)
  2944. def __deepcopy__(self, memo=None) -> Any:
  2945. """Return a deepcopy of the proxy.
  2946. Args:
  2947. memo: The memo dict to use for the deepcopy.
  2948. Returns:
  2949. A deepcopy of the wrapped object, unconnected to the proxy.
  2950. """
  2951. return copy.deepcopy(self.__wrapped__, memo=memo)
  2952. def __reduce_ex__(self, protocol_version):
  2953. """Get the state for redis serialization.
  2954. This method is called by cloudpickle to serialize the object.
  2955. It explicitly serializes the wrapped object, stripping off the mutable proxy.
  2956. Args:
  2957. protocol_version: The protocol version.
  2958. Returns:
  2959. Tuple of (wrapped class, empty args, class __getstate__)
  2960. """
  2961. return self.__wrapped__.__reduce_ex__(protocol_version)
  2962. @serializer
  2963. def serialize_mutable_proxy(mp: MutableProxy):
  2964. """Return the wrapped value of a MutableProxy.
  2965. Args:
  2966. mp: The MutableProxy to serialize.
  2967. Returns:
  2968. The wrapped object.
  2969. """
  2970. return mp.__wrapped__
  2971. class ImmutableMutableProxy(MutableProxy):
  2972. """A proxy for a mutable object that tracks changes.
  2973. This wrapper comes from StateProxy, and will raise an exception if an attempt is made
  2974. to modify the wrapped object when the StateProxy is immutable.
  2975. """
  2976. def _mark_dirty(
  2977. self,
  2978. wrapped=None,
  2979. instance=None,
  2980. args=tuple(),
  2981. kwargs=None,
  2982. ) -> Any:
  2983. """Raise an exception when an attempt is made to modify the object.
  2984. Intended for use with `FunctionWrapper` from the `wrapt` library.
  2985. Args:
  2986. wrapped: The wrapped function.
  2987. instance: The instance of the wrapped function.
  2988. args: The args for the wrapped function.
  2989. kwargs: The kwargs for the wrapped function.
  2990. Returns:
  2991. The result of the wrapped function.
  2992. Raises:
  2993. ImmutableStateError: if the StateProxy is not mutable.
  2994. """
  2995. if not self._self_state._is_mutable():
  2996. raise ImmutableStateError(
  2997. "Background task StateProxy is immutable outside of a context "
  2998. "manager. Use `async with self` to modify state."
  2999. )
  3000. return super()._mark_dirty(
  3001. wrapped=wrapped, instance=instance, args=args, kwargs=kwargs
  3002. )
  3003. def code_uses_state_contexts(javascript_code: str) -> bool:
  3004. """Check if the rendered Javascript uses state contexts.
  3005. Args:
  3006. javascript_code: The Javascript code to check.
  3007. Returns:
  3008. True if the code attempts to access a member of StateContexts.
  3009. """
  3010. return bool("useContext(StateContexts" in javascript_code)
  3011. def reload_state_module(
  3012. module: str,
  3013. state: Type[BaseState] = State,
  3014. ) -> None:
  3015. """Reset rx.State subclasses to avoid conflict when reloading.
  3016. Args:
  3017. module: The module to reload.
  3018. state: Recursive argument for the state class to reload.
  3019. """
  3020. for subclass in tuple(state.class_subclasses):
  3021. reload_state_module(module=module, state=subclass)
  3022. if subclass.__module__ == module and module is not None:
  3023. state.class_subclasses.remove(subclass)
  3024. state._always_dirty_substates.discard(subclass.get_name())
  3025. state._computed_var_dependencies = defaultdict(set)
  3026. state._substate_var_dependencies = defaultdict(set)
  3027. state._init_var_dependency_dicts()
  3028. state.get_class_substate.cache_clear()