Skip to content

Commit b5e4b02

Browse files
authored
New Event Action: temporal (#4404)
When an event/event spec is marked as "temporal", it will not be queued unless the backend is up. This can be used to prevent periodic events (like from `rx.moment`) from queueing up while the backend is down, and then stampeding when the backend comes up and the queue is drained. It can be used to avoid processing many periodic events at once when the app is only expecting to process such an event every so often.
1 parent bbfbc82 commit b5e4b02

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,11 @@ export const useEventLoop = (
705705
_e.stopPropagation();
706706
}
707707
const combined_name = events.map((e) => e.name).join("+++");
708+
if (event_actions?.temporal) {
709+
if (!socket.current || !socket.current.connected) {
710+
return; // don't queue when the backend is not connected
711+
}
712+
}
708713
if (event_actions?.throttle) {
709714
// If throttle returns false, the events are not added to the queue.
710715
if (!throttle(combined_name, event_actions.throttle)) {

reflex/event.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,18 @@ def debounce(self, delay_ms: int):
181181
event_actions={"debounce": delay_ms, **self.event_actions},
182182
)
183183

184+
@property
185+
def temporal(self):
186+
"""Do not queue the event if the backend is down.
187+
188+
Returns:
189+
New EventHandler-like with temporal set to True.
190+
"""
191+
return dataclasses.replace(
192+
self,
193+
event_actions={"temporal": True, **self.event_actions},
194+
)
195+
184196

185197
@dataclasses.dataclass(
186198
init=True,

0 commit comments

Comments
 (0)