Skip to content

preventSleep ​

preventSleep inibe a suspensão automática do sistema/tela até que allowSleep seja chamado.

AssinaturapreventSleep(): 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 infinity e mantém o processo filho vivo.
  • macOS, inicia caffeinate -dims.
  • Windows, chama SetThreadExecutionState com ES_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 ​

CampoTipoSignificado
codestringUm identificador estável e legível por máquina (ex.: "ERR_OS_SLEEP_INHIBIT_FAILED").
messagestringUma descrição legível do que deu errado.