Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions source/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ async function scheduleNextAlarm(interval) {
// Delay less than 1 minute will cause a warning
const delayInMinutes = Math.max(Math.ceil(intervalValue / 60), 1);

browser.alarms.create({delayInMinutes});
browser.alarms.clearAll();
browser.alarms.create('update', {delayInMinutes});
}

async function handleLastModified(newLastModified) {
Expand Down Expand Up @@ -47,15 +48,12 @@ async function updateNotificationCount() {

function handleError(error) {
scheduleNextAlarm();

renderError(error);
}

function handleOfflineStatus() {
renderWarning('offline');

// Keep schedule for next alarm to keep background active
scheduleNextAlarm();
renderWarning('offline');
}

async function update() {
Expand All @@ -80,14 +78,6 @@ function handleInstalled(details) {
}
}

function handleConnectionStatus() {
if (navigator.onLine) {
update();
} else {
handleOfflineStatus();
}
}

async function onMessage(message) {
if (message === 'update') {
await addHandlers();
Expand Down Expand Up @@ -125,11 +115,11 @@ async function addHandlers() {
}

function init() {
window.addEventListener('online', handleConnectionStatus);
window.addEventListener('offline', handleConnectionStatus);
window.addEventListener('online', update);
window.addEventListener('offline', update);

browser.alarms.onAlarm.addListener(update);
browser.alarms.create({when: Date.now() + 2000});
scheduleNextAlarm();

browser.runtime.onMessage.addListener(onMessage);
browser.runtime.onInstalled.addListener(handleInstalled);
Expand Down
2 changes: 1 addition & 1 deletion source/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"notifications"
],
"background": {
"persistent": false,
"persistent": true,
"scripts": [
"browser-polyfill.min.js",
"background.js"
Expand Down