@@ -19,7 +19,8 @@ async function scheduleNextAlarm(interval) {
19
19
// Delay less than 1 minute will cause a warning
20
20
const delayInMinutes = Math . max ( Math . ceil ( intervalValue / 60 ) , 1 ) ;
21
21
22
- browser . alarms . create ( { delayInMinutes} ) ;
22
+ browser . alarms . clearAll ( ) ;
23
+ browser . alarms . create ( 'update' , { delayInMinutes} ) ;
23
24
}
24
25
25
26
async function handleLastModified ( newLastModified ) {
@@ -47,15 +48,12 @@ async function updateNotificationCount() {
47
48
48
49
function handleError ( error ) {
49
50
scheduleNextAlarm ( ) ;
50
-
51
51
renderError ( error ) ;
52
52
}
53
53
54
54
function handleOfflineStatus ( ) {
55
- renderWarning ( 'offline' ) ;
56
-
57
- // Keep schedule for next alarm to keep background active
58
55
scheduleNextAlarm ( ) ;
56
+ renderWarning ( 'offline' ) ;
59
57
}
60
58
61
59
async function update ( ) {
@@ -80,14 +78,6 @@ function handleInstalled(details) {
80
78
}
81
79
}
82
80
83
- function handleConnectionStatus ( ) {
84
- if ( navigator . onLine ) {
85
- update ( ) ;
86
- } else {
87
- handleOfflineStatus ( ) ;
88
- }
89
- }
90
-
91
81
async function onMessage ( message ) {
92
82
if ( message === 'update' ) {
93
83
await addHandlers ( ) ;
@@ -125,11 +115,11 @@ async function addHandlers() {
125
115
}
126
116
127
117
function init ( ) {
128
- window . addEventListener ( 'online' , handleConnectionStatus ) ;
129
- window . addEventListener ( 'offline' , handleConnectionStatus ) ;
118
+ window . addEventListener ( 'online' , update ) ;
119
+ window . addEventListener ( 'offline' , update ) ;
130
120
131
121
browser . alarms . onAlarm . addListener ( update ) ;
132
- browser . alarms . create ( { when : Date . now ( ) + 2000 } ) ;
122
+ scheduleNextAlarm ( ) ;
133
123
134
124
browser . runtime . onMessage . addListener ( onMessage ) ;
135
125
browser . runtime . onInstalled . addListener ( handleInstalled ) ;
0 commit comments