@@ -171,19 +171,18 @@ class SyncService {
171171 this . sending = true
172172 clearInterval ( this . #sendIntervalId)
173173 this . #sendIntervalId = null
174- const data = getSendable ( )
175- if ( data . steps . length > 0 ) {
174+ const sendable = getSendable ( )
175+ if ( sendable . steps . length > 0 ) {
176176 this . emit ( 'stateChange' , { dirty : true } )
177177 }
178- return this . #connection. push ( data )
178+ return this . #connection. push ( sendable )
179179 . then ( ( response ) => {
180+ const { steps } = response . data
180181 this . pushError = 0
181182 this . sending = false
182- this . emit ( 'sync' , {
183- steps : [ ] ,
184- document : this . #connection. document ,
185- version : this . version ,
186- } )
183+ if ( steps ?. length > 0 ) {
184+ this . _receiveSteps ( { steps } )
185+ }
187186 } ) . catch ( err => {
188187 const { response, code } = err
189188 this . sending = false
@@ -194,11 +193,13 @@ class SyncService {
194193 if ( response ?. status === 412 ) {
195194 this . emit ( 'error' , { type : ERROR_TYPE . LOAD_ERROR , data : response } )
196195 } else if ( response ?. status === 403 ) {
197- if ( ! data . document ) {
196+ // TODO: is this really about sendable?
197+ if ( ! sendable . document ) {
198198 // either the session is invalid or the document is read only.
199199 logger . error ( 'failed to write to document - not allowed' )
200200 this . emit ( 'error' , { type : ERROR_TYPE . PUSH_FORBIDDEN , data : { } } )
201201 }
202+ // TODO: does response.data ever have a document? maybe for errors?
202203 // Only emit conflict event if we have synced until the latest version
203204 if ( response . data . document ?. currentVersion === this . version ) {
204205 this . emit ( 'error' , { type : ERROR_TYPE . PUSH_FAILURE , data : { } } )
@@ -211,7 +212,7 @@ class SyncService {
211212 } )
212213 }
213214
214- _receiveSteps ( { steps, document, sessions } ) {
215+ _receiveSteps ( { steps, document = null , sessions = [ ] } ) {
215216 const awareness = sessions
216217 . filter ( s => s . lastContact > ( Math . floor ( Date . now ( ) / 1000 ) - COLLABORATOR_DISCONNECT_TIME ) )
217218 . filter ( s => s . lastAwarenessMessage )
@@ -239,8 +240,7 @@ class SyncService {
239240 this . lastStepPush = Date . now ( )
240241 this . emit ( 'sync' , {
241242 steps : newSteps ,
242- // TODO: do we actually need to dig into the connection here?
243- document : this . #connection. document ,
243+ document,
244244 version : this . version ,
245245 } )
246246 }
0 commit comments