Skip to content

Commit 85853fe

Browse files
committed
Trigger sync on local changes
fixes #24
1 parent 4341258 commit 85853fe

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

src/entries/background-script.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,46 @@ browser.alarms.onAlarm.addListener(alarm => {
2828
})
2929
})
3030

31+
const onchange = (localId, details) => {
32+
browser.storage.local.get('accounts')
33+
.then((d) => {
34+
var accounts = d['accounts']
35+
Object.keys(accounts).forEach(accountId => {
36+
Account.get(accountId)
37+
.then((account) => {
38+
return Promise.all([
39+
account.hasBookmark(localId),
40+
account.getLocalRoot()
41+
])
42+
})
43+
.then(data => {
44+
let [hasBookmark, localRoot] = data
45+
if (!syncing[accountId] && (hasBookmark || details.parentId === localRoot)) syncAccount(accountId)
46+
})
47+
})
48+
})
49+
}
50+
browser.bookmarks.onChanged.addListener(onchange)
51+
browser.bookmarks.onMoved.addListener(onchange)
52+
browser.bookmarks.onRemoved.addListener(onchange)
53+
browser.bookmarks.onCreated.addListener(onchange)
54+
3155
var syncing = {}
56+
, next = {}
3257
window.syncAccount = function(accountId) {
33-
if (syncing[accountId]) return syncing[accountId];
58+
if (syncing[accountId]) {
59+
next[accountId] = () => syncAccount(accountId)
60+
return
61+
}
3462
syncing[accountId] = true
3563
Account.get(accountId)
3664
.then((account) => {
3765
return account.sync()
3866
})
39-
.then(
40-
() => {delete syncing[accountId]},
41-
(er) => {
42-
delete syncing[accountId]
43-
return Promise.reject(er)
44-
}
45-
)
67+
.then(() => {delete syncing[accountId]})
68+
.catch((er) => {
69+
delete syncing[accountId]
70+
console.error(er)
71+
})
72+
.then(() => next[accountId] && next[accountId]())
4673
}

src/lib/Account.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ export default class Account {
5050
return this.storage.setAccountData(data)
5151
})
5252
}
53+
54+
getLocalRoot() {
55+
return this.storage.getLocalRoot()
56+
}
57+
58+
hasBookmark(localId) {
59+
return Promise.resolve()
60+
.then(() => this.storage.getMappings())
61+
.then((mappings) => {
62+
return Object.keys(mappings.LocalToServer).some(id => localId === id)
63+
})
64+
}
5365

5466
renderOptions(ctl) {
5567
return this.server.renderOptions(ctl)

0 commit comments

Comments
 (0)