Skip to content

Runtime API

Runtime update checking functionality.

Functions

checkForUpdates

Perform a synchronous update check.

zig
pub fn checkForUpdates(allocator: std.mem.Allocator, config: UpdateConfig) !UpdateResult;

Parameters

  • allocator: Memory allocator for HTTP request and parsing
  • config: Update check configuration

Returns

An UpdateResult with the check outcome.

Errors

  • Network errors
  • Parse errors
  • Configuration errors

Example

zig
const result = try updater.checkForUpdates(allocator, .{
    .provider = updater.providers.github,
    .owner = "username",
    .repo = "project",
    .current_version = "1.0.0",
});

if (result.has_update) {
    std.debug.print("Update: {s}\n", .{result.latest_version.?});
}

checkGitHub

Convenience function for GitHub checks.

zig
pub fn checkGitHub(
    allocator: std.mem.Allocator,
    owner: []const u8,
    repo: []const u8,
    current_version: []const u8,
) !UpdateResult;

checkGitLab

Convenience function for GitLab checks.

zig
pub fn checkGitLab(
    allocator: std.mem.Allocator,
    owner: []const u8,
    repo: []const u8,
    current_version: []const u8,
) !UpdateResult;

checkCodeberg

Convenience function for Codeberg checks.

zig
pub fn checkCodeberg(
    allocator: std.mem.Allocator,
    owner: []const u8,
    repo: []const u8,
    current_version: []const u8,
) !UpdateResult;

startBackgroundChecker

Start a background update checker.

zig
pub fn startBackgroundChecker(
    allocator: std.mem.Allocator,
    config: UpdateConfig,
) !*BackgroundChecker;

Parameters

  • allocator: Memory allocator
  • config: Configuration with background = true

Returns

A pointer to a BackgroundChecker that runs periodically.

Errors

  • GloballyDisabled: Update checking is globally disabled
  • BackgroundCheckingDisabled: Background mode not enabled in config

Example

zig
const checker = try updater.startBackgroundChecker(allocator, .{
    .enabled = true,
    .background = true,
    .interval_seconds = 3600,
    .provider = updater.providers.github,
    .owner = "username",
    .repo = "project",
    .current_version = "1.0.0",
    .on_update = onUpdateCallback,
});
defer checker.stop();

// Application runs...

isGloballyDisabled

Check if update checking is globally disabled.

zig
pub fn isGloballyDisabled() bool;

Returns true if ZIG_UPDATE_CHECK_DISABLE is set.

isCI

Check if running in a CI environment.

zig
pub fn isCI() bool;

Returns true if any CI environment variable is detected.

Types

BackgroundChecker

Background update checker that runs in a separate thread.

zig
pub const BackgroundChecker = struct {
    /// Start the background checker
    pub fn start(allocator: std.mem.Allocator, config: UpdateConfig) !*BackgroundChecker;

    /// Stop the background checker and clean up
    pub fn stop(self: *BackgroundChecker) void;

    /// Get the last check result (if any)
    pub fn getLastResult(self: *BackgroundChecker) ?UpdateResult;
};

Methods

start
zig
pub fn start(allocator: std.mem.Allocator, config: UpdateConfig) !*BackgroundChecker;

Start the background checker. Spawns a thread that:

  1. Performs an immediate check
  2. Waits for interval_seconds
  3. Repeats until stop() is called
stop
zig
pub fn stop(self: *BackgroundChecker) void;

Stop the background checker and free resources.

getLastResult
zig
pub fn getLastResult(self: *BackgroundChecker) ?UpdateResult;

Get the result of the last check, or null if no check has completed.

Callback

When using background mode with a callback:

zig
fn onUpdateFound(result: UpdateResult) void {
    std.debug.print("Update found: {s}\n", .{result.latest_version.?});
    // Notify user, log, etc.
}

const checker = try updater.startBackgroundChecker(allocator, .{
    // ...
    .on_update = onUpdateFound,
});

The callback is called from the background thread when an update is found.

Released under the MIT License.