Fix event handler lambdas (#804)

This commit is contained in:
Nikhil Rao 2023-04-10 21:29:23 -07:00 committed by GitHub
parent bdafc21d3d
commit 45f533db27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -397,6 +397,7 @@ class App(Base):
compiler.compile_theme(self.style)
# Compile the pages.
self.state.set_handlers()
custom_components = set()
for route, component in self.pages.items():
component.add_style(self.style)
@ -408,6 +409,7 @@ class App(Base):
# Compile the custom components.
compiler.compile_components(custom_components)
# To support calling event handlers from other handlers.
self.state.convert_handlers_to_fns()

View File

@ -185,7 +185,8 @@ class State(Base, ABC):
for name, fn in events.items():
event_handler = EventHandler(fn=fn)
cls.event_handlers[name] = event_handler
setattr(cls, name, event_handler)
cls.set_handlers()
@classmethod
def convert_handlers_to_fns(cls):
@ -196,6 +197,12 @@ class State(Base, ABC):
for name, event_handler in cls.event_handlers.items():
setattr(cls, name, event_handler.fn)
@classmethod
def set_handlers(cls):
"""Set the state class handlers."""
for name, event_handler in cls.event_handlers.items():
setattr(cls, name, event_handler)
@classmethod
@functools.lru_cache()
def get_parent_state(cls) -> Optional[Type[State]]: