@@ -4,9 +4,9 @@ import log from "./logger";
44export function createReconnectingWebSocket (
55 props : CreateReconnectingWebSocketProps ,
66) {
7- const { startInterval , maxInterval, maxRetries, backoffMultiplier } = props ;
7+ const { interval , maxInterval, maxRetries, backoffMultiplier } = props ;
88 let retries = 0 ;
9- let interval = startInterval ;
9+ let currentInterval = interval ;
1010 let everConnected = false ;
1111 const closed = false ;
1212 const socket : { current ?: WebSocket } = { } ;
@@ -18,8 +18,8 @@ export function createReconnectingWebSocket(
1818 socket . current = new WebSocket ( props . url ) ;
1919 socket . current . onopen = ( ) => {
2020 everConnected = true ;
21- log . info ( "ReactPy connected !" ) ;
22- interval = startInterval ;
21+ log . info ( "Connected !" ) ;
22+ currentInterval = interval ;
2323 retries = 0 ;
2424 if ( props . onOpen ) {
2525 props . onOpen ( ) ;
@@ -31,26 +31,28 @@ export function createReconnectingWebSocket(
3131 props . onClose ( ) ;
3232 }
3333 if ( ! everConnected ) {
34- log . info ( "ReactPy failed to connect!" ) ;
34+ log . info ( "Failed to connect!" ) ;
3535 return ;
3636 }
37- log . info ( "ReactPy disconnected !" ) ;
37+ log . info ( "Disconnected !" ) ;
3838 if ( retries >= maxRetries ) {
39- log . info ( "ReactPy connection max retries exhausted!" ) ;
39+ log . info ( "Connection max retries exhausted!" ) ;
4040 return ;
4141 }
4242 log . info (
43- `ReactPy reconnecting in ${ ( interval / 1000 ) . toPrecision ( 4 ) } seconds...` ,
43+ `Reconnecting in ${ ( currentInterval / 1000 ) . toPrecision ( 4 ) } seconds...` ,
44+ ) ;
45+ setTimeout ( connect , currentInterval ) ;
46+ currentInterval = nextInterval (
47+ currentInterval ,
48+ backoffMultiplier ,
49+ maxInterval ,
4450 ) ;
45- setTimeout ( connect , interval ) ;
46- interval = nextInterval ( interval , backoffMultiplier , maxInterval ) ;
4751 retries ++ ;
4852 } ;
4953 } ;
5054
51- props . readyPromise
52- . then ( ( ) => log . info ( "Starting ReactPy client..." ) )
53- . then ( connect ) ;
55+ props . readyPromise . then ( ( ) => log . info ( "Starting client..." ) ) . then ( connect ) ;
5456
5557 return socket ;
5658}
0 commit comments