@@ -103,32 +103,39 @@ let syncThumbnail = 0
103103
104104const syncOfflineStatus = ref ( null )
105105
106- const syncPresentationToServer = async ( hadDroppedConnection ) => {
107- isSaving . value = true
106+ const syncSnapshotToServer = async ( wasConnectionRestored , snapshot ) => {
107+ // if connection was restored, syncing was not triggered by auto-save
108+ // so add badge after coming back online
109+ if ( wasConnectionRestored ) {
110+ syncOfflineStatus . value = 'Syncing local changes'
111+ }
108112
109- try {
110- const snapshot = await getPresentationFromLocalDB ( presentationId . value )
113+ await savePresentationDoc ( snapshot . content )
111114
112- if ( ! snapshot || ! snapshot . dirty ) return
115+ if ( wasConnectionRestored ) {
116+ syncOfflineStatus . value = 'Changes synced'
117+ setTimeout ( ( ) => {
118+ syncOfflineStatus . value = null
119+ } , 2000 )
120+ }
113121
114- if ( hadDroppedConnection ) {
115- syncOfflineStatus . value = 'Syncing local changes'
116- }
122+ // after successful sync, make sure local copy is marked as clean so it's not synced again
123+ await savePresentationToLocalDB ( {
124+ ...snapshot ,
125+ dirty : false ,
126+ updatedAt : Date . now ( ) ,
127+ } )
128+ }
117129
118- await savePresentationDoc ( snapshot . content )
130+ const syncPresentationToServer = async ( wasConnectionRestored ) => {
131+ isSaving . value = true
119132
120- if ( hadDroppedConnection ) {
121- syncOfflineStatus . value = 'Changes synced'
122- setTimeout ( ( ) => {
123- syncOfflineStatus . value = null
124- } , 2000 )
125- }
133+ try {
134+ const snapshot = await getPresentationFromLocalDB ( presentationId . value )
135+ if ( ! snapshot || ! snapshot . dirty ) return
126136
127- await savePresentationToLocalDB ( {
128- ...snapshot ,
129- dirty : false ,
130- updatedAt : Date . now ( ) ,
131- } )
137+ // if there's an unsynced snapshot locally, sync it to server
138+ syncSnapshotToServer ( wasConnectionRestored , snapshot )
132139 } catch ( err ) {
133140 console . error ( 'Sync to server failed: ' , err )
134141 } finally {
0 commit comments