|
1 | | -import browser from '../lib/browser-api' |
2 | | -import Account from '../lib/Account' |
3 | | -import Tree from '../lib/Tree' |
4 | | -import packageJson from '../../package.json' |
5 | | - |
6 | | -const STATUS_ERROR = Symbol('error') |
7 | | -const STATUS_SYNCING = Symbol('syncing') |
8 | | -const STATUS_ALLGOOD = Symbol('allgood') |
9 | | -const INACTIVITY_TIMEOUT = 1000 * 60 |
10 | | - |
11 | | -class AlarmManger { |
12 | | - constructor (ctl) { |
13 | | - this.ctl = ctl |
14 | | - } |
15 | | - |
16 | | - syncAllAccounts () { |
17 | | - browser.storage.local.get('accounts') |
18 | | - .then((d) => { |
19 | | - var accounts = d['accounts'] |
20 | | - for (var accountId in accounts) { |
21 | | - this.ctl.syncAccount(accountId) |
22 | | - } |
23 | | - }) |
24 | | - } |
25 | | -} |
26 | | - |
27 | | -class Controller { |
28 | | - constructor () { |
29 | | - this.syncing = {} |
30 | | - this.schedule = {} |
31 | | - |
32 | | - this.alarms = new AlarmManger(this) |
33 | | - |
34 | | - // set up change listener |
35 | | - browser.bookmarks.onChanged.addListener((localId, details) => this.onchange(localId, details)) |
36 | | - browser.bookmarks.onMoved.addListener((localId, details) => this.onchange(localId, details)) |
37 | | - browser.bookmarks.onRemoved.addListener((localId, details) => this.onchange(localId, details)) |
38 | | - browser.bookmarks.onCreated.addListener((localId, details) => this.onchange(localId, details)) |
39 | | - |
40 | | - // Set up the alarms |
41 | | - |
42 | | - browser.alarms.create('syncAllAccounts', {periodInMinutes: 15}) |
43 | | - browser.alarms.onAlarm.addListener(alarm => { |
44 | | - this.alarms[alarm.name]() |
45 | | - }) |
46 | | - |
47 | | - window.syncAccount = (accountId) => this.syncAccount(accountId) |
48 | | - this.setEnabled(true) |
49 | | - |
50 | | - browser.storage.local.get('currentVersion') |
51 | | - .then(async d => { |
52 | | - if (packageJson.version === d.currentVersion) return |
53 | | - const accounts = await Account.getAllAccounts() |
54 | | - await Promise.all( |
55 | | - accounts.map(account => account.init()) |
56 | | - ) |
57 | | - await browser.storage.local.set({ |
58 | | - currentVersion: packageJson.version |
59 | | - }) |
60 | | - browser.runtime.openOptionsPage() |
61 | | - }) |
62 | | - } |
63 | | - |
64 | | - setEnabled (enabled) { |
65 | | - this.enabled = enabled |
66 | | - } |
67 | | - |
68 | | - async onchange (localId, details) { |
69 | | - if (!this.enabled) { |
70 | | - return |
71 | | - } |
72 | | - const allAccounts = await Account.getAllAccounts() |
73 | | - |
74 | | - // Check which accounts contain the bookmark and which used to contain (track) it |
75 | | - var trackingAccountsFilter = await Promise.all( |
76 | | - allAccounts |
77 | | - .map(async account => { |
78 | | - return account.tracksBookmark(localId) |
79 | | - }) |
80 | | - ) |
81 | | - |
82 | | - const accountsToSync = allAccounts |
83 | | - // Filter out any accounts that are not tracking the bookmark |
84 | | - .filter((account, i) => (trackingAccountsFilter[i])) |
85 | | - // Filter out any accounts that are presently syncing |
86 | | - .filter(account => !this.syncing[account.id]) |
87 | | - |
88 | | - // We should now sync all accounts that are involved in this change (2 at max) |
89 | | - accountsToSync.forEach((account) => { |
90 | | - this.scheduleSyncAccount(account.id) |
91 | | - }) |
92 | | - |
93 | | - var ancestors |
94 | | - try { |
95 | | - ancestors = await Tree.getIdPathFromLocalId(localId) |
96 | | - } catch (e) { |
97 | | - return |
98 | | - } |
99 | | - |
100 | | - const containingAccount = await Account.getAccountContainingLocalId(localId, ancestors, allAccounts) |
101 | | - if (containingAccount && |
102 | | - !this.syncing[containingAccount.id] && |
103 | | - !accountsToSync.some(acc => acc.id === containingAccount.id)) { |
104 | | - this.scheduleSyncAccount(containingAccount.id) |
105 | | - } |
106 | | - } |
107 | | - |
108 | | - scheduleSyncAccount (accountId) { |
109 | | - if (this.schedule[accountId]) { |
110 | | - clearTimeout(this.schedule[accountId]) |
111 | | - } |
112 | | - this.schedule[accountId] = setTimeout(() => this.syncAccount(accountId), INACTIVITY_TIMEOUT) |
113 | | - } |
114 | | - |
115 | | - syncAccount (accountId) { |
116 | | - if (!this.enabled) { |
117 | | - return |
118 | | - } |
119 | | - if (this.syncing[accountId]) { |
120 | | - return this.syncing[accountId].then(() => { |
121 | | - return this.syncAccount(accountId) |
122 | | - }) |
123 | | - } |
124 | | - this.syncing[accountId] = Account.get(accountId) |
125 | | - .then((account) => { |
126 | | - setTimeout(() => this.updateBadge(), 500) |
127 | | - return account.sync() |
128 | | - }) |
129 | | - .then(() => { |
130 | | - this.syncing[accountId] = false |
131 | | - this.updateBadge() |
132 | | - }, (error) => { |
133 | | - console.error(error) |
134 | | - this.syncing[accountId] = false |
135 | | - this.updateBadge() |
136 | | - }) |
137 | | - return this.syncing[accountId] |
138 | | - } |
139 | | - |
140 | | - async updateBadge () { |
141 | | - const accounts = await Account.getAllAccounts() |
142 | | - const overallStatus = accounts |
143 | | - .reduce((status, account) => { |
144 | | - const accData = account.getData() |
145 | | - if (accData.error && !accData.syncing) { |
146 | | - return STATUS_ERROR |
147 | | - } else if (accData.syncing && status !== STATUS_ERROR) { |
148 | | - return STATUS_SYNCING |
149 | | - } else { |
150 | | - return STATUS_ALLGOOD |
151 | | - } |
152 | | - }, STATUS_ALLGOOD) |
153 | | - this.setStatusBadge(overallStatus) |
154 | | - } |
155 | | - |
156 | | - async setStatusBadge (status) { |
157 | | - switch (status) { |
158 | | - case STATUS_ALLGOOD: |
159 | | - browser.browserAction.setBadgeText({text: ''}) |
160 | | - break |
161 | | - case STATUS_SYNCING: |
162 | | - browser.browserAction.setBadgeText({text: '<->'}) |
163 | | - browser.browserAction.setBadgeBackgroundColor({color: '#0088dd'}) |
164 | | - break |
165 | | - case STATUS_ERROR: |
166 | | - browser.browserAction.setBadgeText({text: '!'}) |
167 | | - browser.browserAction.setBadgeBackgroundColor({color: '#dd4d00'}) |
168 | | - break |
169 | | - } |
170 | | - } |
171 | | -} |
| 1 | +import Controller from '../lib/Controller' |
172 | 2 |
|
173 | 3 | window.controller = new Controller() |
| 4 | +window.syncAccount = (accountId) => window.controller.syncAccount(accountId) |
0 commit comments