forked from reactive-python/reactpy-django
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.py
107 lines (86 loc) · 2.85 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from dataclasses import dataclass
from inspect import iscoroutinefunction
from typing import (
TYPE_CHECKING,
Any,
Awaitable,
Callable,
Dict,
Optional,
Sequence,
Type,
Union,
overload,
)
from channels.db import database_sync_to_async
from idom.backend.types import Location
from idom.core.hooks import (
Context,
_EffectApplyFunc,
create_context,
use_context,
use_effect,
)
if not TYPE_CHECKING:
# make flake8 think that this variable exists
ellipsis = type(...)
@dataclass
class IdomWebsocket:
scope: dict
close: Callable[[Optional[int]], Awaitable[None]]
disconnect: Callable[[int], Awaitable[None]]
view_id: str
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
@overload
def use_sync_to_async(
function: None = None,
dependencies: Union[Sequence[Any], ellipsis, None] = ...,
) -> Callable[[_EffectApplyFunc], None]:
...
@overload
def use_sync_to_async(
function: _EffectApplyFunc,
dependencies: Union[Sequence[Any], ellipsis, None] = ...,
) -> None:
...
def use_sync_to_async(
function: Optional[_EffectApplyFunc] = None,
dependencies: Union[Sequence[Any], ellipsis, None] = ...,
) -> Optional[Callable[[_EffectApplyFunc], None]]:
"""This is a sync_to_async wrapper for `idom.hooks.use_effect`.
See the full :ref:`Use Effect` docs for details
Parameters:
function:
Applies the effect and can return a clean-up function
dependencies:
Dependencies for the effect. The effect will only trigger if the identity
of any value in the given sequence changes (i.e. their :func:`id` is
different). By default these are inferred based on local variables that are
referenced by the given function.
Returns:
If a function is not provided:
A decorator.
Otherwise:
`None`
"""
if function and iscoroutinefunction(function):
raise ValueError("use_sync_to_async cannot be used with async functions")
sync_to_async_function = database_sync_to_async(function) if function else None
return use_effect(sync_to_async_function, dependencies)