Add: schedule a faster sync when db changed

This commit is contained in:
eight 2019-10-02 13:39:01 +08:00
parent 0a5e51be36
commit 274c6a75ba

View File

@ -4,6 +4,9 @@
'use strict'; 'use strict';
const sync = (() => { const sync = (() => {
const SYNC_DELAY = 1;
const SYNC_INTERVAL = 30;
const status = { const status = {
state: 'disconnected', state: 'disconnected',
syncing: false, syncing: false,
@ -56,12 +59,25 @@ const sync = (() => {
return { return {
start, start,
stop, stop,
put: ctrl.put, put: (...args) => {
delete: ctrl.delete, schedule();
return ctrl.put(...args);
},
delete: (...args) => {
schedule();
return ctrl.delete(...args);
},
syncNow, syncNow,
getStatus: () => status getStatus: () => status
}; };
function schedule() {
chrome.alarms.create('syncNow', {
delayInMinutes: SYNC_DELAY,
periodInMinutes: SYNC_INTERVAL
});
}
function onPrefChange(key, value) { function onPrefChange(key, value) {
if (value === 'none') { if (value === 'none') {
stop().catch(console.error); stop().catch(console.error);
@ -133,7 +149,7 @@ const sync = (() => {
}) })
.catch(handle401Error), .catch(handle401Error),
() => { () => {
chrome.alarms.create('syncNow', {periodInMinutes: 30}); chrome.alarms.create('syncNow', {periodInMinutes: SYNC_INTERVAL});
status.state = 'connected'; status.state = 'connected';
emitChange(); emitChange();
} }