mirror of
https://github.com/Mastermindzh/tidal-hifi.git
synced 2025-08-05 12:26:41 +02:00
Added app suspension inhibitors when music is playing. fixes #257
This commit is contained in:
65
src/features/idleInhibitor/idleInhibitor.ts
Normal file
65
src/features/idleInhibitor/idleInhibitor.ts
Normal file
@@ -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);
|
||||
};
|
@@ -1,4 +1,4 @@
|
||||
import { IpcMain, ipcRenderer, ipcMain } from "electron";
|
||||
import { IpcMain, ipcMain, IpcMainEvent, ipcRenderer } from "electron";
|
||||
import { globalEvents } from "../constants/globalEvents";
|
||||
|
||||
export class Logger {
|
||||
@@ -7,10 +7,13 @@ export class Logger {
|
||||
* @param ipcMain main thread IPC client so we can subscribe to events
|
||||
*/
|
||||
public static watch(ipcMain: IpcMain) {
|
||||
ipcMain.on(globalEvents.log, (event, message) => {
|
||||
const { content, object } = message;
|
||||
this.logToSTDOut(content, object);
|
||||
});
|
||||
ipcMain.on(
|
||||
globalEvents.log,
|
||||
(event: IpcMainEvent | { content: string; message: string }, message) => {
|
||||
const { content, object } = message ?? event;
|
||||
this.logToSTDOut(content, object);
|
||||
}
|
||||
);
|
||||
}
|
||||
/**
|
||||
* Log content to STDOut
|
||||
@@ -23,7 +26,6 @@ export class Logger {
|
||||
} else {
|
||||
ipcMain.emit(globalEvents.log, { content, object });
|
||||
}
|
||||
this.logToSTDOut(content, object);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user