preventSleep
preventSleep inibe a suspensão automática do sistema/tela até que allowSleep seja chamado.
Assinatura
preventSleep(): Result<void, OsError>tyrus
import { OsError, allowSleep, preventSleep } from "tyrus:os";
match (preventSleep()) {
Ok() => print("Sleep prevention enabled"); // Sleep prevention enabled
Err(error) => printError(`${error.code}: ${error.message}`); // ERR_OS_SLEEP_INHIBIT_FAILED: platform sleep prevention is unavailable
}
match (allowSleep()) {
Ok() => print("Sleep prevention disabled"); // Sleep prevention disabled
Err(error) => printError(`${error.code}: ${error.message}`); // ERR_OS_SLEEP_ALLOW_FAILED: platform sleep prevention could not be released
}Idempotente: chamar isso novamente enquanto a suspensão já está inibida é um sucesso sem efeito.
Implementado por plataforma:
- Linux, inicia
systemd-inhibit --mode=block sleep infinitye mantém o processo filho vivo. - macOS, inicia
caffeinate -dims. - Windows, chama
SetThreadExecutionStatecomES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED; nenhum processo filho envolvido.
Retorna Err(OsError) (code: "ERR_OS_SLEEP_INHIBIT_FAILED") se o mecanismo da plataforma não estiver disponível, por exemplo, systemd-inhibit ausente numa distribuição Linux sem systemd.
Campos de OsError
| Campo | Tipo | Significado |
|---|---|---|
code | string | Um identificador estável e legível por máquina (ex.: "ERR_OS_SLEEP_INHIBIT_FAILED"). |
message | string | Uma descrição legível do que deu errado. |
