preventSleep
preventSleep inhibits automatic system/display sleep until allowSleep is called.
Signature
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
}Idempotent: calling this again while sleep is already inhibited is a no-op success.
Implemented per platform:
- Linux, spawns
systemd-inhibit --mode=block sleep infinityand holds the child process alive. - macOS, spawns
caffeinate -dims. - Windows, calls
SetThreadExecutionStatewithES_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
| Field | Type | Meaning |
|---|---|---|
code | string | A stable, machine-readable identifier (e.g. "ERR_OS_SLEEP_INHIBIT_FAILED"). |
message | string | A human-readable description of what went wrong. |
