Skip to content

v1: Generator event handlers #5383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion sdk/python/packages/flet/src/flet/messaging/session.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import inspect
import logging
import traceback
import weakref
Expand Down Expand Up @@ -193,14 +194,35 @@ async def dispatch_event(
await event_handler()
else:
await event_handler(e)

elif inspect.isasyncgenfunction(event_handler):
if get_param_count(event_handler) == 0:
async for _ in event_handler():
if UpdateBehavior.auto_update_enabled() and self.connection:
await self.auto_update(self.index[control._i])
else:
async for _ in event_handler(e):
if UpdateBehavior.auto_update_enabled() and self.connection:
await self.auto_update(self.index[control._i])

elif inspect.isgeneratorfunction(event_handler):
if get_param_count(event_handler) == 0:
for _ in event_handler():
if UpdateBehavior.auto_update_enabled() and self.connection:
await self.auto_update(self.index[control._i])
else:
for _ in event_handler(e):
if UpdateBehavior.auto_update_enabled() and self.connection:
await self.auto_update(self.index[control._i])

elif callable(event_handler):
if get_param_count(event_handler) == 0:
event_handler()
else:
event_handler(e)

if UpdateBehavior.auto_update_enabled() and self.connection:
await self.auto_update(control)
await self.auto_update(self.index[control._i])

except Exception as ex:
tb = traceback.format_exc()
Expand Down