Skip to content

Commit 5481212

Browse files
authored
Use persistent background pages (sindresorhus#221)
1 parent 2e0a3c3 commit 5481212

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

source/background.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ async function scheduleNextAlarm(interval) {
1919
// Delay less than 1 minute will cause a warning
2020
const delayInMinutes = Math.max(Math.ceil(intervalValue / 60), 1);
2121

22-
browser.alarms.create({delayInMinutes});
22+
browser.alarms.clearAll();
23+
browser.alarms.create('update', {delayInMinutes});
2324
}
2425

2526
async function handleLastModified(newLastModified) {
@@ -47,15 +48,12 @@ async function updateNotificationCount() {
4748

4849
function handleError(error) {
4950
scheduleNextAlarm();
50-
5151
renderError(error);
5252
}
5353

5454
function handleOfflineStatus() {
55-
renderWarning('offline');
56-
57-
// Keep schedule for next alarm to keep background active
5855
scheduleNextAlarm();
56+
renderWarning('offline');
5957
}
6058

6159
async function update() {
@@ -80,14 +78,6 @@ function handleInstalled(details) {
8078
}
8179
}
8280

83-
function handleConnectionStatus() {
84-
if (navigator.onLine) {
85-
update();
86-
} else {
87-
handleOfflineStatus();
88-
}
89-
}
90-
9181
async function onMessage(message) {
9282
if (message === 'update') {
9383
await addHandlers();
@@ -125,11 +115,11 @@ async function addHandlers() {
125115
}
126116

127117
function init() {
128-
window.addEventListener('online', handleConnectionStatus);
129-
window.addEventListener('offline', handleConnectionStatus);
118+
window.addEventListener('online', update);
119+
window.addEventListener('offline', update);
130120

131121
browser.alarms.onAlarm.addListener(update);
132-
browser.alarms.create({when: Date.now() + 2000});
122+
scheduleNextAlarm();
133123

134124
browser.runtime.onMessage.addListener(onMessage);
135125
browser.runtime.onInstalled.addListener(handleInstalled);

source/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"notifications"
2424
],
2525
"background": {
26-
"persistent": false,
26+
"persistent": true,
2727
"scripts": [
2828
"browser-polyfill.min.js",
2929
"background.js"

0 commit comments

Comments
 (0)