2020-06-27 18:30:29 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-27 18:30:29 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibCore/Timer.h>
|
2021-06-27 21:48:34 +02:00
|
|
|
#include <LibJS/Runtime/FunctionObject.h>
|
2020-06-27 18:30:29 +02:00
|
|
|
#include <LibWeb/DOM/Timer.h>
|
|
|
|
#include <LibWeb/DOM/Window.h>
|
|
|
|
|
2020-07-26 19:37:56 +02:00
|
|
|
namespace Web::DOM {
|
2020-06-27 18:30:29 +02:00
|
|
|
|
2021-10-14 18:03:08 +01:00
|
|
|
NonnullRefPtr<Timer> Timer::create_interval(Window& window, int milliseconds, NonnullOwnPtr<Bindings::CallbackType> callback)
|
2020-06-27 18:30:29 +02:00
|
|
|
{
|
2021-10-14 18:03:08 +01:00
|
|
|
return adopt_ref(*new Timer(window, Type::Interval, milliseconds, move(callback)));
|
2020-06-27 18:30:29 +02:00
|
|
|
}
|
|
|
|
|
2021-10-14 18:03:08 +01:00
|
|
|
NonnullRefPtr<Timer> Timer::create_timeout(Window& window, int milliseconds, NonnullOwnPtr<Bindings::CallbackType> callback)
|
2020-06-27 18:30:29 +02:00
|
|
|
{
|
2021-10-14 18:03:08 +01:00
|
|
|
return adopt_ref(*new Timer(window, Type::Timeout, milliseconds, move(callback)));
|
2020-06-27 18:30:29 +02:00
|
|
|
}
|
|
|
|
|
2021-10-14 18:03:08 +01:00
|
|
|
Timer::Timer(Window& window, Type type, int milliseconds, NonnullOwnPtr<Bindings::CallbackType> callback)
|
2020-06-27 18:30:29 +02:00
|
|
|
: m_window(window)
|
|
|
|
, m_type(type)
|
2021-10-14 18:03:08 +01:00
|
|
|
, m_callback(move(callback))
|
2020-06-27 18:30:29 +02:00
|
|
|
{
|
|
|
|
m_id = window.allocate_timer_id({});
|
|
|
|
m_timer = Core::Timer::construct(milliseconds, [this] { m_window.timer_did_fire({}, *this); });
|
|
|
|
if (m_type == Type::Timeout)
|
|
|
|
m_timer->set_single_shot(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer::~Timer()
|
|
|
|
{
|
2020-11-30 08:03:13 +01:00
|
|
|
m_window.deallocate_timer_id({}, m_id);
|
2020-06-27 18:30:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|