forked from reactive-python/reactpy-django
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.py
32 lines (22 loc) · 951 Bytes
/
hooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from typing import Dict, Type, Union
from idom.backend.types import Location
from idom.core.hooks import Context, create_context, use_context
from django_idom.types import IdomWebsocket
WebsocketContext: Type[Context[Union[IdomWebsocket, None]]] = create_context(
None, "WebSocketContext"
)
def use_location() -> Location:
"""Get the current route as a string"""
# TODO: Use the browser's current page, rather than the WS route
scope = use_scope()
search = scope["query_string"].decode()
return Location(scope["path"], f"?{search}" if search else "")
def use_scope() -> Dict:
"""Get the current ASGI scope dictionary"""
return use_websocket().scope
def use_websocket() -> IdomWebsocket:
"""Get the current IdomWebsocket object"""
websocket = use_context(WebsocketContext)
if websocket is None:
raise RuntimeError("No websocket. Are you running with a Django server?")
return websocket