Azle and Kybra now support the timers API

Azle 0.9.0 and Kybra 0.2.0 both have support for the new timers API.

If you haven’t heard of this API, it’s meant to be an improvement over heartbeat, allowing you to set callbacks at arbitrary times in the future. The hope is that this will be much cheaper for time-based tasks when compared with the coarse heartbeat API that forced an update call every second or so.

Azle timers info: GitHub - demergent-labs/azle: TypeScript CDK for the Internet Computer

import { Duration, ic, TimerId, Update } from 'azle';

export function set_timers(delay: Duration): Update<[TimerId, TimerId]> {
    const function_timer_id = ic.set_timer(delay, callback);

    const captured_value = '🚩';

    const closure_timer_id = ic.set_timer(delay, () => {
        console.log(`closure called and captured value ${captured_value}`);
    });

    return [function_timer_id, closure_timer_id];
}

function callback(): void {
    console.log('callback called');
}

Kybra timers info: GitHub - demergent-labs/kybra: Python CDK for the Internet Computer

from kybra import Duration, ic, TimerId, update


@update
def set_timers(delay: Duration) -> tuple[TimerId, TimerId]:
    function_timer_id = ic.set_timer(delay, callback)

    captured_value = "🚩"

    closure_timer_id = ic.set_timer(
        delay,
        lambda: ic.print(f"closure called and captured value {captured_value}"),
    )

    return [function_timer_id, closure_timer_id]


def callback():
    ic.print("callback called")
7 Likes