Add multiple load events for a page (#596)

This commit is contained in:
Lucas 2023-02-25 18:13:02 +00:00 committed by GitHub
parent 9a9a731766
commit 387bacff73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 10 deletions

View File

@ -53,7 +53,7 @@ class App(Base):
middleware: List[Middleware] = [] middleware: List[Middleware] = []
# Event handlers to trigger when a page loads. # Event handlers to trigger when a page loads.
load_events: Dict[str, EventHandler] = {} load_events: Dict[str, Union[EventHandler, List[EventHandler]]] = {}
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
"""Initialize the app. """Initialize the app.
@ -197,7 +197,7 @@ class App(Base):
title: str = constants.DEFAULT_TITLE, title: str = constants.DEFAULT_TITLE,
description: str = constants.DEFAULT_DESCRIPTION, description: str = constants.DEFAULT_DESCRIPTION,
image=constants.DEFAULT_IMAGE, image=constants.DEFAULT_IMAGE,
on_load: Optional[EventHandler] = None, on_load: Optional[Union[EventHandler, List[EventHandler]]] = None,
path: Optional[str] = None, path: Optional[str] = None,
meta: List[Dict] = constants.DEFAULT_META_LIST, meta: List[Dict] = constants.DEFAULT_META_LIST,
): ):
@ -213,7 +213,7 @@ class App(Base):
title: The title of the page. title: The title of the page.
description: The description of the page. description: The description of the page.
image: The image to display on the page. image: The image to display on the page.
on_load: The event handler that will be called each time the page load. on_load: The event handler(s) that will be called each time the page load.
meta: The metadata of the page. meta: The metadata of the page.
""" """
if path is not None: if path is not None:

View File

@ -4,7 +4,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING, Optional from typing import TYPE_CHECKING, Optional
from pynecone import constants, utils from pynecone import constants, utils
from pynecone.event import Event from pynecone.event import Event, EventHandler
from pynecone.middleware.middleware import Middleware from pynecone.middleware.middleware import Middleware
from pynecone.state import Delta, State from pynecone.state import Delta, State
@ -34,8 +34,22 @@ class HydrateMiddleware(Middleware):
load_event = app.load_events.get(route.lstrip("/")) load_event = app.load_events.get(route.lstrip("/"))
else: else:
load_event = None load_event = None
if load_event: if load_event:
substate_path = utils.format_event_handler(load_event).split(".") if isinstance(load_event, list):
ex_state = state.get_substate(substate_path[:-1]) for single_event in load_event:
load_event.fn(ex_state) self.execute_load_event(state, single_event)
else:
self.execute_load_event(state, load_event)
return utils.format_state({state.get_name(): state.dict()}) return utils.format_state({state.get_name(): state.dict()})
def execute_load_event(self, state: State, load_event: EventHandler) -> None:
"""Execute single load event.
Args:
state: The client state.
load_event: A single load event to execute.
"""
substate_path = utils.format_event_handler(load_event).split(".")
ex_state = state.get_substate(substate_path[:-1])
load_event.fn(ex_state)

View File

@ -1,6 +1,6 @@
"""The route decorator and associated variables.""" """The route decorator and associated variables."""
from typing import Optional from typing import List, Optional, Union
from pynecone.event import EventHandler from pynecone.event import EventHandler
@ -12,7 +12,7 @@ def route(
title: Optional[str] = None, title: Optional[str] = None,
image: Optional[str] = None, image: Optional[str] = None,
description: Optional[str] = None, description: Optional[str] = None,
on_load: Optional[EventHandler] = None, on_load: Optional[Union[EventHandler, List[EventHandler]]] = None,
): ):
"""Decorate a function as a page. """Decorate a function as a page.
@ -28,7 +28,7 @@ def route(
title: The title of the page. title: The title of the page.
image: The favicon of the page. image: The favicon of the page.
description: The description of the page description: The description of the page
on_load: The event handler called when the page load. on_load: The event handler(s) called when the page load.
Returns: Returns:
The decorated function. The decorated function.