11import { OutgoingMessage , IncomingMessage } from "./messages" ;
22import { ReactPyModule } from "./reactpy-vdom" ;
33
4- export interface ReactPyServer {
4+ export interface ReactPyClient {
55 receiveMessage : < M extends IncomingMessage > ( type : M [ "type" ] ) => Promise < M > ;
66 sendMessage : ( message : OutgoingMessage ) => Promise < void > ;
77 loadModule : ( moduleName : string ) => Promise < ReactPyModule > ;
88}
99
10- export type StandardServerUrlProps = {
10+ type UrlProps = {
1111 baseUrl : string ;
1212 routePath : string ;
1313 queryString : string ;
1414} ;
1515
16- export type StandardServerReconnectProps = {
16+ type ReconnectProps = {
1717 maxInterval ?: number ;
1818 maxRetries ?: number ;
1919 backoffRate ?: number ;
2020 intervalJitter ?: number ;
2121} ;
2222
23- export class SimpleReactPyServer implements ReactPyServer {
24- private readonly urls : StandardServerUrls ;
23+ export class SimpleReactPyClient implements ReactPyClient {
24+ private readonly urls : ServerUrls ;
2525 private readonly handlers : {
2626 [ key in IncomingMessage [ "type" ] ] ?: ( ( message : any ) => void ) [ ] ;
2727 } ;
2828 private readonly socket : { current ?: WebSocket } ;
2929
3030 constructor ( props : {
31- serverApi : StandardServerUrlProps ;
32- reconnectOptions ?: StandardServerReconnectProps ;
31+ serverApi : UrlProps ;
32+ reconnectOptions ?: ReconnectProps ;
3333 } ) {
3434 this . handlers = { } ;
3535
36- this . urls = getStandardServerUrls ( props . serverApi ) ;
36+ this . urls = getServerUrls ( props . serverApi ) ;
3737
3838 this . socket = startReconnectingWebSocket ( {
3939 url : this . urls . stream ,
@@ -82,25 +82,21 @@ export class SimpleReactPyServer implements ReactPyServer {
8282 }
8383}
8484
85- type StandardServerUrls = {
85+ type ServerUrls = {
8686 base : URL ;
8787 stream : string ;
8888 modules : string ;
8989 assets : string ;
9090} ;
9191
92- function getStandardServerUrls (
93- props : StandardServerUrlProps ,
94- ) : StandardServerUrls {
92+ function getServerUrls ( props : UrlProps ) : ServerUrls {
9593 const base = new URL ( `${ props . baseUrl || document . location . origin } /_reactpy` ) ;
9694 const modules = `${ base } /modules` ;
9795 const assets = `${ base } /assets` ;
9896
99- const wsProtocol = `ws${ base . protocol === "https:" ? "s" : "" } ` ;
100- const wsHost = `${ base . host } :${ base . port } ` ;
101- const wsPath = `${ props . routePath || "" } ${ props . routePath || "" } ` ;
102-
103- const stream = `${ wsProtocol } ://${ wsHost } ${ wsPath } ` ;
97+ const streamProtocol = `ws${ base . protocol === "https:" ? "s" : "" } ` ;
98+ const streamPath = `${ base . pathname } /stream${ props . routePath || "" } ` ;
99+ const stream = `${ streamProtocol } ://${ base . host } ${ streamPath } ` ;
104100
105101 return { base, modules, assets, stream } ;
106102}
@@ -111,7 +107,7 @@ function startReconnectingWebSocket(
111107 onOpen : ( ) => void ;
112108 onMessage : ( message : MessageEvent < any > ) => void ;
113109 onClose : ( ) => void ;
114- } & StandardServerReconnectProps ,
110+ } & ReconnectProps ,
115111) {
116112 const {
117113 maxInterval = 60000 ,
0 commit comments