Skip to content

Commit 9a66a75

Browse files
committed
fix: More breathing room for browser
Signed-off-by: Marcel Klehr <[email protected]>
1 parent 2e0c64b commit 9a66a75

File tree

7 files changed

+888
-322
lines changed

7 files changed

+888
-322
lines changed

src/lib/Account.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ export default class Account {
202202
const needLock = (strategy || this.getData().strategy) !== 'slave'
203203
let status
204204
try {
205+
Logger.log('Calling onSyncStart')
205206
status = await this.server.onSyncStart(needLock, forceSync)
206207
} catch (e) {
207208
// Resource locked
@@ -210,6 +211,7 @@ export default class Account {
210211
if (this.getData().lastSync < Date.now() - this.lockTimeout || forceSync) {
211212
// but if we've been waiting for the lock for more than 2h
212213
// start again without locking the resource
214+
Logger.log('Calling onSyncStart, forcing sync')
213215
status = await this.server.onSyncStart(false, true)
214216
} else {
215217
await this.setData({
@@ -234,13 +236,22 @@ export default class Account {
234236
}
235237

236238
// main sync steps:
239+
240+
Logger.log('Fetching mappings')
237241
mappings = await this.storage.getMappings()
242+
Logger.log('Fetched mappings')
243+
244+
Logger.log('Fetching cache')
238245
const cacheTree = await this.storage.getCache()
246+
Logger.log('Fetched cache')
239247

248+
Logger.log('Fetching pending continuation')
240249
let continuation = await this.storage.getCurrentContinuation()
250+
Logger.log('Fetched pending continuation')
241251

242252
if (typeof continuation !== 'undefined' && continuation !== null) {
243253
try {
254+
Logger.log('Attempting to load pending continuation')
244255
this.syncProcess = await DefaultSyncProcess.fromJSON(
245256
mappings,
246257
this.localCachingResource,
@@ -250,6 +261,7 @@ export default class Account {
250261
},
251262
continuation
252263
)
264+
Logger.log('Loaded pending continuation')
253265
} catch (e) {
254266
continuation = null
255267
if (e.message) Logger.log(e.message)
@@ -285,6 +297,7 @@ export default class Account {
285297
break
286298
}
287299

300+
Logger.log('Creating new sync process')
288301
this.syncProcess = new strategyClass(
289302
mappings,
290303
this.localCachingResource,
@@ -302,27 +315,35 @@ export default class Account {
302315
Logger.log('Found existing persisted pending continuation. Resuming last sync')
303316
// When resuming a continuation, the CachingTreeWrapper is usually not initialized yet, because the localTree is
304317
// set from the persisted continuation
318+
Logger.log('Fetching local bookmarks tree')
319+
this.syncProcess.setCacheTree(cacheTree)
305320
await this.localCachingResource.setCacheTree(await this.localCachingResource.getBookmarksTree())
306321
}
307322

323+
Logger.log('Starting sync process')
308324
await this.syncProcess.sync()
325+
Logger.log('Ended sync process')
309326

310327
await this.setData({ scheduled: false, syncing: 1 })
311328

312329
// update cache
330+
Logger.log('Storing cache')
313331
const cache = (await this.localCachingResource.getCacheTree()).clone(false)
314332
this.syncProcess.filterOutUnacceptedBookmarks(cache)
315333
await this.storage.setCache(cache)
316334

317335
if (this.server.onSyncComplete) {
336+
Logger.log('Calling onSyncComplete')
318337
await this.server.onSyncComplete()
319338
}
320339

321340
if (mappings) {
322341
// Remove superfluous items from mappings
323342
// as we don't remove items immediately for anymore (for Atomic adapters), due to possible interrupts
343+
Logger.log('Removing superfluous mappings')
324344
await mappings.gc(cache)
325345
// store mappings
346+
Logger.log('Storing mappings')
326347
await mappings.persist()
327348
}
328349

@@ -435,12 +456,18 @@ export default class Account {
435456
if (actionsDone) {
436457
const mappings = this.syncProcess.getMappingsInstance()
437458
if (this.server.isAtomic()) {
459+
Logger.log('progressCallback: Persisting cache')
438460
const cache = (await this.localCachingResource.getCacheTree()).clone(false)
439461
this.syncProcess.filterOutUnacceptedBookmarks(cache)
440462
await this.storage.setCache(cache)
463+
Logger.log('progressCallback: Persisting mappings')
441464
await mappings.persist()
442465
} else {
443-
await this.storage.setCurrentContinuation(this.syncProcess.toJSON())
466+
Logger.log('progressCallback: Serializing continuation')
467+
const cont = await this.syncProcess.toJSONAsync()
468+
Logger.log('progressCallback: Persisting continuation')
469+
await this.storage.setCurrentContinuation(cont)
470+
Logger.log('progressCallback: Persisting mappings')
444471
await mappings.persist()
445472
}
446473
}

src/lib/Diff.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import Ordering from './interfaces/Ordering'
44
import batchingToposort from 'batching-toposort'
55
import Logger from './Logger'
66
import { MappingFailureError } from '../errors/Error'
7+
import * as Parallel from 'async-parallel'
8+
import { yieldToEventLoop } from './yieldToEventLoop'
79

810
export const ActionType = {
911
CREATE: 'CREATE',
@@ -277,12 +279,23 @@ export default class Diff<L1 extends TItemLocation, L2 extends TItemLocation, A
277279
return this.getActions().map((action: A) => {
278280
return {
279281
...action,
280-
payload: action.payload.copy(false),
281-
oldItem: action.oldItem && action.oldItem.copy(false),
282+
payload: action.payload.clone(false).toJSON(),
283+
oldItem: action.oldItem && action.oldItem.clone(false).toJSON(),
282284
}
283285
})
284286
}
285287

288+
async toJSONAsync() {
289+
return Parallel.map(this.getActions(), async(action: A) => {
290+
await yieldToEventLoop()
291+
return {
292+
...action,
293+
payload: await action.payload.clone(false).toJSONAsync(),
294+
oldItem: await action.oldItem && action.oldItem.clone(false).toJSONAsync(),
295+
}
296+
}, 1)
297+
}
298+
286299
inspect(depth = 0):string {
287300
return 'Diff\n' + this.getActions().map((action: A) => {
288301
return `\nAction: ${action.type}\nPayload: #${action.payload.id}[${action.payload.title}]${'url' in action.payload ? `(${action.payload.url})` : ''} parentId: ${action.payload.parentId} ${'index' in action ? `Index: ${action.index}\n` : ''}${'order' in action ? `Order: ${JSON.stringify(action.order, null, '\t')}` : ''}`

0 commit comments

Comments
 (0)