Fix substate event handler conversions (#861)
This commit is contained in:
parent
fd80617472
commit
1029d3483a
@ -397,7 +397,6 @@ class App(Base):
|
|||||||
compiler.compile_theme(self.style)
|
compiler.compile_theme(self.style)
|
||||||
|
|
||||||
# Compile the pages.
|
# Compile the pages.
|
||||||
self.state.set_handlers()
|
|
||||||
custom_components = set()
|
custom_components = set()
|
||||||
for route, component in self.pages.items():
|
for route, component in self.pages.items():
|
||||||
component.add_style(self.style)
|
component.add_style(self.style)
|
||||||
@ -409,9 +408,6 @@ class App(Base):
|
|||||||
# Compile the custom components.
|
# Compile the custom components.
|
||||||
compiler.compile_components(custom_components)
|
compiler.compile_components(custom_components)
|
||||||
|
|
||||||
# To support calling event handlers from other handlers.
|
|
||||||
self.state.convert_handlers_to_fns()
|
|
||||||
|
|
||||||
|
|
||||||
async def process(
|
async def process(
|
||||||
app: App, event: Event, sid: str, headers: Dict, client_ip: str
|
app: App, event: Event, sid: str, headers: Dict, client_ip: str
|
||||||
|
@ -21,6 +21,7 @@ from typing import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
import cloudpickle
|
import cloudpickle
|
||||||
|
import pydantic
|
||||||
from redis import Redis
|
from redis import Redis
|
||||||
|
|
||||||
from pynecone import constants
|
from pynecone import constants
|
||||||
@ -32,7 +33,7 @@ from pynecone.var import BaseVar, ComputedVar, PCDict, PCList, Var
|
|||||||
Delta = Dict[str, Any]
|
Delta = Dict[str, Any]
|
||||||
|
|
||||||
|
|
||||||
class State(Base, ABC):
|
class State(Base, ABC, extra=pydantic.Extra.allow):
|
||||||
"""The state of the app."""
|
"""The state of the app."""
|
||||||
|
|
||||||
# A map from the var name to the var.
|
# A map from the var name to the var.
|
||||||
@ -87,6 +88,11 @@ class State(Base, ABC):
|
|||||||
for substate in self.get_substates():
|
for substate in self.get_substates():
|
||||||
self.substates[substate.get_name()] = substate().set(parent_state=self)
|
self.substates[substate.get_name()] = substate().set(parent_state=self)
|
||||||
|
|
||||||
|
# Convert the event handlers to functions.
|
||||||
|
for name, event_handler in self.event_handlers.items():
|
||||||
|
setattr(self, name, event_handler.fn)
|
||||||
|
|
||||||
|
# Initialize the mutable fields.
|
||||||
self._init_mutable_fields()
|
self._init_mutable_fields()
|
||||||
|
|
||||||
def _init_mutable_fields(self):
|
def _init_mutable_fields(self):
|
||||||
@ -189,23 +195,6 @@ class State(Base, ABC):
|
|||||||
if not name.startswith("_") and isinstance(fn, Callable)
|
if not name.startswith("_") and isinstance(fn, Callable)
|
||||||
}
|
}
|
||||||
cls.event_handlers = {name: EventHandler(fn=fn) for name, fn in events.items()}
|
cls.event_handlers = {name: EventHandler(fn=fn) for name, fn in events.items()}
|
||||||
|
|
||||||
cls.set_handlers()
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def convert_handlers_to_fns(cls):
|
|
||||||
"""Convert the event handlers to functions.
|
|
||||||
|
|
||||||
This is done so the state functions can be called as normal functions during runtime.
|
|
||||||
"""
|
|
||||||
for name, event_handler in cls.event_handlers.items():
|
|
||||||
setattr(cls, name, event_handler.fn)
|
|
||||||
for substate in cls.get_substates():
|
|
||||||
substate.convert_handlers_to_fns()
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def set_handlers(cls):
|
|
||||||
"""Set the state class handlers."""
|
|
||||||
for name, event_handler in cls.event_handlers.items():
|
for name, event_handler in cls.event_handlers.items():
|
||||||
setattr(cls, name, event_handler)
|
setattr(cls, name, event_handler)
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ class TestState2(State):
|
|||||||
Chain of EventHandlers
|
Chain of EventHandlers
|
||||||
"""
|
"""
|
||||||
self.num += 1
|
self.num += 1
|
||||||
return [self.change_name()]
|
return self.change_name
|
||||||
|
|
||||||
def change_name(self):
|
def change_name(self):
|
||||||
"""Test handler to change name."""
|
"""Test handler to change name."""
|
||||||
|
Loading…
Reference in New Issue
Block a user