Skip to content

Commit 9ab3b40

Browse files
committed
refactored GatewayContext
1 parent cb73a1c commit 9ab3b40

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed

ellar/socket_io/context.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import typing as t
2+
3+
from engineio import AsyncServer
4+
5+
from ellar.common_types import TReceive, TScope, TSend
6+
from ellar.core import App, IExecutionContext
7+
from ellar.core.context import IHTTPHostContext, IWebSocketHostContext
8+
from ellar.di import EllarInjector
9+
10+
if t.TYPE_CHECKING: # pragma: no cover
11+
from .model import GatewayBase
12+
13+
14+
class GatewayContext(IExecutionContext):
15+
def __init__(
16+
self,
17+
server: AsyncServer,
18+
sid: str,
19+
message: t.Any,
20+
context: "IExecutionContext",
21+
environment: t.Dict,
22+
) -> None:
23+
self.server = server
24+
self.sid = sid
25+
self.message = message
26+
self._context = context
27+
self.environment = environment
28+
29+
def get_handler(self) -> t.Callable:
30+
return self._context.get_handler()
31+
32+
def get_class(self) -> t.Optional[t.Type["GatewayBase"]]: # type: ignore[override]
33+
return t.cast(t.Type["GatewayBase"], self._context.get_class())
34+
35+
def get_service_provider(self) -> EllarInjector: # pragma: no cover
36+
return self._context.get_service_provider()
37+
38+
def switch_to_http_connection(self) -> IHTTPHostContext: # pragma: no cover
39+
return self._context.switch_to_http_connection()
40+
41+
def switch_to_websocket(self) -> IWebSocketHostContext: # pragma: no cover
42+
return self._context.switch_to_websocket()
43+
44+
def get_app(self) -> App: # pragma: no cover
45+
return self._context.get_app()
46+
47+
def get_type(self) -> str: # pragma: no cover
48+
return self._context.get_type()
49+
50+
def get_args(self) -> t.Tuple[TScope, TReceive, TSend]: # pragma: no cover
51+
return self._context.get_args()

ellar/socket_io/model.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
1-
import typing as t
2-
3-
from engineio import AsyncServer
4-
5-
if t.TYPE_CHECKING:
6-
from ellar.core import IExecutionContext
1+
from .context import GatewayContext
72

83

94
class GatewayType(type):
105
pass
116

127

13-
class GatewayContext(t.NamedTuple):
14-
server: AsyncServer
15-
sid: str
16-
message: t.Any
17-
context: "IExecutionContext"
18-
environment: t.Dict
19-
20-
218
class GatewayBase(metaclass=GatewayType):
229
context: GatewayContext

0 commit comments

Comments
 (0)