ladybird/Libraries/LibWeb/WebDriver/HeapTimer.cpp
Shannon Booth f87041bf3a LibGC+Everywhere: Factor out a LibGC from LibJS
Resulting in a massive rename across almost everywhere! Alongside the
namespace change, we now have the following names:

 * JS::NonnullGCPtr -> GC::Ref
 * JS::GCPtr -> GC::Ptr
 * JS::HeapFunction -> GC::Function
 * JS::CellImpl -> GC::Cell
 * JS::Handle -> GC::Root
2024-11-15 14:49:20 +01:00

60 lines
1.1 KiB
C++

/*
* Copyright (c) 2024, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/Timer.h>
#include <LibWeb/WebDriver/HeapTimer.h>
namespace Web::WebDriver {
GC_DEFINE_ALLOCATOR(HeapTimer);
HeapTimer::HeapTimer()
: m_timer(Core::Timer::create())
{
}
HeapTimer::~HeapTimer() = default;
void HeapTimer::visit_edges(JS::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_on_timeout);
}
void HeapTimer::start(u64 timeout_ms, GC::Ref<GC::Function<void()>> on_timeout)
{
m_on_timeout = on_timeout;
m_timer->on_timeout = [this]() {
m_timed_out = true;
if (m_on_timeout) {
m_on_timeout->function()();
m_on_timeout = nullptr;
}
};
m_timer->set_interval(static_cast<int>(timeout_ms));
m_timer->set_single_shot(true);
m_timer->start();
}
void HeapTimer::stop_and_fire_timeout_handler()
{
auto on_timeout = m_on_timeout;
stop();
if (on_timeout)
on_timeout->function()();
}
void HeapTimer::stop()
{
m_on_timeout = nullptr;
m_timer->stop();
}
}