Skip to content

Commit 030b7d5

Browse files
committed
enh(tabs): Make merge strategy work with tabs
reset cache upon browser start and change mergable scanner argument for tabs Signed-off-by: Marcel Klehr <[email protected]>
1 parent c17a7ba commit 030b7d5

File tree

3 files changed

+64
-29
lines changed

3 files changed

+64
-29
lines changed

src/lib/Account.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export default class Account {
208208

209209
// main sync steps:
210210
mappings = await this.storage.getMappings()
211-
const cacheTree = localResource.constructor.name !== 'LocalTabs' ? await this.storage.getCache() : new Folder({title: '', id: 'tabs', location: ItemLocation.LOCAL})
211+
const cacheTree = await this.storage.getCache()
212212

213213
let continuation = await this.storage.getCurrentContinuation()
214214

@@ -280,12 +280,10 @@ export default class Account {
280280
await this.setData({ ...this.getData(), scheduled: false, syncing: 1 })
281281

282282
// update cache
283-
if (localResource.constructor.name !== 'LocalTabs') {
284-
const cache = (await localResource.getBookmarksTree()).clone(false)
285-
this.syncProcess.filterOutUnacceptedBookmarks(cache)
286-
this.syncProcess.filterOutUnmappedItems(cache, await mappings.getSnapshot())
287-
await this.storage.setCache(cache)
288-
}
283+
const cache = (await localResource.getBookmarksTree()).clone(false)
284+
this.syncProcess.filterOutUnacceptedBookmarks(cache)
285+
this.syncProcess.filterOutUnmappedItems(cache, await mappings.getSnapshot())
286+
await this.storage.setCache(cache)
289287

290288
if (this.server.onSyncComplete) {
291289
await this.server.onSyncComplete()

src/lib/browser/BrowserController.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ export default class BrowserController {
167167
}
168168
}
169169
})
170+
171+
// Run some things on browser startup
172+
browser.runtime.onStartup.addListener(this.onStartup)
170173
}
171174

172175
async _receiveEvent(data, sendResponse) {
@@ -412,7 +415,7 @@ export default class BrowserController {
412415
}
413416
}
414417

415-
async onLoad() {
418+
async onStartup() {
416419
const accounts = await Account.getAllAccounts()
417420
await Promise.all(
418421
accounts.map(async acc => {
@@ -423,9 +426,14 @@ export default class BrowserController {
423426
scheduled: acc.getData().enabled,
424427
})
425428
}
429+
if (acc.getData().localRoot === 'tabs') {
430+
await acc.init()
431+
}
426432
})
427433
)
434+
}
428435

436+
async onLoad() {
429437
browser.storage.local.get('telemetryEnabled').then(async d => {
430438
if (!d.telemetryEnabled) {
431439
return

src/lib/strategies/Default.ts

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { CancelledSyncError, FailsafeError } from '../../errors/Error'
2727

2828
import NextcloudBookmarksAdapter from '../adapters/NextcloudBookmarks'
2929
import CachingAdapter from '../adapters/Caching'
30+
import LocalTabs from '../LocalTabs'
3031

3132
const ACTION_CONCURRENCY = 12
3233

@@ -424,27 +425,55 @@ export default class SyncProcess {
424425

425426
const newMappings = []
426427

427-
// if we have the cache available, Diff cache and both trees
428-
const localScanner = new Scanner(
429-
this.cacheTreeRoot,
430-
this.localTreeRoot,
431-
(oldItem, newItem) =>
432-
(oldItem.type === newItem.type && String(oldItem.id) === String(newItem.id)),
433-
this.preserveOrder
434-
)
435-
const serverScanner = new Scanner(
436-
this.cacheTreeRoot,
437-
this.serverTreeRoot,
438-
// We also allow canMergeWith here, because e.g. for NextcloudFolders the id of moved bookmarks changes (because their id is "<bookmarkID>;<folderId>")
439-
(oldItem, newItem) => {
440-
if ((oldItem.type === newItem.type && Mappings.mappable(mappingsSnapshot, oldItem, newItem)) || (oldItem.type === 'bookmark' && oldItem.canMergeWith(newItem))) {
441-
newMappings.push([oldItem, newItem])
442-
return true
443-
}
444-
return false
445-
},
446-
this.preserveOrder
447-
)
428+
let localScanner, serverScanner
429+
if (this.localTree instanceof LocalTabs) {
430+
// if we have the cache available, Diff cache and both trees
431+
localScanner = new Scanner(
432+
this.cacheTreeRoot,
433+
this.localTreeRoot,
434+
// We also allow canMergeWith for folders here, because Window IDs are not stable
435+
(oldItem, newItem) =>
436+
(oldItem.type === newItem.type && String(oldItem.id) === String(newItem.id)) || (oldItem.type === 'folder' && oldItem.canMergeWith(newItem)),
437+
this.preserveOrder,
438+
)
439+
serverScanner = new Scanner(
440+
this.cacheTreeRoot,
441+
this.serverTreeRoot,
442+
// We also allow canMergeWith here
443+
// (for bookmarks, because e.g. for NextcloudFolders the id of moved bookmarks changes (because their id is "<bookmarkID>;<folderId>")
444+
// (for folders because Window IDs are not stable)
445+
(oldItem, newItem) => {
446+
if ((oldItem.type === newItem.type && Mappings.mappable(mappingsSnapshot, oldItem, newItem)) || (oldItem.canMergeWith(newItem))) {
447+
newMappings.push([oldItem, newItem])
448+
return true
449+
}
450+
return false
451+
},
452+
this.preserveOrder,
453+
)
454+
} else {
455+
// if we have the cache available, Diff cache and both trees
456+
localScanner = new Scanner(
457+
this.cacheTreeRoot,
458+
this.localTreeRoot,
459+
(oldItem, newItem) =>
460+
(oldItem.type === newItem.type && String(oldItem.id) === String(newItem.id)),
461+
this.preserveOrder
462+
)
463+
serverScanner = new Scanner(
464+
this.cacheTreeRoot,
465+
this.serverTreeRoot,
466+
// We also allow canMergeWith here, because e.g. for NextcloudFolders the id of moved bookmarks changes (because their id is "<bookmarkID>;<folderId>")
467+
(oldItem, newItem) => {
468+
if ((oldItem.type === newItem.type && Mappings.mappable(mappingsSnapshot, oldItem, newItem)) || (oldItem.type === 'bookmark' && oldItem.canMergeWith(newItem))) {
469+
newMappings.push([oldItem, newItem])
470+
return true
471+
}
472+
return false
473+
},
474+
this.preserveOrder
475+
)
476+
}
448477
Logger.log('Calculating diffs for local and server trees relative to cache tree')
449478
const localScanResult = await localScanner.run()
450479
const serverScanResult = await serverScanner.run()

0 commit comments

Comments
 (0)