mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-09 18:16:09 +03:00
LibWeb: Make HTML::Timer GC-allocated
These are the timers used internally by setTimeout() and setInterval().
This commit is contained in:
parent
c569c88e63
commit
c7ac82ec33
Notes:
sideshowbarker
2024-07-17 09:47:09 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/c7ac82ec33 Pull-request: https://github.com/SerenityOS/serenity/pull/14816 Reviewed-by: https://github.com/ADKaster Reviewed-by: https://github.com/linusg ✅
@ -10,24 +10,29 @@
|
||||
|
||||
namespace Web::HTML {
|
||||
|
||||
NonnullRefPtr<Timer> Timer::create(Window& window, i32 milliseconds, Function<void()> callback, i32 id)
|
||||
JS::NonnullGCPtr<Timer> Timer::create(Window& window, i32 milliseconds, Function<void()> callback, i32 id)
|
||||
{
|
||||
return adopt_ref(*new Timer(window, milliseconds, move(callback), id));
|
||||
return *window.heap().allocate_without_realm<Timer>(window, milliseconds, move(callback), id);
|
||||
}
|
||||
|
||||
Timer::Timer(Window& window, i32 milliseconds, Function<void()> callback, i32 id)
|
||||
: m_window(window)
|
||||
, m_callback(move(callback))
|
||||
, m_id(id)
|
||||
{
|
||||
m_timer = Core::Timer::create_single_shot(milliseconds, [this, callback = move(callback)] {
|
||||
NonnullRefPtr strong_timer { *this };
|
||||
callback();
|
||||
m_timer = Core::Timer::create_single_shot(milliseconds, [this] {
|
||||
m_callback();
|
||||
});
|
||||
}
|
||||
|
||||
void Timer::visit_edges(Cell::Visitor& visitor)
|
||||
{
|
||||
Base::visit_edges(visitor);
|
||||
visitor.visit(m_window.ptr());
|
||||
}
|
||||
|
||||
Timer::~Timer()
|
||||
{
|
||||
m_window.deallocate_timer_id({}, m_id);
|
||||
}
|
||||
|
||||
void Timer::start()
|
||||
|
@ -8,23 +8,31 @@
|
||||
|
||||
#include <AK/Forward.h>
|
||||
#include <AK/Function.h>
|
||||
#include <AK/WeakPtr.h>
|
||||
#include <LibCore/Forward.h>
|
||||
#include <LibJS/Heap/Cell.h>
|
||||
#include <LibJS/Heap/GCPtr.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
|
||||
namespace Web::HTML {
|
||||
|
||||
class Timer final : public RefCounted<Timer> {
|
||||
class Timer final : public JS::Cell {
|
||||
JS_CELL(Timer, JS::Cell);
|
||||
|
||||
public:
|
||||
static NonnullRefPtr<Timer> create(Window& window, i32 milliseconds, Function<void()> callback, i32 id);
|
||||
~Timer();
|
||||
static JS::NonnullGCPtr<Timer> create(Window&, i32 milliseconds, Function<void()> callback, i32 id);
|
||||
virtual ~Timer() override;
|
||||
|
||||
void start();
|
||||
|
||||
private:
|
||||
Timer(Window& window, i32 milliseconds, Function<void()> callback, i32 id);
|
||||
|
||||
virtual void visit_edges(Cell::Visitor&) override;
|
||||
|
||||
RefPtr<Core::Timer> m_timer;
|
||||
Window& m_window;
|
||||
JS::NonnullGCPtr<Window> m_window;
|
||||
Function<void()> m_callback;
|
||||
i32 m_id { 0 };
|
||||
};
|
||||
|
||||
|
@ -118,6 +118,8 @@ void Window::visit_edges(JS::Cell::Visitor& visitor)
|
||||
visitor.visit(it.value);
|
||||
for (auto& it : m_constructors)
|
||||
visitor.visit(it.value);
|
||||
for (auto& it : m_timers)
|
||||
visitor.visit(it.value.ptr());
|
||||
}
|
||||
|
||||
Window::~Window() = default;
|
||||
|
@ -144,7 +144,7 @@ private:
|
||||
JS::GCPtr<DOM::Event> m_current_event;
|
||||
|
||||
IDAllocator m_timer_id_allocator;
|
||||
HashMap<int, NonnullRefPtr<Timer>> m_timers;
|
||||
HashMap<int, JS::NonnullGCPtr<Timer>> m_timers;
|
||||
|
||||
JS::GCPtr<HighResolutionTime::Performance> m_performance;
|
||||
NonnullRefPtr<Crypto::Crypto> m_crypto;
|
||||
|
Loading…
Reference in New Issue
Block a user