Przeglądaj źródła

LibWeb: Implement the "queue a task" steps as a distinct AO

This will be needed by EventSource.
Timothy Flynn 1 rok temu
rodzic
commit
9cc186b929

+ 26 - 4
Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp

@@ -369,6 +369,31 @@ void EventLoop::process()
     });
 }
 
+// https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-task
+int queue_a_task(HTML::Task::Source source, JS::GCPtr<EventLoop> event_loop, JS::GCPtr<DOM::Document> document, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps)
+{
+    // 1. If event loop was not given, set event loop to the implied event loop.
+    if (!event_loop)
+        event_loop = main_thread_event_loop();
+
+    // FIXME: 2. If document was not given, set document to the implied document.
+
+    // 3. Let task be a new task.
+    // 4. Set task's steps to steps.
+    // 5. Set task's source to source.
+    // 6. Set task's document to the document.
+    // 7. Set task's script evaluation environment settings object set to an empty set.
+    auto task = HTML::Task::create(event_loop->vm(), source, document, steps);
+
+    // 8. Let queue be the task queue to which source is associated on event loop.
+    auto& queue = event_loop->task_queue();
+
+    // 9. Append task to queue.
+    queue.add(task);
+
+    return queue.last_added_task()->id();
+}
+
 // https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-global-task
 int queue_global_task(HTML::Task::Source source, JS::Object& global_object, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps)
 {
@@ -384,10 +409,7 @@ int queue_global_task(HTML::Task::Source source, JS::Object& global_object, JS::
     }
 
     // 3. Queue a task given source, event loop, document, and steps.
-    auto& vm = global_object.vm();
-    event_loop->task_queue().add(HTML::Task::create(vm, source, document, steps));
-
-    return event_loop->task_queue().last_added_task()->id();
+    return queue_a_task(source, *event_loop, document, steps);
 }
 
 // https://html.spec.whatwg.org/#queue-a-microtask

+ 1 - 0
Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.h

@@ -116,6 +116,7 @@ private:
 };
 
 EventLoop& main_thread_event_loop();
+int queue_a_task(HTML::Task::Source, JS::GCPtr<EventLoop>, JS::GCPtr<DOM::Document>, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps);
 int queue_global_task(HTML::Task::Source, JS::Object&, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps);
 void queue_a_microtask(DOM::Document const*, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps);
 void perform_a_microtask_checkpoint();