@@ -151,6 +151,9 @@ class BasePreview extends React.Component<Props, State> {
151151 } ;
152152 // TODO: Find typedefs for this
153153 $socket : ?any ;
154+ connectTimeout : ?number ;
155+ // indicates if the socket closing is initiated by us
156+ localClose : boolean ;
154157
155158 constructor ( props : Props ) {
156159 super ( props ) ;
@@ -182,6 +185,8 @@ class BasePreview extends React.Component<Props, State> {
182185 } ;
183186
184187 if ( this . serverPreview ) {
188+ this . connectTimeout = null ;
189+ this . localClose = false ;
185190 this . setupSSESockets ( ) ;
186191 }
187192 this . listener = listen ( this . handleMessage ) ;
@@ -204,12 +209,11 @@ class BasePreview extends React.Component<Props, State> {
204209
205210 setupSSESockets = async ( ) => {
206211 const hasInitialized = ! ! this . $socket ;
207- let connectTimeout = null ;
208212
209- function onTimeout ( setServerStatus ) {
210- connectTimeout = null ;
211- if ( setServerStatus ) {
212- setServerStatus ( 'disconnected' ) ;
213+ function onTimeout ( comp ) {
214+ comp . connectTimeout = null ;
215+ if ( comp . props . setServerStatus ) {
216+ comp . props . setServerStatus ( 'disconnected' ) ;
213217 }
214218 }
215219
@@ -218,27 +222,30 @@ class BasePreview extends React.Component<Props, State> {
218222 frameInitialized : false ,
219223 } ) ;
220224 if ( this . $socket ) {
225+ this . localClose = true ;
221226 this . $socket . close ( ) ;
227+ // we need this setTimeout() for socket open() to work immediately after close()
222228 setTimeout ( ( ) => {
223- if ( this . $socket ) {
224- connectTimeout = setTimeout (
225- ( ) => onTimeout ( this . props . setServerStatus ) ,
226- 3000
227- ) ;
228- this . $socket . open ( ) ;
229- }
229+ this . connectTimeout = setTimeout ( ( ) => onTimeout ( this ) , 3000 ) ;
230+ this . $socket . open ( ) ;
230231 } , 0 ) ;
231232 }
232233 } else {
233234 const socket = io ( getSSEUrl ( ) , {
234235 autoConnect : false ,
236+ transports : [ 'websocket' , 'polling' ] ,
235237 } ) ;
236238 this . $socket = socket ;
237239 if ( process . env . NODE_ENV === 'development' ) {
238240 window . $socket = socket ;
239241 }
240242
241243 socket . on ( 'disconnect' , ( ) => {
244+ if ( this . localClose ) {
245+ this . localClose = false ;
246+ return ;
247+ }
248+
242249 if ( this . props . setServerStatus ) {
243250 let status = 'disconnected' ;
244251 if ( this . state . hibernated ) {
@@ -252,9 +259,9 @@ class BasePreview extends React.Component<Props, State> {
252259 } ) ;
253260
254261 socket . on ( 'connect' , async ( ) => {
255- if ( connectTimeout ) {
256- clearTimeout ( connectTimeout ) ;
257- connectTimeout = null ;
262+ if ( this . connectTimeout ) {
263+ clearTimeout ( this . connectTimeout ) ;
264+ this . connectTimeout = null ;
258265 }
259266
260267 if ( this . props . setServerStatus ) {
@@ -352,10 +359,7 @@ class BasePreview extends React.Component<Props, State> {
352359 }
353360 } ) ;
354361
355- connectTimeout = setTimeout (
356- ( ) => onTimeout ( this . props . setServerStatus ) ,
357- 3000
358- ) ;
362+ this . connectTimeout = setTimeout ( ( ) => onTimeout ( this ) , 3000 ) ;
359363 socket . open ( ) ;
360364 }
361365 } ;
@@ -378,6 +382,7 @@ class BasePreview extends React.Component<Props, State> {
378382 }
379383
380384 if ( this . $socket ) {
385+ this . localClose = true ;
381386 this . $socket . close ( ) ;
382387 }
383388 }
0 commit comments