Skip to content

Commit

Permalink
refactor: use messages to trigger alarm audio play
Browse files Browse the repository at this point in the history
  • Loading branch information
cdrani committed Sep 30, 2024
1 parent dad950b commit 1450372
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
15 changes: 9 additions & 6 deletions packages/extension/src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ chrome.runtime.onInstalled.addListener(async details => {
}
})

await notificationManager.startTimer()
keepAlive()
chrome.alarms.onAlarm.addListener(async alarm => {
await notificationManager.handleAlarm(alarm)
})

chrome.runtime.onStartup.addListener(keepAlive)
chrome.tabs.onActivated.addListener(keepAlive)
chrome.tabs.onUpdated.addListener(keepAlive)
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
if (message.type == 'playSound') {
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
if (!tabs[0]?.id) return

chrome.runtime.onMessage.addListener((_message, _sender, sendResponse) => {
chrome.tabs.sendMessage(tabs[0].id, { type: 'playSound', sound: message.sound })
})
}
sendResponse({ status: true })
return true
})
Expand Down
27 changes: 16 additions & 11 deletions packages/extension/src/utils/notification.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import NotificationBase from 'common/notification'
import { ensureOffscreenDocument, sendOffscreenMessage } from './offscreen'

const INITIAL_ALARM = 'initialAlarm'
const INTERVAL_ALARM = 'intervalAlarm'

class ExtensionNotification extends NotificationBase {
protected async clearAlarms() {
await chrome.alarms.clearAll()
}

protected setupAlarms(delay: number) {
chrome.alarms.create('initialAlarm', { delayInMinutes: delay / 60_000, periodInMinutes: 1440 })
chrome.alarms.create(INITIAL_ALARM, { delayInMinutes: delay / 60_000, periodInMinutes: 1440 })

chrome.alarms.create('intervalAlarm', {
chrome.alarms.create(INTERVAL_ALARM, {
periodInMinutes: this.settings.interval,
when: Date.now() + this.settings.interval * 60 * 1000,
})
}

chrome.alarms.onAlarm.addListener(async (alarm) => {
if (!['initialAlarm', 'intervalAlarm'].includes(alarm.name)) return
public async handleAlarm(alarm: chrome.alarms.Alarm) {
if (![INITIAL_ALARM, INTERVAL_ALARM].includes(alarm.name)) return

await this.getSettings()
const { nowMS, startMS, endMS } = this.getTimeBoundaries()
if (nowMS >= startMS && nowMS < endMS) await this.notifyAlert()
})
await this.getSettings()
const { nowMS, startMS, endMS } = this.getTimeBoundaries()
if (nowMS >= startMS && nowMS < endMS) await this.notifyAlert()
}

protected async createNotification({ id, title, message }: { id: string, title: string, message: string }) {
Expand All @@ -47,8 +49,11 @@ class ExtensionNotification extends NotificationBase {
}

protected async playSound(sound: string) {
await ensureOffscreenDocument()
await sendOffscreenMessage(sound)
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (!tabs[0]?.id) return

chrome.tabs.sendMessage(tabs[0].id, { type: 'playSound', sound })
})
}
}

Expand Down

0 comments on commit 1450372

Please sign in to comment.