@@ -147,35 +147,40 @@ export class ConvexYjsProvider extends ObservableV2<ProviderEvents> {
147147
148148 destroy ( ) {
149149 if ( this . destroyed ) return
150+ this . destroyed = true
150151
151152 this . stopPersistInterval ( )
152153 this . clearAwarenessTimer ( )
153154
154- if ( this . _writable ) {
155- this . flushUpdates ( )
156- this . persist ( )
157- }
158-
159- this . flushAwareness ( )
155+ const teardown = async ( ) => {
156+ if ( this . _writable ) {
157+ await this . flushUpdates ( )
158+ this . persist ( )
159+ }
160160
161- this . destroyed = true
161+ await this . flushAwareness ( )
162162
163- this . config . removeAwareness ( {
164- documentId : this . documentId ,
165- clientId : this . doc . clientID ,
166- } )
163+ this . config
164+ . removeAwareness ( {
165+ documentId : this . documentId ,
166+ clientId : this . doc . clientID ,
167+ } )
168+ . catch ( ( ) => { } )
169+ }
167170
168- removeAwarenessStates (
169- this . awareness ,
170- [ this . doc . clientID ] ,
171- 'local-disconnect' ,
172- )
171+ teardown ( ) . finally ( ( ) => {
172+ removeAwarenessStates (
173+ this . awareness ,
174+ [ this . doc . clientID ] ,
175+ 'local-disconnect' ,
176+ )
173177
174- this . doc . off ( 'update' , this . handleDocUpdate )
175- this . awareness . off ( 'update' , this . handleAwarenessUpdate )
176- this . awareness . destroy ( )
178+ this . doc . off ( 'update' , this . handleDocUpdate )
179+ this . awareness . off ( 'update' , this . handleAwarenessUpdate )
180+ this . awareness . destroy ( )
177181
178- super . destroy ( )
182+ super . destroy ( )
183+ } )
179184 }
180185
181186 private persist ( ) {
@@ -214,10 +219,10 @@ export class ConvexYjsProvider extends ObservableV2<ProviderEvents> {
214219 }
215220 }
216221
217- private flushUpdates ( ) {
222+ private flushUpdates ( ) : Promise < void > {
218223 this . clearUpdateTimers ( )
219- if ( this . pushInFlight ) return
220- if ( this . pendingUpdates . length === 0 ) return
224+ if ( this . pushInFlight ) return Promise . resolve ( )
225+ if ( this . pendingUpdates . length === 0 ) return Promise . resolve ( )
221226
222227 const merged =
223228 this . pendingUpdates . length === 1
@@ -226,7 +231,7 @@ export class ConvexYjsProvider extends ObservableV2<ProviderEvents> {
226231 this . pendingUpdates = [ ]
227232
228233 this . pushInFlight = true
229- this . config
234+ return this . config
230235 . pushUpdate ( {
231236 documentId : this . documentId ,
232237 update : uint8ToArrayBuffer ( merged ) ,
@@ -236,6 +241,8 @@ export class ConvexYjsProvider extends ObservableV2<ProviderEvents> {
236241 } )
237242 . catch ( ( err : unknown ) => {
238243 console . error ( '[YJS] push failed for' , this . documentId , err )
244+ this . pendingUpdates . unshift ( merged )
245+ this . scheduleFlush ( )
239246 } )
240247 . finally ( ( ) => {
241248 this . pushInFlight = false
@@ -301,20 +308,21 @@ export class ConvexYjsProvider extends ObservableV2<ProviderEvents> {
301308 }
302309 }
303310
304- private flushAwareness ( ) {
311+ private flushAwareness ( ) : Promise < void > {
305312 this . clearAwarenessTimer ( )
306- if ( this . awarenessInFlight || this . destroyed ) return
313+ if ( this . awarenessInFlight ) return Promise . resolve ( )
307314
308315 const myClientId = this . doc . clientID
309316 const encoded = encodeAwarenessUpdate ( this . awareness , [ myClientId ] )
310317
311318 this . awarenessInFlight = true
312- this . config
319+ return this . config
313320 . pushAwareness ( {
314321 documentId : this . documentId ,
315322 clientId : myClientId ,
316323 state : uint8ToArrayBuffer ( encoded ) ,
317324 } )
325+ . then ( ( ) => { } )
318326 . catch ( ( err : unknown ) => {
319327 console . error ( '[YJS] awareness push failed for' , this . documentId , err )
320328 } )
0 commit comments