2022-12-06 16:14:56 -06:00
|
|
|
// track state of nosleep locally to avoid a null case error
|
|
|
|
|
// when nosleep.disable is called without first calling .enable
|
|
|
|
|
|
|
|
|
|
let wakeLock = false;
|
|
|
|
|
|
|
|
|
|
const noSleep = (enable = false) => {
|
|
|
|
|
// get a nosleep controller
|
|
|
|
|
if (!noSleep.controller) noSleep.controller = new NoSleep();
|
|
|
|
|
// don't call anything if the states match
|
2025-06-24 22:38:25 -04:00
|
|
|
if (wakeLock === enable) return Promise.resolve(false);
|
2022-12-06 16:14:56 -06:00
|
|
|
// store the value
|
|
|
|
|
wakeLock = enable;
|
|
|
|
|
// call the function
|
2025-06-24 22:38:25 -04:00
|
|
|
if (enable) {
|
|
|
|
|
return noSleep.controller.enable().catch((error) => {
|
|
|
|
|
// Handle wake lock request failures gracefully
|
|
|
|
|
console.warn('Wake lock request failed:', error.message);
|
|
|
|
|
wakeLock = false;
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return Promise.resolve(noSleep.controller.disable());
|
2022-12-06 16:14:56 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default noSleep;
|