EventLoop.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. void spin_until(Function<bool()> goal_condition);
  28. void process();
  29. Task const* currently_running_task() const { return m_currently_running_task; }
  30. JS::VM& vm() { return *m_vm; }
  31. JS::VM const& vm() const { return *m_vm; }
  32. void set_vm(JS::VM&);
  33. void schedule();
  34. private:
  35. Type m_type { Type::Window };
  36. TaskQueue m_task_queue;
  37. // https://html.spec.whatwg.org/multipage/webappapis.html#currently-running-task
  38. Task* m_currently_running_task { nullptr };
  39. JS::VM* m_vm { nullptr };
  40. RefPtr<Core::Timer> m_system_event_loop_timer;
  41. };
  42. EventLoop& main_thread_event_loop();
  43. }