Skip to content

Commit d437604

Browse files
committed
fix(SaveService): Don't autosave if yjs reports pendingStructs
Yjs pendingStructs most likely mean that the local yjs document state is missing a step. So let's not autosave such an inconstent state to the server. Signed-off-by: Jonas <[email protected]>
1 parent 07cc286 commit d437604

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/composables/useSaveService.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const provideSaveService = (
2323
syncService,
2424
serialize,
2525
getDocumentState: () => getDocumentState(ydoc),
26+
hasYjsPendingStructs: () => !!ydoc.store.pendingStructs,
2627
})
2728
provide(saveServiceKey, saveService)
2829
return { saveService }

src/services/SaveService.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,27 @@ class SaveService {
2323
syncService
2424
serialize
2525
getDocumentState
26+
hasYjsPendingStructs
2627
autosave
2728

2829
constructor({
2930
connection,
3031
syncService,
3132
serialize,
3233
getDocumentState,
34+
hasYjsPendingStructs,
3335
}: {
3436
connection: ShallowRef<Connection | undefined>
3537
syncService: SyncService
3638
serialize: () => string
3739
getDocumentState: () => string
40+
hasYjsPendingStructs: () => boolean
3841
}) {
3942
this.connection = connection
4043
this.syncService = syncService
4144
this.serialize = serialize
4245
this.getDocumentState = getDocumentState
46+
this.hasYjsPendingStructs = hasYjsPendingStructs
4347
this.autosave = debounce(this._autosave.bind(this), AUTOSAVE_INTERVAL)
4448
this.syncService.on('close', () => {
4549
this.autosave.clear()
@@ -98,6 +102,10 @@ class SaveService {
98102
}
99103

100104
_autosave() {
105+
// Don't autosave if we have pendingStructs (i.e. document state misses a step)
106+
if (this.hasYjsPendingStructs()) {
107+
return
108+
}
101109
return this.save({ manualSave: false }).catch((error) => {
102110
logger.error('Failed to autosave document.', { error })
103111
})

0 commit comments

Comments
 (0)