Skip to content

Commit d1284bf

Browse files
committed
ENG-8050: background event updates set final=None
When `final=None`, the frontend does not update the `event_processing` flag.
1 parent fc0a6ca commit d1284bf

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

reflex/.templates/web/utils/state.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,9 @@ export const connect = async (
665665
}
666666
}
667667
applyClientStorageDelta(client_storage, update.delta);
668-
event_processing = !update.final;
668+
if (update.final !== null) {
669+
event_processing = !update.final;
670+
}
669671
if (update.events) {
670672
queueEvents(update.events, socket, false, navigate, params);
671673
}

reflex/app.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,11 +1556,14 @@ def all_routes(_request: Request) -> Response:
15561556
)
15571557

15581558
@contextlib.asynccontextmanager
1559-
async def modify_state(self, token: str) -> AsyncIterator[BaseState]:
1559+
async def modify_state(
1560+
self, token: str, background: bool = False
1561+
) -> AsyncIterator[BaseState]:
15601562
"""Modify the state out of band.
15611563
15621564
Args:
15631565
token: The token to modify the state for.
1566+
background: Whether the modification is happening in a background task.
15641567
15651568
Yields:
15661569
The state to modify.
@@ -1581,7 +1584,10 @@ async def modify_state(self, token: str) -> AsyncIterator[BaseState]:
15811584
# When the state is modified reset dirty status and emit the delta to the frontend.
15821585
state._clean()
15831586
await self.event_namespace.emit_update(
1584-
update=StateUpdate(delta=delta),
1587+
update=StateUpdate(
1588+
delta=delta,
1589+
final=True if not background else None,
1590+
),
15851591
token=token,
15861592
)
15871593

reflex/istate/proxy.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ async def __aenter__(self) -> StateProxy:
131131

132132
await self._self_actx_lock.acquire()
133133
self._self_actx_lock_holder = current_task
134-
self._self_actx = self._self_app.modify_state(token=self._self_substate_token)
134+
self._self_actx = self._self_app.modify_state(
135+
token=self._self_substate_token, background=True
136+
)
135137
mutable_state = await self._self_actx.__aenter__()
136138
super().__setattr__(
137139
"__wrapped__", mutable_state.get_substate(self._self_substate_path)

reflex/state.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,7 @@ async def _as_state_update(
17791779
return StateUpdate(
17801780
delta=delta,
17811781
events=fixed_events,
1782-
final=final if not handler.is_background else True,
1782+
final=final if not handler.is_background else None,
17831783
)
17841784
except Exception as ex:
17851785
state._clean()
@@ -2684,7 +2684,7 @@ class StateUpdate:
26842684
events: list[Event] = dataclasses.field(default_factory=list)
26852685

26862686
# Whether this is the final state update for the event.
2687-
final: bool = True
2687+
final: bool | None = True
26882688

26892689
def json(self) -> str:
26902690
"""Convert the state update to a JSON string.

0 commit comments

Comments
 (0)