Skip to content

preventSleep ​

preventSleep inhibits automatic system/display sleep until allowSleep is called.

SignaturepreventSleep(): 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
}

Idempotent: calling this again while sleep is already inhibited is a no-op success.

Implemented per platform:

  • Linux, spawns systemd-inhibit --mode=block sleep infinity and holds the child process alive.
  • macOS, spawns caffeinate -dims.
  • Windows, calls SetThreadExecutionState with ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED; no child process involved.

Returns Err(OsError) (code: "ERR_OS_SLEEP_INHIBIT_FAILED") if the platform's mechanism is unavailable, e.g. systemd-inhibit missing on a non-systemd Linux distribution.

OsError Fields ​

FieldTypeMeaning
codestringA stable, machine-readable identifier (e.g. "ERR_OS_SLEEP_INHIBIT_FAILED").
messagestringA human-readable description of what went wrong.