mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 13:27:34 +00:00
LibWeb: Move Timer from DOM directory & namespace to HTML
Timers are part of the HTML spec. :^) https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timers
This commit is contained in:
parent
1422bd45eb
commit
0706f0d487
6 changed files with 15 additions and 15 deletions
38
Userland/Libraries/LibWeb/HTML/Timer.cpp
Normal file
38
Userland/Libraries/LibWeb/HTML/Timer.cpp
Normal file
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibCore/Timer.h>
|
||||
#include <LibWeb/HTML/Timer.h>
|
||||
#include <LibWeb/HTML/Window.h>
|
||||
|
||||
namespace Web::HTML {
|
||||
|
||||
NonnullRefPtr<Timer> Timer::create(Window& window, i32 milliseconds, Function<void()> callback, i32 id)
|
||||
{
|
||||
return adopt_ref(*new Timer(window, milliseconds, move(callback), id));
|
||||
}
|
||||
|
||||
Timer::Timer(Window& window, i32 milliseconds, Function<void()> callback, i32 id)
|
||||
: m_window(window)
|
||||
, m_id(id)
|
||||
{
|
||||
m_timer = Core::Timer::create_single_shot(milliseconds, [this, callback = move(callback)] {
|
||||
NonnullRefPtr strong_timer { *this };
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
Timer::~Timer()
|
||||
{
|
||||
m_window.deallocate_timer_id({}, m_id);
|
||||
}
|
||||
|
||||
void Timer::start()
|
||||
{
|
||||
m_timer->start();
|
||||
}
|
||||
|
||||
}
|
31
Userland/Libraries/LibWeb/HTML/Timer.h
Normal file
31
Userland/Libraries/LibWeb/HTML/Timer.h
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Forward.h>
|
||||
#include <AK/Function.h>
|
||||
#include <LibCore/Forward.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
|
||||
namespace Web::HTML {
|
||||
|
||||
class Timer final : public RefCounted<Timer> {
|
||||
public:
|
||||
static NonnullRefPtr<Timer> create(Window& window, i32 milliseconds, Function<void()> callback, i32 id);
|
||||
~Timer();
|
||||
|
||||
void start();
|
||||
|
||||
private:
|
||||
Timer(Window& window, i32 milliseconds, Function<void()> callback, i32 id);
|
||||
|
||||
RefPtr<Core::Timer> m_timer;
|
||||
Window& m_window;
|
||||
i32 m_id { 0 };
|
||||
};
|
||||
|
||||
}
|
|
@ -15,7 +15,6 @@
|
|||
#include <LibWeb/DOM/Document.h>
|
||||
#include <LibWeb/DOM/Event.h>
|
||||
#include <LibWeb/DOM/EventDispatcher.h>
|
||||
#include <LibWeb/DOM/Timer.h>
|
||||
#include <LibWeb/HTML/BrowsingContext.h>
|
||||
#include <LibWeb/HTML/EventLoop/EventLoop.h>
|
||||
#include <LibWeb/HTML/MessageEvent.h>
|
||||
|
@ -23,6 +22,7 @@
|
|||
#include <LibWeb/HTML/Scripting/ClassicScript.h>
|
||||
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
|
||||
#include <LibWeb/HTML/Storage.h>
|
||||
#include <LibWeb/HTML/Timer.h>
|
||||
#include <LibWeb/HTML/Window.h>
|
||||
#include <LibWeb/HighResolutionTime/Performance.h>
|
||||
#include <LibWeb/Layout/InitialContainingBlock.h>
|
||||
|
@ -174,7 +174,7 @@ void Window::clear_interval(i32 id)
|
|||
m_timers.remove(id);
|
||||
}
|
||||
|
||||
void Window::deallocate_timer_id(Badge<DOM::Timer>, i32 id)
|
||||
void Window::deallocate_timer_id(Badge<Timer>, i32 id)
|
||||
{
|
||||
m_timer_id_allocator.deallocate(id);
|
||||
}
|
||||
|
@ -262,7 +262,7 @@ i32 Window::run_timer_initialization_steps(Bindings::TimerHandler handler, i32 t
|
|||
};
|
||||
|
||||
// 13. Run steps after a timeout given global, "setTimeout/setInterval", timeout, completionStep, and id.
|
||||
auto timer = DOM::Timer::create(*this, timeout, move(completion_step), id);
|
||||
auto timer = Timer::create(*this, timeout, move(completion_step), id);
|
||||
m_timers.set(id, timer);
|
||||
timer->start();
|
||||
|
||||
|
|
|
@ -76,7 +76,7 @@ public:
|
|||
|
||||
void set_wrapper(Badge<Bindings::WindowObject>, Bindings::WindowObject&);
|
||||
|
||||
void deallocate_timer_id(Badge<DOM::Timer>, i32);
|
||||
void deallocate_timer_id(Badge<Timer>, i32);
|
||||
|
||||
HighResolutionTime::Performance& performance() { return *m_performance; }
|
||||
|
||||
|
@ -127,7 +127,7 @@ private:
|
|||
WeakPtr<Bindings::WindowObject> m_wrapper;
|
||||
|
||||
IDAllocator m_timer_id_allocator;
|
||||
HashMap<int, NonnullRefPtr<DOM::Timer>> m_timers;
|
||||
HashMap<int, NonnullRefPtr<Timer>> m_timers;
|
||||
|
||||
NonnullOwnPtr<HighResolutionTime::Performance> m_performance;
|
||||
NonnullRefPtr<Crypto::Crypto> m_crypto;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue