Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / external / lib_core / timer.lucb

#==============================================================================================
#
#   timer - Core::Timer: a single-shot or repeating timer on the event loop
#
#   DESCRIPTION:
#       Port of Libraries/LibCore/Timer.cpp (lines 1-97) with the inline members of Timer.h and
#       the timer half of its base EventReceiver (EventReceiver.cpp lines 22-57: event,
#       start_timer, stop_timer), over the embedder's event loop (event_loop.lucb). Platform::
#       Timer and SVGImageElement hold one. The embedder fires a registered timer by calling
#       core_timer_timer_event.
#
#       Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibCore/Timer.cpp:12 Core::Timer::create.
pub func core_timer_create() -> CoreTimer*:
    return new CoreTimer() else trap("out of memory")

## Ported from Libraries/LibCore/Timer.cpp:17 Core::Timer::create_repeating.
pub func core_timer_create_repeating(interval_ms: i32, timeout_handler: ak.Function0[unit]) -> CoreTimer*:
    return new CoreTimer(on_timeout = timeout_handler, m_interval_ms = interval_ms) else trap("out of memory")

## Ported from Libraries/LibCore/Timer.cpp:22 Core::Timer::create_single_shot.
pub func core_timer_create_single_shot(interval_ms: i32, timeout_handler: ak.Function0[unit]) -> CoreTimer*:
    let timer = new CoreTimer(on_timeout = timeout_handler, m_interval_ms = interval_ms) else trap("out of memory")
    core_timer_set_single_shot(timer, true)
    return timer

## Ported from Libraries/LibCore/Timer.cpp:39 Core::Timer::start.
pub func core_timer_start(this: CoreTimer*):
    core_timer_start_i32(this, this.m_interval_ms)

## Ported from Libraries/LibCore/Timer.cpp:44 Core::Timer::start(int).
pub func core_timer_start_i32(this: CoreTimer*, interval_ms: i32):
    if this.m_active:
        return
    this.m_interval_ms = interval_ms
    core_timer_start_timer(this, interval_ms)
    this.m_active = true

## Ported from Libraries/LibCore/Timer.cpp:53 Core::Timer::restart.
pub func core_timer_restart(this: CoreTimer*):
    core_timer_restart_i32(this, this.m_interval_ms)

## Ported from Libraries/LibCore/Timer.cpp:58 Core::Timer::restart(int).
pub func core_timer_restart_i32(this: CoreTimer*, interval_ms: i32):
    if this.m_active:
        core_timer_stop(this)
    core_timer_start_i32(this, interval_ms)

## Ported from Libraries/LibCore/Timer.cpp:65 Core::Timer::stop.
pub func core_timer_stop(this: CoreTimer*):
    if not this.m_active:
        return
    core_timer_stop_timer(this)
    this.m_active = false

## Ported from Libraries/LibCore/Timer.cpp:73 Core::Timer::set_active.
pub func core_timer_set_active(this: CoreTimer*, active: bool):
    if active:
        core_timer_start(this)
    else:
        core_timer_stop(this)

## Ported from Libraries/LibCore/Timer.h:34 Core::Timer::is_active.
pub func core_timer_is_active(this: const CoreTimer*) -> bool:
    return this.m_active

## Ported from Libraries/LibCore/Timer.h:35 Core::Timer::interval.
pub func core_timer_interval(this: const CoreTimer*) -> i32:
    return this.m_interval_ms

## Ported from Libraries/LibCore/Timer.h:36 Core::Timer::set_interval.
pub func core_timer_set_interval(this: CoreTimer*, interval_ms: i32):
    if this.m_interval_ms == interval_ms:
        return
    this.m_interval_ms = interval_ms
    this.m_interval_dirty = true

## Ported from Libraries/LibCore/Timer.h:44 Core::Timer::is_single_shot.
pub func core_timer_is_single_shot(this: const CoreTimer*) -> bool:
    return this.m_single_shot

## Ported from Libraries/LibCore/Timer.h:45 Core::Timer::set_single_shot.
pub func core_timer_set_single_shot(this: CoreTimer*, single_shot: bool):
    this.m_single_shot = single_shot

## Ported from Libraries/LibCore/EventReceiver.cpp:22 Core::EventReceiver::event (Event::Timer)
## and Libraries/LibCore/Timer.cpp:81 Core::Timer::timer_event: the embedder calls this when a
## registered timer fires.
pub func core_timer_timer_event(this: CoreTimer*):
    if this.m_timer_id == 0:
        return  # Too late, the timer was already stopped.

    if this.m_single_shot:
        core_timer_stop(this)
    else:
        if this.m_interval_dirty:
            core_timer_stop(this)
            core_timer_start_i32(this, this.m_interval_ms)
            this.m_interval_dirty = false

    if ak.function0_is_set(&this.on_timeout):
        ak.function0_call(&this.on_timeout)

## Ported from Libraries/LibCore/EventReceiver.cpp:41 Core::EventReceiver::start_timer.
func core_timer_start_timer(this: CoreTimer*, ms: i32):
    if this.m_timer_id != 0:
        ak.dbgln(f"Timer 0x{(usize)this:x} already has a timer!")
        trap("VERIFY_NOT_REACHED")

    this.m_timer_id = core_event_loop_register_timer(this, ms, true)

## Ported from Libraries/LibCore/EventReceiver.cpp:51 Core::EventReceiver::stop_timer.
func core_timer_stop_timer(this: CoreTimer*):
    if this.m_timer_id == 0:
        return
    core_event_loop_unregister_timer(this.m_timer_id)
    this.m_timer_id = 0