-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsleep-timer.js
More file actions
57 lines (52 loc) · 1.54 KB
/
Copy pathsleep-timer.js
File metadata and controls
57 lines (52 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// sleep-timer.js — manages the sleep timer alarm and state
const SleepTimer = {
/**
* Starts a sleep timer for the given duration.
* @param {number} minutes
*/
start(minutes) {
chrome.alarms.create('sleepTimer', { delayInMinutes: minutes });
const timerData = {
active: true,
minutesSet: minutes,
startedAt: Date.now()
};
chrome.storage.local.set({ sleepTimer: timerData });
console.log(`[MusicPlus] Sleep timer started for ${minutes} minutes.`);
},
/**
* Cancels the active sleep timer.
*/
cancel() {
chrome.alarms.clear('sleepTimer');
chrome.storage.local.set({ sleepTimer: { active: false } });
console.log('[MusicPlus] Sleep timer cancelled.');
},
/**
* Gets the current status of the sleep timer.
* @returns {Promise<Object>}
*/
getStatus() {
return new Promise((resolve) => {
chrome.storage.local.get(['sleepTimer'], (result) => {
resolve(result.sleepTimer || { active: false });
});
});
},
/**
* Registers a callback to run when the sleep timer alarm fires.
* @param {Function} callback
*/
onFired(callback) {
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === 'sleepTimer') {
console.log('[MusicPlus] Sleep timer alarm fired.');
// We set active to false as soon as it fires
chrome.storage.local.set({ sleepTimer: { active: false } });
callback();
}
});
}
};
// Exporting as a global for use in background.js (service worker)
self.SleepTimer = SleepTimer;