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