Skip to content

Commit 267a1b7

Browse files
committed
fix(Account#progressCallback): persist cache and mappings for atomic backends
Signed-off-by: Marcel Klehr <[email protected]>
1 parent e5768b8 commit 267a1b7

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

src/lib/Account.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,15 @@ export default class Account {
8383
protected localTabs: TLocalTree
8484
protected lockTimeout: number
8585

86+
private localCachingResource: CachingTreeWrapper
87+
8688
constructor(id:string, storageAdapter:IAccountStorage, serverAdapter: TAdapter, treeAdapter:TLocalTree) {
8789
this.server = serverAdapter
8890
this.id = id
8991
this.storage = storageAdapter
9092
this.localTree = treeAdapter
9193
this.lockTimeout = LOCK_TIMEOUT
94+
this.localCachingResource = null
9295
}
9396

9497
async delete():Promise<void> {
@@ -164,8 +167,9 @@ export default class Account {
164167
try {
165168
if (this.getData().syncing || this.syncing) return
166169

167-
const localResource = new CachingTreeWrapper(await this.getResource())
168-
if (!(await this.server.isAvailable()) || !(await localResource.isAvailable())) return
170+
if (!(await this.server.isAvailable()) || !(await (await this.getResource()).isAvailable())) return
171+
172+
this.localCachingResource = new CachingTreeWrapper(await this.getResource())
169173

170174
Logger.log('Starting sync process for account ' + this.getLabel())
171175
Sentry.setUser({ id: this.id })
@@ -221,7 +225,7 @@ export default class Account {
221225
try {
222226
this.syncProcess = await DefaultSyncProcess.fromJSON(
223227
mappings,
224-
localResource,
228+
this.localCachingResource,
225229
this.server,
226230
async(progress, actionDone) => {
227231
await this.progressCallback(progress, actionDone)
@@ -264,7 +268,7 @@ export default class Account {
264268

265269
this.syncProcess = new strategyClass(
266270
mappings,
267-
localResource,
271+
this.localCachingResource,
268272
this.server,
269273
async(progress, actionsDone?) => {
270274
await this.progressCallback(progress, actionsDone)
@@ -279,14 +283,14 @@ export default class Account {
279283
// if there is a pending continuation, we resume it
280284

281285
Logger.log('Found existing persisted pending continuation. Resuming last sync')
282-
await localResource.setCacheTree(await localResource.getBookmarksTree())
286+
await this.localCachingResource.setCacheTree(await this.localCachingResource.getBookmarksTree())
283287
await this.syncProcess.sync()
284288
}
285289

286290
await this.setData({ scheduled: false, syncing: 1 })
287291

288292
// update cache
289-
const cache = (await localResource.getCacheTree()).clone(false)
293+
const cache = (await this.localCachingResource.getCacheTree()).clone(false)
290294
this.syncProcess.filterOutUnacceptedBookmarks(cache)
291295
await this.storage.setCache(cache)
292296

@@ -333,6 +337,12 @@ export default class Account {
333337
}
334338
})
335339

340+
if (this.server.onSyncFail) {
341+
await this.server.onSyncFail()
342+
}
343+
344+
this.syncing = false
345+
336346
await this.setData({
337347
error: message,
338348
errorCount: this.getData().errorCount + 1,
@@ -342,10 +352,6 @@ export default class Account {
342352
if (matchAllErrors(e, e => e.code !== 27 && (!isTest || e.code !== 26))) {
343353
await this.storage.setCurrentContinuation(null)
344354
}
345-
this.syncing = false
346-
if (this.server.onSyncFail) {
347-
await this.server.onSyncFail()
348-
}
349355

350356
// reset cache and mappings after error
351357
// (but not after interruption or NetworkError)
@@ -377,9 +383,16 @@ export default class Account {
377383
if (!this.syncProcess) {
378384
return
379385
}
380-
if (actionsDone && !this.server.isAtomic()) {
381-
await this.storage.setCurrentContinuation(this.syncProcess.toJSON())
382-
await this.syncProcess.getMappingsInstance().persist()
386+
if (actionsDone) {
387+
if (this.server.isAtomic()) {
388+
const cache = (await this.localCachingResource.getCacheTree()).clone(false)
389+
this.syncProcess.filterOutUnacceptedBookmarks(cache)
390+
await this.storage.setCache(cache)
391+
await this.syncProcess.getMappingsInstance().persist()
392+
} else {
393+
await this.storage.setCurrentContinuation(this.syncProcess.toJSON())
394+
await this.syncProcess.getMappingsInstance().persist()
395+
}
383396
}
384397
}
385398

0 commit comments

Comments
 (0)