EventLoop.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Function.h>
  8. #include <LibCore/Forward.h>
  9. #include <LibJS/Forward.h>
  10. #include <LibWeb/HTML/EventLoop/TaskQueue.h>
  11. namespace Web::HTML {
  12. class EventLoop {
  13. public:
  14. enum class Type {
  15. // https://html.spec.whatwg.org/multipage/webappapis.html#window-event-loop
  16. Window,
  17. // https://html.spec.whatwg.org/multipage/webappapis.html#worker-event-loop
  18. Worker,
  19. // https://html.spec.whatwg.org/multipage/webappapis.html#worklet-event-loop
  20. Worklet,
  21. };
  22. EventLoop();
  23. ~EventLoop();
  24. Type type() const { return m_type; }
  25. TaskQueue& task_queue() { return m_task_queue; }
  26. TaskQueue const& task_queue() const { return m_task_queue; }
  27. TaskQueue& microtask_queue() { return m_microtask_queue; }
  28. TaskQueue const& microtask_queue() const { return m_microtask_queue; }
  29. void spin_until(Function<bool()> goal_condition);
  30. void process();
  31. Task const* currently_running_task() const { return m_currently_running_task; }
  32. JS::VM& vm() { return *m_vm; }
  33. JS::VM const& vm() const { return *m_vm; }
  34. void set_vm(JS::VM&);
  35. void schedule();
  36. void perform_a_microtask_checkpoint();
  37. private:
  38. Type m_type { Type::Window };
  39. TaskQueue m_task_queue;
  40. TaskQueue m_microtask_queue;
  41. // https://html.spec.whatwg.org/multipage/webappapis.html#currently-running-task
  42. Task* m_currently_running_task { nullptr };
  43. JS::VM* m_vm { nullptr };
  44. RefPtr<Core::Timer> m_system_event_loop_timer;
  45. // https://html.spec.whatwg.org/#performing-a-microtask-checkpoint
  46. bool m_performing_a_microtask_checkpoint { false };
  47. };
  48. EventLoop& main_thread_event_loop();
  49. void queue_global_task(HTML::Task::Source, DOM::Document&, Function<void()> steps);
  50. void queue_a_microtask(DOM::Document&, Function<void()> steps);
  51. void perform_a_microtask_checkpoint();
  52. }