2022-09-07 20:30:31 +02:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
|
2022-09-07 20:30:31 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2026-02-28 10:28:49 +01:00
|
|
|
#include <LibCore/Timer.h>
|
2024-11-15 04:01:23 +13:00
|
|
|
#include <LibGC/Function.h>
|
2026-02-28 10:28:49 +01:00
|
|
|
#include <LibGC/Heap.h>
|
2022-09-07 20:30:31 +02:00
|
|
|
#include <LibWeb/Platform/Timer.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::Platform {
|
|
|
|
|
|
2026-02-28 10:28:49 +01:00
|
|
|
GC_DEFINE_ALLOCATOR(Timer);
|
|
|
|
|
|
|
|
|
|
Timer::Timer()
|
|
|
|
|
: m_timer(Core::Timer::create())
|
|
|
|
|
{
|
|
|
|
|
m_timer->on_timeout = [this] {
|
|
|
|
|
if (on_timeout)
|
|
|
|
|
on_timeout->function()();
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-07 20:30:31 +02:00
|
|
|
Timer::~Timer() = default;
|
|
|
|
|
|
2024-10-30 21:42:05 +13:00
|
|
|
void Timer::visit_edges(JS::Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
visitor.visit(on_timeout);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<Timer> Timer::create(GC::Heap& heap)
|
2022-09-07 20:30:31 +02:00
|
|
|
{
|
2026-02-28 10:28:49 +01:00
|
|
|
return heap.allocate<Timer>();
|
2022-09-07 20:30:31 +02:00
|
|
|
}
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<Timer> Timer::create_repeating(GC::Heap& heap, int interval_ms, GC::Ptr<GC::Function<void()>> timeout_handler)
|
2022-09-07 20:30:31 +02:00
|
|
|
{
|
2026-02-28 10:28:49 +01:00
|
|
|
auto timer = heap.allocate<Timer>();
|
2022-09-07 20:30:31 +02:00
|
|
|
timer->set_single_shot(false);
|
|
|
|
|
timer->set_interval(interval_ms);
|
|
|
|
|
timer->on_timeout = move(timeout_handler);
|
|
|
|
|
return timer;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<Timer> Timer::create_single_shot(GC::Heap& heap, int interval_ms, GC::Ptr<GC::Function<void()>> timeout_handler)
|
2022-09-07 20:30:31 +02:00
|
|
|
{
|
2026-02-28 10:28:49 +01:00
|
|
|
auto timer = heap.allocate<Timer>();
|
2022-09-07 20:30:31 +02:00
|
|
|
timer->set_single_shot(true);
|
|
|
|
|
timer->set_interval(interval_ms);
|
|
|
|
|
timer->on_timeout = move(timeout_handler);
|
|
|
|
|
return timer;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-28 10:28:49 +01:00
|
|
|
void Timer::start()
|
|
|
|
|
{
|
|
|
|
|
m_timer->start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Timer::start(int interval_ms)
|
|
|
|
|
{
|
|
|
|
|
m_timer->start(interval_ms);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Timer::restart()
|
|
|
|
|
{
|
|
|
|
|
m_timer->restart();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Timer::restart(int interval_ms)
|
|
|
|
|
{
|
|
|
|
|
m_timer->restart(interval_ms);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Timer::stop()
|
|
|
|
|
{
|
|
|
|
|
m_timer->stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Timer::set_active(bool active)
|
|
|
|
|
{
|
|
|
|
|
m_timer->set_active(active);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Timer::is_active() const
|
|
|
|
|
{
|
|
|
|
|
return m_timer->is_active();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Timer::interval() const
|
|
|
|
|
{
|
|
|
|
|
return m_timer->interval();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Timer::set_interval(int interval_ms)
|
|
|
|
|
{
|
|
|
|
|
m_timer->set_interval(interval_ms);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Timer::is_single_shot() const
|
|
|
|
|
{
|
|
|
|
|
return m_timer->is_single_shot();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Timer::set_single_shot(bool single_shot)
|
|
|
|
|
{
|
|
|
|
|
m_timer->set_single_shot(single_shot);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-07 20:30:31 +02:00
|
|
|
}
|