1717 SendCoroutine ,
1818 RecvCoroutine ,
1919)
20- from idom .core .layout import LayoutEvent , Layout
20+ from idom .core .layout import LayoutEvent , Layout , LayoutUpdate
2121from idom .client .manage import BUILD_DIR
2222
2323from .base import AbstractRenderServer
@@ -54,10 +54,10 @@ def _create_config(self, config: Optional[Config]) -> Config:
5454 def _default_application (self , config : Config ) -> Sanic :
5555 return Sanic ()
5656
57- def _setup_application (self , app : Sanic , config : Config ) -> None :
57+ def _setup_application (self , config : Config , app : Sanic ) -> None :
5858 bp = Blueprint (f"idom_dispatcher_{ id (self )} " , url_prefix = config ["url_prefix" ])
5959
60- self ._setup_blueprint_routes (bp , config )
60+ self ._setup_blueprint_routes (config , bp )
6161
6262 cors_config = config ["cors" ]
6363 if cors_config :
@@ -66,25 +66,26 @@ def _setup_application(self, app: Sanic, config: Config) -> None:
6666
6767 app .blueprint (bp )
6868
69- def _setup_application_did_start_event (self , app : Sanic , event : Event ) -> None :
69+ def _setup_application_did_start_event (
70+ self , config : Config , app : Sanic , event : Event
71+ ) -> None :
7072 async def server_did_start (app : Sanic , loop : asyncio .AbstractEventLoop ) -> None :
7173 event .set ()
7274
7375 app .register_listener (server_did_start , "after_server_start" )
7476
75- def _setup_blueprint_routes (self , blueprint : Blueprint , config : Config ) -> None :
77+ def _setup_blueprint_routes (self , config : Config , blueprint : Blueprint ) -> None :
7678 """Add routes to the application blueprint"""
7779
7880 @blueprint .websocket ("/stream" ) # type: ignore
7981 async def model_stream (
8082 request : request .Request , socket : WebSocketCommonProtocol
8183 ) -> None :
82- async def sock_send (value : Any ) -> None :
84+ async def sock_send (value : LayoutUpdate ) -> None :
8385 await socket .send (json .dumps (value ))
8486
8587 async def sock_recv () -> LayoutEvent :
86- event = json .loads (await socket .recv ())
87- return LayoutEvent (event ["target" ], event ["data" ])
88+ return LayoutEvent (** json .loads (await socket .recv ()))
8889
8990 element_params = {k : request .args .get (k ) for k in request .args }
9091 await self ._run_dispatcher (sock_send , sock_recv , element_params )
@@ -103,13 +104,25 @@ def redirect_to_index(
103104 )
104105
105106 def _run_application (
106- self , app : Sanic , config : Config , args : Tuple [Any , ...], kwargs : Dict [str , Any ]
107+ self ,
108+ config : Config ,
109+ app : Sanic ,
110+ host : str ,
111+ port : int ,
112+ args : Tuple [Any , ...],
113+ kwargs : Dict [str , Any ],
107114 ) -> None :
108115 self ._loop = asyncio .get_event_loop ()
109- app .run (* args , ** kwargs )
116+ app .run (host , port , * args , ** kwargs )
110117
111118 def _run_application_in_thread (
112- self , app : Sanic , config : Config , args : Tuple [Any , ...], kwargs : Dict [str , Any ]
119+ self ,
120+ config : Config ,
121+ app : Sanic ,
122+ host : str ,
123+ port : int ,
124+ args : Tuple [Any , ...],
125+ kwargs : Dict [str , Any ],
113126 ) -> None :
114127 try :
115128 loop = asyncio .get_event_loop ()
@@ -122,7 +135,9 @@ def _run_application_in_thread(
122135 # what follows was copied from:
123136 # https://github.com/sanic-org/sanic/blob/7028eae083b0da72d09111b9892ddcc00bce7df4/examples/run_async_advanced.py
124137
125- serv_coro = app .create_server (* args , ** kwargs , return_asyncio_server = True )
138+ serv_coro = app .create_server (
139+ host , port , * args , ** kwargs , return_asyncio_server = True
140+ )
126141 serv_task = asyncio .ensure_future (serv_coro , loop = loop )
127142 server = loop .run_until_complete (serv_task )
128143 server .after_start ()
@@ -167,10 +182,10 @@ class SharedClientStateServer(SanicRenderServer):
167182 _dispatcher_type = SharedViewDispatcher
168183 _dispatcher : SharedViewDispatcher
169184
170- def _setup_application (self , app : Sanic , config : Config ) -> None :
185+ def _setup_application (self , config : Config , app : Sanic ) -> None :
171186 app .register_listener (self ._activate_dispatcher , "before_server_start" )
172187 app .register_listener (self ._deactivate_dispatcher , "before_server_stop" )
173- super ()._setup_application (app , config )
188+ super ()._setup_application (config , app )
174189
175190 async def _activate_dispatcher (
176191 self , app : Sanic , loop : asyncio .AbstractEventLoop
0 commit comments