File tree Expand file tree Collapse file tree
src/js/packages/@reactpy/client/src Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -59,6 +59,7 @@ export class ReactPyClient
5959 urls : ReactPyUrls ;
6060 socket : { current ?: WebSocket } ;
6161 mountElement : HTMLElement ;
62+ private readonly messageQueue : any [ ] = [ ] ;
6263
6364 constructor ( props : GenericReactPyClientProps ) {
6465 super ( ) ;
@@ -69,12 +70,24 @@ export class ReactPyClient
6970 url : this . urls . componentUrl ,
7071 readyPromise : this . ready ,
7172 ...props . reconnectOptions ,
73+ onOpen : ( ) => {
74+ while ( this . messageQueue . length > 0 ) {
75+ this . sendMessage ( this . messageQueue . shift ( ) ) ;
76+ }
77+ } ,
7278 onMessage : async ( { data } ) => this . handleIncoming ( JSON . parse ( data ) ) ,
7379 } ) ;
7480 }
7581
7682 sendMessage ( message : any ) : void {
77- this . socket . current ?. send ( JSON . stringify ( message ) ) ;
83+ if (
84+ this . socket . current &&
85+ this . socket . current . readyState === WebSocket . OPEN
86+ ) {
87+ this . socket . current . send ( JSON . stringify ( message ) ) ;
88+ } else {
89+ this . messageQueue . push ( message ) ;
90+ }
7891 }
7992
8093 loadModule ( moduleName : string ) : Promise < ReactPyModule > {
You can’t perform that action at this time.
0 commit comments