2020-06-27 18:30:29 +02:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
2020-06-27 18:30:29 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-27 18:30:29 +02:00
|
|
|
*/
|
|
|
|
|
2023-09-26 11:53:37 +02:00
|
|
|
#include <LibCore/Timer.h>
|
2023-03-14 06:55:34 -04:00
|
|
|
#include <LibJS/Runtime/Object.h>
|
2022-03-07 23:54:56 +01:00
|
|
|
#include <LibWeb/HTML/Timer.h>
|
2022-03-07 23:08:26 +01:00
|
|
|
#include <LibWeb/HTML/Window.h>
|
2020-06-27 18:30:29 +02:00
|
|
|
|
2022-03-07 23:54:56 +01:00
|
|
|
namespace Web::HTML {
|
2020-06-27 18:30:29 +02:00
|
|
|
|
2023-11-19 19:47:52 +01:00
|
|
|
JS_DEFINE_ALLOCATOR(Timer);
|
|
|
|
|
2023-03-14 06:55:34 -04:00
|
|
|
JS::NonnullGCPtr<Timer> Timer::create(JS::Object& window_or_worker_global_scope, i32 milliseconds, Function<void()> callback, i32 id)
|
2020-06-27 18:30:29 +02:00
|
|
|
{
|
2023-09-26 14:48:53 +02:00
|
|
|
auto heap_function_callback = JS::create_heap_function(window_or_worker_global_scope.heap(), move(callback));
|
2024-11-14 06:13:46 +13:00
|
|
|
return window_or_worker_global_scope.heap().allocate<Timer>(window_or_worker_global_scope, milliseconds, heap_function_callback, id);
|
2020-06-27 18:30:29 +02:00
|
|
|
}
|
|
|
|
|
2023-09-26 14:48:53 +02:00
|
|
|
Timer::Timer(JS::Object& window_or_worker_global_scope, i32 milliseconds, JS::NonnullGCPtr<JS::HeapFunction<void()>> callback, i32 id)
|
2023-03-14 06:55:34 -04:00
|
|
|
: m_window_or_worker_global_scope(window_or_worker_global_scope)
|
2022-09-01 12:21:47 +02:00
|
|
|
, m_callback(move(callback))
|
2022-03-04 10:41:12 -05:00
|
|
|
, m_id(id)
|
2020-06-27 18:30:29 +02:00
|
|
|
{
|
2023-09-26 11:53:37 +02:00
|
|
|
m_timer = Core::Timer::create_single_shot(milliseconds, [this] {
|
2023-09-26 14:48:53 +02:00
|
|
|
m_callback->function()();
|
2024-04-16 20:34:01 +02:00
|
|
|
});
|
2020-06-27 18:30:29 +02:00
|
|
|
}
|
|
|
|
|
2022-09-01 12:21:47 +02:00
|
|
|
void Timer::visit_edges(Cell::Visitor& visitor)
|
|
|
|
{
|
|
|
|
Base::visit_edges(visitor);
|
2023-11-19 16:18:00 +13:00
|
|
|
visitor.visit(m_window_or_worker_global_scope);
|
2023-09-26 14:48:53 +02:00
|
|
|
visitor.visit(m_callback);
|
2022-09-01 12:21:47 +02:00
|
|
|
}
|
|
|
|
|
2020-06-27 18:30:29 +02:00
|
|
|
Timer::~Timer()
|
|
|
|
{
|
2023-09-26 11:44:56 +02:00
|
|
|
VERIFY(!m_timer->is_active());
|
2020-06-27 18:30:29 +02:00
|
|
|
}
|
|
|
|
|
2022-03-04 10:41:12 -05:00
|
|
|
void Timer::start()
|
|
|
|
{
|
|
|
|
m_timer->start();
|
|
|
|
}
|
|
|
|
|
2023-09-26 11:44:56 +02:00
|
|
|
void Timer::stop()
|
|
|
|
{
|
|
|
|
m_timer->stop();
|
|
|
|
}
|
|
|
|
|
2020-06-27 18:30:29 +02:00
|
|
|
}
|