forked from reactive-python/reactpy-django
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.py
121 lines (84 loc) · 3.3 KB
/
types.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from __future__ import annotations
from dataclasses import dataclass, field
from typing import (
TYPE_CHECKING,
Any,
Callable,
Generic,
MutableMapping,
NamedTuple,
Protocol,
Sequence,
TypeVar,
Union,
)
from django.http import HttpRequest
from reactpy.types import Connection
from typing_extensions import ParamSpec
if TYPE_CHECKING:
from reactpy_django.websocket.consumer import ReactpyAsyncWebsocketConsumer
FuncParams = ParamSpec("FuncParams")
Inferred = TypeVar("Inferred")
ConnectionType = Connection[Union["ReactpyAsyncWebsocketConsumer", HttpRequest]]
@dataclass
class Query(Generic[Inferred]):
"""Queries generated by the `use_query` hook."""
data: Inferred
loading: bool
error: Exception | None
refetch: Callable[[], None]
@dataclass
class Mutation(Generic[FuncParams]):
"""Mutations generated by the `use_mutation` hook."""
execute: Callable[FuncParams, None]
loading: bool
error: Exception | None
reset: Callable[[], None]
def __call__(self, *args: FuncParams.args, **kwargs: FuncParams.kwargs) -> None:
"""Execute the mutation."""
self.execute(*args, **kwargs)
class AsyncPostprocessor(Protocol):
async def __call__(self, data: Any) -> Any:
...
class SyncPostprocessor(Protocol):
def __call__(self, data: Any) -> Any:
...
@dataclass
class QueryOptions:
"""Configuration options that can be provided to `use_query`."""
from reactpy_django.config import REACTPY_DEFAULT_QUERY_POSTPROCESSOR
postprocessor: AsyncPostprocessor | SyncPostprocessor | None = (
REACTPY_DEFAULT_QUERY_POSTPROCESSOR
)
"""A callable that can modify the query `data` after the query has been executed.
The first argument of postprocessor must be the query `data`. All proceeding arguments
are optional `postprocessor_kwargs` (see below). This postprocessor function must return
the modified `data`.
If unset, REACTPY_DEFAULT_QUERY_POSTPROCESSOR is used.
ReactPy's default django_query_postprocessor prevents Django's lazy query execution, and
additionally can be configured via `postprocessor_kwargs` to recursively fetch
`many_to_many` and `many_to_one` fields."""
postprocessor_kwargs: MutableMapping[str, Any] = field(default_factory=lambda: {})
"""Keyworded arguments directly passed into the `postprocessor` for configuration."""
thread_sensitive: bool = True
"""Whether to run the query in thread-sensitive mode. This setting only applies to sync query functions."""
@dataclass
class MutationOptions:
"""Configuration options that can be provided to `use_mutation`."""
thread_sensitive: bool = True
"""Whether to run the mutation in thread-sensitive mode. This setting only applies to sync mutation functions."""
@dataclass
class ComponentParams:
"""Container used for serializing component parameters.
This dataclass is pickled & stored in the database, then unpickled when needed."""
args: Sequence
kwargs: MutableMapping[str, Any]
class UserData(NamedTuple):
query: Query[dict | None]
mutation: Mutation[dict]
class AsyncMessageReceiver(Protocol):
async def __call__(self, message: dict) -> None:
...
class AsyncMessageSender(Protocol):
async def __call__(self, message: dict) -> None:
...