Skip to content

Commit

Permalink
fix(background/Heartbeat): use alarm instead of storage.set
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi committed Aug 8, 2024
1 parent fd9e4bd commit 5fc209c
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 deletions src/background/services/heartbeat.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
import type { StorageService } from '.'
import type { Browser } from 'webextension-polyfill'

export class Heartbeat {
private interval: ReturnType<typeof setInterval>

constructor(private storage: StorageService) {}
constructor(private browser: Browser) {}

start() {
this.interval = setInterval(this.run.bind(this), 20 * 1000)
}
setTimeout(
() =>
this.browser.alarms.create('keep-alive-alarm-0', {
periodInMinutes: 1
}),
0
)
setTimeout(
() =>
this.browser.alarms.create('keep-alive-alarm-1', {
periodInMinutes: 1
}),
15 * 1000
)
setTimeout(
() =>
this.browser.alarms.create('keep-alive-alarm-2', {
periodInMinutes: 1
}),
30 * 1000
)
setTimeout(
() =>
this.browser.alarms.create('keep-alive-alarm-3', {
periodInMinutes: 1
}),
45 * 1000
)

run() {
void this.storage.get(['version'])
this.browser.alarms.onAlarm.addListener((ev) => {
console.count('keeping extension alive - ' + ev.name)
})
}

stop() {
clearInterval(this.interval)
this.browser.alarms.clear('keep-alive-alarm-0')
this.browser.alarms.clear('keep-alive-alarm-1')
this.browser.alarms.clear('keep-alive-alarm-2')
this.browser.alarms.clear('keep-alive-alarm-3')
}
}

0 comments on commit 5fc209c

Please sign in to comment.