-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added app suspension inhibitors when music is playing. fixes #257
- Loading branch information
1 parent
a0c7359
commit c7b3921
Showing
5 changed files
with
89 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,4 +72,4 @@ | |
"typescript": "^5.1.6" | ||
}, | ||
"prettier": "@mastermindzh/prettier-config" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { PowerSaveBlocker, powerSaveBlocker } from "electron"; | ||
import { Logger } from "../logger"; | ||
|
||
/** | ||
* Start blocking idle/screen timeouts | ||
* @param blocker optional instance of the powerSaveBlocker to use | ||
* @returns id of current block | ||
*/ | ||
export const acquireInhibitor = (blocker?: PowerSaveBlocker): number => { | ||
const currentBlocker = blocker ?? powerSaveBlocker; | ||
const blockId = currentBlocker.start("prevent-app-suspension"); | ||
Logger.log(`Started preventing app suspension with id: ${blockId}`); | ||
return blockId; | ||
}; | ||
|
||
/** | ||
* Check whether there is a blocker active for the current id, if not start it. | ||
* @param id id of inhibitor you want to check activity against | ||
* @param blocker optional instance of the powerSaveBlocker to use | ||
*/ | ||
export const acquireInhibitorIfInactive = (id: number, blocker?: PowerSaveBlocker): number => { | ||
const currentBlocker = blocker ?? powerSaveBlocker; | ||
if (!isInhibitorActive(id, currentBlocker)) { | ||
return acquireInhibitor(); | ||
} | ||
|
||
return id; | ||
}; | ||
|
||
/** | ||
* stop blocking idle/screen timeouts | ||
* @param id id of inhibitor you want to check activity against | ||
* @param blocker optional instance of the powerSaveBlocker to use | ||
*/ | ||
export const releaseInhibitor = (id: number, blocker?: PowerSaveBlocker) => { | ||
try { | ||
const currentBlocker = blocker ?? powerSaveBlocker; | ||
currentBlocker.stop(id); | ||
Logger.log(`Released inhibitor with id: ${id}`); | ||
} catch (error) { | ||
Logger.log("Releasing inhibitor failed"); | ||
} | ||
}; | ||
|
||
/** | ||
* stop blocking idle/screen timeouts if a inhibitor is active | ||
* @param id id of inhibitor you want to check activity against | ||
* @param blocker optional instance of the powerSaveBlocker to use | ||
*/ | ||
export const releaseInhibitorIfActive = (id: number, blocker?: PowerSaveBlocker) => { | ||
const currentBlocker = blocker ?? powerSaveBlocker; | ||
if (isInhibitorActive(id, currentBlocker)) { | ||
releaseInhibitor(id, currentBlocker); | ||
} | ||
}; | ||
|
||
/** | ||
* check whether the inhibitor is active | ||
* @param id id of inhibitor you want to check activity against | ||
* @param blocker optional instance of the powerSaveBlocker to use | ||
*/ | ||
export const isInhibitorActive = (id: number, blocker?: PowerSaveBlocker) => { | ||
const currentBlocker = blocker ?? powerSaveBlocker; | ||
return currentBlocker.isStarted(id); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters