Skip to content

Commit

Permalink
Fix event handler lambdas (#804)
Browse files Browse the repository at this point in the history
  • Loading branch information
picklelo authored Apr 11, 2023
1 parent bdafc21 commit 45f533d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pynecone/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ def compile(self, force_compile: bool = False):
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)
Expand All @@ -408,6 +409,7 @@ def compile(self, force_compile: bool = False):
# Compile the custom components.
compiler.compile_components(custom_components)

# To support calling event handlers from other handlers.
self.state.convert_handlers_to_fns()


Expand Down
9 changes: 8 additions & 1 deletion pynecone/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ def __init_subclass__(cls, **kwargs):
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):
Expand All @@ -196,6 +197,12 @@ def convert_handlers_to_fns(cls):
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]]:
Expand Down

0 comments on commit 45f533d

Please sign in to comment.