Skip to content

Commit 7878e02

Browse files
add config option to allow state mutation with contextmanager from non-background events
1 parent c4254ed commit 7878e02

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

reflex/environment.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,11 @@ class EnvironmentVariables:
727727
# How long to wait between automatic reload on frontend error to avoid reload loops.
728728
REFLEX_AUTO_RELOAD_COOLDOWN_TIME_MS: EnvVar[int] = env_var(10_000)
729729

730+
# Wheter to allow entering a state mutation with `async with self` in normal (non background) event handlers
731+
REFLEX_STATE_ALLOW_CONTEXTMANAGER_WITHOUT_BACKGROUND_TASK: EnvVar[bool] = env_var(
732+
False
733+
)
734+
730735

731736
environment = EnvironmentVariables()
732737

reflex/state.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,9 +2192,14 @@ async def __aenter__(self) -> BaseState:
21922192
This should not be used for the State class, but exists for
21932193
type-compatibility with StateProxy.
21942194
2195+
Returns:
2196+
The unmodified state (self)
2197+
21952198
Raises:
21962199
TypeError: always, because async contextmanager protocol is only supported for background task.
21972200
"""
2201+
if environment.REFLEX_STATE_ALLOW_CONTEXTMANAGER_WITHOUT_BACKGROUND_TASK.get():
2202+
return self
21982203
msg = "Only background task should use `async with self` to modify state."
21992204
raise TypeError(msg)
22002205

0 commit comments

Comments
 (0)