Add event token to router_data (#316)
This commit is contained in:
parent
2ccbfff223
commit
554e6d919b
@ -368,6 +368,7 @@ async def process(app: App, event: Event) -> StateUpdate:
|
||||
state = app.state_manager.get_state(event.token)
|
||||
|
||||
state.router_data = event.router_data
|
||||
state.router_data[constants.RouteVar.CLIENT_TOKEN] = event.token
|
||||
|
||||
# Preprocess the event.
|
||||
pre = app.preprocess(state, event)
|
||||
|
@ -200,6 +200,14 @@ class PathArgType(SimpleNamespace):
|
||||
LIST = str("arg_list")
|
||||
|
||||
|
||||
class RouteVar(SimpleNamespace):
|
||||
"""Names of variables used in the router_data dict stored in State."""
|
||||
|
||||
CLIENT_TOKEN = "token"
|
||||
PATH = "pathname"
|
||||
QUERY = "query"
|
||||
|
||||
|
||||
class RouteRegex(SimpleNamespace):
|
||||
"""Regex used for extracting path args in path."""
|
||||
|
||||
|
@ -261,13 +261,21 @@ class State(Base, ABC):
|
||||
field.required = False
|
||||
field.default = default_value
|
||||
|
||||
def get_token(self) -> str:
|
||||
"""Return the token of the client associated with this state.
|
||||
|
||||
Returns:
|
||||
The token of the client.
|
||||
"""
|
||||
return self.router_data.get(constants.RouteVar.CLIENT_TOKEN, "")
|
||||
|
||||
def get_current_page(self) -> str:
|
||||
"""Obtain the path of current page from the router data.
|
||||
|
||||
Returns:
|
||||
The current page.
|
||||
"""
|
||||
return self.router_data.get("pathname", "")
|
||||
return self.router_data.get(constants.RouteVar.PATH, "")
|
||||
|
||||
def get_query_params(self) -> Dict[str, str]:
|
||||
"""Obtain the query parameters for the queried page.
|
||||
@ -277,7 +285,7 @@ class State(Base, ABC):
|
||||
Returns:
|
||||
The dict of query parameters.
|
||||
"""
|
||||
return self.router_data.get("query", {})
|
||||
return self.router_data.get(constants.RouteVar.QUERY, {})
|
||||
|
||||
@classmethod
|
||||
def setup_dynamic_args(cls, args: dict[str, str]):
|
||||
|
@ -7,6 +7,7 @@ from pynecone.base import Base
|
||||
from pynecone.event import Event
|
||||
from pynecone.state import State
|
||||
from pynecone.var import BaseVar, ComputedVar
|
||||
from pynecone.constants import RouteVar
|
||||
|
||||
|
||||
class Object(Base):
|
||||
@ -609,3 +610,31 @@ def test_format_event_handler():
|
||||
utils.format_event_handler(GrandchildState.do_nothing) # type: ignore
|
||||
== "test_state.child_state.grandchild_state.do_nothing"
|
||||
)
|
||||
|
||||
|
||||
def test_get_token(test_state):
|
||||
assert test_state.get_token() == ""
|
||||
|
||||
token = "b181904c-3953-4a79-dc18-ae9518c22f05"
|
||||
test_state.router_data = {RouteVar.CLIENT_TOKEN: token}
|
||||
|
||||
assert test_state.get_token() == token
|
||||
|
||||
|
||||
def test_get_current_page(test_state):
|
||||
|
||||
assert test_state.get_current_page() == ""
|
||||
|
||||
route = "mypage/subpage"
|
||||
test_state.router_data = {RouteVar.PATH: route}
|
||||
|
||||
assert test_state.get_current_page() == route
|
||||
|
||||
|
||||
def test_get_query_params(test_state):
|
||||
assert test_state.get_query_params() == {}
|
||||
|
||||
params = {"p1": "a", "p2": "b"}
|
||||
test_state.router_data = {RouteVar.QUERY: params}
|
||||
|
||||
assert test_state.get_query_params() == params
|
||||
|
Loading…
Reference in New Issue
Block a user