@@ -39,6 +39,7 @@ type Props = {
3939 alignDirection ?: 'right' | 'bottom' ,
4040 delay ?: number ,
4141 setServerStatus ?: ( status : string ) => void ,
42+ syncSandbox ?: ( updates : any ) => void ,
4243} ;
4344
4445type State = {
@@ -148,6 +149,7 @@ class BasePreview extends React.Component<Props, State> {
148149 modules : {
149150 [ path : string ] : any ,
150151 } ,
152+ ignoreNextUpdate : boolean ,
151153 } ;
152154 // TODO: Find typedefs for this
153155 $socket : ?any ;
@@ -179,10 +181,7 @@ class BasePreview extends React.Component<Props, State> {
179181 // when the user navigates the iframe app, which shows the loading screen
180182 this . initialPath = props . initialPath ;
181183
182- this . lastSent = {
183- sandboxId : props . sandbox . id ,
184- modules : this . getModulesToSend ( ) ,
185- } ;
184+ this . initializeLastSent ( ) ;
186185
187186 if ( this . serverPreview ) {
188187 this . connectTimeout = null ;
@@ -198,6 +197,14 @@ class BasePreview extends React.Component<Props, State> {
198197 window . openNewWindow = this . openNewWindow ;
199198 }
200199
200+ initializeLastSent = ( ) => {
201+ this . lastSent = {
202+ sandboxId : this . props . sandbox . id ,
203+ modules : this . getModulesToSend ( ) ,
204+ ignoreNextUpdate : false ,
205+ } ;
206+ } ;
207+
201208 componentWillUpdate ( nextProps : Props , nextState : State ) {
202209 if (
203210 nextState . frameInitialized !== this . state . frameInitialized &&
@@ -295,6 +302,14 @@ class BasePreview extends React.Component<Props, State> {
295302 } ) ;
296303 } ) ;
297304
305+ socket . on ( 'sandbox:update' , message => {
306+ this . lastSent . ignoreNextUpdate = true ;
307+
308+ if ( this . props . syncSandbox ) {
309+ this . props . syncSandbox ( { updates : message . updates } ) ;
310+ }
311+ } ) ;
312+
298313 socket . on ( 'sandbox:start' , ( ) => {
299314 sseTerminalMessage ( `sandbox ${ this . props . sandbox . id } started.` ) ;
300315
@@ -405,6 +420,7 @@ class BasePreview extends React.Component<Props, State> {
405420 : frameUrl ( newId , this . props . initialPath || '' ) ;
406421
407422 if ( this . serverPreview ) {
423+ this . initializeLastSent ( ) ;
408424 this . setupSSESockets ( ) ;
409425 }
410426 this . setState (
@@ -553,8 +569,10 @@ class BasePreview extends React.Component<Props, State> {
553569
554570 this . lastSent . modules = modulesToSend ;
555571
556- if ( Object . keys ( diff ) . length > 0 && this . $socket ) {
572+ const ignoreUpdate = this . lastSent . ignoreNextUpdate ;
573+ if ( ! ignoreUpdate && Object . keys ( diff ) . length > 0 && this . $socket ) {
557574 this . $socket . emit ( 'sandbox:update' , diff ) ;
575+ this . lastSent . ignoreNextUpdate = false ;
558576 }
559577 } else {
560578 dispatch ( {
0 commit comments