Skip to content

Commit 412467c

Browse files
Zaggy1024gmta
authored andcommitted
LibWeb: Add a hook in EventLoop to call a task when reaching step 1
1 parent d9e663f commit 412467c

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ EventLoop::~EventLoop() = default;
4949
void EventLoop::visit_edges(Visitor& visitor)
5050
{
5151
Base::visit_edges(visitor);
52+
visitor.visit(m_reached_step_1_tasks);
5253
visitor.visit(m_task_queue);
5354
visitor.visit(m_microtask_queue);
5455
visitor.visit(m_currently_running_task);
@@ -163,6 +164,12 @@ void EventLoop::process()
163164
GC::Ptr<Task> oldest_task;
164165
[[maybe_unused]] double task_start_time = 0;
165166

167+
// Some algorithms request that steps or states only occur once the event loop has reached step 1.
168+
// Invoke a set of tasks that these algorithms request us to in order to achieve this.
169+
auto reached_step_1_tasks = move(m_reached_step_1_tasks);
170+
for (auto& reached_step_1_task : reached_step_1_tasks)
171+
reached_step_1_task->function()();
172+
166173
// 2. If the event loop has a task queue with at least one runnable task, then:
167174
if (m_task_queue->has_runnable_tasks()) {
168175
// 1. Let taskQueue be one such task queue, chosen in an implementation-defined manner.
@@ -508,6 +515,12 @@ void EventLoop::update_the_rendering()
508515
}
509516
}
510517

518+
void run_when_event_loop_reaches_step_1(GC::Ref<GC::Function<void()>> steps)
519+
{
520+
auto& event_loop = main_thread_event_loop();
521+
event_loop.run_upon_reaching_step_1(steps);
522+
}
523+
511524
// https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-task
512525
TaskID queue_a_task(HTML::Task::Source source, GC::Ptr<EventLoop> event_loop, GC::Ptr<DOM::Document> document, GC::Ref<GC::Function<void()>> steps)
513526
{

Libraries/LibWeb/HTML/EventLoop/EventLoop.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class WEB_API EventLoop : public JS::Cell {
4848

4949
Type type() const { return m_type; }
5050

51+
void run_upon_reaching_step_1(GC::Ref<GC::Function<void()>> task) { m_reached_step_1_tasks.append(task); }
52+
5153
TaskQueue& task_queue() { return *m_task_queue; }
5254
TaskQueue const& task_queue() const { return *m_task_queue; }
5355

@@ -103,6 +105,8 @@ class WEB_API EventLoop : public JS::Cell {
103105

104106
Type m_type { Type::Window };
105107

108+
Vector<GC::Ref<GC::Function<void()>>> m_reached_step_1_tasks;
109+
106110
GC::Ptr<TaskQueue> m_task_queue;
107111
GC::Ptr<TaskQueue> m_microtask_queue;
108112

@@ -142,6 +146,7 @@ class WEB_API EventLoop : public JS::Cell {
142146
};
143147

144148
WEB_API EventLoop& main_thread_event_loop();
149+
WEB_API void run_when_event_loop_reaches_step_1(GC::Ref<GC::Function<void()>> steps);
145150
WEB_API TaskID queue_a_task(HTML::Task::Source, GC::Ptr<EventLoop>, GC::Ptr<DOM::Document>, GC::Ref<GC::Function<void()>> steps);
146151
WEB_API TaskID queue_global_task(HTML::Task::Source, JS::Object&, GC::Ref<GC::Function<void()>> steps);
147152
WEB_API void queue_a_microtask(DOM::Document const*, GC::Ref<GC::Function<void()>> steps);

0 commit comments

Comments
 (0)